mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-22 00:04:57 +01:00
The settings keep only the keys master reads, Vulkan left out. The standard mode is the old starter's panel; the advanced mode opens behind a warning whose yes takes a click only after 20 seconds in front of the player. - Sliders go by fixed steps (SliderRanges), held to the standard ranges there: fpslimit up to 240, sleeperdistance up to 1000 m. - On the first start everyone is moved to the standard mode once: what is outside it goes back to its default, the sliders' values to their ranges, and a copy of eu07.ini is kept as eu07.ini.before-standard. - "Restore defaults" puts every setting back, the language, the simulator, the UART port and the control keys kept. - Monitors and renderers are found anew (Monitors, Renderers), Displays and SettingReach gone. - mousescale writes its second number as the old starter did, 0.5 or -0.5 for an inverted vertical axis; an axis that would not move gets the default. - Depot, in the standard mode: no AI or drivable-only filters, empty consists not listed, the crew shown but not changed. - Randomizing the textures of a whole scenery no longer rebuilds the view per consist, and finds its candidates by mini, so the window keeps answering. - A long operator in the vehicle list is cut short instead of over the name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
325 lines
9.7 KiB
C#
325 lines
9.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace StarterNG.Classes;
|
|
|
|
public sealed class ConfigFile
|
|
{
|
|
private readonly List<string> _lines = new();
|
|
|
|
private readonly Dictionary<string, int> _index =
|
|
new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
// The simulator stops reading at this word (Globals.cpp), whatever its case.
|
|
private const string EndKey = "endconfig";
|
|
|
|
// Its first line, or -1 when the file runs to its end.
|
|
private int _end = -1;
|
|
|
|
// While lines are indexed: the one at hand starts inside a block comment.
|
|
private bool _inBlock;
|
|
|
|
public static ConfigFile Parse(string text)
|
|
{
|
|
var cfg = new ConfigFile();
|
|
|
|
text = text.Replace("\r\n", "\n").Replace('\r', '\n');
|
|
foreach (var line in text.Split('\n'))
|
|
cfg.AddLine(line);
|
|
return cfg;
|
|
}
|
|
|
|
private void AddLine(string raw)
|
|
{
|
|
_lines.Add(raw);
|
|
IndexLine(_lines.Count - 1);
|
|
}
|
|
|
|
private void IndexLine(int line)
|
|
{
|
|
// The simulator skips what /* */ encloses, over as many lines as it runs.
|
|
bool startsInBlock = _inBlock;
|
|
_inBlock = EndsInBlock(_lines[line], startsInBlock);
|
|
if (startsInBlock || !TryParseKey(_lines[line], out string key))
|
|
return;
|
|
|
|
if (_end < 0 && key.Equals(EndKey, StringComparison.OrdinalIgnoreCase))
|
|
_end = line;
|
|
|
|
// Of a key on both sides of the end, the line the simulator reads counts.
|
|
if (_end < 0 || line == _end || !_index.TryGetValue(key, out int seen) || seen > _end)
|
|
_index[key] = line;
|
|
}
|
|
|
|
private void Reindex()
|
|
{
|
|
_index.Clear();
|
|
_end = -1;
|
|
_inBlock = false;
|
|
for (int line = 0; line < _lines.Count; line++)
|
|
IndexLine(line);
|
|
}
|
|
|
|
public bool Has(string key) => _index.ContainsKey(key);
|
|
|
|
/// <summary>The line the key is on, or -1. The simulator reads the file top down, so a later line wins.</summary>
|
|
public int LineOf(string key) => _index.TryGetValue(key, out int i) ? i : -1;
|
|
|
|
/// <summary>Whether the line setting the key is one the simulator never gets to.</summary>
|
|
public bool IsPastEnd(string key) =>
|
|
_end >= 0 && _index.TryGetValue(key, out int line) && line > _end;
|
|
|
|
/// <summary>Every key the file sets, in the order of its lines, with its value.</summary>
|
|
public IEnumerable<KeyValuePair<string, string>> Entries()
|
|
{
|
|
foreach (var (key, _) in _index.OrderBy(pair => pair.Value))
|
|
yield return new KeyValuePair<string, string>(key, GetRaw(key) ?? string.Empty);
|
|
}
|
|
|
|
public string? GetRaw(string key)
|
|
{
|
|
if (!_index.TryGetValue(key, out int i))
|
|
return null;
|
|
SplitLine(_lines[i], out _, out _, out string value, out _);
|
|
return value;
|
|
}
|
|
|
|
public string GetString(string key, string fallback) =>
|
|
GetRaw(key) is { Length: > 0 } v ? v : fallback;
|
|
|
|
public bool GetBool(string key, bool fallback)
|
|
{
|
|
var v = FirstToken(GetRaw(key));
|
|
if (v is null) return fallback;
|
|
return v.ToLowerInvariant() switch
|
|
{
|
|
"yes" or "true" or "1" or "tak" => true,
|
|
"no" or "false" or "0" or "nie" => false,
|
|
_ => fallback
|
|
};
|
|
}
|
|
|
|
public int GetInt(string key, int fallback)
|
|
{
|
|
var v = FirstToken(GetRaw(key));
|
|
return v != null && int.TryParse(v, NumberStyles.Integer,
|
|
CultureInfo.InvariantCulture, out int n) ? n : fallback;
|
|
}
|
|
|
|
public double GetDouble(string key, double fallback)
|
|
{
|
|
var v = FirstToken(GetRaw(key));
|
|
return v != null && double.TryParse(v, NumberStyles.Float,
|
|
CultureInfo.InvariantCulture, out double d) ? d : fallback;
|
|
}
|
|
|
|
public string[] GetTokens(string key)
|
|
{
|
|
var v = GetRaw(key);
|
|
return string.IsNullOrEmpty(v)
|
|
? Array.Empty<string>()
|
|
: v.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);
|
|
}
|
|
|
|
public void Set(string key, string value)
|
|
{
|
|
value ??= string.Empty;
|
|
if (_index.TryGetValue(key, out int i))
|
|
{
|
|
SplitLine(_lines[i], out string indent, out _, out _, out string comment);
|
|
string line = Line(indent, key, value, comment);
|
|
|
|
if (i > _end && _end >= 0)
|
|
{
|
|
_lines.RemoveAt(i);
|
|
InsertBeforeEnd(line);
|
|
}
|
|
else
|
|
{
|
|
_lines[i] = line;
|
|
}
|
|
}
|
|
else if (_end >= 0)
|
|
{
|
|
InsertBeforeEnd(Line("", key, value, ""));
|
|
}
|
|
else
|
|
{
|
|
int idx = _lines.Count;
|
|
_lines.Add(Line("", key, value, ""));
|
|
_index[key] = idx;
|
|
}
|
|
}
|
|
|
|
private static string Line(string indent, string key, string value, string comment)
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.Append(indent).Append(key);
|
|
if (value.Length > 0)
|
|
sb.Append(' ').Append(value);
|
|
if (comment.Length > 0)
|
|
sb.Append('\t').Append(comment);
|
|
return sb.ToString();
|
|
}
|
|
|
|
private void InsertBeforeEnd(string line)
|
|
{
|
|
_lines.Insert(_end, line);
|
|
Reindex();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Puts the line of <paramref name="key"/> right below that of <paramref name="anchor"/> when
|
|
/// it is above it, for keys of which the simulator, reading top down, heeds the later.
|
|
/// </summary>
|
|
public void MoveAfter(string key, string anchor)
|
|
{
|
|
if (!_index.TryGetValue(key, out int from) || !_index.TryGetValue(anchor, out int to) || from > to)
|
|
return;
|
|
|
|
string line = _lines[from];
|
|
_lines.RemoveAt(from);
|
|
_lines.Insert(to, line);
|
|
Reindex();
|
|
}
|
|
|
|
/// <summary>Comments out every line setting the key: the last one alone would bring the one before it back.</summary>
|
|
public void Remove(string key)
|
|
{
|
|
if (!_index.ContainsKey(key))
|
|
return;
|
|
|
|
for (int i = 0; i < _lines.Count; i++)
|
|
{
|
|
SplitLine(_lines[i], out string indent, out string found, out string value, out string comment);
|
|
if (!found.Equals(key, StringComparison.OrdinalIgnoreCase))
|
|
continue;
|
|
|
|
_lines[i] = Line(indent + "// ", found, value, comment);
|
|
}
|
|
|
|
_index.Remove(key);
|
|
}
|
|
|
|
public void SetBool(string key, bool value) => Set(key, value ? "yes" : "no");
|
|
public void SetInt(string key, int value) =>
|
|
Set(key, value.ToString(CultureInfo.InvariantCulture));
|
|
public void SetDouble(string key, double value) =>
|
|
Set(key, value.ToString("0.###", CultureInfo.InvariantCulture));
|
|
|
|
public string ToText() => string.Join(Environment.NewLine, _lines);
|
|
|
|
private static bool TryParseKey(string raw, out string key)
|
|
{
|
|
SplitLine(raw, out _, out key, out _, out _);
|
|
return key.Length > 0;
|
|
}
|
|
|
|
private static void SplitLine(string raw, out string indent, out string key,
|
|
out string value, out string comment)
|
|
{
|
|
key = value = comment = string.Empty;
|
|
|
|
int start = 0;
|
|
while (start < raw.Length && char.IsWhiteSpace(raw[start]))
|
|
start++;
|
|
indent = raw.Substring(0, start);
|
|
|
|
string body = raw.Substring(start);
|
|
|
|
if (body.StartsWith("//"))
|
|
{
|
|
comment = body;
|
|
return;
|
|
}
|
|
if (body.Length == 0)
|
|
return;
|
|
|
|
int commentAt = FindCommentStart(body);
|
|
string content = commentAt >= 0 ? body.Substring(0, commentAt) : body;
|
|
if (commentAt >= 0)
|
|
comment = body.Substring(commentAt).TrimEnd();
|
|
|
|
content = content.TrimEnd();
|
|
if (content.Length == 0)
|
|
return;
|
|
|
|
int sp = IndexOfWhitespace(content);
|
|
if (sp < 0)
|
|
{
|
|
key = content;
|
|
}
|
|
else
|
|
{
|
|
key = content.Substring(0, sp);
|
|
value = content.Substring(sp).Trim();
|
|
}
|
|
}
|
|
|
|
// Where a comment starts as the simulator's parser sees one: // or /* anywhere, even glued
|
|
// to a value, but not within quotes, which it keeps whole.
|
|
private static int FindCommentStart(string body)
|
|
{
|
|
bool quoted = false;
|
|
for (int i = 0; i < body.Length; i++)
|
|
{
|
|
if (body[i] == '"')
|
|
quoted = !quoted;
|
|
else if (!quoted && i + 1 < body.Length && body[i] == '/' && body[i + 1] is '/' or '*')
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Whether a block comment is still open at the end of the line.
|
|
private static bool EndsInBlock(string raw, bool inBlock)
|
|
{
|
|
bool quoted = false;
|
|
for (int i = 0; i < raw.Length; i++)
|
|
{
|
|
if (inBlock)
|
|
{
|
|
if (raw[i] == '*' && i + 1 < raw.Length && raw[i + 1] == '/')
|
|
{
|
|
inBlock = false;
|
|
i++;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (raw[i] == '"')
|
|
quoted = !quoted;
|
|
else if (!quoted && raw[i] == '/' && i + 1 < raw.Length)
|
|
{
|
|
if (raw[i + 1] == '/')
|
|
return false;
|
|
if (raw[i + 1] == '*')
|
|
{
|
|
inBlock = true;
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
return inBlock;
|
|
}
|
|
|
|
private static int IndexOfWhitespace(string s)
|
|
{
|
|
for (int i = 0; i < s.Length; i++)
|
|
if (char.IsWhiteSpace(s[i]))
|
|
return i;
|
|
return -1;
|
|
}
|
|
|
|
private static string? FirstToken(string? value)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
return null;
|
|
int sp = IndexOfWhitespace(value);
|
|
return sp < 0 ? value : value.Substring(0, sp);
|
|
}
|
|
}
|