diff --git a/GodSeekerPlus/GodSeekerPlus.csproj b/GodSeekerPlus/GodSeekerPlus.csproj index 61443a3..216fd97 100644 --- a/GodSeekerPlus/GodSeekerPlus.csproj +++ b/GodSeekerPlus/GodSeekerPlus.csproj @@ -1,7 +1,7 @@ GodSeekerPlus - 0.7.2 + 0.7.3 A Hollow Knight mod to enhance your Godhome experience Clazex Copyright © 2021 Clazex @@ -19,6 +19,8 @@ + + @@ -55,26 +57,21 @@ - - - - - - - + - + - - - - - - - - + + diff --git a/GodSeekerPlus/ModuleManager.cs b/GodSeekerPlus/ModuleManager.cs index 9ed87d2..e2fa970 100644 --- a/GodSeekerPlus/ModuleManager.cs +++ b/GodSeekerPlus/ModuleManager.cs @@ -10,7 +10,7 @@ internal sealed class ModuleManager { internal void LoadModules() => Modules = ModuleHelper .FindModules() .Map(ModuleHelper.ConstructModule) - .ToDictionary(module => module.GetType().Name); + .ToDictionary(module => module.Name); internal void UnloadModules() => Modules.Clear(); } diff --git a/GodSeekerPlus/Util/IEnumerableUtil.cs b/GodSeekerPlus/Util/EnumerableUtil.cs similarity index 90% rename from GodSeekerPlus/Util/IEnumerableUtil.cs rename to GodSeekerPlus/Util/EnumerableUtil.cs index 76e69cf..7062199 100644 --- a/GodSeekerPlus/Util/IEnumerableUtil.cs +++ b/GodSeekerPlus/Util/EnumerableUtil.cs @@ -18,11 +18,11 @@ internal static IEnumerable Filter(this IEnumerable self, Predicate } internal static U Reduce(this IEnumerable self, Func f, U init) { - U last = init; + U acc = init; foreach (T i in self) { - last = f(last, i); + acc = f(acc, i); } - return last; + return acc; } } } diff --git a/GodSeekerPlus/Util/LocalizationUtil.cs b/GodSeekerPlus/Util/LocalizationUtil.cs index 3a0bd98..36b902a 100644 --- a/GodSeekerPlus/Util/LocalizationUtil.cs +++ b/GodSeekerPlus/Util/LocalizationUtil.cs @@ -1,39 +1,44 @@ using System.Collections.Generic; -using System.IO; +using System.Linq; using System.Reflection; -using Newtonsoft.Json; +using Language; namespace GodSeekerPlus.Util { internal static class LocalizationUtil { - private static Dictionary> ReadLangs() { - string path = Assembly.GetExecutingAssembly().Location; - path = Path.GetFullPath(path); - path = Path.GetDirectoryName(path); - path = Path.Combine(path, "lang"); + private const string resPrefix = "GodSeekerPlus.Lang."; + private const string resPostfix = ".json"; + private static readonly List langs = Language.Language + .GetLanguages() + .Map(str => str.ToLower()) + .ToList(); - return Directory.GetFiles(path) - .Filter(path => Path.GetExtension(path) == ".json") - .Map(path => (Path.GetFileNameWithoutExtension(path), new StreamReader(path).ReadToEnd())) - .Map(tuple => (tuple.Item1, - (Dictionary) JsonConvert.DeserializeObject(tuple.Item2, typeof(Dictionary)) - )) - .Reduce( - (dict, tuple) => { - dict[tuple.Item1] = tuple.Item2; - return dict; - }, - new Dictionary>() - ); - } + private static string ToIdentifier(this LanguageCode code) => + code.ToString().ToLower().Replace('_', '-'); + + private static string CurrentLang => + Language.Language.CurrentLanguage().ToIdentifier(); + + private static Dictionary> ReadLangs() => Assembly + .GetExecutingAssembly() + .GetManifestResourceNames() + .Filter(name => name.EnclosedWith(resPrefix, resPostfix)) + .Map(name => (lang: name.StripStart(resPrefix).StripEnd(resPostfix), path: name)) + .Filter(tuple => langs.Contains(tuple.lang)) + .Map(tuple => (tuple.lang, stream: Assembly.GetExecutingAssembly().GetManifestResourceStream(tuple.path))) + .Map(tuple => (tuple.lang, json: tuple.stream.ReadToString())) + .Map(tuple => (tuple.lang, table: MiscUtil.DeserializeJson>(tuple.json))) + .Reduce( + (dict, tuple) => { + Logger.LogDebug($"Loaded localization for lang: {tuple.lang}"); + dict[tuple.lang] = tuple.table; + return dict; + }, + new Dictionary>() + ); private static Dictionary> Dict { get; set; } = ReadLangs(); - internal static string TryLocalize(string key) { - try { - return Dict[Language.Language.CurrentLanguage().ToString().ToLower().Replace('_', '-')][key]; - } catch { - return key; - } - } + internal static string TryLocalize(string key) => + MiscUtil.Try(() => Dict[CurrentLang][key], key); } } diff --git a/GodSeekerPlus/Util/MiscUtil.cs b/GodSeekerPlus/Util/MiscUtil.cs new file mode 100644 index 0000000..4f2533e --- /dev/null +++ b/GodSeekerPlus/Util/MiscUtil.cs @@ -0,0 +1,33 @@ +using System; +using System.IO; +using Newtonsoft.Json; + +namespace GodSeekerPlus.Util { + internal static class MiscUtil { + internal static bool EnclosedWith(this string self, string start, string end) => + self.StartsWith(start) && self.EndsWith(end); + + internal static string StripStart(this string self, string val) => + self.StartsWith(val) ? self.Substring(val.Length) : self; + + internal static string StripEnd(this string self, string val) => + self.EndsWith(val) ? self.Substring(0, self.Length - val.Length) : self; + + + internal static string ReadToString(this Stream self) => + new StreamReader(self).ReadToEnd(); + + + internal static T DeserializeJson(string json) => + (T) JsonConvert.DeserializeObject(json, typeof(T)); + + + internal static T Try(Func f, T @default) { + try { + return f(); + } catch { + return @default; + } + } + } +}