91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using System;
|
|
using Godot;
|
|
|
|
namespace SideScrollerGame.Debug;
|
|
|
|
public sealed class DebugSettings
|
|
{
|
|
public DebugSettings(DebugBootMode bootMode, int seed)
|
|
{
|
|
BootMode = bootMode;
|
|
Seed = seed;
|
|
}
|
|
|
|
public static DebugSettings Load()
|
|
{
|
|
DebugBootMode bootMode = LoadBootModeFromProjectSettings();
|
|
int seed = LoadSeedFromProjectSettings();
|
|
|
|
foreach (string argument in OS.GetCmdlineUserArgs())
|
|
{
|
|
if (argument.StartsWith(DebugBootModePrefix, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
string value = argument[DebugBootModePrefix.Length..];
|
|
if (TryParseBootMode(value, out DebugBootMode parsedBootMode))
|
|
{
|
|
bootMode = parsedBootMode;
|
|
}
|
|
else
|
|
{
|
|
GD.PushWarning($"Unknown debug boot mode '{value}'. Falling back to Menu.");
|
|
bootMode = DebugBootMode.Menu;
|
|
}
|
|
}
|
|
else if (argument.StartsWith(SeedPrefix, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
string value = argument[SeedPrefix.Length..];
|
|
if (int.TryParse(value, out int parsedSeed))
|
|
{
|
|
seed = parsedSeed;
|
|
}
|
|
else
|
|
{
|
|
GD.PushWarning($"Unknown debug seed '{value}'. Keeping seed {seed}.");
|
|
}
|
|
}
|
|
}
|
|
|
|
return new DebugSettings(bootMode, seed);
|
|
}
|
|
|
|
public DebugBootMode BootMode { get; }
|
|
|
|
public int Seed { get; }
|
|
|
|
private static DebugBootMode LoadBootModeFromProjectSettings()
|
|
{
|
|
if (!ProjectSettings.HasSetting(DebugBootModeSetting))
|
|
{
|
|
return DebugBootMode.Menu;
|
|
}
|
|
|
|
string configuredBootMode = ProjectSettings.GetSetting(DebugBootModeSetting).AsString();
|
|
if (TryParseBootMode(configuredBootMode, out DebugBootMode bootMode))
|
|
{
|
|
return bootMode;
|
|
}
|
|
|
|
GD.PushWarning($"Unknown configured debug boot mode '{configuredBootMode}'. Falling back to Menu.");
|
|
return DebugBootMode.Menu;
|
|
}
|
|
|
|
private static int LoadSeedFromProjectSettings()
|
|
{
|
|
if (!ProjectSettings.HasSetting(DebugSeedSetting))
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return ProjectSettings.GetSetting(DebugSeedSetting).AsInt32();
|
|
}
|
|
|
|
private static bool TryParseBootMode(string value, out DebugBootMode bootMode)
|
|
{
|
|
return Enum.TryParse(value, true, out bootMode);
|
|
}
|
|
|
|
private const string DebugBootModePrefix = "--debug-boot=";
|
|
private const string DebugBootModeSetting = "application/run/debug_boot_mode";
|
|
private const string DebugSeedSetting = "application/run/debug_seed";
|
|
private const string SeedPrefix = "--seed=";
|
|
} |