Fix cp1250 decoding, log unsupported loads

The repositories resolved GetEncoding(1250) in their constructors, before
anything registered CodePagesEncodingProvider - reads fell back to Latin-1 and
Polish characters came out as mojibake.
This commit is contained in:
maj00r
2026-09-04 22:06:59 +02:00
parent 673cda57d7
commit b33d7697a0
13 changed files with 113 additions and 92 deletions

View File

@@ -0,0 +1,43 @@
using System;
using System.Text;
namespace StarterNG.Infrastructure.Adapters;
/// <summary>
/// The code page every file the simulator owns is written in: sceneries,
/// textures.txt, .fiz, timetables, eu07.ini.
/// </summary>
/// <remarks>
/// Code page 1250 is not built into .NET; it arrives with
/// <see cref="CodePagesEncodingProvider"/>, which has to be registered before the
/// first <c>GetEncoding</c> call. Doing that registration at each call site is how
/// Polish characters end up as Latin-1 mojibake ("Skład" as "Sk³ad"): whichever
/// site runs first wins, and the composition root builds its repositories before
/// any of them. One property, resolved once, removes the ordering question.
/// </remarks>
public static class LegacyText
{
/// <summary>The .scn / .fiz / .ini code page, or Latin-1 if it is unavailable.</summary>
public static Encoding CodePage1250 { get; } = Resolve();
/// <summary>
/// True when the code page could not be loaded and text is being read as
/// Latin-1 instead. Polish characters will be wrong; the startup check
/// reports it rather than leaving it to be discovered on screen.
/// </summary>
public static bool IsFallback { get; private set; }
private static Encoding Resolve()
{
try
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return Encoding.GetEncoding(1250);
}
catch (Exception)
{
IsFallback = true;
return Encoding.Latin1;
}
}
}

View File

@@ -8,6 +8,7 @@ using System.Threading;
using System.Threading.Tasks;
using StarterNG.Application.Abstractions;
using StarterNG.Domain.Sceneries;
using StarterNG.Infrastructure.Adapters;
namespace StarterNG.Infrastructure.Sceneries;
@@ -36,7 +37,7 @@ public sealed class SceneryRepository : ISceneryRepository
_paths = paths;
_log = log;
_parser = parser;
_encoding = CodePage1250();
_encoding = LegacyText.CodePage1250;
}
public IReadOnlyList<Scenery> LoadAll(IProgress<SceneryLoadProgress>? progress = null)
@@ -83,19 +84,4 @@ public sealed class SceneryRepository : ISceneryRepository
_files.GetFiles(_paths.Scenery, "*.scn")
.Where(path => Path.GetFileName(path).FirstOrDefault() != ExcludedPrefix);
/// <summary>
/// The .scn dialect predates Unicode. The provider is registered by the entry
/// point; fall back to Latin-1 if that ever fails so loading still works.
/// </summary>
private static Encoding CodePage1250()
{
try
{
return Encoding.GetEncoding(1250);
}
catch (Exception)
{
return Encoding.Latin1;
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using StarterNG.Application.Abstractions;
using StarterNG.Domain.Vehicles;
using StarterNG.Infrastructure.Adapters;
namespace StarterNG.Infrastructure.Vehicles;
@@ -51,7 +52,7 @@ public sealed class FizPhysicsRepository : IPhysicsRepository
_files = files;
_paths = paths;
_log = log;
_encoding = LegacyEncoding();
_encoding = LegacyText.CodePage1250;
}
public void Preload() => Index();
@@ -270,15 +271,4 @@ public sealed class FizPhysicsRepository : IPhysicsRepository
.ToList();
}
private static Encoding LegacyEncoding()
{
try
{
return Encoding.GetEncoding(1250);
}
catch (Exception)
{
return Encoding.Latin1;
}
}
}

View File

@@ -5,6 +5,7 @@ using System.IO;
using System.Text;
using StarterNG.Application.Abstractions;
using StarterNG.Domain.Vehicles;
using StarterNG.Infrastructure.Adapters;
namespace StarterNG.Infrastructure.Vehicles;
@@ -51,7 +52,7 @@ public sealed class LoadWeightsRepository
try
{
return new LoadWeightsTable(ParsePairs(_files.ReadAllText(path, LegacyEncoding())));
return new LoadWeightsTable(ParsePairs(_files.ReadAllText(path, LegacyText.CodePage1250)));
}
catch (Exception ex)
{
@@ -100,15 +101,4 @@ public sealed class LoadWeightsRepository
return weights;
}
private static Encoding LegacyEncoding()
{
try
{
return Encoding.GetEncoding(1250);
}
catch (Exception)
{
return Encoding.Latin1;
}
}
}

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Text;
using StarterNG.Application.Abstractions;
using StarterNG.Domain.Vehicles;
using StarterNG.Infrastructure.Adapters;
namespace StarterNG.Infrastructure.Vehicles;
@@ -28,7 +29,7 @@ public sealed class TexturesTxtVehicleRepository : IVehicleRepository
_paths = paths;
_log = log;
_parser = parser;
_encoding = LegacyEncoding();
_encoding = LegacyText.CodePage1250;
}
public int Load(VehicleCatalog catalog)
@@ -98,15 +99,4 @@ public sealed class TexturesTxtVehicleRepository : IVehicleRepository
return relative.Replace('\\', '/').TrimEnd('/') + "/";
}
private static Encoding LegacyEncoding()
{
try
{
return Encoding.GetEncoding(1250);
}
catch (Exception)
{
return Encoding.Latin1;
}
}
}