Config file and multiple modes

This commit is contained in:
Terry Hearst 2023-08-22 21:07:51 -04:00
parent 7792850970
commit dfe235ebf4
3 changed files with 123 additions and 1 deletions

101
Config.cs Normal file
View file

@ -0,0 +1,101 @@
using I2.Loc.SimpleJSON;
using System;
using System.IO;
using System.Reflection;
using System.Web;
using UnityEngine;
namespace MiuuDiamondTimeViewer
{
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);
}
}
}

View file

@ -13,6 +13,7 @@ namespace MiuuDiamondTimeViewer
public static void OnStartup() public static void OnStartup()
{ {
SceneManager.sceneLoaded += beginPatch; SceneManager.sceneLoaded += beginPatch;
Config.Init();
} }
private static void beginPatch(Scene scene, LoadSceneMode loadSceneMode) private static void beginPatch(Scene scene, LoadSceneMode loadSceneMode)

View file

@ -11,6 +11,9 @@ namespace MiuuDiamondTimeViewer.Patches
{ {
static bool Prefix(MedalsDisplay __instance, float silver, float gold, List<string> eggUnlock) static bool Prefix(MedalsDisplay __instance, float silver, float gold, List<string> eggUnlock)
{ {
if (!Config.Enabled)
return true;
// Get diamond time (since it is not passed in as a parameter) // Get diamond time (since it is not passed in as a parameter)
float diamond = LevelSelect.instance.level.DiamondTime; float diamond = LevelSelect.instance.level.DiamondTime;
@ -21,8 +24,25 @@ namespace MiuuDiamondTimeViewer.Patches
if (hasEgg) if (hasEgg)
eggText = (gotEgg ? "<space=0.5em> <sprite=9>" : "<space=0.5em> <sprite=8>"); eggText = (gotEgg ? "<space=0.5em> <sprite=9>" : "<space=0.5em> <sprite=8>");
bool showDiamondTime = false;
switch (Config.Mode)
{
case DisplayMode.Always:
showDiamondTime = true;
break;
case DisplayMode.Diamond:
showDiamondTime = LevelSelect.instance.bestScore > 0f && LevelSelect.instance.bestScore <= diamond;
break;
case DisplayMode.Gold:
showDiamondTime = LevelSelect.instance.bestScore > 0f && LevelSelect.instance.bestScore <= gold;
break;
case DisplayMode.Never:
showDiamondTime = false;
break;
}
string diamondText = ""; string diamondText = "";
if (LevelSelect.instance.bestScore > 0f && LevelSelect.instance.bestScore <= diamond) if (showDiamondTime)
diamondText = "<sprite=3> " + SegmentedTime.SPTimeText(diamond); diamondText = "<sprite=3> " + SegmentedTime.SPTimeText(diamond);
__instance.MedalTimes.text = string.Concat(new string[] __instance.MedalTimes.text = string.Concat(new string[]