Skip to content

Commit

Permalink
Throw an exception in tsprocess instead of returning 0
Browse files Browse the repository at this point in the history
  • Loading branch information
xxCherry committed Dec 16, 2023
1 parent f19e17c commit 993e7a3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
7 changes: 6 additions & 1 deletion packages/tsprocess/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
'sources': [ 'lib/functions.cc' ],
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
"cflags": ["-std=c++23", "-fno-exceptions"],
"cflags_cc": ["-std=c++23", "-fno-exceptions"],
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
"msbuild_settings": {
"ClCompile": {
"LanguageStandard": "stdcpplatest"
}
}
},
{
"target_name": "copy_binary",
Expand Down
76 changes: 68 additions & 8 deletions packages/tsprocess/lib/functions.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "process.h"
#include <format>
#include <napi.h>

Napi::Value readByte(const Napi::CallbackInfo &args) {
Expand All @@ -12,7 +13,14 @@ Napi::Value readByte(const Napi::CallbackInfo &args) {
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Int32Value());
auto address = args[1].As<Napi::Number>().Uint32Value();
return Napi::Number::New(env, memory::read<int8_t>(handle, address));
auto result = memory::read<int8_t>(handle, address);
if (!std::get<1>(result)) {
Napi::TypeError::New(env,
std::format("Couldn't read byte at {:x}", address))
.ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, std::get<0>(result));
}

Napi::Value readShort(const Napi::CallbackInfo &args) {
Expand All @@ -26,7 +34,14 @@ Napi::Value readShort(const Napi::CallbackInfo &args) {
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Int32Value());
auto address = args[1].As<Napi::Number>().Uint32Value();
return Napi::Number::New(env, memory::read<int16_t>(handle, address));
auto result = memory::read<int16_t>(handle, address);
if (!std::get<1>(result)) {
Napi::TypeError::New(env,
std::format("Couldn't read short at {:x}", address))
.ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, std::get<0>(result));
}

Napi::Value readInt(const Napi::CallbackInfo &args) {
Expand All @@ -40,7 +55,13 @@ Napi::Value readInt(const Napi::CallbackInfo &args) {
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Int32Value());
auto address = args[1].As<Napi::Number>().Uint32Value();
return Napi::Number::New(env, memory::read<int32_t>(handle, address));
auto result = memory::read<int32_t>(handle, address);
if (!std::get<1>(result)) {
Napi::TypeError::New(env, std::format("Couldn't read int at {:x}", address))
.ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, std::get<0>(result));
}

Napi::Value readFloat(const Napi::CallbackInfo &args) {
Expand All @@ -54,7 +75,14 @@ Napi::Value readFloat(const Napi::CallbackInfo &args) {
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Int32Value());
auto address = args[1].As<Napi::Number>().Uint32Value();
return Napi::Number::New(env, memory::read<float_t>(handle, address));
auto result = memory::read<float_t>(handle, address);
if (!std::get<1>(result)) {
Napi::TypeError::New(env,
std::format("Couldn't read float at {:x}", address))
.ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, std::get<0>(result));
}

Napi::Value readLong(const Napi::CallbackInfo &args) {
Expand All @@ -68,7 +96,14 @@ Napi::Value readLong(const Napi::CallbackInfo &args) {
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Int32Value());
auto address = args[1].As<Napi::Number>().Uint32Value();
return Napi::Number::New(env, memory::read<int64_t>(handle, address));
auto result = memory::read<int64_t>(handle, address);
if (!std::get<1>(result)) {
Napi::TypeError::New(env,
std::format("Couldn't read long at {:x}", address))
.ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, std::get<0>(result));
}

Napi::Value readDouble(const Napi::CallbackInfo &args) {
Expand All @@ -82,7 +117,14 @@ Napi::Value readDouble(const Napi::CallbackInfo &args) {
auto handle =
reinterpret_cast<HANDLE>(args[0].As<Napi::Number>().Int32Value());
auto address = args[1].As<Napi::Number>().Uint32Value();
return Napi::Number::New(env, memory::read<double_t>(handle, address));
auto result = memory::read<double_t>(handle, address);
if (!std::get<1>(result)) {
Napi::TypeError::New(env,
std::format("Couldn't read double at {:x}", address))
.ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, std::get<0>(result));
}

Napi::Value scanSync(const Napi::CallbackInfo &args) {
Expand All @@ -102,6 +144,15 @@ Napi::Value scanSync(const Napi::CallbackInfo &args) {
auto vec = std::vector<uint8_t>(signature.ByteLength());
memcpy(vec.data(), signature.Data(), signature.ByteLength());

auto result = memory::find_pattern(handle, vec, refresh, baseAddress);

if (!result) {
Napi::TypeError::New(env, "Couldn't find signature")
.ThrowAsJavaScriptException();

return env.Null();
}

return Napi::Number::New(
env, memory::find_pattern(handle, vec, refresh, baseAddress));
}
Expand All @@ -119,8 +170,17 @@ Napi::Value readBuffer(const Napi::CallbackInfo &args) {
auto address = args[1].As<Napi::Number>().Uint32Value();
auto size = args[2].As<Napi::Number>().Uint32Value();
auto buffer = new char[size];
char *data = (char *)malloc(sizeof(char) * size);
memory::read_buffer(handle, address, size, data);
auto data = (char *)malloc(sizeof(char) * size);
auto result = memory::read_buffer(handle, address, size, data);

if (!result) {
free(data);
Napi::TypeError::New(env,
std::format("Couldn't read buffer at {:x}", address))
.ThrowAsJavaScriptException();

return env.Null();
}

auto out = Napi::Buffer<char>::Copy(env, data, size);
free(data);
Expand Down
6 changes: 3 additions & 3 deletions packages/tsprocess/lib/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace memory {
template <class T>
inline T read(HANDLE hProcess, uintptr_t address) {
inline std::tuple<T, bool> read(HANDLE hProcess, uintptr_t address) {
T value;
ReadProcessMemory(hProcess, reinterpret_cast<void*>(address), &value, sizeof(T), 0);
return value;
auto result = ReadProcessMemory(hProcess, reinterpret_cast<void*>(address), &value, sizeof(T), 0);
return {value, result};
}

inline bool read_buffer(HANDLE hProcess, uintptr_t address, size_t size, char* dstBuffer) {
Expand Down

0 comments on commit 993e7a3

Please sign in to comment.