vrcauthproxy/VRCAuthProxy/Config.cs
MiscFrizzy eb4349031b ### Commit Summary
- **TestSetup.cs**
  - Updated `CreateTestConfig` method to initialize `Config` with required properties using object initializer syntax.

- **ProxyIntegrationTests.cs**
  - Added null checks for `mockServer.Urls` before accessing it to prevent potential null reference exceptions.
  - Improved error handling for mock server URL access.

- **VRChatAuthenticationTests.cs**
  - Added null checks for `mockServer.Urls` before accessing it to prevent potential null reference exceptions.
  - Enhanced the mock server setup to include null checks for request body content.

- **Config.cs**
  - Added the `required` modifier to non-nullable properties in `ConfigAccount` and `iConfig` classes.
  - Updated the `Load` method to initialize the `Config` instance with required properties using object initializer syntax.

- **Program.cs**
  - Added a null check for `result.CloseStatus` in WebSocket handling to prevent potential null reference exceptions.
2025-04-07 07:30:34 -04:00

71 lines
No EOL
1.7 KiB
C#

using System.Text.Json;
namespace VRCAuthProxy;
public class ConfigAccount
{
public required string username { get; set; }
public required string password { get; set; }
public string? totpSecret { get; set; }
}
public class iConfig
{
public required List<ConfigAccount> accounts { get; set; }
}
// Load config from appsettings.json
public class Config
{
private static Config? _instance;
public required List<ConfigAccount> Accounts { get; set; }
public Config()
{
Accounts = new List<ConfigAccount>();
}
public static Config Instance
{
get
{
if (_instance == null) _instance = Load();
return _instance;
}
}
public static Config Load()
{
var configPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
var config = new Config
{
Accounts = new List<ConfigAccount>()
};
if (File.Exists(configPath))
{
var json = File.ReadAllText(configPath);
// load iConfig
var iConfig = JsonSerializer.Deserialize<iConfig>(json);
if (iConfig == null) throw new Exception("Failed to load config");
config.Accounts = iConfig.accounts;
}
else
{
Console.WriteLine("No config found at " + configPath);
}
return config;
}
public void Save()
{
var configPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
var iConfig = new iConfig
{
accounts = Accounts
};
var json = JsonSerializer.Serialize(iConfig, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(configPath, json);
}
}