-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModSettings.cs
53 lines (46 loc) · 1.59 KB
/
ModSettings.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 JetBrains.Annotations;
using SFS.IO;
using SFS.Parsers.Json;
namespace UITools
{
/// <summary>
/// Abstract class that let you easily create your mod configuration
/// </summary>
/// <typeparam name="T">Data type which will be stored in config file</typeparam>
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
public abstract class ModSettings<T> where T : new()
{
/// <summary>
/// Static variable for getting settings
/// </summary>
public static T settings;
/// <summary>
/// Getting settings file path
/// </summary>
protected abstract FilePath SettingsFile { get; }
void Load()
{
settings = SettingsFile.FileExists() ? JsonWrapper.FromJson<T>(SettingsFile.ReadText()) : new T();
settings ??= new T();
}
void Save()
{
SettingsFile.WriteText(JsonWrapper.ToJson(settings, true));
}
/// <summary>
/// You should call this function after creating an instance of class
/// </summary>
public void Initialize()
{
Load();
Save();
RegisterOnVariableChange(Save);
}
/// <summary>
/// Allow you to subscribe save event to config variables change
/// </summary>
/// <param name="onChange">Action that you should subscribe to data variables onChange</param>
protected abstract void RegisterOnVariableChange(Action onChange);
}
}