Skip to content

Commit

Permalink
Optimize loading language resources
Browse files Browse the repository at this point in the history
  • Loading branch information
shatyuka committed Jan 7, 2025
1 parent 8ef11fb commit d17f190
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CollapseLauncher/CollapseLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ShouldComputeInputPris>true</ShouldComputeInputPris>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions Hi3Helper.Core/Classes/Preset/Classes/ClassesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public sealed partial class CoreLibraryJSONContext : JsonSerializerContext;

[JsonSourceGenerationOptions(IncludeFields = true, GenerationMode = JsonSourceGenerationMode.Metadata, IgnoreReadOnlyFields = true)]
[JsonSerializable(typeof(LocalizationParams))]
[JsonSerializable(typeof(LocalizationParamsBase))]
internal sealed partial class CoreLibraryFieldsJSONContext : JsonSerializerContext;
}
26 changes: 20 additions & 6 deletions Hi3Helper.Core/Lang/Localization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public LangMetadata(string filePath, int index)

try
{
_ = LoadLang(filePath);
_ = LoadLangBase(filePath);
LogWriteLine($"Locale file: {langRelativePath} loaded as {this.LangName} by {this.LangAuthor}", LogType.Scheme, true);
}
catch (Exception ex)
Expand Down Expand Up @@ -72,20 +72,20 @@ public LocalizationParams LoadLang()
#endif
}

public LocalizationParams LoadLang(string langPath)
public LocalizationParamsBase LoadLangBase(string langPath)
{
using (Stream s = new FileStream(langPath!, FileMode.Open, FileAccess.Read))
{
return LoadLang(s);
return LoadLangBase(s);
}
}

#if APPLYUPDATE
public LocalizationParams LoadLang(Uri langUri)
public LocalizationParamsBase LoadLangBase(Uri langUri)
{
using (Stream s = AssetLoader.Open(langUri))
{
return LoadLang(s);
return LoadLangBase(s);
}
}
#endif
Expand All @@ -101,6 +101,17 @@ public LocalizationParams LoadLang(Stream langStream)
return _langData;
}

public LocalizationParamsBase LoadLangBase(Stream langStream)
{
LocalizationParamsBase _langData = JsonSerializer.Deserialize(langStream!, CoreLibraryFieldsJSONContext.Default.LocalizationParamsBase);
this.LangAuthor = _langData!.Author;
this.LangID = _langData.LanguageID.ToLower();
this.LangName = _langData.LanguageName;
this.LangIsLoaded = true;

return _langData;
}

public int LangIndex;
public string LangFilePath;
public string LangAuthor;
Expand Down Expand Up @@ -234,12 +245,15 @@ private static void TryLoadSizePrefix(LocalizationParams langData)
public static LocalizationParams? LangFallback;

[GeneratedBindableCustomProperty]
public sealed partial class LocalizationParams
public partial class LocalizationParamsBase
{
public string LanguageName { get; set; } = "";
public string LanguageID { get; set; } = "";
public string Author { get; set; } = "Unknown";
}

[GeneratedBindableCustomProperty]
public sealed partial class LocalizationParams : LocalizationParamsBase;
}

public class LocalizationNotFoundException : Exception
Expand Down

0 comments on commit d17f190

Please sign in to comment.