PlombirLauncher/Launcher-UI/ConfigManager.cs
2025-02-26 17:04:42 +03:00

140 lines
4.2 KiB
C#

using System;
using System.IO;
using Tommy;
namespace Plombir;
public class ConfigManager
{
static private readonly string defaultRuntime = "./runtime";
static private readonly string defaultVersion = "1.20.1";
static private readonly string defaultNickname = "slugcat";
static private readonly string currentTweaksVersion = "1.2.1";
static private readonly string defaultLoginType = "offline";
static private readonly string defaultUuid = "offline";
static private readonly string defaultAccsessToken = "offline";
public static void BuildDefault()
{
TomlTable toml = new TomlTable
{
["title"] = "Launcher tweaks file!",
["tweaks-version"] = currentTweaksVersion,
["runtime-path"] = new TomlString
{
Value = defaultRuntime,
Comment = "Path to folder that will hold all minecraft versions"
},
["last-version-launched"] = new TomlString
{
Value = defaultVersion,
Comment = "Saving last launched version to use it after launch"
},
["nickname"] = defaultNickname,
["login-method"] = defaultLoginType,
["uuid"] = defaultUuid,
["accsess-token"] = defaultAccsessToken,
};
using (StreamWriter writer = File.CreateText("tweaks.toml"))
{
toml.WriteTo(writer);
// Remember to flush the data if needed!
writer.Flush();
}
}
public static dynamic? ReadConfig(string key)
{
if (!File.Exists("tweaks.toml"))
{
BuildDefault();
}
using (StreamReader reader = File.OpenText("tweaks.toml"))
{
TomlTable table = TOML.Parse(reader);
// If config don't contains it means it's oudated or broken.
if (!IsValueExists(table, key))
{
WriteInConfig(key, "NaN");
}
// In case if config is fine return its value
if (table[key].ToString() != "NaN")
{
if ((key == "tweaks-version") & (table[key] != currentTweaksVersion))
{
Console.WriteLine("Warning! Tweaks version mismatch!");
WriteInConfig("tweaks-version", "wrong-version");
}
return table[key];
}
return RepairConfig(key);
}
}
public static bool IsValueExists(TomlTable table, string key)
{
if (table.TryGetNode(key, out var _))
return true;
return false;
}
private static string? RepairConfig(string key)
{
Console.WriteLine($"Warning! Can't locate {key} in tweaks! Fallback to defaults!");
// Handling out-dated or broken toml files by filling with defaults
switch (key)
{
case "runtime-path": { WriteInConfig(key, defaultRuntime); return defaultRuntime; }
case "last-version-launched": { WriteInConfig(key, defaultVersion); return defaultVersion; }
case "nickname": { WriteInConfig(key, defaultNickname); return defaultNickname; }
case "accsess-token": { WriteInConfig(key, defaultAccsessToken); return defaultAccsessToken; }
case "uuid": { WriteInConfig(key, defaultUuid); return defaultUuid; }
case "login-method": { WriteInConfig(key, defaultLoginType); return defaultLoginType; }
case "tweaks-version":
{
WriteInConfig(key, currentTweaksVersion);
return "possibly broken";
}
}
return null;
}
public static void WriteInConfig(string key, dynamic value)
{
if (!File.Exists("tweaks.toml"))
{
BuildDefault();
}
TomlTable table;
using (StreamReader reader = File.OpenText("tweaks.toml"))
{
table = TOML.Parse(reader);
table[key] = value;
}
using (StreamWriter writer = File.CreateText("tweaks.toml"))
{
table.WriteTo(writer);
// Remember to flush the data if needed!
writer.Flush();
}
}
}