mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-22 10:56:20 +08:00
Gui Settings Null list Entries (#430)
This commit is contained in:
@@ -71,7 +71,7 @@ public sealed class GuiSettings
|
||||
if (File.Exists(SettingsPath))
|
||||
{
|
||||
var json = File.ReadAllText(SettingsPath);
|
||||
return JsonSerializer.Deserialize<GuiSettings>(json, SerializerOptions) ?? new GuiSettings();
|
||||
return NormalizeFromJson(json);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -82,6 +82,35 @@ public sealed class GuiSettings
|
||||
return new GuiSettings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes settings and normalizes null references and null or empty list
|
||||
/// entries introduced by JSON. Empty scalar strings remain unchanged.
|
||||
/// </summary>
|
||||
internal static GuiSettings NormalizeFromJson(string json)
|
||||
{
|
||||
var settings = JsonSerializer.Deserialize<GuiSettings>(json, SerializerOptions) ?? new GuiSettings();
|
||||
|
||||
settings.GameFolders = FilterNullOrEmpty(settings.GameFolders);
|
||||
settings.ExcludedGames = FilterNullOrEmpty(settings.ExcludedGames);
|
||||
settings.EnvironmentToggles = FilterNullOrEmpty(settings.EnvironmentToggles);
|
||||
settings.LogLevel ??= "Info";
|
||||
settings.Language ??= "en";
|
||||
settings.DiscordClientId ??= "1525606762248540221";
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
// JSON can populate non-nullable lists with null references and entries.
|
||||
private static List<string> FilterNullOrEmpty(List<string>? source)
|
||||
{
|
||||
if (source is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
return source.Where(entry => !string.IsNullOrEmpty(entry)).ToList();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
try
|
||||
|
||||
@@ -24,6 +24,10 @@ SPDX-License-Identifier: GPL-2.0-or-later
|
||||
<ProjectReference Include="..\SharpEmu.Logging\SharpEmu.Logging.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SharpEmu.Libs.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" />
|
||||
<PackageReference Include="Avalonia.Desktop" />
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// Copyright (C) 2026 SharpEmu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
using SharpEmu.GUI;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpEmu.Libs.Tests.GUI;
|
||||
|
||||
public sealed class GuiSettingsTests
|
||||
{
|
||||
[Fact]
|
||||
public void NormalizeFromJson_AllPropertiesNull_FallsBackToDefaults()
|
||||
{
|
||||
const string json = """
|
||||
{
|
||||
"LogLevel": null,
|
||||
"GameFolders": null,
|
||||
"ExcludedGames": null,
|
||||
"EnvironmentToggles": null,
|
||||
"Language": null,
|
||||
"DiscordClientId": null
|
||||
}
|
||||
""";
|
||||
|
||||
var settings = GuiSettings.NormalizeFromJson(json);
|
||||
|
||||
Assert.Equal("Info", settings.LogLevel);
|
||||
Assert.Equal("en", settings.Language);
|
||||
Assert.Equal("1525606762248540221", settings.DiscordClientId);
|
||||
Assert.Empty(settings.GameFolders);
|
||||
Assert.Empty(settings.ExcludedGames);
|
||||
Assert.Empty(settings.EnvironmentToggles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalizeFromJson_ValidValues_ArePreserved()
|
||||
{
|
||||
const string json = """
|
||||
{
|
||||
"LogLevel": "Debug",
|
||||
"GameFolders": ["C:\\Games"],
|
||||
"ExcludedGames": ["C:\\Games\\skip.bin"],
|
||||
"EnvironmentToggles": ["SHARPEMU_TRACE"],
|
||||
"Language": "pt-BR",
|
||||
"DiscordClientId": "999"
|
||||
}
|
||||
""";
|
||||
|
||||
var settings = GuiSettings.NormalizeFromJson(json);
|
||||
|
||||
Assert.Equal("Debug", settings.LogLevel);
|
||||
Assert.Equal("pt-BR", settings.Language);
|
||||
Assert.Equal("999", settings.DiscordClientId);
|
||||
Assert.Equal(["C:\\Games"], settings.GameFolders);
|
||||
Assert.Equal(["C:\\Games\\skip.bin"], settings.ExcludedGames);
|
||||
Assert.Equal(["SHARPEMU_TRACE"], settings.EnvironmentToggles);
|
||||
}
|
||||
|
||||
// An empty Discord client ID intentionally disables Rich Presence.
|
||||
[Fact]
|
||||
public void NormalizeFromJson_EmptyDiscordClientId_IsPreservedNotNormalized()
|
||||
{
|
||||
const string json = """{ "DiscordClientId": "" }""";
|
||||
|
||||
var settings = GuiSettings.NormalizeFromJson(json);
|
||||
|
||||
Assert.Equal(string.Empty, settings.DiscordClientId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalizeFromJson_NullOrEmptyListEntries_AreFilteredOut()
|
||||
{
|
||||
const string json = """
|
||||
{
|
||||
"GameFolders": ["C:\\Games", null, ""],
|
||||
"ExcludedGames": [null],
|
||||
"EnvironmentToggles": [null, "SHARPEMU_TRACE", ""]
|
||||
}
|
||||
""";
|
||||
|
||||
var settings = GuiSettings.NormalizeFromJson(json);
|
||||
|
||||
Assert.Equal(["C:\\Games"], settings.GameFolders);
|
||||
Assert.Empty(settings.ExcludedGames);
|
||||
Assert.Equal(["SHARPEMU_TRACE"], settings.EnvironmentToggles);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalizeFromJson_EmptyObject_UsesConstructorDefaults()
|
||||
{
|
||||
var settings = GuiSettings.NormalizeFromJson("{}");
|
||||
|
||||
Assert.Equal("Info", settings.LogLevel);
|
||||
Assert.Equal("en", settings.Language);
|
||||
Assert.Equal("1525606762248540221", settings.DiscordClientId);
|
||||
Assert.Empty(settings.GameFolders);
|
||||
Assert.Empty(settings.ExcludedGames);
|
||||
Assert.Empty(settings.EnvironmentToggles);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
|
||||
<ProjectReference Include="..\..\src\SharpEmu.Core\SharpEmu.Core.csproj" />
|
||||
<ProjectReference Include="..\..\src\SharpEmu.Libs\SharpEmu.Libs.csproj" />
|
||||
<ProjectReference Include="..\..\src\SharpEmu.HLE\SharpEmu.HLE.csproj" />
|
||||
<ProjectReference Include="..\..\src\SharpEmu.GUI\SharpEmu.GUI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user