This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigValue.cs
53 lines (41 loc) · 1.67 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
47
48
49
50
51
52
53
using System;
using System.Linq;
using MelonLoader;
using MerinoClient.Core.Managers;
namespace MerinoClient.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 string DisplayName => _entry.DisplayName;
public string Name => _entry.Identifier;
public ConfigValue(string name, T defaultValue, string displayName = null, MelonPreferences_Category customCategory = null, string description = null, bool isHidden = true)
{
var category = MelonPreferences.CreateCategory(customCategory == null ? ConfigManager.Instance.CategoryName : customCategory.Identifier);
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 void SoftSetValue(T value)
{
//TODO: make a better solution for MirroredWingToggle not saving preferences every-time it's made
_entry.Value = value;
}
public override string ToString()
{
return _entry.Value.ToString();
}
}
}