-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigLoader.cs
More file actions
63 lines (50 loc) · 2.42 KB
/
ConfigLoader.cs
File metadata and controls
63 lines (50 loc) · 2.42 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
using System.Reflection;
using Rage;
namespace INIUtility{
public static class ConfigLoader{
public static T LoadSettings<T>(string iniPath) where T : new()
{
Game.LogTrivial("IniConfigUtility: Loading settings...");
var manager = new ConfigManager(iniPath);
var settings = new T();
foreach (var prop in typeof(T).GetProperties())
{
var attr = prop.GetCustomAttribute<ConfigOptionAttribute>();
if (attr == null) continue;
var defaultValue = prop.GetValue(settings);
var getValueMethod = typeof(ConfigManager).GetMethod(nameof(ConfigManager.GetValue))?.MakeGenericMethod(prop.PropertyType);
if (getValueMethod != null)
{
var loadedValue = getValueMethod.Invoke(manager, new[] { attr.Section, attr.Key, defaultValue, attr.Comment });
prop.SetValue(settings, loadedValue);
}
else
{
Game.LogTrivial($"IniConfigUtility: Failed to get value for property '{prop.Name}' from section '{attr.Section}' with key '{attr.Key}'. Using default value.");
}
}
Game.LogTrivial("IniConfigUtility: Settings loaded successfully.");
return settings;
}
/// <summary>
/// Saves the current settings object back to the INI file.
/// </summary>
public static void SaveSettings<T>(T settings, string iniPath)
{
Game.LogTrivial("IniConfigUtility: Saving settings...");
var manager = new ConfigManager(iniPath);
foreach (var prop in typeof(T).GetProperties())
{
var attr = prop.GetCustomAttribute<ConfigOptionAttribute>();
if (attr == null) continue;
var value = prop.GetValue(settings);
var setValueMethod = typeof(ConfigManager).GetMethod(nameof(ConfigManager.SetValue))?.MakeGenericMethod(prop.PropertyType);
if (setValueMethod != null)
setValueMethod.Invoke(manager, new[] { attr.Section, attr.Key, value });
else
Game.LogTrivial($"IniConfigUtility: Failed to find SetValue method for property '{prop.Name}'.");
}
Game.LogTrivial("IniConfigUtility: Settings saved successfully.");
}
}
}