2025-02-21 16:09:06 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using Tommy;
|
|
|
|
|
2025-02-22 12:19:42 +03:00
|
|
|
namespace Plombir;
|
2025-02-21 16:09:06 +03:00
|
|
|
|
|
|
|
public class ConfigManager
|
|
|
|
{
|
2025-02-25 22:43:25 +03:00
|
|
|
static private readonly string defaultRuntime = "./runtime";
|
|
|
|
static private readonly string defaultVersion = "1.20.1";
|
|
|
|
static private readonly string defaultNickname = "slugcat";
|
|
|
|
|
2025-02-26 16:04:13 +03:00
|
|
|
static private readonly string currentTweaksVersion = "1.2.1";
|
2025-02-25 22:43:25 +03:00
|
|
|
|
|
|
|
static private readonly string defaultLoginType = "offline";
|
|
|
|
static private readonly string defaultUuid = "offline";
|
|
|
|
static private readonly string defaultAccsessToken = "offline";
|
|
|
|
|
2025-02-21 16:09:06 +03:00
|
|
|
public static void BuildDefault()
|
|
|
|
{
|
|
|
|
TomlTable toml = new TomlTable
|
|
|
|
{
|
|
|
|
["title"] = "Launcher tweaks file!",
|
2025-02-25 22:43:25 +03:00
|
|
|
["tweaks-version"] = currentTweaksVersion,
|
2025-02-21 16:09:06 +03:00
|
|
|
|
|
|
|
["runtime-path"] = new TomlString
|
|
|
|
{
|
2025-02-25 22:43:25 +03:00
|
|
|
Value = defaultRuntime,
|
2025-02-21 16:09:06 +03:00
|
|
|
Comment = "Path to folder that will hold all minecraft versions"
|
|
|
|
},
|
|
|
|
|
|
|
|
["last-version-launched"] = new TomlString
|
|
|
|
{
|
2025-02-25 22:43:25 +03:00
|
|
|
Value = defaultVersion,
|
2025-02-21 16:09:06 +03:00
|
|
|
Comment = "Saving last launched version to use it after launch"
|
|
|
|
},
|
|
|
|
|
2025-02-25 22:43:25 +03:00
|
|
|
["nickname"] = defaultNickname,
|
|
|
|
["login-method"] = defaultLoginType,
|
|
|
|
["uuid"] = defaultUuid,
|
|
|
|
["accsess-token"] = defaultAccsessToken,
|
|
|
|
|
|
|
|
|
2025-02-21 16:09:06 +03:00
|
|
|
};
|
|
|
|
|
2025-02-22 12:19:42 +03:00
|
|
|
using (StreamWriter writer = File.CreateText("tweaks.toml"))
|
2025-02-21 16:09:06 +03:00
|
|
|
{
|
|
|
|
toml.WriteTo(writer);
|
|
|
|
// Remember to flush the data if needed!
|
|
|
|
writer.Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static dynamic? ReadConfig(string key)
|
|
|
|
{
|
|
|
|
if (!File.Exists("tweaks.toml"))
|
|
|
|
{
|
|
|
|
BuildDefault();
|
|
|
|
}
|
2025-02-22 12:19:42 +03:00
|
|
|
using (StreamReader reader = File.OpenText("tweaks.toml"))
|
2025-02-21 16:09:06 +03:00
|
|
|
{
|
|
|
|
TomlTable table = TOML.Parse(reader);
|
|
|
|
|
2025-02-25 22:43:25 +03:00
|
|
|
// If config don't contains it means it's oudated or broken.
|
|
|
|
if (!table.TryGetNode(key, out var node))
|
|
|
|
{
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
return table[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
Console.WriteLine("Warning! Can't locate tweaks version! Attempting auto recovery...");
|
|
|
|
return "possibly broken";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
2025-02-21 16:09:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void WriteInConfig(string key, dynamic value)
|
|
|
|
{
|
|
|
|
if (!File.Exists("tweaks.toml"))
|
|
|
|
{
|
|
|
|
BuildDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
TomlTable table;
|
|
|
|
|
2025-02-22 12:19:42 +03:00
|
|
|
using (StreamReader reader = File.OpenText("tweaks.toml"))
|
2025-02-21 16:09:06 +03:00
|
|
|
{
|
|
|
|
table = TOML.Parse(reader);
|
|
|
|
|
|
|
|
table[key] = value;
|
|
|
|
}
|
|
|
|
|
2025-02-22 12:19:42 +03:00
|
|
|
using (StreamWriter writer = File.CreateText("tweaks.toml"))
|
2025-02-21 16:09:06 +03:00
|
|
|
{
|
|
|
|
table.WriteTo(writer);
|
|
|
|
// Remember to flush the data if needed!
|
|
|
|
writer.Flush();
|
|
|
|
}
|
|
|
|
}
|
2025-02-22 12:19:42 +03:00
|
|
|
}
|