Skip to content

Commit

Permalink
revert cpp version to make it work with pkg (#20)
Browse files Browse the repository at this point in the history
* fix: revert c++ version to make it work with pkg

* revert: revert tosu.exe
  • Loading branch information
xxCherry committed Dec 17, 2023
1 parent a9bbb97 commit a2e228d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ yarn-error.log
static/
package-lock.json
config.ini
tosu.exe
**/tosu/gameOverlay/
7 changes: 1 addition & 6 deletions packages/tsprocess/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
'sources': [ 'lib/functions.cc' ],
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
"cflags_cc": ["-std=c++23", "-fno-exceptions"],
"cflags": ["-std=c++23", "-fno-exceptions"],
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
"msbuild_settings": {
"ClCompile": {
"LanguageStandard": "stdcpplatest"
}
}
},
{
"target_name": "copy_binary",
Expand Down
33 changes: 25 additions & 8 deletions packages/tsprocess/lib/functions.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#include "process.h"
#include <format>
#include <memory>
#include <napi.h>
#include <stdexcept>
#include <string>

// https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
template <typename... Args>
std::string string_format(const std::string &format, Args... args) {
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) +
1; // Extra space for '\0'
if (size_s <= 0) {
throw std::runtime_error("Error during formatting.");
}
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(),
buf.get() + size - 1); // We don't want the '\0' inside
}

Napi::Value readByte(const Napi::CallbackInfo &args) {
Napi::Env env = args.Env();
Expand All @@ -16,7 +33,7 @@ Napi::Value readByte(const Napi::CallbackInfo &args) {
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))
string_format("Couldn't read byte at %x", address))
.ThrowAsJavaScriptException();
return env.Null();
}
Expand All @@ -37,7 +54,7 @@ Napi::Value readShort(const Napi::CallbackInfo &args) {
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))
string_format("Couldn't read short at %x", address))
.ThrowAsJavaScriptException();
return env.Null();
}
Expand All @@ -57,7 +74,7 @@ Napi::Value readInt(const Napi::CallbackInfo &args) {
auto address = args[1].As<Napi::Number>().Uint32Value();
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))
Napi::TypeError::New(env, string_format("Couldn't read int at %x", address))
.ThrowAsJavaScriptException();
return env.Null();
}
Expand All @@ -78,7 +95,7 @@ Napi::Value readFloat(const Napi::CallbackInfo &args) {
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))
string_format("Couldn't read float at %x", address))
.ThrowAsJavaScriptException();
return env.Null();
}
Expand All @@ -99,7 +116,7 @@ Napi::Value readLong(const Napi::CallbackInfo &args) {
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))
string_format("Couldn't read long at %x", address))
.ThrowAsJavaScriptException();
return env.Null();
}
Expand All @@ -120,7 +137,7 @@ Napi::Value readDouble(const Napi::CallbackInfo &args) {
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))
string_format("Couldn't read double at %x", address))
.ThrowAsJavaScriptException();
return env.Null();
}
Expand Down Expand Up @@ -176,7 +193,7 @@ Napi::Value readBuffer(const Napi::CallbackInfo &args) {
if (!result) {
free(data);
Napi::TypeError::New(env,
std::format("Couldn't read buffer at {:x}", address))
string_format("Couldn't read buffer at %x", address))
.ThrowAsJavaScriptException();

return env.Null();
Expand Down

0 comments on commit a2e228d

Please sign in to comment.