Skip to content

Commit

Permalink
1.0.10.0 - provides unicode encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
karikera committed Jan 3, 2022
1 parent 1d21877 commit a17f4d3
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 91 deletions.
2 changes: 2 additions & 0 deletions bdsx/bdsx.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ echo #define BUILD_TIME "%25date%-%time2:.=%"&gt;"gen/buildtime.h"</Command>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="forward.cpp" />
<ClCompile Include="vcstring.cpp" />
<ClCompile Include="mtqueue.cpp" />
<ClCompile Include="uvasync.cpp" />
<ClCompile Include="structurepointer.cpp" />
Expand All @@ -162,6 +163,7 @@ echo #define BUILD_TIME "%25date%-%time2:.=%"&gt;"gen/buildtime.h"</Command>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcstring.h" />
<ClInclude Include="mtqueue.h" />
<ClInclude Include="unwind.h" />
<ClInclude Include="uvasync.h" />
Expand Down
2 changes: 2 additions & 0 deletions bdsx/bdsx.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ClCompile Include="staticpointer.cpp" />
<ClCompile Include="mtqueue.cpp" />
<ClCompile Include="forward.cpp" />
<ClCompile Include="vcstring.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="jsctx.h" />
Expand All @@ -35,6 +36,7 @@
<ClInclude Include="staticpointer.h" />
<ClInclude Include="mtqueue.h" />
<ClInclude Include="unwind.h" />
<ClInclude Include="vcstring.h" />
</ItemGroup>
<ItemGroup>
<Filter Include="src">
Expand Down
10 changes: 4 additions & 6 deletions bdsx/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "uvasync.h"
#include "mtqueue.h"
#include "gen/version.h"
#include "vcstring.h"

#include <KR3/win/windows.h>
#include <KRWin/hook.h>
Expand Down Expand Up @@ -151,11 +152,6 @@ BOOL WINAPI DllMain(

namespace
{
float tester(double a, float b, int64_t c) noexcept
{
return (float)a;
}

void trycatch(void* param, void(*func)(void*), void(*_catch)(void*, pcstr)) noexcept
{
try
Expand Down Expand Up @@ -264,14 +260,16 @@ void nodegate::initNativeModule(void* exports_raw) noexcept
});
cgate.setMethod(u"nodeLoopOnce", nodegate::loopOnce);
cgate.set(u"nodeLoop", VoidPointer::make(nodegate::loop));
cgate.set(u"tester", VoidPointer::make(tester));

cgate.setMethod(u"allocExecutableMemory", [](uint64_t size, uint64_t align) {
hook::ExecutableAllocator* alloc = hook::ExecutableAllocator::getInstance();
StaticPointer* ptr = StaticPointer::newInstance();
ptr->setAddressRaw(alloc->alloc(size, max(align, 1)));
return ptr;
});

cgate.set(u"toWide", VoidPointer::make(String_toWide));
cgate.set(u"toUtf8", VoidPointer::make(String_toUtf8));
}

{
Expand Down
87 changes: 3 additions & 84 deletions bdsx/staticpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,13 @@

#include "nativepointer.h"
#include "encoding.h"
#include "vcstring.h"

#include <KR3/util/unaligned.h>
#include <KR3/win/handle.h>

using namespace kr;

struct Ucrtbase
{
void*(*malloc)(size_t size);
void(*free)(void* ptr);

Ucrtbase() noexcept
{
HMODULE dll = GetModuleHandleW(L"ucrtbase.dll");
_assert(dll != nullptr);
malloc = (autoptr)GetProcAddress(dll, "malloc");
free = (autoptr)GetProcAddress(dll, "free");
}
};
namespace
{
Ucrtbase ucrtbase;

void* allocate16(size_t size) noexcept {
if (size >= 0x1000) {
uintptr_t res = (uintptr_t)ucrtbase.malloc(size+0x27);
if (res == 0) return nullptr;
void** out = (void**)((res + 0x27) & (~0x1f));
out[-1] = (void*)res;
return out;
}
else {
return ucrtbase.malloc(size);
}
}

void free16(void* p, size_t size) noexcept {
if (size >= 0x1000) {
p = ((void**)p)[-1];
}
ucrtbase.free(p);
}
}

struct String
{
union
{
char buffer[16];
char* pointer;
};
size_t size;
size_t capacity;

char* data() noexcept
{
if (capacity >= 0x10) return pointer;
else return buffer;
}
void assign(Text text) noexcept
{
size_t nsize = text.size();
char* dest;
if (nsize > capacity)
{
if (capacity >= 0x10) free16(pointer, capacity);
capacity = nsize;
dest = pointer = (char*)allocate16(nsize + 1);
if (dest == nullptr)
{
memcpy(buffer, "[out of memory]", 16);
capacity = 15;
size = 15;
return;
}
}
else
{
dest = data();
}
memcpy(dest, text.data(), nsize);
dest[nsize] = '\0';
size = nsize;
}
};


ATTR_NORETURN void accessViolation(const void* address) throws(JsException)
{
throw JsException((Text16)(TSZ16() << u"Access Violation: " << address));
Expand Down Expand Up @@ -252,7 +172,7 @@ JsValue StaticPointer::getBuffer(int bytes, int offset) throws(JsException)
}
TText16 StaticPointer::getCxxString(int offset, int encoding) throws(JsException)
{
String* str = (String*)(m_address + offset);
String<char>* str = (String<char>*)(m_address + offset);
TText16 text;
try
{
Expand Down Expand Up @@ -413,7 +333,7 @@ void StaticPointer::setBuffer(JsValue buffer, int offset) throws(JsException)
}
void StaticPointer::setCxxString(Text16 text, int offset, int encoding) throws(JsException)
{
String* str = (String*)(m_address + offset);
String<char>* str = (String<char>*)(m_address + offset);
TSZ utf8;
try
{
Expand Down Expand Up @@ -702,7 +622,6 @@ void StaticPointer::_setas(T value, int offset) throws(JsException)
}
}


AllocatedPointer::AllocatedPointer(const kr::JsArguments& args) noexcept
:JsObjectT<AllocatedPointer, StaticPointer>(args)
{
Expand Down
1 change: 1 addition & 0 deletions bdsx/staticpointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "voidpointer.h"

class NativePointer;
template <typename CHR> struct String;

class StaticPointer :public kr::JsObjectT<StaticPointer, VoidPointer>
{
Expand Down
93 changes: 93 additions & 0 deletions bdsx/vcstring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "stdafx.h"
#include "vcstring.h"

#include <KR3/win/handle.h>

using namespace kr;

struct Ucrtbase
{
void* (*malloc)(size_t size);
void(*free)(void* ptr);

Ucrtbase() noexcept
{
HMODULE dll = GetModuleHandleW(L"ucrtbase.dll");
_assert(dll != nullptr);
malloc = (autoptr)GetProcAddress(dll, "malloc");
free = (autoptr)GetProcAddress(dll, "free");
}
};
namespace {
Ucrtbase ucrtbase;
}

const char String<char>::ERRMSG[BUF_SIZE] = "[out of memory]";
const char16_t String<char16_t>::ERRMSG[BUF_SIZE] = u"[OOMem]";

template <typename CHR>
CHR* String<CHR>::data() noexcept {
if (capacity >= BUF_SIZE) return pointer;
else return buffer;
}
template <typename CHR>
CHR* String<CHR>::resize(size_t nsize) noexcept {
if (nsize > capacity)
{
if (capacity >= BUF_SIZE) String_free16(pointer, capacity);
capacity = nsize;
pointer = (CHR*)String_allocate16(nsize + 1);
if (pointer == nullptr)
{
memcpy(buffer, "[out of memory]", 16);
capacity = BUF_SIZE-1;
size = BUF_SIZE-1;
}
return pointer;
}
else
{
return data();
}
}
template <typename CHR>
void String<CHR>::assign(kr::Text text) noexcept {
size_t nsize = text.size();
CHR* dest = resize(nsize);
memcpy(dest, text.data(), nsize);
dest[nsize] = (CHR)'\0';
size = nsize;
}

template String<char>;
template String<char16_t>;

void* String_allocate16(size_t size) noexcept {
if (size >= 0x1000) {
uintptr_t res = (uintptr_t)ucrtbase.malloc(size + 0x27);
if (res == 0) return nullptr;
void** out = (void**)((res + 0x27) & (~0x1f));
out[-1] = (void*)res;
return out;
}
else {
return ucrtbase.malloc(size);
}
}

void String_free16(void* p, size_t size) noexcept {
if (size >= 0x1000) {
p = ((void**)p)[-1];
}
ucrtbase.free(p);
}
String<char16_t>* String_toWide(String<char16_t>* out_str, StringSpan<char>* in_str) noexcept {
Utf8ToUtf16 cvt = Text(in_str->data, in_str->size);
out_str->init(cvt);
return out_str;
}
String<char>* String_toUtf8(String<char>* out_str, StringSpan<char16_t>* in_str) noexcept {
Utf16ToUtf8 cvt = Text16(in_str->data, in_str->size);
out_str->init(cvt);
return out_str;
}
51 changes: 51 additions & 0 deletions bdsx/vcstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

void* String_allocate16(size_t size) noexcept;
void String_free16(void* p, size_t size) noexcept;

template<typename CHR>
struct String {
static constexpr size_t BUF_SIZE = 16 / sizeof(CHR);
static const CHR ERRMSG[BUF_SIZE];

union {
CHR buffer[BUF_SIZE];
CHR* pointer;
};
size_t size;
size_t capacity;

CHR* data() noexcept;
CHR* resize(size_t nsize) noexcept;
void assign(kr::Text text) noexcept;

template <typename Derived, typename Component, typename Parent>
void init(const kr::HasCopyTo<Derived, Component, Parent> &text) noexcept {
size_t sz = text.size();

if (sz < BUF_SIZE) {
size = sz;
capacity = BUF_SIZE-1;
text.copyTo(buffer);
buffer[sz] = (CHR)'\0';
}
else {
pointer = (CHR*)String_allocate16(sz*sizeof(CHR) + sizeof(CHR));
text.copyTo(pointer);
pointer[sz] = (CHR)'\0';
capacity = size = sz;
}
}
};

template <typename CHR>
struct StringSpan {
size_t size;
const CHR* data;
};

String<char16_t>* String_toWide(String<char16_t>* out_str, StringSpan<char>* in_str) noexcept;
String<char>* String_toUtf8(String<char>* out_str, StringSpan<char16_t>* in_str) noexcept;

extern template String<char>;
extern template String<char16_t>;
2 changes: 1 addition & 1 deletion bdsx/version.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

set BDSX_CORE_VERSION=1.0.9.3
set BDSX_CORE_VERSION=1.0.10.0

0 comments on commit a17f4d3

Please sign in to comment.