| [509] | 1 | using System;
|
|---|
| 2 | using System.IO;
|
|---|
| 3 | using Newtonsoft.Json;
|
|---|
| 4 | using UnityEngine;
|
|---|
| 5 |
|
|---|
| 6 | namespace FiniteItemRepairs
|
|---|
| 7 | {
|
|---|
| 8 | public static class Settings
|
|---|
| 9 | {
|
|---|
| 10 | [JsonObject (MemberSerialization.Fields)]
|
|---|
| 11 | public class SettingsData
|
|---|
| 12 | {
|
|---|
| 13 | private float degradationPercent = 0.5f;
|
|---|
| 14 | public float DegradationPercent
|
|---|
| 15 | {
|
|---|
| 16 | get => degradationPercent;
|
|---|
| 17 | set
|
|---|
| 18 | {
|
|---|
| 19 | if (Mathf.Approximately(value, degradationPercent))
|
|---|
| 20 | {
|
|---|
| 21 | return;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | degradationPercent = Mathf.Clamp01(value);
|
|---|
| 25 | Save();
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | public static SettingsData Data { get; private set; }
|
|---|
| 31 |
|
|---|
| 32 | private static bool initDone;
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | public static void Init()
|
|---|
| 36 | {
|
|---|
| 37 | if (initDone)
|
|---|
| 38 | {
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | initDone = true;
|
|---|
| 43 | InitFileWatcher();
|
|---|
| 44 | Load();
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | #region IO
|
|---|
| 48 |
|
|---|
| 49 | private static readonly string ConfigFileName = $"{ModMain.Mod.Name}_Settings.json";
|
|---|
| 50 |
|
|---|
| 51 | private static FileSystemWatcher fileWatcher;
|
|---|
| 52 |
|
|---|
| 53 | private static void InitFileWatcher()
|
|---|
| 54 | {
|
|---|
| 55 | fileWatcher = new FileSystemWatcher(GetFilePath(), ConfigFileName);
|
|---|
| 56 | fileWatcher.Changed += OnFileChanged;
|
|---|
| 57 | fileWatcher.Created += OnFileChanged;
|
|---|
| 58 | fileWatcher.Deleted += OnFileChanged;
|
|---|
| 59 | fileWatcher.EnableRaisingEvents = true;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public static void DestroyFileWatcher()
|
|---|
| 63 | {
|
|---|
| 64 | if (fileWatcher == null)
|
|---|
| 65 | {
|
|---|
| 66 | return;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | fileWatcher.Dispose();
|
|---|
| 70 | fileWatcher = null;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | private static void OnFileChanged(object source, FileSystemEventArgs e)
|
|---|
| 74 | {
|
|---|
| 75 | Log.Out($"[MOD] [{ModMain.Mod.DisplayName}] Reloading {ConfigFileName}");
|
|---|
| 76 | Load();
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | private static string GetFilePath()
|
|---|
| 80 | {
|
|---|
| 81 | return GameIO.GetUserGameDataDir();
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private static string GetFullPath()
|
|---|
| 85 | {
|
|---|
| 86 | return $"{GetFilePath()}/{ConfigFileName}";
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | private static void Load()
|
|---|
| 90 | {
|
|---|
| 91 | if (!SdFile.Exists(GetFullPath()))
|
|---|
| 92 | {
|
|---|
| 93 | Log.Out($"[MOD] [{ModMain.Mod.DisplayName}] config file '{ConfigFileName}' not found, creating.");
|
|---|
| 94 | Data = new SettingsData();
|
|---|
| 95 | Save();
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | Log.Out($"[MOD] [{ModMain.Mod.DisplayName}] Loading config file from '{GetFullPath()}'");
|
|---|
| 100 |
|
|---|
| 101 | try
|
|---|
| 102 | {
|
|---|
| 103 | string jsonText = SdFile.ReadAllText(GetFullPath());
|
|---|
| 104 | Data = JsonConvert.DeserializeObject<SettingsData>(jsonText);
|
|---|
| 105 | }
|
|---|
| 106 | catch (JsonException e)
|
|---|
| 107 | {
|
|---|
| 108 | Log.Error($"[MOD] [{ModMain.Mod.DisplayName}] Exception while trying to load config file:");
|
|---|
| 109 | Log.Exception(e);
|
|---|
| 110 |
|
|---|
| 111 | Data = new SettingsData();
|
|---|
| 112 | Save();
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public static void Save()
|
|---|
| 117 | {
|
|---|
| 118 | try
|
|---|
| 119 | {
|
|---|
| 120 | string jsonText = JsonConvert.SerializeObject(Data);
|
|---|
| 121 |
|
|---|
| 122 | fileWatcher.EnableRaisingEvents = false;
|
|---|
| 123 | SdFile.WriteAllText(GetFullPath(), jsonText);
|
|---|
| 124 | fileWatcher.EnableRaisingEvents = true;
|
|---|
| 125 |
|
|---|
| 126 | Log.Out($"[MOD] [{ModMain.Mod.DisplayName}] Saved config file to '{GetFullPath()}'.");
|
|---|
| 127 | }
|
|---|
| 128 | catch (Exception e)
|
|---|
| 129 | {
|
|---|
| 130 | Log.Error($"[MOD] [{ModMain.Mod.DisplayName}] Exception while trying to save config file to '{GetFullPath()}':");
|
|---|
| 131 | Log.Exception(e);
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | #endregion
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|