This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSettings.cs
71 lines (62 loc) · 2.01 KB
/
Settings.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NoStopMod.Helper;
using NoStopMod.Helper.Abstraction;
using SimpleJSON;
namespace NoStopMod
{
public class Settings
{
public const String path = "\\Mods\\NoStopMod\\settings.json";
public static List<SettingsBase> settings = new List<SettingsBase>();
public static EventListener<bool> settingsLoadListener = new EventListener<bool>("SettingsLoad");
public static void Init()
{
NoStopMod.onToggleListener.Add(_ => Load());
NoStopMod.onApplicationQuitListener.Add(_ => Save());
}
public static void Load()
{
try
{
string text = File.ReadAllText(Environment.CurrentDirectory + path);
JSONNode jsonNode = JSON.Parse(text);
for (int i = 0; i < settings.Count(); i++)
{
try
{
settings[i].Load(ref jsonNode);
}
catch (Exception e)
{
NoStopMod.mod.Logger.Error("While Loading : " + e.Message + ", " + e.StackTrace);
}
}
settingsLoadListener.Invoke(true);
}
catch
{
Save();
settingsLoadListener.Invoke(false);
}
}
public static void Save()
{
JSONNode node = JSONHelper.CreateEmptyNode();
for (int i = 0; i < settings.Count();i++)
{
try
{
settings[i].Save(ref node);
}
catch (Exception e)
{
NoStopMod.mod.Logger.Error("While Saving : " + e.Message + ", " + e.StackTrace);
}
}
File.WriteAllText(Environment.CurrentDirectory + path, node.ToString());
}
}
}