Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Feb 3, 2023
1 parent 636bd09 commit 79c8a2f
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 94 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/publish-native-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ jobs:
with:
dotnet-version: 7.x.x

- run: dotnet publish -r win-x64 --self-contained -c Debug src/FetchBannerlordVersion.Native
- run: dotnet publish -r win-x64 --self-contained -c Release src/FetchBannerlordVersion.Native

- run: dotnet test test/FetchBannerlordVersion.Native.Tests
- run: dotnet test test/FetchBannerlordVersion.Native.Tests -c Debug
- run: dotnet test test/FetchBannerlordVersion.Native.Tests -c Release

- uses: actions/[email protected]
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,4 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
/src/FetchBannerlordVersion.Native/FetchBannerlordVersion.Native.h
3 changes: 1 addition & 2 deletions src/FetchBannerlordVersion.Native.TypeScript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"native:1": "copyfiles -f ../FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/native/FetchBannerlordVersion.Native.dll .",
"native:2": "copyfiles -f ../FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/native/FetchBannerlordVersion.Native.lib .",
"native:3": "copyfiles -f ../FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/FetchBannerlordVersion.Native.h .",
"native:4": "copyfiles -f ../FetchBannerlordVersion.Native/bin/Release/net7.0/win-x64/Common.Native.h .",
"build": "run-s native:* && run-s build:*",
"build-ts": "run-s build:*",
"build-native": "run-s native:*",
Expand Down Expand Up @@ -105,4 +104,4 @@
"**/*.spec.*"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BFE_COMMON_GUARD_HPP_
#define BFE_COMMON_GUARD_HPP_

#include <napi.h>
#include "FetchBannerlordVersion.Native.h"

using namespace Napi;
using namespace FetchBannerlordVersion::Native;

namespace Bannerlord::Common
{

Value AllocAliveCount(const CallbackInfo &info)
{
const auto env = info.Env();

const auto result = common_alloc_alive_count();
return Number::New(env, result);
}

Object Init(const Env env, const Object exports)
{
exports.Set("allocAliveCount", Function::New(env, AllocAliveCount));

return exports;
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
#define BFE_GUARD_HPP_

#include "utils.hpp"
#include "Common.Native.h"
#include "FetchBannerlordVersion.Native.h"
#include <codecvt>

using namespace Napi;
using namespace Common;
using namespace Utils;
using namespace Bannerlord::FetchVersion;
using namespace FetchBannerlordVersion::Native;

namespace Bannerlord::FetchVersion
{

const Value GetChangeSetWrapped(const CallbackInfo &info)
Value GetChangeSetWrapped(const CallbackInfo &info)
{
const auto env = info.Env();
const auto gameFolderPath = info[0].As<String>();
Expand All @@ -27,7 +24,7 @@ namespace Bannerlord::FetchVersion
return ThrowOrReturnUInt32(env, result);
}

const Value GetVersionWrapped(const CallbackInfo &info)
Value GetVersionWrapped(const CallbackInfo &info)
{
const auto env = info.Env();
const auto gameFolderPath = info[0].As<String>();
Expand All @@ -40,7 +37,7 @@ namespace Bannerlord::FetchVersion
return ThrowOrReturnString(env, result);
}

const Value GetVersionTypeWrapped(const CallbackInfo &info)
Value GetVersionTypeWrapped(const CallbackInfo &info)
{
const auto env = info.Env();
const auto gameFolderPath = info[0].As<String>();
Expand All @@ -53,7 +50,7 @@ namespace Bannerlord::FetchVersion
return ThrowOrReturnUInt32(env, result);
}

const Object Init(Env env, Object exports)
Object Init(const Env env, const Object exports)
{
exports.Set("getChangeSet", Function::New(env, GetChangeSetWrapped));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { IFetchBannerlordVersion } from "./types";

const fetcher: IFetchBannerlordVersion = require('./../../fetchblversion.node');
const fetcher: IFetchBannerlordVersion & { allocAliveCount(): number; } = require('./../../fetchblversion.node');

export const allocAliveCount = (): number => {
return fetcher.allocAliveCount();
}

export class FetchBannerlordVersion {
/* istanbul ignore next */
Expand Down
6 changes: 4 additions & 2 deletions src/FetchBannerlordVersion.Native.TypeScript/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "Bannerlord.Common.hpp"
#include "Bannerlord.FetchVersion.hpp"
#include <napi.h>

using namespace Napi;
using namespace Bannerlord::FetchVersion;

Object InitAll(const Env env, const Object exports)
{
return Init(env, exports);
Bannerlord::Common::Init(env, exports);
Bannerlord::FetchVersion::Init(env, exports);
return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import test from 'ava';

import { FetchBannerlordVersion } from '../lib/FetchBannerlordVersion';
import { FetchBannerlordVersion, allocAliveCount } from '../lib/FetchBannerlordVersion';

const isDebug = process.argv[2] == "Debug";

test('Main', async (t) => {
const path = __dirname;
Expand All @@ -15,5 +17,8 @@ test('Main', async (t) => {
const versionType = FetchBannerlordVersion.getVersionType(path, dllName);
t.is(versionType, 4);

if (isDebug)
t.deepEqual(allocAliveCount(), 0);

t.pass();
});
76 changes: 65 additions & 11 deletions src/FetchBannerlordVersion.Native.TypeScript/src/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,91 @@
#ifndef BFE_UTILS_GUARD_HPP_
#define BFE_UTILS_GUARD_HPP_

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

using namespace Napi;
using namespace Common;
using namespace FetchBannerlordVersion::Native;

namespace Utils
{

template <typename T>
struct deleter
{
void operator()(T *const ptr) const { common_dealloc(static_cast<void *const>(ptr)); }
};

using del_void = std::unique_ptr<return_value_void, deleter<return_value_void>>;
using del_string = std::unique_ptr<return_value_string, deleter<return_value_string>>;
using del_json = std::unique_ptr<return_value_json, deleter<return_value_json>>;
using del_bool = std::unique_ptr<return_value_bool, deleter<return_value_bool>>;
using del_int32 = std::unique_ptr<return_value_int32, deleter<return_value_int32>>;
using del_uint32 = std::unique_ptr<return_value_uint32, deleter<return_value_uint32>>;
using del_ptr = std::unique_ptr<return_value_ptr, deleter<return_value_ptr>>;

char16_t *const Copy(const std::u16string str)
{
const auto src = str.c_str();
const auto srcChar16Length = str.length();
const auto srcByteLength = srcChar16Length * sizeof(char16_t);
const auto size = srcByteLength + sizeof(char16_t);

auto dst = static_cast<char16_t *const>(common_alloc(size));
if (dst == nullptr)
{
throw std::bad_alloc();
}
std::memmove(dst, src, srcByteLength);
dst[srcChar16Length] = '\0';
return dst;
}

std::unique_ptr<char16_t[], deleter<char16_t>> CopyWithFree(const std::u16string str)
{
return std::unique_ptr<char16_t[], deleter<char16_t>>(Copy(str));
}

const char16_t *const NoCopy(const std::u16string str) noexcept
{
return str.c_str();
}

template <typename T>
T *const Create(const T val)
{
const auto size = sizeof(T);
auto dst = static_cast<T *const>(alloc(size));
if (dst == nullptr)
{
throw std::bad_alloc();
}
std::memcpy(dst, &val, sizeof(T));
return dst;
}

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)
String JSONStringify(const Env env, const Object object)
{
const auto jsonObject = env.Global().Get("JSON").As<Object>();
const auto stringify = jsonObject.Get("stringify").As<Function>();
return stringify.Call(jsonObject, {object}).As<String>();
}
const Object JSONParse(const Env env, const String json)
Object JSONParse(const Env env, const String json)
{
const auto jsonObject = env.Global().Get("JSON").As<Object>();
const auto parse = jsonObject.Get("parse").As<Function>();
return parse.Call(jsonObject, {json}).As<Object>();
}

void ThrowOrReturn(Env env, return_value_void *val)
void ThrowOrReturn(const Env env, return_value_void *const val)
{
const del_void del{val};

Expand All @@ -42,7 +96,7 @@ namespace Utils
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)
Value ThrowOrReturnString(const Env env, return_value_string *const val)
{
const del_string del{val};

Expand All @@ -59,7 +113,7 @@ namespace Utils
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)
Value ThrowOrReturnJson(const Env env, return_value_json *const val)
{
const del_json del{val};

Expand All @@ -76,7 +130,7 @@ namespace Utils
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)
Value ThrowOrReturnBoolean(const Env env, return_value_bool *const val)
{
const del_bool del{val};

Expand All @@ -87,7 +141,7 @@ namespace Utils
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)
Value ThrowOrReturnInt32(const Env env, return_value_int32 *const val)
{
const del_int32 del{val};

Expand All @@ -98,7 +152,7 @@ namespace Utils
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)
Value ThrowOrReturnUInt32(const Env env, return_value_uint32 *const val)
{
const del_uint32 del{val};

Expand All @@ -109,7 +163,7 @@ namespace Utils
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)
void *ThrowOrReturnPtr(const Env env, return_value_ptr *const val)
{
const del_ptr del{val};

Expand Down
21 changes: 13 additions & 8 deletions src/FetchBannerlordVersion.Native/Bindings.Shared.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using BUTR.NativeAOT.Shared;

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace FetchBannerlordVersion.Native
{
public static unsafe partial class Bindings
{
[UnmanagedCallersOnly(EntryPoint = "alloc")]
public static void* Alloc(nuint size)
[UnmanagedCallersOnly(EntryPoint = "common_alloc", CallConvs = new[] { typeof(CallConvCdecl) }), IsNotConst<IsPtrConst>]
public static void* CommonAlloc(nuint size)
{
Logger.LogInput(size);
try
{
var result = Allocator.Alloc(size);

Logger.LogOutput(new IntPtr(result).ToString("x16"), nameof(Alloc));
Logger.LogOutput(new IntPtr(result).ToString("x16"), nameof(CommonAlloc));
return result;
}
catch (Exception e)
Expand All @@ -25,10 +26,10 @@ public static unsafe partial class Bindings
}
}

[UnmanagedCallersOnly(EntryPoint = "dealloc")]
public static void Dealloc(param_ptr* ptr)
[UnmanagedCallersOnly(EntryPoint = "common_dealloc", CallConvs = new[] { typeof(CallConvCdecl) })]
public static void Common_Dealloc([IsConst<IsPtrConst>] param_ptr* ptr)
{
Logger.LogInput(new IntPtr(ptr).ToString("x16"), nameof(Dealloc));
Logger.LogInput(new IntPtr(ptr).ToString("x16"), nameof(Common_Dealloc));
try
{
Allocator.Free(ptr);
Expand All @@ -41,13 +42,17 @@ public static void Dealloc(param_ptr* ptr)
}
}

[UnmanagedCallersOnly(EntryPoint = "alloc_alive_count")]
public static int AllocAliveCount()
[UnmanagedCallersOnly(EntryPoint = "common_alloc_alive_count", CallConvs = new[] { typeof(CallConvCdecl) })]
public static int CommonAllocAliveCount()
{
Logger.LogInput();
try
{
#if TRACK_ALLOCATIONS
var result = Allocator.GetCurrentAllocations();
#else
var result = 0;
#endif

Logger.LogOutput(result);
return result;
Expand Down
Loading

0 comments on commit 79c8a2f

Please sign in to comment.