diff --git a/src/BmsConfig/Core/CfgDocument.cs b/src/BmsConfig/Core/CfgDocument.cs index 39d8e9b..ec5b075 100644 --- a/src/BmsConfig/Core/CfgDocument.cs +++ b/src/BmsConfig/Core/CfgDocument.cs @@ -16,6 +16,8 @@ public enum CfgLineKind Set, /// The Launcher's cut line, in Falcon BMS User.cfg. CutLine, + /// A commented-out "set" line, eg. "// set {name} {value} // note". + CommentedSet, } /// @@ -27,7 +29,7 @@ public sealed class CfgLine public string Text { get; internal set; } public CfgLineKind Kind { get; internal set; } - // Kind == Set + // Kind == Set or CommentedSet public string Name { get; internal set; } public string RawValue { get; internal set; } public string Description { get; internal set; } @@ -54,6 +56,11 @@ public sealed class CfgDocument @"^\s*set\s+(?[A-Za-z_][A-Za-z0-9_]*)\s+(?""[^""]*""|[^\s""]+)\s*(?:(?://|#)\s?(?.*))?$", RegexOptions.CultureInvariant); + // A commented-out set line: the comment marker, then a well-formed set line. + static readonly Regex s_commentedSetLine = new Regex( + @"^(?\s*)(?://|#)\s*(?set\s.*)$", + RegexOptions.CultureInvariant); + readonly List _lines = new List(); public string FilePath { get; private set; } @@ -122,6 +129,14 @@ public static CfgLine ParseLine(string text) return line; } + // Checked before categories: "// set g_bFoo 1 //" is a commented-out param, not a category header. + Match commented = s_commentedSetLine.Match(text); + if (commented.Success && TryParseSet(line, text, commented.Groups["body"].Index)) + { + line.Kind = CfgLineKind.CommentedSet; + return line; + } + if (trimmed.Length >= 4 && trimmed.StartsWith("//", StringComparison.Ordinal) && trimmed.EndsWith("//", StringComparison.Ordinal)) @@ -136,27 +151,57 @@ public static CfgLine ParseLine(string text) return line; } - Match m = s_setLine.Match(text); - if (m.Success) - { - string name = m.Groups["name"].Value; - string value = m.Groups["value"].Value; - - // Values that don't conform to the param's datatype are ignored (treated as comments). - if (!ValueConverter.IsValidCfgToken(ParamTypes.FromName(name), value)) - return line; - + if (TryParseSet(line, text, 0)) line.Kind = CfgLineKind.Set; - line.Name = name; - line.RawValue = value; - line.ValueStart = m.Groups["value"].Index; - line.ValueLength = m.Groups["value"].Length; - line.Description = m.Groups["desc"].Success ? m.Groups["desc"].Value.Trim() : null; - } return line; } + /// + /// Parses the set statement starting at text[start], filling in the line's Name, RawValue etc. + /// (but not its Kind). Returns false if it isn't a well-formed set statement. + /// + static bool TryParseSet(CfgLine line, string text, int start) + { + Match m = s_setLine.Match(text.Substring(start)); + if (!m.Success) + return false; + + string name = m.Groups["name"].Value; + string value = m.Groups["value"].Value; + + // Values that don't conform to the param's datatype are ignored (treated as comments). + if (!ValueConverter.IsValidCfgToken(ParamTypes.FromName(name), value)) + return false; + + line.Name = name; + line.RawValue = value; + line.ValueStart = start + m.Groups["value"].Index; + line.ValueLength = m.Groups["value"].Length; + line.Description = m.Groups["desc"].Success ? m.Groups["desc"].Value.Trim() : null; + return true; + } + + /// True if the set line has a non-empty trailing comment, eg. a note the user wrote. + static bool HasTrailingComment(CfgLine line) + { + return !string.IsNullOrEmpty(line.Description); + } + + /// "set x 1 // note" -> "// set x 1 // note", keeping the indentation. + static string CommentOut(string text) + { + int indent = text.Length - text.TrimStart().Length; + return text.Substring(0, indent) + "// " + text.Substring(indent); + } + + /// "// set x 1 // note" -> "set x 1 // note", keeping the indentation. + static string Uncomment(string text) + { + Match m = s_commentedSetLine.Match(text); + return m.Groups["indent"].Value + m.Groups["body"].Value; + } + /// Index of the Launcher cut line, or -1 if none. public int CutLineIndex { @@ -179,7 +224,14 @@ public CfgLine FindLast(string name, bool aboveCutLine = false) return index >= 0 ? _lines[index] : null; } - int FindLastIndex(string name, bool aboveCutLine) + /// The last commented-out set line for the given param, or null. + public CfgLine FindLastCommented(string name, bool aboveCutLine = false) + { + int index = FindLastIndex(name, aboveCutLine, CfgLineKind.CommentedSet); + return index >= 0 ? _lines[index] : null; + } + + int FindLastIndex(string name, bool aboveCutLine, CfgLineKind kind = CfgLineKind.Set) { int end = _lines.Count; if (aboveCutLine) @@ -192,7 +244,7 @@ int FindLastIndex(string name, bool aboveCutLine) for (int i = end - 1; i >= 0; i--) { CfgLine line = _lines[i]; - if (line.Kind == CfgLineKind.Set && string.Equals(line.Name, name, StringComparison.OrdinalIgnoreCase)) + if (line.Kind == kind && string.Equals(line.Name, name, StringComparison.OrdinalIgnoreCase)) return i; } return -1; @@ -200,14 +252,22 @@ int FindLastIndex(string name, bool aboveCutLine) /// /// Sets a param's value. If a set line already exists, only its value token is replaced (preserving - /// whitespace and trailing comment). Otherwise a new line is inserted -- above the Launcher cut line - /// if there is one, else at the end of the file. + /// whitespace and trailing comment). Failing that, if a commented-out set line exists (eg. from + /// RemoveValue), it is uncommented and its value replaced. Otherwise a new line is inserted -- above + /// the Launcher cut line if there is one, else at the end of the file. /// /// The value as it should appear in the file (eg. already quoted for strings). /// If true, never edit or insert at/below the Launcher cut line. public void SetValue(string name, string serializedValue, bool respectCutLine) { int index = FindLastIndex(name, respectCutLine); + if (index < 0) + { + index = FindLastIndex(name, respectCutLine, CfgLineKind.CommentedSet); + if (index >= 0) + _lines[index] = ParseLine(Uncomment(_lines[index].Text)); + } + if (index >= 0) { CfgLine old = _lines[index]; @@ -230,7 +290,8 @@ public void SetValue(string name, string serializedValue, bool respectCutLine) /// /// Removes all set lines for the given param (only those above the cut line, if respectCutLine). - /// Returns the number of lines removed. + /// Lines with a trailing comment are commented out rather than deleted, so the user's notes are kept. + /// Returns the number of lines removed or commented out. /// public int RemoveValue(string name, bool respectCutLine) { @@ -238,7 +299,10 @@ public int RemoveValue(string name, bool respectCutLine) int index; while ((index = FindLastIndex(name, respectCutLine)) >= 0) { - _lines.RemoveAt(index); + if (HasTrailingComment(_lines[index])) + _lines[index] = ParseLine(CommentOut(_lines[index].Text)); + else + _lines.RemoveAt(index); removed++; } return removed; diff --git a/src/BmsConfig/Core/ConfigSet.cs b/src/BmsConfig/Core/ConfigSet.cs index a492d9c..981ab54 100644 --- a/src/BmsConfig/Core/ConfigSet.cs +++ b/src/BmsConfig/Core/ConfigSet.cs @@ -42,6 +42,11 @@ public sealed class ParamEntry /// Effective value in Falcon BMS VR.cfg, or null. public string VrValue { get; internal set; } + /// True if User.cfg has a commented-out set line for this param (above the cut line). + public bool UserCommentedOut { get; internal set; } + /// True if VR.cfg has a commented-out set line for this param. + public bool VrCommentedOut { get; internal set; } + /// Order of first appearance (Falcon BMS.cfg first, then User.cfg, then VR.cfg). public int Ordinal { get; internal set; } @@ -182,6 +187,14 @@ void Merge() if (e.Category == OtherCategory) hasOther = true; } + else if (line.Kind == CfgLineKind.CommentedSet && !belowCut) + { + // Listed even if it's not in Falcon BMS.cfg, so the user can set it again. + ParamEntry e = GetOrAdd(line, OtherCategory); + e.UserCommentedOut = true; + if (e.Category == OtherCategory) + hasOther = true; + } } // Falcon BMS VR.cfg: overrides, only when launched in VR. @@ -194,6 +207,13 @@ void Merge() if (e.Category == OtherCategory) hasOther = true; } + else if (line.Kind == CfgLineKind.CommentedSet) + { + ParamEntry e = GetOrAdd(line, OtherCategory); + e.VrCommentedOut = true; + if (e.Category == OtherCategory) + hasOther = true; + } } foreach (ParamEntry e in _entries) @@ -252,7 +272,8 @@ public static void WriteValue(string folder, CfgFile file, string name, string c /// /// Removes a param from User.cfg or VR.cfg (reverting it to the default), and saves immediately. - /// In User.cfg, only lines above the Launcher cut line are removed. + /// Lines with a trailing comment (eg. the user's notes) are commented out rather than deleted. + /// In User.cfg, only lines above the Launcher cut line are affected. /// public static bool RemoveValue(string folder, CfgFile file, string name) { diff --git a/src/BmsConfig/MainWindow.xaml b/src/BmsConfig/MainWindow.xaml index 0f674e7..455fb91 100644 --- a/src/BmsConfig/MainWindow.xaml +++ b/src/BmsConfig/MainWindow.xaml @@ -244,7 +244,7 @@