miuu-diamond-time-viewer/Config.cs
2023-10-06 21:26:05 -04:00

100 lines
1.9 KiB
C#

using I2.Loc.SimpleJSON;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
namespace DiamondTimeViewer
{
public enum DisplayMode
{
Never,
Diamond,
Gold,
Always,
}
public class Config
{
private const string CONFIG_FILE_NAME = "config.json";
public static bool Enabled = true;
public static DisplayMode Mode = DisplayMode.Diamond;
public static void Init()
{
if (File.Exists(GetConfigPath()))
ReadConfig();
else
SaveConfig();
}
public static void ReadConfig()
{
if (!File.Exists(GetConfigPath()))
return;
JSONNode data;
try
{
data = JSON.Parse(File.ReadAllText(GetConfigPath()));
}
catch (Exception e)
{
Debug.LogError("Exception reading DiamondViewer config: " + e.Message);
return;
}
if (data["enabled"] != null)
Enabled = data["enabled"].AsBool;
if (data["mode"] != null)
{
string mode = data["mode"].Value.ToLower();
switch(mode)
{
case "never":
Mode = DisplayMode.Never;
break;
case "diamond":
Mode = DisplayMode.Diamond;
break;
case "gold":
Mode = DisplayMode.Gold;
break;
case "always":
Mode = DisplayMode.Always;
break;
}
}
}
public static void SaveConfig()
{
JSONNode node = new JSONClass();
node.Add("enabled", Enabled ? "true" : "false");
switch(Mode)
{
case DisplayMode.Never:
node.Add("mode", "never");
break;
case DisplayMode.Diamond:
node.Add("mode", "diamond");
break;
case DisplayMode.Gold:
node.Add("mode", "gold");
break;
case DisplayMode.Always:
node.Add("mode", "always");
break;
}
File.WriteAllText(GetConfigPath(), node.ToString());
}
public static string GetConfigPath()
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_FILE_NAME);
}
}
}