From 8d1d64e601a24197488ed6ea7d51ef3058074ece Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Sun, 2 Jun 2024 13:16:10 +0300 Subject: [PATCH] Removed most launcher options Moved localization stuff to utils Added SetLanguage --- .../LauncherOptions.cs | 11 +-- .../src/Bannerlord.LauncherManager.hpp | 28 -------- .../src/Bannerlord.Utils.hpp | 39 +++++++++++ .../src/lib/LauncherManager.ts | 6 -- .../src/lib/Utils.ts | 13 ++++ .../src/lib/types/LauncherManager.ts | 2 - .../src/lib/types/Utils.ts | 4 ++ .../Bindings.LauncherManager.cs | 52 -------------- .../Bindings.Utils.cs | 67 ++++++++++++++++++- .../HandlerTests.cs | 2 +- 10 files changed, 124 insertions(+), 100 deletions(-) diff --git a/src/Bannerlord.LauncherManager.Models/LauncherOptions.cs b/src/Bannerlord.LauncherManager.Models/LauncherOptions.cs index 36a33f0..2514c84 100644 --- a/src/Bannerlord.LauncherManager.Models/LauncherOptions.cs +++ b/src/Bannerlord.LauncherManager.Models/LauncherOptions.cs @@ -7,25 +7,16 @@ public record LauncherOptions { public static readonly LauncherOptions Empty = new() { - Language = "English", - UnblockFiles = true, - FixCommonIssues = false, BetaSorting = false }; - public required string Language { get; set; } - public required bool UnblockFiles { get; set; } - public required bool FixCommonIssues { get; set; } public required bool BetaSorting { get; set; } public LauncherOptions() { } [SetsRequiredMembers, JsonConstructor] - public LauncherOptions(bool betaSorting, bool fixCommonIssues, bool unblockFiles, string language) + public LauncherOptions(bool betaSorting) { BetaSorting = betaSorting; - FixCommonIssues = fixCommonIssues; - UnblockFiles = unblockFiles; - Language = language; } } \ No newline at end of file diff --git a/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.LauncherManager.hpp b/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.LauncherManager.hpp index e30a0e9..fd09bb1 100644 --- a/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.LauncherManager.hpp +++ b/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.LauncherManager.hpp @@ -49,8 +49,6 @@ namespace Bannerlord::LauncherManager Napi::Value GetSaveMetadata(const CallbackInfo &info); Napi::Value InstallModule(const CallbackInfo &info); Napi::Value IsSorting(const CallbackInfo &info); - void LoadLocalization(const CallbackInfo &info); - Napi::Value LocalizeString(const CallbackInfo &info); void ModuleListHandlerExport(const CallbackInfo &info); void ModuleListHandlerExportSaveFile(const CallbackInfo &info); Napi::Value ModuleListHandlerImport(const CallbackInfo &info); @@ -97,8 +95,6 @@ namespace Bannerlord::LauncherManager InstanceMethod<&LauncherManager::GetSaveMetadata>("getSaveMetadata", static_cast(napi_writable | napi_configurable)), InstanceMethod<&LauncherManager::InstallModule>("installModule", static_cast(napi_writable | napi_configurable)), InstanceMethod<&LauncherManager::IsSorting>("isSorting", static_cast(napi_writable | napi_configurable)), - InstanceMethod<&LauncherManager::LoadLocalization>("loadLocalization", static_cast(napi_writable | napi_configurable)), - InstanceMethod<&LauncherManager::LocalizeString>("localizeString", static_cast(napi_writable | napi_configurable)), InstanceMethod<&LauncherManager::ModuleListHandlerExport>("moduleListHandlerExport", static_cast(napi_writable | napi_configurable)), InstanceMethod<&LauncherManager::ModuleListHandlerExportSaveFile>("moduleListHandlerExportSaveFile", static_cast(napi_writable | napi_configurable)), InstanceMethod<&LauncherManager::ModuleListHandlerImport>("moduleListHandlerImport", static_cast(napi_writable | napi_configurable)), @@ -626,30 +622,6 @@ namespace Bannerlord::LauncherManager ThrowOrReturn(env, result); } - void LauncherManager::LoadLocalization(const CallbackInfo &info) - { - const auto env = info.Env(); - const auto xml = info[0].As(); - - const auto xmlCopy = CopyWithFree(xml.Utf16Value()); - - const auto result = ve_load_localization(this->_pInstance, xmlCopy.get()); - ThrowOrReturn(env, result); - } - - Value LauncherManager::LocalizeString(const CallbackInfo &info) - { - const auto env = info.Env(); - const auto templateStr = info[0].As(); - const auto values = JSONStringify(env, info[1].As()); - - const auto templateStrCopy = CopyWithFree(templateStr.Utf16Value()); - const auto valuesCopy = CopyWithFree(values.Utf16Value()); - - const auto result = ve_localize_string(this->_pInstance, templateStrCopy.get(), valuesCopy.get()); - return ThrowOrReturnString(env, result); - } - void LauncherManager::ModuleListHandlerExport(const CallbackInfo &info) { const auto env = info.Env(); diff --git a/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.Utils.hpp b/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.Utils.hpp index f9ad62b..9cab5e0 100644 --- a/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.Utils.hpp +++ b/src/Bannerlord.LauncherManager.Native.TypeScript/src/Bannerlord.Utils.hpp @@ -33,12 +33,51 @@ namespace Bannerlord::Utils return ThrowOrReturnString(env, result); } + void LoadLocalization(const CallbackInfo &info) + { + const auto env = info.Env(); + const auto xml = info[0].As(); + + const auto xmlCopy = CopyWithFree(xml.Utf16Value()); + + const auto result = utils_load_localization(xmlCopy.get()); + ThrowOrReturn(env, result); + } + + void SetLanguage(const CallbackInfo &info) + { + const auto env = info.Env(); + const auto language = info[0].As(); + + const auto languageCopy = CopyWithFree(language.Utf16Value()); + + const auto result = utils_set_language(languageCopy.get()); + return ThrowOrReturn(env, result); + } + + Value LocalizeString(const CallbackInfo &info) + { + const auto env = info.Env(); + const auto templateStr = info[0].As(); + const auto values = JSONStringify(env, info[1].As()); + + const auto templateStrCopy = CopyWithFree(templateStr.Utf16Value()); + const auto valuesCopy = CopyWithFree(values.Utf16Value()); + + const auto result = utils_localize_string(templateStrCopy.get(), valuesCopy.get()); + return ThrowOrReturnString(env, result); + } + Object Init(const Env env, const Object exports) { exports.Set("isLoadOrderCorrect", Function::New(env, IsLoadOrderCorrect)); exports.Set("getDependencyHint", Function::New(env, GetDependencyHint)); + exports.Set("loadLocalization", Function::New(env, LoadLocalization)); + exports.Set("setLanguage", Function::New(env, SetLanguage)); + exports.Set("localizeString", Function::New(env, LocalizeString)); + return exports; } diff --git a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/LauncherManager.ts b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/LauncherManager.ts index 35636a7..81613fe 100644 --- a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/LauncherManager.ts +++ b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/LauncherManager.ts @@ -71,12 +71,6 @@ export class NativeLauncherManager implements LauncherManagerWithoutConstructor public isSorting = (): boolean => { return this.manager.isSorting(); } - public loadLocalization = (xml: string): void => { - return this.manager.loadLocalization(xml); - } - public localizeString = (template: string, values: { [value: string]: string }): string => { - return this.manager.localizeString(template, values); - } public moduleListHandlerExport = (): void => { return this.manager.moduleListHandlerExport(); } diff --git a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/Utils.ts b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/Utils.ts index 1969331..353d850 100644 --- a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/Utils.ts +++ b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/Utils.ts @@ -21,4 +21,17 @@ export class Utils { Utils.initialize(); return Utils.addon.getDependencyHint(module); } + + public loadLocalization = (xml: string): void => { + Utils.initialize(); + return Utils.addon.loadLocalization(xml); + } + public setLanguage = (language: string): void => { + Utils.initialize(); + return Utils.addon.setLanguage(language); + } + public localizeString = (template: string, values: { [value: string]: string }): string => { + Utils.initialize(); + return Utils.addon.localizeString(template, values); + } } \ No newline at end of file diff --git a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/LauncherManager.ts b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/LauncherManager.ts index 1d8e00c..ccc773e 100644 --- a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/LauncherManager.ts +++ b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/LauncherManager.ts @@ -102,8 +102,6 @@ export type LauncherManager = { getSaveMetadata(saveFile: string, data: ArrayBuffer): SaveMetadata; installModule(files: string[], moduleInfos: ModuleInfoExtendedWithMetadata[]): InstallResult; isSorting(): boolean; - loadLocalization(xml: string): void; - localizeString(template: string, values: { [value: string]: string }): string; moduleListHandlerExport(): void; moduleListHandlerExportSaveFile(saveFile: string): void; moduleListHandlerImport(): Promise; diff --git a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/Utils.ts b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/Utils.ts index db36a0a..8f4eff6 100644 --- a/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/Utils.ts +++ b/src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/types/Utils.ts @@ -3,4 +3,8 @@ import { ModuleInfoExtended } from "./BannerlordModuleManager"; export interface IUtils { isLoadOrderCorrect(modules: Array): Array; getDependencyHint(module: ModuleInfoExtended): string; + + loadLocalization(xml: string): void; + setLanguage(language: string): void; + localizeString(template: string, values: { [value: string]: string }): string; } \ No newline at end of file diff --git a/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs b/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs index 896221a..8b31757 100644 --- a/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs +++ b/src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs @@ -2,7 +2,6 @@ using Bannerlord.LauncherManager.Models; using Bannerlord.LauncherManager.Native.Adapters; using Bannerlord.LauncherManager.Native.Models; -using Bannerlord.LauncherManager.Native.Utils; using Bannerlord.LauncherManager.Utils; using BUTR.NativeAOT.Shared; @@ -107,29 +106,6 @@ public static unsafe partial class Bindings } - [UnmanagedCallersOnly(EntryPoint = "ve_load_localization", CallConvs = [typeof(CallConvCdecl)])] - public static return_value_void* LoadLocalization(param_ptr* p_handle, param_string* p_xml) - { - Logger.LogInput(); - try - { - if (p_handle is null || LauncherManagerHandlerNative.FromPointer(p_handle) is not { } handler) - return return_value_void.AsError(BUTR.NativeAOT.Shared.Utils.Copy("Handler is null or wrong!", false), false); - - var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml)); - BUTRLocalizationManager.LoadLanguage(doc); - - Logger.LogOutput(); - return return_value_void.AsValue(false); - } - catch (Exception e) - { - Logger.LogException(e); - return return_value_void.AsException(e, false); - } - } - - [UnmanagedCallersOnly(EntryPoint = "ve_get_game_version", CallConvs = [typeof(CallConvCdecl)])] public static return_value_string* GetGameVersion(param_ptr* p_handle) { @@ -845,34 +821,6 @@ public record OrderByLoadOrderResult(bool Result, IReadOnlyList? Issues, } - [UnmanagedCallersOnly(EntryPoint = "ve_localize_string", CallConvs = [typeof(CallConvCdecl)])] - public static return_value_string* LocalizeString(param_ptr* p_handle, param_string* p_template, param_json* p_values) - { - Logger.LogInput(p_template, p_values); - try - { - if (p_handle is null || LauncherManagerHandlerNative.FromPointer(p_handle) is not { } handler) - return return_value_string.AsError(BUTR.NativeAOT.Shared.Utils.Copy("Handler is null or wrong!", false), false); - - //if (p_values is null) - // return return_value_string.AsValue(string.Empty, false); - - var template = new string(param_string.ToSpan(p_template)); - var values = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_values, CustomSourceGenerationContext.DictionaryStringString); - - var result = new BUTRTextObject(template, values).ToString(); - - Logger.LogOutput(result); - return return_value_string.AsValue(result, false); - } - catch (Exception e) - { - Logger.LogException(e); - return return_value_string.AsException(e, false); - } - } - - [UnmanagedCallersOnly(EntryPoint = "ve_dialog_test_warning", CallConvs = [typeof(CallConvCdecl)])] public static return_value_void* DialogTestWarning(param_ptr* p_handle, param_ptr* p_callback_handler, delegate* unmanaged[Cdecl] p_callback) { diff --git a/src/Bannerlord.LauncherManager.Native/Bindings.Utils.cs b/src/Bannerlord.LauncherManager.Native/Bindings.Utils.cs index 6377e50..903037d 100644 --- a/src/Bannerlord.LauncherManager.Native/Bindings.Utils.cs +++ b/src/Bannerlord.LauncherManager.Native/Bindings.Utils.cs @@ -1,4 +1,6 @@ -using Bannerlord.LauncherManager.Utils; +using Bannerlord.LauncherManager.Localization; +using Bannerlord.LauncherManager.Native.Utils; +using Bannerlord.LauncherManager.Utils; using BUTR.NativeAOT.Shared; @@ -57,4 +59,67 @@ public static unsafe partial class Bindings return return_value_string.AsException(e, false); } } + + [UnmanagedCallersOnly(EntryPoint = "utils_load_localization", CallConvs = [typeof(CallConvCdecl)])] + public static return_value_void* LoadLocalization(param_string* p_xml) + { + Logger.LogInput(); + try + { + var doc = ReaderUtils2.Read(param_string.ToSpan(p_xml)); + BUTRLocalizationManager.LoadLanguage(doc); + + Logger.LogOutput(); + return return_value_void.AsValue(false); + } + catch (Exception e) + { + Logger.LogException(e); + return return_value_void.AsException(e, false); + } + } + + [UnmanagedCallersOnly(EntryPoint = "utils_set_language", CallConvs = [typeof(CallConvCdecl)])] + public static return_value_void* SetLanguage(param_string* p_language) + { + Logger.LogInput(p_language); + try + { + var language = new string(param_string.ToSpan(p_language)); + + BUTRLocalizationManager.ActiveLanguage = language; + + Logger.LogOutput(); + return return_value_void.AsValue(false); + } + catch (Exception e) + { + Logger.LogException(e); + return return_value_void.AsException(e, false); + } + } + + [UnmanagedCallersOnly(EntryPoint = "utils_localize_string", CallConvs = [typeof(CallConvCdecl)])] + public static return_value_string* LocalizeString(param_string* p_template, param_json* p_values) + { + Logger.LogInput(p_template, p_values); + try + { + //if (p_values is null) + // return return_value_string.AsValue(string.Empty, false); + + var template = new string(param_string.ToSpan(p_template)); + var values = BUTR.NativeAOT.Shared.Utils.DeserializeJson(p_values, CustomSourceGenerationContext.DictionaryStringString); + + var result = new BUTRTextObject(template, values).ToString(); + + Logger.LogOutput(result); + return return_value_string.AsValue(result, false); + } + catch (Exception e) + { + Logger.LogException(e); + return return_value_string.AsException(e, false); + } + } } \ No newline at end of file diff --git a/test/Bannerlord.LauncherManager.Tests/HandlerTests.cs b/test/Bannerlord.LauncherManager.Tests/HandlerTests.cs index def5ca4..edff425 100644 --- a/test/Bannerlord.LauncherManager.Tests/HandlerTests.cs +++ b/test/Bannerlord.LauncherManager.Tests/HandlerTests.cs @@ -165,7 +165,7 @@ public void OrderBy_Test() ), launcherStateUProvider: new CallbackLauncherStateProvider( setGameParameters: (executable, parameters) => { }, - getOptions: () => new LauncherOptions(false, false, false, "English"), + getOptions: () => new LauncherOptions(false), getState: () => new LauncherState(true) ), loadOrderPersistenceProvider: new CallbackLoadOrderPersistenceProvider(