-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigValue.cs
46 lines (38 loc) · 1.58 KB
/
ConfigValue.cs
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
using System;
using System.Linq;
using System.Reflection;
using MelonLoader;
using ReMod.Core.Managers;
namespace ReMod.Core
{
public class ConfigValue<T>
{
public event Action OnValueChanged;
private readonly MelonPreferences_Entry<T> _entry;
public T Value => _entry.Value;
public T DefaultValue => _entry.DefaultValue;
public ConfigValue(string name, T defaultValue, string displayName = null, string description = null, bool isHidden = false, string filePath = null)
{
if(!ConfigManager.Instances.ContainsKey(Assembly.GetCallingAssembly().Location))
throw new Exception("ConfigManager was not found. Please create it first.");
var category = MelonPreferences.CreateCategory(ConfigManager.Instances[Assembly.GetCallingAssembly().Location]);
if(filePath != null) category.SetFilePath(filePath);
var entryName = string.Concat(name.Where(c => char.IsLetter(c) || char.IsNumber(c)));
_entry = category.GetEntry<T>(entryName) ?? category.CreateEntry(entryName, defaultValue, displayName, description, isHidden);
_entry.OnValueChangedUntyped += () => OnValueChanged?.Invoke();
}
public static implicit operator T(ConfigValue<T> conf)
{
return conf._entry.Value;
}
public void SetValue(T value)
{
_entry.Value = value;
MelonPreferences.Save();
}
public override string ToString()
{
return _entry.Value.ToString();
}
}
}