-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
96 lines (81 loc) · 2.92 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#include <Windows.h>
#include <Shlwapi.h>
#include <string>
#include <array>
#include <filesystem>
std::filesystem::path getRootFolder()
{
std::array<WCHAR, MAX_PATH> res;
GetModuleFileNameW(NULL, res.data(), res.size());
PathRemoveFileSpecW(res.data());
return res.data();
}
bool findNode(std::filesystem::path& NodePath)
{
const std::wstring NodeName = L"node.exe";
std::array<WCHAR, MAX_PATH> res;
res.fill(0);
std::copy(NodeName.begin(), NodeName.end(), res.begin());
if (!PathFindOnPathW(res.data(), NULL))
return false;
NodePath = res.data();
return true;
}
bool launch(const std::filesystem::path& ExecutableName, const std::wstring& CommandLine)
{
STARTUPINFOW StartupInfo;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
PROCESS_INFORMATION ProcessInformation;
ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
if (!CreateProcessW(ExecutableName.wstring().c_str(), (LPWSTR)CommandLine.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInformation))
return false;
CloseHandle(ProcessInformation.hProcess);
CloseHandle(ProcessInformation.hThread);
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
{
const auto RootFolder = getRootFolder();
std::filesystem::current_path(RootFolder);
#ifdef LAUNCH_CLI
std::filesystem::path NodeExe;
if (!findNode(NodeExe))
{
MessageBoxW(NULL, L"Node.JS is not installed!\nPlease go to https://discord.gg/dUNDDtw and follow the installation guide.", L"Error", 0);
return 1;
}
if (!launch(NodeExe, L"--use-strict ./bin/index-cli.js"))
{
MessageBoxW(NULL, L"Unable to launch TERA Toolbox!\nPlease go to https://discord.gg/dUNDDtw and ask for help.", L"Error", 0);
return 1;
}
#else
const auto ElectronExe = RootFolder / L"node_modules/electron/dist/electron.exe";
if (std::filesystem::exists(ElectronExe))
{
if (!launch(ElectronExe, L"--high-dpi-support=1 --force-device-scale-factor=1 --js-flags=\"--use-strict\" ./bin/index-gui.js"))
{
MessageBoxW(NULL, L"Unable to launch TERA Toolbox!\nPlease go to https://discord.gg/dUNDDtw and ask for help.", L"Error", 0);
return 1;
}
}
else
{
std::filesystem::path NodeExe;
if (!findNode(NodeExe))
{
MessageBoxW(NULL, L"Node.JS is not installed!\nPlease go to https://discord.gg/dUNDDtw and follow the installation guide.", L"Error", 0);
return 1;
}
if (!launch(NodeExe, L"--use-strict ./bin/install-electron.js"))
{
MessageBoxW(NULL, L"Unable to launch TERA Toolbox!\nPlease go to https://discord.gg/dUNDDtw and ask for help.", L"Error", 0);
return 1;
}
}
#endif
return 0;
}