From a17f4d3974c3a97e7e64568d60355d38a5a69b6c Mon Sep 17 00:00:00 2001 From: karikera Date: Mon, 3 Jan 2022 18:23:08 +0900 Subject: [PATCH] 1.0.10.0 - provides unicode encoders --- bdsx/bdsx.vcxproj | 2 + bdsx/bdsx.vcxproj.filters | 2 + bdsx/main.cpp | 10 ++--- bdsx/staticpointer.cpp | 87 ++---------------------------------- bdsx/staticpointer.h | 1 + bdsx/vcstring.cpp | 93 +++++++++++++++++++++++++++++++++++++++ bdsx/vcstring.h | 51 +++++++++++++++++++++ bdsx/version.bat | 2 +- 8 files changed, 157 insertions(+), 91 deletions(-) create mode 100644 bdsx/vcstring.cpp create mode 100644 bdsx/vcstring.h diff --git a/bdsx/bdsx.vcxproj b/bdsx/bdsx.vcxproj index 484efef..0e45070 100644 --- a/bdsx/bdsx.vcxproj +++ b/bdsx/bdsx.vcxproj @@ -137,6 +137,7 @@ echo #define BUILD_TIME "%25date%-%time2:.=%">"gen/buildtime.h" + @@ -162,6 +163,7 @@ echo #define BUILD_TIME "%25date%-%time2:.=%">"gen/buildtime.h" + diff --git a/bdsx/bdsx.vcxproj.filters b/bdsx/bdsx.vcxproj.filters index 86874e6..923355d 100644 --- a/bdsx/bdsx.vcxproj.filters +++ b/bdsx/bdsx.vcxproj.filters @@ -17,6 +17,7 @@ + @@ -35,6 +36,7 @@ + diff --git a/bdsx/main.cpp b/bdsx/main.cpp index 79f4f32..0451c85 100644 --- a/bdsx/main.cpp +++ b/bdsx/main.cpp @@ -12,6 +12,7 @@ #include "uvasync.h" #include "mtqueue.h" #include "gen/version.h" +#include "vcstring.h" #include #include @@ -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 @@ -264,7 +260,6 @@ 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(); @@ -272,6 +267,9 @@ void nodegate::initNativeModule(void* exports_raw) noexcept 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)); } { diff --git a/bdsx/staticpointer.cpp b/bdsx/staticpointer.cpp index a773be1..f93dfee 100644 --- a/bdsx/staticpointer.cpp +++ b/bdsx/staticpointer.cpp @@ -3,93 +3,13 @@ #include "nativepointer.h" #include "encoding.h" +#include "vcstring.h" #include #include 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)); @@ -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* str = (String*)(m_address + offset); TText16 text; try { @@ -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* str = (String*)(m_address + offset); TSZ utf8; try { @@ -702,7 +622,6 @@ void StaticPointer::_setas(T value, int offset) throws(JsException) } } - AllocatedPointer::AllocatedPointer(const kr::JsArguments& args) noexcept :JsObjectT(args) { diff --git a/bdsx/staticpointer.h b/bdsx/staticpointer.h index 35c4caa..6d637a4 100644 --- a/bdsx/staticpointer.h +++ b/bdsx/staticpointer.h @@ -3,6 +3,7 @@ #include "voidpointer.h" class NativePointer; +template struct String; class StaticPointer :public kr::JsObjectT { diff --git a/bdsx/vcstring.cpp b/bdsx/vcstring.cpp new file mode 100644 index 0000000..ed803cb --- /dev/null +++ b/bdsx/vcstring.cpp @@ -0,0 +1,93 @@ +#include "stdafx.h" +#include "vcstring.h" + +#include + +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::ERRMSG[BUF_SIZE] = "[out of memory]"; +const char16_t String::ERRMSG[BUF_SIZE] = u"[OOMem]"; + +template +CHR* String::data() noexcept { + if (capacity >= BUF_SIZE) return pointer; + else return buffer; +} +template +CHR* String::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 +void String::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; +template String; + +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* String_toWide(String* out_str, StringSpan* in_str) noexcept { + Utf8ToUtf16 cvt = Text(in_str->data, in_str->size); + out_str->init(cvt); + return out_str; +} +String* String_toUtf8(String* out_str, StringSpan* in_str) noexcept { + Utf16ToUtf8 cvt = Text16(in_str->data, in_str->size); + out_str->init(cvt); + return out_str; +} diff --git a/bdsx/vcstring.h b/bdsx/vcstring.h new file mode 100644 index 0000000..cf100e9 --- /dev/null +++ b/bdsx/vcstring.h @@ -0,0 +1,51 @@ +#pragma once + +void* String_allocate16(size_t size) noexcept; +void String_free16(void* p, size_t size) noexcept; + +template +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 + void init(const kr::HasCopyTo &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 +struct StringSpan { + size_t size; + const CHR* data; +}; + +String* String_toWide(String* out_str, StringSpan* in_str) noexcept; +String* String_toUtf8(String* out_str, StringSpan* in_str) noexcept; + +extern template String; +extern template String; diff --git a/bdsx/version.bat b/bdsx/version.bat index ffb8742..48e1c14 100644 --- a/bdsx/version.bat +++ b/bdsx/version.bat @@ -1,2 +1,2 @@ -set BDSX_CORE_VERSION=1.0.9.3 +set BDSX_CORE_VERSION=1.0.10.0