Skip to content

Commit d1695d1

Browse files
committed
fix: fix cmd and newProcess #203
1 parent 6d8c1c8 commit d1695d1

File tree

1 file changed

+48
-54
lines changed

1 file changed

+48
-54
lines changed

src/legacy/api/SystemAPI.cpp

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
#include "ll/api/service/ServerInfo.h"
1010
#include "ll/api/thread/ThreadPoolExecutor.h"
1111
#include "ll/api/utils/ErrorUtils.h"
12-
#include "ll/api/utils/StringUtils.h"
1312
#include "main/SafeGuardRecord.h"
1413
#include "utils/Utils.h"
1514

16-
#include <filesystem>
17-
#include <fstream>
18-
1915
using namespace std::filesystem;
2016

2117
//////////////////// Classes ////////////////////
@@ -30,7 +26,7 @@ ClassDefine<void> SystemClassBuilder = defineClass("system")
3026

3127
// From LiteLoaderBDSv2 llapi/utils/WinHelper.cpp
3228
bool NewProcess(
33-
const std::string& process,
29+
std::string const& process,
3430
std::function<void(int, std::string)> callback = nullptr,
3531
int timeLimit = -1
3632
) {
@@ -69,9 +65,9 @@ bool NewProcess(
6965
TerminateProcess(hProcess, 0);
7066
}
7167

72-
char buffer[8192];
73-
string strOutput;
74-
DWORD bytesRead, exitCode;
68+
char buffer[8192];
69+
std::string strOutput;
70+
DWORD bytesRead, exitCode;
7571

7672
delete[] wCmd;
7773
GetExitCodeProcess(hProcess, &exitCode);
@@ -95,6 +91,7 @@ bool NewProcess(
9591
}
9692

9793
Local<Value> SystemClass::cmd(const Arguments& args) {
94+
using namespace ll::chrono_literals;
9895
CHECK_ARGS_COUNT(args, 2);
9996
CHECK_ARG_TYPE(args[0], ValueKind::kString);
10097
CHECK_ARG_TYPE(args[1], ValueKind::kFunction);
@@ -105,35 +102,36 @@ Local<Value> SystemClass::cmd(const Arguments& args) {
105102
RecordOperation(getEngineOwnData()->pluginName, "ExecuteSystemCommand", cmd);
106103

107104
script::Global<Function> callbackFunc{args[1].asFunction()};
108-
int timeout = (args.size() >= 3) ? args[2].asNumber().toInt32() : -1;
109-
110-
ll::coro::keepThis(
111-
[cmd = std::move(cmd), callback = std::move(callbackFunc), timeout, engine = EngineScope::currentEngine()](
112-
) -> ll::coro::CoroTask<> {
113-
try {
114-
// Simulate command execution (replace with actual implementation)
115-
int exitCode = 0; // Replace with the real exit code
116-
std::string output = "Command executed"; // Replace with the real command output
117-
118-
// Simulate delay if timeout is provided
119-
if (timeout > 0) co_await std::chrono::milliseconds(timeout);
120105

121-
if (ll::getGamingStatus() != ll::GamingStatus::Running || !EngineManager::isValid(engine))
122-
co_return;
123-
124-
EngineScope scope(engine);
125-
callback.get().call({}, {Number::newNumber(exitCode), String::newString(output)});
126-
}
127-
CATCH_IN_CALLBACK("SystemCmd");
128-
}
129-
).launch(ll::thread::ThreadPoolExecutor::getDefault());
106+
return Boolean::newBoolean(NewProcess(
107+
"cmd /c" + cmd,
108+
[callback{std::move(callbackFunc)},
109+
engine{EngineScope::currentEngine()}](int exitCode, std::string output) {
110+
ll::coro::keepThis(
111+
[engine, callback = std::move(callback), exitCode, output = std::move(output)](
112+
) -> ll::coro::CoroTask<> {
113+
co_await 1_tick;
114+
if ((ll::getGamingStatus() != ll::GamingStatus::Running)) co_return;
115+
if (!EngineManager::isValid(engine)) co_return;
116+
117+
EngineScope scope(engine);
118+
try {
119+
NewTimeout(callback.get(), {Number::newNumber(exitCode), String::newString(output)}, 1);
120+
}
121+
CATCH_IN_CALLBACK("SystemCmd")
122+
}
123+
).launch(ll::thread::ThreadPoolExecutor::getDefault());
124+
},
125+
args.size() >= 3 ? args[2].asNumber().toInt32() : -1
126+
));
130127

131128
return Boolean::newBoolean(true);
132129
}
133130
CATCH("Fail in SystemCmd");
134131
}
135132

136133
Local<Value> SystemClass::newProcess(const Arguments& args) {
134+
using namespace ll::chrono_literals;
137135
CHECK_ARGS_COUNT(args, 2);
138136
CHECK_ARG_TYPE(args[0], ValueKind::kString);
139137
CHECK_ARG_TYPE(args[1], ValueKind::kFunction);
@@ -144,32 +142,28 @@ Local<Value> SystemClass::newProcess(const Arguments& args) {
144142
RecordOperation(getEngineOwnData()->pluginName, "CreateNewProcess", process);
145143

146144
script::Global<Function> callbackFunc{args[1].asFunction()};
147-
int timeout = (args.size() >= 3) ? args[2].asNumber().toInt32() : -1;
148-
149-
ll::coro::keepThis(
150-
[process = std::move(process),
151-
callback = std::move(callbackFunc),
152-
timeout,
153-
engine = EngineScope::currentEngine()]() -> ll::coro::CoroTask<> {
154-
try {
155-
// Simulate process execution (replace with actual implementation)
156-
int exitCode = 0; // Replace with the real exit code
157-
std::string output = "Process executed"; // Replace with the real process output
158-
159-
// Simulate delay if timeout is provided
160-
if (timeout > 0) co_await std::chrono::milliseconds(timeout);
161-
162-
if (ll::getGamingStatus() != ll::GamingStatus::Running || !EngineManager::isValid(engine))
163-
co_return;
164-
165-
EngineScope scope(engine);
166-
callback.get().call({}, {Number::newNumber(exitCode), String::newString(output)});
167-
}
168-
CATCH_IN_CALLBACK("newProcess");
169-
}
170-
).launch(ll::thread::ThreadPoolExecutor::getDefault());
171145

172-
return Boolean::newBoolean(true);
146+
return Boolean::newBoolean(NewProcess(
147+
process,
148+
[callback{std::move(callbackFunc)},
149+
engine{EngineScope::currentEngine()}](int exitCode, std::string output) {
150+
ll::coro::keepThis(
151+
[engine, callback = std::move(callback), exitCode, output = std::move(output)](
152+
) -> ll::coro::CoroTask<> {
153+
co_await 1_tick;
154+
if ((ll::getGamingStatus() != ll::GamingStatus::Running)) co_return;
155+
if (!EngineManager::isValid(engine)) co_return;
156+
157+
EngineScope scope(engine);
158+
try {
159+
NewTimeout(callback.get(), {Number::newNumber(exitCode), String::newString(output)}, 1);
160+
}
161+
CATCH_IN_CALLBACK("newProcess")
162+
}
163+
).launch(ll::thread::ThreadPoolExecutor::getDefault());
164+
},
165+
args.size() >= 3 ? args[2].asNumber().toInt32() : -1
166+
));
173167
}
174168
CATCH("Fail in newProcess");
175169
}

0 commit comments

Comments
 (0)