Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:IceBlcokMC/Js_Engine into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Jan 6, 2025
2 parents 9740ad4 + 8cd6a4a commit 2889e20
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
51 changes: 47 additions & 4 deletions src/Manager/NodeManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,53 @@
#include <unordered_map>
#include <utility>

#ifdef _WIN32
#include "Windows.h"
#include "shellapi.h"
#else
#include <cstdio>
#endif


namespace jse {

#ifdef _WIN32
#pragma comment(lib, "Shell32.lib")
std::string wstr2str(const std::wstring& ws) {
auto len = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr);
std::string res(len + 1, 0);
WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), res.data(), len, nullptr, nullptr);
return res;
}
std::vector<std::string> GetArgs() {
int argc = 0;
auto argv = CommandLineToArgvW(GetCommandLineW(), &argc);
std::vector<std::string> res(argc);
for (int i = 0; i < argc; i++) {
res[i] = wstr2str(argv[i]);
}
LocalFree(argv);
return res;
}
#else
std::vector<std::string> GetArgs() {
FILE* cmdline = fopen("/proc/self/cmdline", "rb");
std::vector<std::string> res;
std::string t;
int c = fgetc(cmdline);
while (c != EOF) {
if (c == 0) {
if (!t.empty()) res.push_back(t);
t.clear();
} else {
t += c;
}
c = fgetc(cmdline);
}
fclose(cmdline);
return res;
}
#endif

NodeManager& NodeManager::getInstance() {
static NodeManager instance;
Expand All @@ -32,11 +76,10 @@ void NodeManager::initNodeJs() {
if (mIsInitialized) {
return;
}
static auto args = GetArgs();
mArgs = args;

auto workingDir = fs::current_path() / "bedrock_server.exe";
mArgs = {workingDir.string()};

char* cWorkingDir = const_cast<char*>(workingDir.string().c_str());
char* cWorkingDir = args[0].data();
uv_setup_args(1, &cWorkingDir);
cppgc::InitializeProcess();
std::vector<string> errors;
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ Local<Value> DoScriptTypeConvert(const T& value) {
}
return obj;
} else if constexpr (std::is_same_v<ScriptType, Value>) {
VariantConvert(value);
return VariantConvert(value);
}
}
template <typename T, std::size_t I>
Local<Value> VariantConvert(const T& value) {
if (auto res = std::get_if<I>(&value)) {
return DoScriptTypeConvert(*res);
} else {
if constexpr (I < std::variant_size_v<T>) return VariantConvert<T, I + 1>(value);
if constexpr (I + 1 < std::variant_size_v<T>) return VariantConvert<T, I + 1>(value);
else {
throw std::runtime_error("Invalid variant");
}
Expand Down Expand Up @@ -244,7 +244,7 @@ struct FromScriptType<T, std::enable_if_t<IsReflectable<T>>> {
static T Convert(const Local<Value>& value, T res = {}) {
auto obj = value.asObject();
boost::pfr::for_each_field(res, [&](auto& field, std::size_t index) {
field = FromScriptType<std::remove_cvref_t<T>>::Convert(
field = FromScriptType<std::remove_cvref_t<decltype(field)>>::Convert(
obj.get(boost::pfr::names_as_array<std::remove_cvref_t<T>>()[index])
);
});
Expand Down

0 comments on commit 2889e20

Please sign in to comment.