89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
namespace PersonalAuthMod;
|
|
|
|
public class AuthConfig
|
|
{
|
|
[JsonPropertyName("db_url")]
|
|
public string DbUrl { get; set; } = "localhost";
|
|
|
|
[JsonPropertyName("db_port")]
|
|
public int DbPort { get; set; } = 5432;
|
|
|
|
[JsonPropertyName("db_user")]
|
|
public string DbUser { get; set; } = "spt";
|
|
|
|
[JsonPropertyName("db_password")]
|
|
public string DbPassword { get; set; } = "mypassword";
|
|
|
|
[JsonPropertyName("db_name")]
|
|
public string DbName { get; set; } = "postgres";
|
|
|
|
public static AuthConfig Load()
|
|
{
|
|
Console.WriteLine("[PersonalAuthMod] AuthConfig.Load() called.");
|
|
|
|
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
string currentDir = Directory.GetCurrentDirectory();
|
|
|
|
Console.WriteLine($"[PersonalAuthMod] AppDomain BaseDirectory: {baseDir}");
|
|
Console.WriteLine($"[PersonalAuthMod] Current Directory: {currentDir}");
|
|
|
|
// Try multiple potential paths to find the config
|
|
var paths = new[]
|
|
{
|
|
Path.Combine(baseDir, "user", "mods", "PersonalAuthMod", "configs.jsonc"),
|
|
Path.Combine(currentDir, "user", "mods", "PersonalAuthMod", "configs.jsonc"),
|
|
"user/mods/PersonalAuthMod/configs.jsonc",
|
|
"configs.jsonc"
|
|
};
|
|
|
|
string? configPath = null;
|
|
foreach (var p in paths)
|
|
{
|
|
bool exists = File.Exists(p);
|
|
Console.WriteLine($"[PersonalAuthMod] Checking path: {p} (Exists: {exists})");
|
|
if (exists)
|
|
{
|
|
configPath = p;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (configPath == null)
|
|
{
|
|
Console.WriteLine("[PersonalAuthMod] configPath is NULL. Returning default AuthConfig (localhost).");
|
|
return new AuthConfig();
|
|
}
|
|
|
|
try
|
|
{
|
|
Console.WriteLine($"[PersonalAuthMod] Loading Config from: {Path.GetFullPath(configPath)}");
|
|
var json = File.ReadAllText(configPath);
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
ReadCommentHandling = JsonCommentHandling.Skip,
|
|
PropertyNameCaseInsensitive = true,
|
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
|
};
|
|
|
|
var config = JsonSerializer.Deserialize<AuthConfig>(json, options);
|
|
if (config != null)
|
|
{
|
|
Console.WriteLine($"[PersonalAuthMod] Config loaded successfully. DB Host: {config.DbUrl}");
|
|
return config;
|
|
}
|
|
|
|
Console.WriteLine("[PersonalAuthMod] Deserialization returned null. Returning default.");
|
|
return new AuthConfig();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[PersonalAuthMod] Config Load Error: {ex.Message}");
|
|
return new AuthConfig();
|
|
}
|
|
}
|
|
}
|