miuu-diamond-time-viewer/Config.cs

106 lines
2.1 KiB
C#
Raw Normal View History

2023-08-22 21:07:51 -04:00
using I2.Loc.SimpleJSON;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
namespace DiamondTimeViewer
2023-08-22 21:07:51 -04:00
{
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 bool HideSilver = false;
2023-08-22 21:07:51 -04:00
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;
2023-08-22 21:07:51 -04:00
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;
}
}
if (data["hideSilver"] != null)
HideSilver = data["hideSilver"].AsBool;
2023-08-22 21:07:51 -04:00
}
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;
}
node.Add("hideSilver", HideSilver ? "true" : "false");
2024-01-01 15:11:59 -05:00
File.WriteAllText(GetConfigPath(), JsonHelper.FormatJson(node.ToString(), "\t"));
2023-08-22 21:07:51 -04:00
}
public static string GetConfigPath()
{
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_FILE_NAME);
}
}
}