Skip to content

Commit

Permalink
Removed most launcher options
Browse files Browse the repository at this point in the history
Moved localization stuff to utils
Added SetLanguage
  • Loading branch information
Aragas committed Jun 2, 2024
1 parent 86967e5 commit 8d1d64e
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 100 deletions.
11 changes: 1 addition & 10 deletions src/Bannerlord.LauncherManager.Models/LauncherOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -97,8 +95,6 @@ namespace Bannerlord::LauncherManager
InstanceMethod<&LauncherManager::GetSaveMetadata>("getSaveMetadata", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::InstallModule>("installModule", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::IsSorting>("isSorting", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::LoadLocalization>("loadLocalization", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::LocalizeString>("localizeString", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::ModuleListHandlerExport>("moduleListHandlerExport", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::ModuleListHandlerExportSaveFile>("moduleListHandlerExportSaveFile", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&LauncherManager::ModuleListHandlerImport>("moduleListHandlerImport", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
Expand Down Expand Up @@ -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<String>();

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<String>();
const auto values = JSONStringify(env, info[1].As<Object>());

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>();

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<String>();

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<String>();
const auto values = JSONStringify(env, info[1].As<Object>());

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
13 changes: 13 additions & 0 deletions src/Bannerlord.LauncherManager.Native.TypeScript/src/lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ import { ModuleInfoExtended } from "./BannerlordModuleManager";
export interface IUtils {
isLoadOrderCorrect(modules: Array<ModuleInfoExtended>): Array<string>;
getDependencyHint(module: ModuleInfoExtended): string;

loadLocalization(xml: string): void;
setLanguage(language: string): void;
localizeString(template: string, values: { [value: string]: string }): string;
}
52 changes: 0 additions & 52 deletions src/Bannerlord.LauncherManager.Native/Bindings.LauncherManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -845,34 +821,6 @@ public record OrderByLoadOrderResult(bool Result, IReadOnlyList<string>? 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]<param_ptr*, param_string*, void> p_callback)
{
Expand Down
67 changes: 66 additions & 1 deletion src/Bannerlord.LauncherManager.Native/Bindings.Utils.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion test/Bannerlord.LauncherManager.Tests/HandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8d1d64e

Please sign in to comment.