-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.cs
More file actions
112 lines (93 loc) · 4.36 KB
/
ConfigManager.cs
File metadata and controls
112 lines (93 loc) · 4.36 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using Rage;
namespace INIUtility{
public sealed class ConfigManager{
private readonly string _filePath;
private InitializationFile _iniFile;
public ConfigManager(string filePath)
{
_filePath = filePath;
_iniFile = new InitializationFile(_filePath);
_iniFile.Create();
}
public T GetValue<T>(string section, string key, T defaultValue, string comment = "")
{
if (_iniFile.DoesKeyExist(section, key)) return ReadValue(section, key, defaultValue);
Game.LogTrivial($"ReportsPlus {{CONFIG}}: Key '{key}' not found in section '{section}'. Creating with default value '{defaultValue}'.");
string valueStr;
if (defaultValue is Color color)
valueStr = $"{color.A},{color.R},{color.G},{color.B}";
else
valueStr = Convert.ToString(defaultValue, CultureInfo.InvariantCulture);
WriteIniEntryManual(_filePath, section, key, valueStr, comment);
_iniFile = new InitializationFile(_filePath);
return ReadValue(section, key, defaultValue);
}
/// <summary>
/// Writes a value to the INI file.
/// </summary>
public void SetValue<T>(string section, string key, T value)
{
try
{
string valueStr;
// Handle Color serialization specifically to match GetValue
if (value is Color color)
valueStr = $"{color.A},{color.R},{color.G},{color.B}";
else
valueStr = Convert.ToString(value, CultureInfo.InvariantCulture);
_iniFile.Write(section, key, valueStr);
}
catch (Exception ex)
{
Game.LogTrivial($"ReportsPlus {{CONFIG}}: Failed to write key '{key}' in section '{section}'. Error: {ex.Message}");
}
}
private void WriteIniEntryManual(string path, string section, string key, string value, string comment)
{
var lines = File.Exists(path) ? File.ReadAllLines(path).ToList() : new List<string>();
var formattedSection = $"[{section}]";
var sectionIndex = lines.FindIndex(line => line.Trim().Equals(formattedSection, StringComparison.OrdinalIgnoreCase));
var linesToAdd = new List<string>();
if (!string.IsNullOrWhiteSpace(comment)) linesToAdd.Add($"; {comment}");
linesToAdd.Add($"{key}={value}");
if (sectionIndex != -1)
{
var nextSectionIndex = lines.FindIndex(sectionIndex + 1, line => line.Trim().StartsWith("["));
var insertIndex = nextSectionIndex == -1 ? lines.Count : nextSectionIndex;
lines.InsertRange(insertIndex, linesToAdd);
}
else
{
if (lines.Any() && !string.IsNullOrWhiteSpace(lines.Last())) lines.Add("");
lines.Add(formattedSection);
lines.AddRange(linesToAdd);
}
File.WriteAllLines(path, lines);
}
private T ReadValue<T>(string section, string key, T defaultValue)
{
try
{
var type = typeof(T);
if (type.IsEnum) return _iniFile.ReadEnum(section, key, defaultValue);
if (type != typeof(Color)) return (T)Convert.ChangeType(_iniFile.ReadString(section, key, defaultValue.ToString()), typeof(T), CultureInfo.InvariantCulture);
var colorStr = _iniFile.ReadString(section, key, "");
var parts = colorStr.Split(',');
if (parts.Length == 4 && int.TryParse(parts[0], out var a) && int.TryParse(parts[1], out var r) && int.TryParse(parts[2], out var g) && int.TryParse(parts[3], out var b))
return (T)(object)Color.FromArgb(a, r, g, b);
return defaultValue;
}
catch (Exception ex)
{
Game.LogTrivial($"ReportsPlus {{CONFIG}}: Failed to read or convert key '{key}' in section '{section}'. Using default value. Error: {ex.Message}");
return defaultValue;
}
}
}
}