-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathConfDocument.cs
More file actions
559 lines (476 loc) · 22.4 KB
/
ConfDocument.cs
File metadata and controls
559 lines (476 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
using SS.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
namespace SS.Core.Configuration
{
/// <summary>
/// A configuration "document" which is loaded from 1 or more <see cref="ConfFile"/> objects
/// starting from a base/root .conf file.
///
/// Whereas a <see cref="ConfFile"/> tokenizes a .conf file into a line by line object model.
/// This interprets the tokens further by first handling preprocessor directives,
/// and then finally into a section, property representation.
/// </summary>
public class ConfDocument
{
private readonly string? _name;
private readonly IConfFileProvider _fileProvider;
private readonly IConfigLogger? _logger = null;
/// <summary>
/// The base (root) file.
/// </summary>
private ConfFile? _baseFile = null;
/// <summary>
/// All of the files that the document consists of.
/// </summary>
private readonly HashSet<ConfFile> _files = [];
/// <summary>
/// Active lines
/// </summary>
private readonly List<LineReference> _lines = [];
/// <summary>
/// Active settings
/// </summary>
private readonly Dictionary<string, SettingInfo> _settings = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, SettingInfo>.AlternateLookup<ReadOnlySpan<char>> _settingsLookup;
/// <summary>
/// The file currently being updated.
/// The purpose of this is to detect changes made by a document itself to one of its files,
/// such that the change isn't considered to be one that requires the document to be fully reloaded.
/// </summary>
private ConfFile? _updatingFile = null;
/// <summary>
/// Initializes a new instance of the <see cref="ConfDocument"/> class.
/// </summary>
/// <param name="name">Name of the base .conf file.</param>
/// <param name="fileProvider">Service that provides files by name/path..</param>
public ConfDocument(
string name,
IConfFileProvider fileProvider) : this(name, fileProvider, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ConfDocument"/> class.
/// </summary>
/// <param name="name">Name of the base .conf file.</param>
/// <param name="fileProvider">Service that provides files by name/path..</param>
/// <param name="logger">Service for logging errors.</param>
public ConfDocument(
string? name,
IConfFileProvider fileProvider,
IConfigLogger? logger)
{
_name = name;
_fileProvider = fileProvider ?? throw new ArgumentNullException(nameof(fileProvider));
_logger = logger;
_settingsLookup = _settings.GetAlternateLookup<ReadOnlySpan<char>>();
}
/// <summary>
/// Whether the document needs to be reloaded because one of the files it depends on has changed.
/// </summary>
public bool IsReloadNeeded { get; private set; } = false;
/// <summary>
/// Loads the document. Subsequent calls reload the document.
/// </summary>
public async Task LoadAsync()
{
//
// reset data members
//
_baseFile = null;
foreach (var file in _files)
{
file.Changed -= File_Changed;
}
_files.Clear();
_lines.Clear();
_settings.Clear();
_updatingFile = null;
IsReloadNeeded = false;
//
// read and pre-process
//
_baseFile = await _fileProvider.GetFileAsync(_name).ConfigureAwait(false);
if (_baseFile is null)
{
_logger?.Log(ComponentInterfaces.LogLevel.Error, $"Failed to load base conf file '{_name}'.");
return; // can't do anything without a file to start with
}
AddFile(_baseFile);
using (PreprocessorReader reader = new(_fileProvider, _baseFile, _logger))
{
LineReference? lineReference;
while ((lineReference = await reader.ReadLineAsync().ConfigureAwait(false)) is not null)
{
RawLine rawLine = lineReference.Line;
if (rawLine.LineType == ConfLineType.Section
|| rawLine.LineType == ConfLineType.Property)
{
_lines.Add(lineReference);
}
}
foreach (var file in reader.ProcessedFiles)
{
AddFile(file);
}
}
//
// interpret - takes the lines and determine section, key/value pairs
//
string? currentSection = null;
foreach (LineReference lineRef in _lines)
{
if (lineRef.Line.LineType == ConfLineType.Section)
{
RawSection rawSection = (RawSection)lineRef.Line;
currentSection = rawSection.Name;
}
else if (lineRef.Line.LineType == ConfLineType.Property)
{
RawProperty rawProperty = (RawProperty)lineRef.Line;
string? section = rawProperty.SectionOverride ?? currentSection;
AddOrReplaceSettingInfo(
section,
rawProperty.Key,
new SettingInfo()
{
Value = rawProperty.Value,
PropertyReference = lineRef,
});
}
}
}
private void AddFile(ConfFile file)
{
ArgumentNullException.ThrowIfNull(file);
if (_files.Add(file))
{
file.Changed += File_Changed;
}
}
private void File_Changed(object? sender, EventArgs e)
{
if (_updatingFile != sender)
{
IsReloadNeeded = true;
}
}
private void AddOrReplaceSettingInfo(ReadOnlySpan<char> section, ReadOnlySpan<char> key, SettingInfo settingInfo)
{
if (section.IsEmpty && key.IsEmpty)
throw new Exception("No section or key specified");
ArgumentNullException.ThrowIfNull(settingInfo);
Span<char> settingKey = stackalloc char[!section.IsEmpty && !key.IsEmpty ? section.Length + 1 + key.Length : (!section.IsEmpty ? section.Length : key.Length)];
if (!section.IsEmpty && !key.IsEmpty)
{
bool success = settingKey.TryWrite($"{section}:{key}", out int charsWritten);
Debug.Assert(success && charsWritten == settingKey.Length);
}
else if (!section.IsEmpty)
{
section.CopyTo(settingKey);
}
else
{
key.CopyTo(settingKey);
}
_settingsLookup[settingKey] = settingInfo;
}
private bool TryGetSettingInfo(ReadOnlySpan<char> section, ReadOnlySpan<char> key, [MaybeNullWhen(false)] out SettingInfo settingInfo)
{
if (section.IsEmpty && key.IsEmpty)
throw new Exception("No section or key specified");
Span<char> trieKey = stackalloc char[!section.IsEmpty && !key.IsEmpty ? section.Length + 1 + key.Length : (!section.IsEmpty ? section.Length : key.Length)];
if (!section.IsEmpty && !key.IsEmpty)
{
bool success = trieKey.TryWrite($"{section}:{key}", out int charsWritten);
Debug.Assert(success && charsWritten == trieKey.Length);
}
else if (!section.IsEmpty)
{
section.CopyTo(trieKey);
}
else
{
key.CopyTo(trieKey);
}
return _settingsLookup.TryGetValue(trieKey, out settingInfo);
}
/// <summary>
/// Gets the value of a property.
/// </summary>
/// <param name="section">The section of the property to get.</param>
/// <param name="key">The key of the property.</param>
/// <param name="value">When this method returns, contains the value of the property if found; otherwise, null.</param>
/// <returns><see langword="true"/> if the property was found; otherwise, <see langword="false"/>.</returns>
public bool TryGetValue(ReadOnlySpan<char> section, ReadOnlySpan<char> key, [MaybeNullWhen(false)] out string value)
{
if (!TryGetSettingInfo(section, key, out SettingInfo? settingInfo))
{
value = default;
return false;
}
value = settingInfo.Value;
return true;
}
/// <summary>
/// Saves a copy of the document as a single standalone conf file.
/// </summary>
/// <param name="filePath">The complete file path to save the resulting file to.</param>
/// <returns><see langword="true"/> if the file was successfully saved; otherwise, <see langword="false"/>.</returns>
/// <exception cref="ArgumentException"><paramref name="filePath"/> is null or white-space.</exception>
public async Task<bool> SaveAsStandaloneConfAsync(string filePath)
{
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
if (_baseFile is null)
throw new InvalidOperationException("Not loaded.");
try
{
await using FileStream fileStream = await Task.Factory.StartNew(
static (obj) => new FileStream((string)obj!, FileMode.CreateNew, FileAccess.Write, FileShare.Read, 4096, true),
filePath).ConfigureAwait(false);
await using StreamWriter writer = new(fileStream, StringUtils.DefaultEncoding);
using PreprocessorReader reader = new(_fileProvider, _baseFile, _logger);
LineReference? lineReference;
while ((lineReference = await reader.ReadLineAsync().ConfigureAwait(false)) is not null)
{
RawLine rawLine = lineReference.Line;
await rawLine.WriteToAsync(writer).ConfigureAwait(false);
}
return true;
}
catch (Exception ex)
{
_logger?.Log(ComponentInterfaces.LogLevel.Error, $"Error saving standalone conf to '{filePath}'. {ex}");
return false;
}
}
/// <summary>
/// Sets a property's value. An existing property will be updated, otherwise a new one will be added.
/// </summary>
/// <remarks>
/// This method does not block.
/// The appropriate underlying <see cref="ConfFile"/> will be modified for changes that are <paramref name="permanent"/>.
/// However, this method does not save the modified <see cref="ConfFile"/> to disk.
/// Writing dirty <see cref="ConfFile"/>s to disk is done separately.
/// </remarks>
/// <param name="section">The section of the property to add.</param>
/// <param name="key">The key of the property to add.</param>
/// <param name="value">The value of the property.</param>
/// <param name="permanent"><see langword="true"/> if the change should be persisted to disk. <see langword="false"/> to only change it in memory.</param>
/// <param name="comment">An optional comment for a change that is <paramref name="permanent"/>.</param>
/// <param name="options">Options that affect how <paramref name="permanent"/> settings are saved to conf files.</param>
public void UpdateOrAddProperty(string section, string key, string value, bool permanent, string? comment = null, ModifyOptions options = ModifyOptions.None)
{
if (TryGetSettingInfo(section, key, out SettingInfo? settingInfo))
{
// The setting exists, update it.
settingInfo.Value = value;
}
else
{
// The setting does not exist yet.
// Create the setting.
settingInfo = new SettingInfo()
{
Value = value,
};
// Add the setting to our in-memory collection of settings.
AddOrReplaceSettingInfo(section, key, settingInfo);
}
if (permanent)
{
// The setting is permanent, meaning it needs to be changed in a ConfFile.
if (settingInfo.PropertyReference is not null
&& (options & ModifyOptions.AppendOverrideOnly) != ModifyOptions.AppendOverrideOnly)
{
// The setting has a reference to an existing ConfFile's RawProperty
// and we're not being told to override only.
// This means we can update the existing line.
UpdatePermanent(settingInfo, value, comment, options);
}
else
{
// There is no reference to a ConfFile's RawProperty or we're being told override only.
// This means we only can add it.
// Add the setting as a RawProperty to the underlying conf file.
// This figures out which ConfFile to add it to and what line(s) to change within it.
settingInfo.PropertyReference = AddPermanent(section, key, value, comment, options);
}
}
// Local function that adds a RawProperty to the underlying ConfFile.
LineReference AddPermanent(string section, string key, string value, string? comment, ModifyOptions options)
{
// First, try to add the setting to the proper section (if the section already exists).
if ((options & ModifyOptions.AppendOverrideOnly) != ModifyOptions.AppendOverrideOnly
&& TryAddToExistingSection(section, key, value, comment, out LineReference? propertyReference))
{
return propertyReference;
}
// Otherwise, add it to the end of the base file (effectively how ASSS saves conf changes).
return AddToBaseAsOverride(section, key, value, comment);
// Local function that tries to add a RawProperty to an existing section.
bool TryAddToExistingSection(string section, string key, string value, string? comment, [MaybeNullWhen(false)] out LineReference propertyReference)
{
// Find the section.
int docIndex = IndexOfSection(section);
if (docIndex == -1)
{
// The section doesn't exist yet.
propertyReference = null;
return false;
}
ConfFile file = _lines[docIndex].File;
// Find the last property in the section, as long as it's in the same file.
for (int i = docIndex + 1; i < _lines.Count; i++)
{
if (_lines[i].File != file || _lines[i].Line.LineType == ConfLineType.Section)
break;
if (_lines[i].Line.LineType == ConfLineType.Property)
docIndex = i;
}
// Find the spot in the file to insert the property.
int fileIndex = file.Lines.IndexOf(_lines[docIndex].Line);
if (fileIndex == -1)
{
// Couldn't find the spot in the file to insert the property to.
propertyReference = null;
return false;
}
RawProperty propertyToInsert = new(
sectionOverride: null,
key: key,
value: value,
hasDelimiter: value is not null);
// Change the file
_updatingFile = file;
try
{
if (!string.IsNullOrWhiteSpace(comment))
{
file.Lines.Insert(
++fileIndex,
new RawComment(RawComment.DefaultCommentChar, comment));
}
file.Lines.Insert(++fileIndex, propertyToInsert);
file.SetDirty();
}
finally
{
_updatingFile = null;
}
propertyReference = new LineReference(propertyToInsert, file);
// Add it to the lines we consider active.
_lines.Insert(docIndex + 1, propertyReference);
return true;
// Local function that finds the index of a section in _lines. -1 if not found.
int IndexOfSection(ReadOnlySpan<char> section)
{
for (int docIndex = _lines.Count - 1; docIndex >= 0; docIndex--)
{
LineReference lineReference = _lines[docIndex];
if (lineReference.Line.LineType == ConfLineType.Section
&& lineReference.Line is RawSection rawSection
&& section.Equals(rawSection.Name, StringComparison.OrdinalIgnoreCase))
{
return docIndex;
}
}
return -1; // not found
}
}
// Local function that adds a RawProperty to the end of the base ConfFile with a section override.
LineReference AddToBaseAsOverride(string section, string key, string value, string? comment)
{
RawProperty propertyToAdd = new(
sectionOverride: section,
key: key,
value: value,
hasDelimiter: value is not null);
_updatingFile = _baseFile;
try
{
if (!string.IsNullOrWhiteSpace(comment))
{
_baseFile!.Lines.Add(new RawComment(RawComment.DefaultCommentChar, comment));
}
_baseFile!.Lines.Add(propertyToAdd);
_baseFile.SetDirty();
}
finally
{
_updatingFile = null;
}
LineReference lineRef = new(propertyToAdd, _baseFile);
_lines.Add(lineRef);
return lineRef;
}
}
// Local function that updates the value of an existing ConfFile's RawProperty.
void UpdatePermanent(SettingInfo settingInfo, string value, string? comment, ModifyOptions options)
{
_updatingFile = settingInfo.PropertyReference!.File;
try
{
int fileIndex = _updatingFile.Lines.IndexOf(settingInfo.PropertyReference.Line);
int docIndex = _lines.IndexOf(settingInfo.PropertyReference);
if (fileIndex != -1 && docIndex != -1)
{
RawProperty old = (RawProperty)settingInfo.PropertyReference.Line;
RawProperty replacement = new(
sectionOverride: old.SectionOverride,
key: old.Key,
value: value,
hasDelimiter: value is not null || old.HasDelimiter);
_updatingFile.Lines[fileIndex] = replacement;
LineReference lineReference = new(replacement, _updatingFile);
_lines[docIndex] = lineReference;
settingInfo.PropertyReference = lineReference;
// update comment line(s)
if (!string.IsNullOrWhiteSpace(comment))
{
_updatingFile.Lines.Insert(
fileIndex,
new RawComment(RawComment.DefaultCommentChar, comment));
if ((options & ModifyOptions.LeaveExistingComments) != ModifyOptions.LeaveExistingComments)
{
// remove old comments (comment lines immediately before the property line)
int commentIndex = fileIndex;
while (--commentIndex >= 0 && _updatingFile.Lines[commentIndex].LineType == ConfLineType.Comment)
{
_updatingFile.Lines.RemoveAt(commentIndex);
}
}
}
_updatingFile.SetDirty();
}
}
finally
{
_updatingFile = null;
}
}
}
#region Helper types
/// <summary>
/// Information for one setting, including its value and optionally, what <see cref="ConfFile"/> line it is related to.
/// </summary>
private class SettingInfo
{
/// <summary>
/// The current value of the setting.
/// </summary>
public required string Value { get; set; }
/// <summary>
/// The underlying line and file that the setting is related to.
/// <see langword="null"/> for a newly added, non-permanent setting.
/// </summary>
public LineReference? PropertyReference { get; set; }
}
#endregion
}
}