Files
ppc_amnet/AMNet.Server/Config.cs
T
2025-06-17 18:23:55 +01:00

64 lines
2.4 KiB
C#

using System;
using System.Reflection;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace AMNet.Server;
internal static class Config
{
internal const string IOSection = "aimeio";
static Config()
{
GameId = ReadKey(IOSection, "gameId", 4);
ServerName = ReadKey(IOSection, "serverName", 26, Environment.MachineName);
PhysicalCardUseIDm = PInvoke.GetPrivateProfileInt(IOSection, "useAimeDBForPhysicalCards", 0, ConfigFilePath) > 0;
EnableAimeTxt = PInvoke.GetPrivateProfileInt(IOSection, "enableKeyboardMode", 1, ConfigFilePath) > 0;
AimeTxtPath = ReadKey("aime", "aimePath", PInvoke.MAX_PATH, @"DEVICE\aime.txt");
}
public static string GameId { get; }
/// <summary>
/// The server name to show on devices. Defaults to the computer name.
/// </summary>
public static string ServerName { get; }
/// <summary>
/// Whether physically scanned cards should have their IDm forwarded to the game instead of the access code.
/// </summary>
public static bool PhysicalCardUseIDm { get; }
/// <summary>
/// Whether holding the enter key should trigger a card scan using the code from the aime.txt file
/// </summary>
public static bool EnableAimeTxt { get; }
/// <summary>
/// Path to the aime.txt file. Defaults to .\DEVICE\aime.txt
/// </summary>
public static string AimeTxtPath { get; }
public static Version ServerVersion { get; } = new(Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? "1.0");
private static string ConfigFilePath { get; } = Environment.GetEnvironmentVariable("SEGATOOLS_CONFIG_PATH") ?? ".\\segatools.ini";
internal static unsafe string ReadKey(string section, string key, uint maxLength, string @default = null)
{
// +1 for null terminator
var buffer = stackalloc char[(int)maxLength + 1];
var bufferStr = new PWSTR(buffer);
fixed (char* pSection = section)
fixed (char* pKey = key)
fixed (char* pDefault = @default ?? string.Empty)
fixed (char* pDefaultPath = ConfigFilePath)
{
PInvoke.GetPrivateProfileString(new PCWSTR(pSection), new PCWSTR(pKey), new PCWSTR(pDefault), bufferStr, maxLength + 1, new PCWSTR(pDefaultPath));
}
return bufferStr.ToString();
}
}