Skip to content

Commit

Permalink
feat: embed lang into bin
Browse files Browse the repository at this point in the history
  • Loading branch information
Clazex committed Oct 26, 2021
1 parent 2c729f0 commit 933c0c8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
31 changes: 14 additions & 17 deletions GodSeekerPlus/GodSeekerPlus.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyTitle>GodSeekerPlus</AssemblyTitle>
<Version>0.7.2</Version>
<Version>0.7.3</Version>
<Description>A Hollow Knight mod to enhance your Godhome experience</Description>
<Authors>Clazex</Authors>
<Copyright>Copyright © 2021 Clazex</Copyright>
Expand All @@ -19,6 +19,8 @@
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Lang/**" />

<Compile Remove="dist/**" />
<EmbeddedResource Remove="dist/**" />
<None Remove="dist/**" />
Expand Down Expand Up @@ -55,26 +57,21 @@
</ItemGroup>

<Target Name="CopyMod" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(TargetPath);$(TargetDir)$(TargetName).pdb;$(SolutionDir)/README.md;$(SolutionDir)/README.zh.md;$(SolutionDir)/LICENSE" DestinationFolder="$(HKModRefs)/$(TargetName)" SkipUnchangedFiles="true" />

<RemoveDir Directories="$(HKModRefs)/$(TargetName)/lang" />
<ItemGroup>
<LangFiles Include="lang\**" />
</ItemGroup>
<Copy SourceFiles="@(LangFiles)" DestinationFiles="@(LangFiles->'$(HKModRefs)/$(TargetName)/lang/%(Filename)%(Extension)')" />
<Copy
SourceFiles="$(TargetPath);$(TargetDir)$(TargetName).pdb;$(SolutionDir)/README.md;$(SolutionDir)/README.zh.md;$(SolutionDir)/LICENSE"
DestinationFolder="$(HKModRefs)/$(TargetName)"
SkipUnchangedFiles="true"
/>
</Target>

<Target Name="CreateReleaseZip" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'">
<Target Name="CreateReleaseZip" AfterTargets="PostBuildEvent" Condition="$(Configuration) == 'Release'">
<RemoveDir Directories="$(ProjectDir)/dist" />
<MakeDir Directories="$(ProjectDir)/dist/tmp" />

<Copy SourceFiles="$(TargetPath);$(TargetDir)$(TargetName).pdb;$(SolutionDir)/README.md;$(SolutionDir)/README.zh.md;$(SolutionDir)/LICENSE" DestinationFolder="$(ProjectDir)/dist/tmp" />
<ItemGroup>
<LangFiles Include="lang\**" />
</ItemGroup>
<Copy SourceFiles="@(LangFiles)" DestinationFiles="@(LangFiles->'$(ProjectDir)/dist/tmp/lang/%(Filename)%(Extension)')" />

<ZipDirectory DestinationFile="$(ProjectDir)/dist/$(TargetName)-v$(Version).zip" SourceDirectory="$(ProjectDir)/dist/tmp" />
<Copy
SourceFiles="$(TargetPath);$(TargetDir)$(TargetName).pdb;$(SolutionDir)/README.md;$(SolutionDir)/README.zh.md;$(SolutionDir)/LICENSE"
DestinationFolder="$(ProjectDir)/dist/tmp"
/>
<ZipDirectory SourceDirectory="$(ProjectDir)/dist/tmp" DestinationFile="$(ProjectDir)/dist/$(TargetName)-v$(Version).zip" />
<RemoveDir Directories="$(ProjectDir)/dist/tmp" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion GodSeekerPlus/ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ internal static IEnumerable<T> Filter<T>(this IEnumerable<T> self, Predicate<T>
}

internal static U Reduce<T, U>(this IEnumerable<T> self, Func<U, T, U> 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;
}
}
}
61 changes: 33 additions & 28 deletions GodSeekerPlus/Util/LocalizationUtil.cs
Original file line number Diff line number Diff line change
@@ -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<string, Dictionary<string, string>> 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<string> 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<string, string>) JsonConvert.DeserializeObject(tuple.Item2, typeof(Dictionary<string, string>))
))
.Reduce(
(dict, tuple) => {
dict[tuple.Item1] = tuple.Item2;
return dict;
},
new Dictionary<string, Dictionary<string, string>>()
);
}
private static string ToIdentifier(this LanguageCode code) =>
code.ToString().ToLower().Replace('_', '-');

private static string CurrentLang =>
Language.Language.CurrentLanguage().ToIdentifier();

private static Dictionary<string, Dictionary<string, string>> 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<Dictionary<string, string>>(tuple.json)))
.Reduce(
(dict, tuple) => {
Logger.LogDebug($"Loaded localization for lang: {tuple.lang}");
dict[tuple.lang] = tuple.table;
return dict;
},
new Dictionary<string, Dictionary<string, string>>()
);

private static Dictionary<string, Dictionary<string, string>> 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);
}
}
33 changes: 33 additions & 0 deletions GodSeekerPlus/Util/MiscUtil.cs
Original file line number Diff line number Diff line change
@@ -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<T>(string json) =>
(T) JsonConvert.DeserializeObject(json, typeof(T));


internal static T Try<T>(Func<T> f, T @default) {
try {
return f();
} catch {
return @default;
}
}
}
}

0 comments on commit 933c0c8

Please sign in to comment.