From 77ef82d92a235fac8e2c25fe038591fdd2bfa9d8 Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Sat, 21 Dec 2024 18:57:05 -0600 Subject: [PATCH] misc: Cache LocalesJson when loading locale --- src/Ryujinx.Common/Utilities/JsonHelper.cs | 10 ++++- src/Ryujinx/Common/LocaleManager.cs | 49 +++++++++------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/Ryujinx.Common/Utilities/JsonHelper.cs b/src/Ryujinx.Common/Utilities/JsonHelper.cs index 276dd5f8c..82eeaddc1 100644 --- a/src/Ryujinx.Common/Utilities/JsonHelper.cs +++ b/src/Ryujinx.Common/Utilities/JsonHelper.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Text; using System.Text.Json; @@ -27,9 +28,14 @@ namespace Ryujinx.Common.Utilities ReadCommentHandling = JsonCommentHandling.Skip }; - public static string Serialize(T value, JsonTypeInfo typeInfo) => JsonSerializer.Serialize(value, typeInfo); + public static string Serialize(T value, JsonTypeInfo typeInfo) + => JsonSerializer.Serialize(value, typeInfo); - public static T Deserialize(string value, JsonTypeInfo typeInfo) => JsonSerializer.Deserialize(value, typeInfo); + public static T Deserialize(string value, JsonTypeInfo typeInfo) + => JsonSerializer.Deserialize(value, typeInfo); + + public static T Deserialize(ReadOnlySpan utf8Value, JsonTypeInfo typeInfo) + => JsonSerializer.Deserialize(utf8Value, typeInfo); public static void SerializeToFile(string filePath, T value, JsonTypeInfo typeInfo) { diff --git a/src/Ryujinx/Common/LocaleManager.cs b/src/Ryujinx/Common/LocaleManager.cs index 2ea5c83ee..613d92e0d 100644 --- a/src/Ryujinx/Common/LocaleManager.cs +++ b/src/Ryujinx/Common/LocaleManager.cs @@ -1,3 +1,4 @@ +using Gommon; using Ryujinx.Ava.UI.ViewModels; using Ryujinx.Common; using Ryujinx.Common.Logging; @@ -7,12 +8,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; -using System.IO; -using System.Linq; -using System.Text.Encodings.Web; -using System.Text.Json; using System.Text.Json.Serialization; -using System.Text.Unicode; namespace Ryujinx.Ava.Common.Locale { @@ -147,39 +143,34 @@ namespace Ryujinx.Ava.Common.Locale LocaleChanged?.Invoke(); } + #nullable enable + + private static LocalesJson? _localeData; + + #nullable disable + private static Dictionary LoadJsonLanguage(string languageCode) { var localeStrings = new Dictionary(); - string fileData = EmbeddedResources.ReadAllText($"Ryujinx/Assets/locales.json"); - if (fileData == null) + _localeData ??= EmbeddedResources.ReadAllText("Ryujinx/Assets/locales.json") + .Into(it => JsonHelper.Deserialize(it, LocalesJsonContext.Default.LocalesJson)); + + foreach (LocalesEntry locale in _localeData.Value.Locales) { - // We were unable to find file for that language code. - return null; - } - - LocalesJson json = JsonHelper.Deserialize(fileData, LocalesJsonContext.Default.LocalesJson); - - foreach (LocalesEntry locale in json.Locales) - { - if (locale.Translations.Count != json.Languages.Count) + if (locale.Translations.Count != _localeData.Value.Languages.Count) { Logger.Error?.Print(LogClass.UI, $"Locale key {{{locale.ID}}} is missing languages!"); throw new Exception("Missing locale data!"); } - if (Enum.TryParse(locale.ID, out var localeKey)) - { - if (locale.Translations.TryGetValue(languageCode, out string val) && val != "") - { - localeStrings[localeKey] = val; - } - else - { - locale.Translations.TryGetValue("en_US", out val); - localeStrings[localeKey] = val; - } - } + if (!Enum.TryParse(locale.ID, out var localeKey)) + continue; + + localeStrings[localeKey] = + locale.Translations.TryGetValue(languageCode, out string val) && val != string.Empty + ? val + : locale.Translations[DefaultLanguageCode]; } return localeStrings; @@ -200,5 +191,5 @@ namespace Ryujinx.Ava.Common.Locale [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(LocalesJson))] - internal partial class LocalesJsonContext : JsonSerializerContext { } + internal partial class LocalesJsonContext : JsonSerializerContext; }