Skip to content

Commit

Permalink
Merge pull request Librelancer#166 from Librelancer/2024.06.1
Browse files Browse the repository at this point in the history
Hotfix: Fix locale regression in ini parsing
  • Loading branch information
CallumDev authored Jul 19, 2024
2 parents ca6b32c + c7648c4 commit 109e7d3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/LibreLancer.Data/Ini/LancerTextIniParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
namespace LibreLancer.Ini
{
/// <summary>
/// Implementation of a text ini parser which comes as close to the original as possible.
/// Implementation of a text ini parser which comes as close to the original as possible.
/// </summary>
/// <remarks>
/// Freelancer data contains a mixture of binary ini and text ini files. The text ini files
/// seem to contain nbsp (0xa0) characters from the windows-1252 code page which can be seen in
/// DATA/initialworld.ini. The means that unless the ini file has BOM we should interpret the
/// file data as from this code page.
/// file data as from this code page.
/// </remarks>
public class LancerTextIniParser : IIniParser
{
Expand Down Expand Up @@ -70,8 +70,7 @@ private static void ParseKeyValue(Section section, string key, string value, int
{
entry.Add(new Int32Value((int)tempLong) { Entry = entry, Line = line });
}
else if (float.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out float tempFloat)
&& tempFloat.ToString() == Convert.ToDouble(part).ToString())
else if (float.TryParse(part, NumberStyles.Float, CultureInfo.InvariantCulture, out float tempFloat))
{
entry.Add(new SingleValue(tempFloat, null) { Entry = entry, Line = line });
}
Expand Down
44 changes: 44 additions & 0 deletions src/LibreLancer.Tests/Ini/LocaleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using LibreLancer.Ini;
using System.Text;
using FluentAssertions;
using Xunit;

namespace LibreLancer.Tests.Ini;

public class LocaleTests
{
void RunWithLocale(string locale, Action action)
{
var currentLocale = CultureInfo.CurrentCulture;
var locale2 = CultureInfo.GetCultureInfo(locale);
CultureInfo.CurrentCulture = locale2;
action();
CultureInfo.CurrentCulture = currentLocale;
}

[Theory]
[InlineData("en-US")]
[InlineData("de-DE")]
[InlineData("es-ES")]
public void TestDecimalSeparator(string locale)
{
RunWithLocale(locale, () =>
{
var parser = new LancerTextIniParser();
var s = new MemoryStream("[Section]\nValue = 1.1"u8.ToArray());

var p = parser.ParseIniFile("[data]", s, true, false).ToArray();
p.Should().HaveCount(1);
p[0].Name.Should().Be("Section");
p[0].Should().HaveCount(1);
p[0][0].Name.Should().Be("Value");
p[0][0].Should().HaveCount(1);
p[0][0][0].Should().BeOfType<SingleValue>();
p[0][0][0].ToSingle().Should().Be(1.1f);
});
}
}

0 comments on commit 109e7d3

Please sign in to comment.