SHA256
forked from ADOFAIEditorForum/ADOFAIRenderer
80 lines
2.0 KiB
C#
80 lines
2.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using MelonLoader;
|
|
using MelonLoader.Utils;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace ADOFAIRenderer.Data;
|
|
|
|
public class GlobalConfig
|
|
{
|
|
public string? OutputDirectory { get; set; }
|
|
}
|
|
|
|
public static class GlobalSettings
|
|
{
|
|
private static string SettingsFilePath =>
|
|
Path.Combine(MelonEnvironment.GameRootDirectory, "UserData", "ADOFAIRenderer", "global_settings.json");
|
|
|
|
public static GlobalConfig Current { get; set; } = new();
|
|
|
|
public static string DefaultOutputDirectory =>
|
|
Path.Combine(MelonEnvironment.GameRootDirectory, "Render");
|
|
|
|
public static string EffectiveOutputDirectory
|
|
{
|
|
get
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(Current.OutputDirectory) && Directory.Exists(Current.OutputDirectory))
|
|
{
|
|
return Current.OutputDirectory;
|
|
}
|
|
|
|
return DefaultOutputDirectory;
|
|
}
|
|
}
|
|
|
|
static GlobalSettings()
|
|
{
|
|
Load();
|
|
}
|
|
|
|
public static void Load()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(SettingsFilePath))
|
|
{
|
|
var json = File.ReadAllText(SettingsFilePath);
|
|
Current = JsonConvert.DeserializeObject<GlobalConfig>(json) ?? new GlobalConfig();
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MelonLogger.Warning($"[GlobalSettings] Failed to load global settings: {ex.Message}");
|
|
}
|
|
|
|
Current = new GlobalConfig();
|
|
}
|
|
|
|
public static void Save()
|
|
{
|
|
try
|
|
{
|
|
var dir = Path.GetDirectoryName(SettingsFilePath);
|
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
var json = JsonConvert.SerializeObject(Current, Formatting.Indented);
|
|
File.WriteAllText(SettingsFilePath, json);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MelonLogger.Warning($"[GlobalSettings] Failed to save global settings: {ex.Message}");
|
|
}
|
|
}
|
|
}
|