mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-23 00:24:41 +01:00
fix(settings): a monitor's name outside ASCII is written as the UTF-8 the simulator compares
The simulator compares the word after fullscreenmonitor byte by byte with GLFW's name of the monitor, which is UTF-8 (application.cpp, find_monitor), and has no other way to name one. eu07.ini is written in code page 1250, so a name with a letter outside ASCII never matched and the game opened on the primary monitor. That one word now goes into the file as its UTF-8 bytes, the rest staying in the code page; a name an earlier save left in the code page is read as it is and put right by the next save.
This commit is contained in:
@@ -58,4 +58,66 @@ public class MonitorsTests
|
||||
new StarterNG.Infrastructure.Settings.SettingsSerializer().Write(s, c);
|
||||
Assert.Equal("HDMI-1:1920,0", c.GetRaw("fullscreenmonitor"));
|
||||
}
|
||||
|
||||
// ── A name outside ASCII: the simulator compares the file's bytes with GLFW's UTF-8 name ──
|
||||
|
||||
private static readonly string UserIni = Path.Combine("/home/kolejarz", ".config", "MaSzyna", "eu07.ini");
|
||||
|
||||
private static StarterNG.Tests.Fakes.TestInstallation WithIni(byte[] ini)
|
||||
{
|
||||
var files = new StarterNG.Tests.Fakes.InMemoryFileSystem();
|
||||
files.WriteAllBytes(UserIni, ini);
|
||||
var installation = new StarterNG.Tests.Fakes.TestInstallation(files);
|
||||
installation.Environment.With("HOME", "/home/kolejarz");
|
||||
installation.SettingsStore.Load();
|
||||
return installation;
|
||||
}
|
||||
|
||||
private static bool Holds(byte[] file, byte[] part) => file.AsSpan().IndexOf(part) >= 0;
|
||||
|
||||
[Theory]
|
||||
[InlineData("Obecný_monitor_PnP:1920,0")]
|
||||
[InlineData("Écran_à_Ábc:0,0")]
|
||||
[InlineData("Монитор_Рабочий:-1920,0")]
|
||||
[InlineData("显示器:0,0")]
|
||||
public void A_monitors_name_goes_into_the_file_as_the_utf8_the_simulator_compares(string id)
|
||||
{
|
||||
var cp1250 = StarterNG.Infrastructure.Adapters.LegacyText.CodePage1250;
|
||||
var installation = WithIni(cp1250.GetBytes("lang pl\n// zażółć\nfullscreen yes\n"));
|
||||
installation.SettingsStore.Settings.FullscreenMonitor = id;
|
||||
|
||||
installation.SettingsStore.Save();
|
||||
|
||||
byte[] saved = installation.Files.ReadAllBytes(UserIni);
|
||||
Assert.True(Holds(saved, System.Text.Encoding.UTF8.GetBytes("fullscreenmonitor " + id)));
|
||||
// The rest of the file stays in the simulator's code page.
|
||||
Assert.True(Holds(saved, cp1250.GetBytes("// zażółć")));
|
||||
Assert.Equal(id, WithIni(saved).SettingsStore.Settings.FullscreenMonitor);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_monitors_name_written_in_the_code_page_is_read_and_goes_as_utf8_on_a_save()
|
||||
{
|
||||
const string id = "Obecný_monitor_PnP:1920,0";
|
||||
var cp1250 = StarterNG.Infrastructure.Adapters.LegacyText.CodePage1250;
|
||||
var installation = WithIni(cp1250.GetBytes($"fullscreen yes\nfullscreenmonitor {id}\n"));
|
||||
Assert.Equal(id, installation.SettingsStore.Settings.FullscreenMonitor);
|
||||
|
||||
installation.SettingsStore.Save();
|
||||
|
||||
byte[] saved = installation.Files.ReadAllBytes(UserIni);
|
||||
Assert.True(Holds(saved, System.Text.Encoding.UTF8.GetBytes("fullscreenmonitor " + id)));
|
||||
Assert.False(Holds(saved, cp1250.GetBytes("fullscreenmonitor " + id)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_ascii_name_of_a_monitor_stays_as_the_file_has_it()
|
||||
{
|
||||
var installation = WithIni(System.Text.Encoding.ASCII.GetBytes("fullscreenmonitor HDMI-1:1920,0\n"));
|
||||
|
||||
installation.SettingsStore.Save();
|
||||
|
||||
Assert.True(Holds(installation.Files.ReadAllBytes(UserIni),
|
||||
System.Text.Encoding.ASCII.GetBytes("fullscreenmonitor HDMI-1:1920,0")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,12 +481,18 @@ public sealed class SettingsStore
|
||||
return false;
|
||||
if (overCap.Contains(key))
|
||||
return false;
|
||||
// The simulator goes by it to the letter: the file's own, which it does not find, is no choice to keep.
|
||||
if (AsWritten(key) && _config.GetRaw(key) != value)
|
||||
return false;
|
||||
string? synced = _synced.GetValueOrDefault(key);
|
||||
if (shown.TryGetValue(key, out var onScreen) && onScreen.Shown == value && onScreen.Held == synced)
|
||||
return true;
|
||||
return _config.Has(key) && synced == value;
|
||||
}
|
||||
|
||||
bool AsWritten(string key) =>
|
||||
string.Equals(key, "fullscreenmonitor", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
void Add(string key, string? value)
|
||||
{
|
||||
// To the simulator a key past endconfig is not set yet, whatever it says.
|
||||
|
||||
@@ -56,6 +56,34 @@ public static class LegacyText
|
||||
|
||||
private static readonly byte[] Utf8Bom = { 0xEF, 0xBB, 0xBF };
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="text"/> as the characters that, written in code page 1250 with the rest
|
||||
/// of the file, give its UTF-8 bytes: for a word the simulator compares with a UTF-8 one
|
||||
/// byte by byte, as fullscreenmonitor with GLFW's name of the monitor. ASCII stays as it is.
|
||||
/// </summary>
|
||||
public static string Utf8As1250(string text) =>
|
||||
IsAscii(text) ? text : CodePage1250.GetString(Encoding.UTF8.GetBytes(text));
|
||||
|
||||
/// <summary>
|
||||
/// The other way: what a word of a code page 1250 file says when its bytes are UTF-8.
|
||||
/// One whose bytes are not UTF-8 was written in the code page itself and stays as read.
|
||||
/// </summary>
|
||||
public static string Utf8From1250(string text)
|
||||
{
|
||||
if (IsAscii(text))
|
||||
return text;
|
||||
byte[] bytes = CodePage1250.GetBytes(text);
|
||||
return Utf8.IsValid(bytes) && CodePage1250.GetString(bytes) == text ? Encoding.UTF8.GetString(bytes) : text;
|
||||
}
|
||||
|
||||
private static bool IsAscii(string text)
|
||||
{
|
||||
foreach (char c in text)
|
||||
if (c > 0x7F)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Encoding Resolve()
|
||||
{
|
||||
try
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using StarterNG.Classes;
|
||||
using StarterNG.Domain.Settings;
|
||||
using StarterNG.Infrastructure.Adapters;
|
||||
|
||||
namespace StarterNG.Infrastructure.Settings;
|
||||
|
||||
@@ -123,7 +124,7 @@ public sealed class SettingsSerializer
|
||||
if (s.FpsLimitEnabled) s.FpsLimit = fpsLimit;
|
||||
s.ShadowAngleLimit = c.GetDouble("gfx.shadow.angle.min", 0.2);
|
||||
s.FullscreenWindowed = c.GetBool("fullscreenwindowed", false);
|
||||
s.FullscreenMonitor = c.GetString("fullscreenmonitor", "").Trim();
|
||||
s.FullscreenMonitor = LegacyText.Utf8From1250(c.GetString("fullscreenmonitor", "").Trim());
|
||||
s.RenderAngleVulkan = string.Equals(c.GetString("gfx.angleplatform", ""), "vulkan", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
s.SplineFidelity = Clamp(c.GetInt("splinefidelity", 1), 1, 4);
|
||||
@@ -312,7 +313,9 @@ public sealed class SettingsSerializer
|
||||
if (s.FpsLimitEnabled) c.SetDouble("fpslimit", s.FpsLimit);
|
||||
c.SetDouble("gfx.shadow.angle.min", s.ShadowAngleLimit);
|
||||
c.SetBool("fullscreenwindowed", s.FullscreenWindowed);
|
||||
if (!string.IsNullOrWhiteSpace(s.FullscreenMonitor)) c.Set("fullscreenmonitor", s.FullscreenMonitor.Trim());
|
||||
// The simulator compares the word with GLFW's name of the monitor, which is UTF-8.
|
||||
if (!string.IsNullOrWhiteSpace(s.FullscreenMonitor))
|
||||
c.Set("fullscreenmonitor", LegacyText.Utf8As1250(s.FullscreenMonitor.Trim()));
|
||||
if (s.RenderAngleVulkan) c.Set("gfx.angleplatform", "vulkan");
|
||||
|
||||
c.SetInt("splinefidelity", s.SplineFidelity);
|
||||
|
||||
Reference in New Issue
Block a user