Skip to content

Commit

Permalink
Native fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Jan 29, 2023
1 parent e2d4826 commit 4b1db7e
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 81 deletions.
9 changes: 9 additions & 0 deletions src/Bannerlord.ModuleManager.DotNetJS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,14 @@ public static void DisableModule(ModuleInfoExtended[] modules, ModuleInfoExtende

[JSInvokable]
public static int CompareVersions(ApplicationVersion x, ApplicationVersion y) => _applicationVersionComparer.Compare(x, y);

[JSInvokable]
public static DependentModuleMetadata[] GetDependenciesAll(ModuleInfoExtended module) => module.DependenciesAllDistinct().ToArray();
[JSInvokable]
public static DependentModuleMetadata[] GetDependenciesToLoadBeforeThis(ModuleInfoExtended module) => module.DependenciesLoadBeforeThisDistinct().ToArray();
[JSInvokable]
public static DependentModuleMetadata[] GetDependenciesToLoadAfterThis(ModuleInfoExtended module) => module.DependenciesLoadAfterThisDistinct().ToArray();
[JSInvokable]
public static DependentModuleMetadata[] GetDependenciesIncompatibles(ModuleInfoExtended module) => module.DependenciesIncompatiblesDistinct().ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef BMM_BLMANAGER_GUARD_H_
#define BMM_BLMANAGER_GUARD_H_
#ifndef BMM_BLMANAGER_GUARD_HPP_
#define BMM_BLMANAGER_GUARD_HPP_

#include "utils.h"
#include "utils.hpp"
#include "Common.Native.h"
#include "Bannerlord.ModuleManager.Native.h"
#include <codecvt>
Expand All @@ -26,7 +26,7 @@ namespace Bannerlord::ModuleManager
const auto data = (ValidationData *)p_owner;
const auto env = data->Env;

const auto moduleId = String::New(env, Copy(p_module_id));
const auto moduleId = String::New(env, p_module_id);

return Create(return_value_bool{nullptr, data->FIsSelected({moduleId}).As<Boolean>()});
}
Expand All @@ -52,7 +52,7 @@ namespace Bannerlord::ModuleManager
const auto data = (EnableDisableData *)p_owner;
const auto env = data->Env;

const auto moduleId = String::New(env, Copy(p_module_id));
const auto moduleId = String::New(env, p_module_id);

return Create(return_value_bool{nullptr, data->FGetSelected({moduleId}).As<Boolean>()});
}
Expand All @@ -62,15 +62,15 @@ namespace Bannerlord::ModuleManager
return Create(return_value_bool{Copy(conv.from_bytes(e.what())), false});
}
}
static return_value_void *setSelected(const void *p_owner, const param_string *p_module_id, bool value_raw)
static return_value_void *setSelected(const void *p_owner, const param_string *p_module_id, param_bool value_raw)
{
try
{
const auto data = (EnableDisableData *)p_owner;
const auto env = data->Env;

const auto moduleId = String::New(env, Copy(p_module_id));
const auto value = Boolean::New(env, value_raw);
const auto moduleId = String::New(env, p_module_id);
const auto value = Boolean::New(env, value_raw == 1);

data->FSetSelected({moduleId, value});
return Create(return_value_void{nullptr});
Expand Down Expand Up @@ -98,15 +98,15 @@ namespace Bannerlord::ModuleManager
return Create(return_value_bool{Copy(conv.from_bytes(e.what())), false});
}
}
static return_value_void *setDisabled(const void *p_owner, const param_string *p_module_id, bool value_raw)
static return_value_void *setDisabled(const void *p_owner, const param_string *p_module_id, param_bool value_raw)
{
try
{
const auto data = (EnableDisableData *)p_owner;
const auto env = data->Env;

const auto moduleId = String::New(env, Copy(p_module_id));
const auto value = Boolean::New(env, value_raw);
const auto moduleId = String::New(env, p_module_id);
const auto value = Boolean::New(env, value_raw == 1);

data->FSetDisabled({moduleId, value});
return Create(return_value_void{nullptr});
Expand Down Expand Up @@ -287,6 +287,47 @@ namespace Bannerlord::ModuleManager
return ThrowOrReturnInt32(env, result);
}

Value GetDependenciesAll(const CallbackInfo &info)
{
const auto env = info.Env();
const auto module = JSONStringify(env, info[0].As<Object>());

const auto moduleCopy = CopyWithFree(module.Utf16Value());

const auto result = bmm_get_dependencies_all(moduleCopy.get());
return ThrowOrReturnJson(env, result);
}
Value GetDependenciesToLoadBeforeThis(const CallbackInfo &info)
{
const auto env = info.Env();
const auto module = JSONStringify(env, info[0].As<Object>());

const auto moduleCopy = CopyWithFree(module.Utf16Value());

const auto result = bmm_get_dependencies_load_before_this(moduleCopy.get());
return ThrowOrReturnJson(env, result);
}
Value GetDependenciesToLoadAfterThis(const CallbackInfo &info)
{
const auto env = info.Env();
const auto module = JSONStringify(env, info[0].As<Object>());

const auto moduleCopy = CopyWithFree(module.Utf16Value());

const auto result = bmm_get_dependencies_load_after_this(moduleCopy.get());
return ThrowOrReturnJson(env, result);
}
Value GetDependenciesIncompatibles(const CallbackInfo &info)
{
const auto env = info.Env();
const auto module = JSONStringify(env, info[0].As<Object>());

const auto moduleCopy = CopyWithFree(module.Utf16Value());

const auto result = bmm_get_dependencies_incompatibles(moduleCopy.get());
return ThrowOrReturnJson(env, result);
}

const Object Init(Env env, Object exports)
{
exports.Set("sort", Function::New(env, Sort));
Expand All @@ -308,6 +349,11 @@ namespace Bannerlord::ModuleManager

exports.Set("compareVersions", Function::New(env, CompareVersions));

exports.Set("getDependenciesAll", Function::New(env, GetDependenciesAll));
exports.Set("getDependenciesToLoadBeforeThis", Function::New(env, GetDependenciesToLoadBeforeThis));
exports.Set("getDependenciesToLoadAfterThis", Function::New(env, GetDependenciesToLoadAfterThis));
exports.Set("getDependenciesIncompatibles", Function::New(env, GetDependenciesIncompatibles));

return exports;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ export class BannerlordModuleManager {
static compareVersions(x: types.ApplicationVersion, y: types.ApplicationVersion): number {
return blmanager.compareVersions(x, y);
}

static getDependenciesAll(module: types.ModuleInfoExtended): types.DependentModuleMetadata[] {
return blmanager.getDependenciesAll(module);
}
static getDependenciesToLoadBeforeThis(module: types.ModuleInfoExtended): types.DependentModuleMetadata[] {
return blmanager.getDependenciesToLoadBeforeThis(module);
}
static getDependenciesToLoadAfterThis(module: types.ModuleInfoExtended): types.DependentModuleMetadata[] {
return blmanager.getDependenciesToLoadAfterThis(module);
}
static getDependenciesIncompatibles(module: types.ModuleInfoExtended): types.DependentModuleMetadata[] {
return blmanager.getDependenciesIncompatibles(module);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,9 @@ export interface IBannerlordModuleManager {
getSubModuleInfo(xml: string): SubModuleInfoExtended | undefined;

compareVersions(x: ApplicationVersion, y: ApplicationVersion): number;

getDependenciesAll(module: ModuleInfoExtended): DependentModuleMetadata[];
getDependenciesToLoadBeforeThis(module: ModuleInfoExtended): DependentModuleMetadata[];
getDependenciesToLoadAfterThis(module: ModuleInfoExtended): DependentModuleMetadata[];
getDependenciesIncompatibles(module: ModuleInfoExtended): DependentModuleMetadata[];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Bannerlord.ModuleManager.h"
#include "Bannerlord.ModuleManager.hpp"
#include <napi.h>

using namespace Napi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#ifndef BMM_UTILS_GUARD_H_
#define BMM_UTILS_GUARD_H_
#ifndef BMM_UTILS_GUARD_HPP_
#define BMM_UTILS_GUARD_HPP_

#include <Common.Native.h>
#include <napi.h>
#include <cstdint>

using namespace Napi;
using namespace Common;

namespace Utils
{

void ConsoleLog(const Env env, const String message)
{
const auto consoleObject = env.Global().Get("console").As<Object>();
const auto log = consoleObject.Get("log").As<Function>();
log.Call(consoleObject, {message});
}

const String JSONStringify(const Env env, const Object object)
{
const auto jsonObject = env.Global().Get("JSON").As<Object>();
Expand All @@ -31,8 +39,8 @@ namespace Utils
{
return;
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}
const Value ThrowOrReturnString(Env env, return_value_string *val)
{
Expand All @@ -42,14 +50,14 @@ namespace Utils
{
if (val->value == nullptr)
{
throw Error::New(env, String::New(env, "Return value was null!"));
NAPI_THROW(Error::New(env, String::New(env, "Return value was null!")));
}

const auto value = std::unique_ptr<char16_t[]>(val->value);
const auto value = std::unique_ptr<char16_t[], deleter<char16_t>>(val->value);
return String::New(env, val->value);
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}
const Value ThrowOrReturnJson(Env env, return_value_json *val)
{
Expand All @@ -59,14 +67,14 @@ namespace Utils
{
if (val->value == nullptr)
{
throw Error::New(env, String::New(env, "Return value was null!"));
NAPI_THROW(Error::New(env, String::New(env, "Return value was null!")));
}

const auto value = std::unique_ptr<char16_t[]>(val->value);
const auto value = std::unique_ptr<char16_t[], deleter<char16_t>>(val->value);
return JSONParse(env, String::New(env, val->value));
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}
const Value ThrowOrReturnBoolean(Env env, return_value_bool *val)
{
Expand All @@ -76,8 +84,8 @@ namespace Utils
{
return Boolean::New(env, val->value);
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}
const Value ThrowOrReturnInt32(Env env, return_value_int32 *val)
{
Expand All @@ -87,8 +95,8 @@ namespace Utils
{
return Number::New(env, val->value);
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}
const Value ThrowOrReturnUInt32(Env env, return_value_uint32 *val)
{
Expand All @@ -98,8 +106,8 @@ namespace Utils
{
return Number::New(env, val->value);
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}
void *ThrowOrReturnPtr(Env env, return_value_ptr *val)
{
Expand All @@ -109,8 +117,8 @@ namespace Utils
{
return val->value;
}
const auto error = std::unique_ptr<char16_t[]>(val->error);
throw Error::New(env, String::New(env, error.get()));
const auto error = std::unique_ptr<char16_t[], deleter<char16_t>>(val->error);
NAPI_THROW(Error::New(env, String::New(env, error.get())));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.10" GeneratePathProperty="true" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="BUTR.NativeAOT.Shared.Source" Version="1.0.16" GeneratePathProperty="true" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>

<Target Name="CopyHeaders" AfterTargets="AfterBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

#include "Common.Native.h"

#ifdef __cplusplus
using namespace Common;

namespace Bannerlord::ModuleManager {

#ifdef __cplusplus
extern "C" {
#endif

Expand All @@ -28,24 +27,29 @@ namespace Bannerlord::ModuleManager {

return_value_void* __cdecl bmm_enable_module(const void* p_owner, const param_json* p_modules, const param_json* p_target_module
, return_value_bool* (__cdecl *p_get_selected)(const void* p_owner, const param_string* p_module_id)
, return_value_void* (__cdecl *p_set_selected)(const void* p_owner, const param_string* p_module_id, bool value)
, return_value_void* (__cdecl *p_set_selected)(const void* p_owner, const param_string* p_module_id, param_bool value)
, return_value_bool* (__cdecl *p_get_disabled)(const void* p_owner, const param_string* p_module_id)
, return_value_void* (__cdecl *p_set_disabled)(const void* p_owner, const param_string* p_module_id, bool value));
, return_value_void* (__cdecl *p_set_disabled)(const void* p_owner, const param_string* p_module_id, param_bool value));
return_value_void* __cdecl bmm_disable_module(const void* p_owner, const param_json* p_modules, const param_json* p_target_module
, return_value_bool* (__cdecl *p_get_selected)(const void* p_owner, const param_string* p_module_id)
, return_value_void* (__cdecl *p_set_selected)(const void* p_owner, const param_string* p_module_id, bool value)
, return_value_void* (__cdecl *p_set_selected)(const void* p_owner, const param_string* p_module_id, param_bool value)
, return_value_bool* (__cdecl *p_get_disabled)(const void* p_owner, const param_string* p_module_id)
, return_value_void* (__cdecl *p_set_disabled)(const void* p_owner, const param_string* p_module_id, bool value));
, return_value_void* (__cdecl *p_set_disabled)(const void* p_owner, const param_string* p_module_id, param_bool value));

return_value_json* __cdecl bmm_get_module_info(const param_string* p_xml_content);
return_value_json* __cdecl bmm_get_sub_module_info(const param_string* p_xml_content);

return_value_int32* __cdecl bmm_compare_versions(const param_json* p_x, const param_json* p_y);

return_value_json* __cdecl bmm_get_dependencies_all(const param_json* p_module);
return_value_json* __cdecl bmm_get_dependencies_load_before_this(const param_json* p_module);
return_value_json* __cdecl bmm_get_dependencies_load_after_this(const param_json* p_module);
return_value_json* __cdecl bmm_get_dependencies_incompatibles(const param_json* p_module);


#ifdef __cplusplus
}
#endif

}
#endif

#endif
22 changes: 22 additions & 0 deletions src/Bannerlord.ModuleManager.Native/Bindings.Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;

namespace Bannerlord.ModuleManager.Native
{
public static partial class Bindings
{
private static readonly JsonSerializerOptions _options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IgnoreReadOnlyFields = true,
IgnoreReadOnlyProperties = true,
IncludeFields = false,
WriteIndented = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin)
};
internal static readonly SourceGenerationContext CustomSourceGenerationContext = new(_options);
}
}
Loading

0 comments on commit 4b1db7e

Please sign in to comment.