mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-10-16 23:49:19 +02:00
Show key descriptions with Polish letters
The game's key file has its descriptions stripped of them. Where a description is the bundled one without the letters, the bundled one is shown; the file itself is left as it is.
This commit is contained in:
10
StarterNG.Tests/KeyboardConfigTests.cs
Normal file
10
StarterNG.Tests/KeyboardConfigTests.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using StarterNG.Classes;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class KeyboardConfigTests
|
||||
{
|
||||
[Fact]
|
||||
public void A_description_without_polish_letters_matches_the_bundled_one() =>
|
||||
Assert.Equal("zwiekszenie nastawnika glownego", KeyboardConfig.StripPolish("zwiększenie nastawnika głównego"));
|
||||
}
|
||||
@@ -86,9 +86,65 @@ public sealed class KeyboardConfig
|
||||
_savePath = UserConfigPath();
|
||||
string source = FirstExisting(_savePath, WorkingDirDefaultPath(), BundledDefaultPath());
|
||||
ParseSource(source);
|
||||
LoadPolishDescriptions();
|
||||
Dirty = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The game ships its key file with the descriptions stripped of Polish letters
|
||||
/// ("zwiekszenie nastawnika glownego"); the copy bundled with the starter has
|
||||
/// them. Read once, shown in place of the bare ones, never written back.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, string> _polishDescriptions = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private void LoadPolishDescriptions()
|
||||
{
|
||||
_polishDescriptions.Clear();
|
||||
try
|
||||
{
|
||||
string path = BundledDefaultPath();
|
||||
if (!File.Exists(path))
|
||||
return;
|
||||
|
||||
var bundled = new KeyboardConfig();
|
||||
bundled.Parse(File.ReadAllText(path, FileEncoding));
|
||||
foreach (var b in bundled.Bindings)
|
||||
if (!string.IsNullOrEmpty(b.Description))
|
||||
_polishDescriptions.TryAdd(b.Command, b.Description);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
StarterNG.Infrastructure.Diagnostics.Log("reading the bundled key descriptions", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The description to show: the bundled one with Polish letters when the file's
|
||||
/// is the same text without them, the file's own when someone wrote their own.
|
||||
/// </summary>
|
||||
public string DescriptionOf(KeyBinding binding)
|
||||
{
|
||||
if (_polishDescriptions.TryGetValue(binding.Command, out string? polish) &&
|
||||
(string.IsNullOrEmpty(binding.Description) ||
|
||||
string.Equals(StripPolish(polish), binding.Description.Trim(), StringComparison.OrdinalIgnoreCase)))
|
||||
return polish;
|
||||
|
||||
return binding.Description;
|
||||
}
|
||||
|
||||
public static string StripPolish(string text)
|
||||
{
|
||||
var sb = new StringBuilder(text.Length);
|
||||
foreach (char c in text.Trim())
|
||||
sb.Append(c switch
|
||||
{
|
||||
'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z', 'ż' => 'z',
|
||||
'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'E', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S', 'Ź' => 'Z', 'Ż' => 'Z',
|
||||
_ => c
|
||||
});
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public void LoadDefaults()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_savePath))
|
||||
|
||||
@@ -886,7 +886,7 @@ public partial class Settings : UserControl, ISettingsCapture
|
||||
|
||||
private static bool MatchesFilter(KeyBinding b, string filter) =>
|
||||
TextMatch.Contains(b.Command, filter) ||
|
||||
TextMatch.Contains(b.Description, filter);
|
||||
TextMatch.Contains(KeyboardConfig.Instance.DescriptionOf(b), filter);
|
||||
|
||||
private Control BuildBindingRow(KeyBinding b, HashSet<string> conflicts)
|
||||
{
|
||||
@@ -1384,7 +1384,7 @@ public partial class Settings : UserControl, ISettingsCapture
|
||||
}
|
||||
|
||||
private static string CommandLabel(KeyBinding b) =>
|
||||
string.IsNullOrEmpty(b.Description) ? b.Command : Capitalize(b.Description);
|
||||
KeyboardConfig.Instance.DescriptionOf(b) is { Length: > 0 } description ? Capitalize(description) : b.Command;
|
||||
|
||||
private static string Capitalize(string s) =>
|
||||
string.IsNullOrEmpty(s) ? s : char.ToUpperInvariant(s[0]) + s.Substring(1);
|
||||
|
||||
Reference in New Issue
Block a user