-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMain.cpp
More file actions
133 lines (108 loc) · 2.92 KB
/
Copy pathMain.cpp
File metadata and controls
133 lines (108 loc) · 2.92 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "hacklib/Main.h"
#include "hacklib/CrashHandler.h"
#include "hacklib/Memory.h"
#include "hacklib/MessageBox.h"
#include <chrono>
#include <stdexcept>
#include <thread>
hl::ModuleHandle hl::GetCurrentModule()
{
static hl::ModuleHandle hModule = 0;
if (!hModule)
{
hModule = hl::GetModuleByAddress((uintptr_t)hl::GetCurrentModule);
}
return hModule;
}
std::string hl::GetCurrentModulePath()
{
static std::string modulePath;
if (modulePath == "")
{
modulePath = hl::GetModulePath(hl::GetCurrentModule());
}
return modulePath;
}
bool hl::Main::init()
{
return true;
}
bool hl::Main::step()
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return true;
}
void hl::Main::shutdown() {}
static void ProtectedCode(const std::string& location, const std::function<void()>& body)
{
auto errorStr = "Hacklib error: " + location;
hl::CrashHandler(
[&]
{
try
{
body();
}
catch (std::exception& e)
{
hl::MsgBox(errorStr, std::string("C++ exception: ") + e.what());
}
catch (...)
{
hl::MsgBox(errorStr, "Unknown C++ exception");
}
},
[&](uint32_t code)
{
char buf[128];
#ifdef WIN32
sprintf(buf, "SEH exception 0x%08X", code);
#else
sprintf(buf, "signal %i", code);
#endif
hl::MsgBox(errorStr, buf);
});
}
hl::StaticInitImpl::StaticInitImpl()
{
ProtectedCode("hl::StaticInit construction", [&] { runMainThread(); });
}
void hl::StaticInitImpl::mainThread()
{
// Wait until the derived class has been constructed.
// This is only an issue on Linux. On Windows, we have the loader lock that prevents the thread from running before
// static initialization is done.
{
std::unique_lock l(m_mutex);
m_condVar.wait(l, [&] { return m_derivedConstructed; });
}
{
std::unique_ptr<hl::Main> pMain;
ProtectedCode("hl::Main construction", [&] { pMain = makeMain(); });
if (pMain)
{
m_pMain = pMain.get();
bool initSuccess = false;
ProtectedCode("hl::Main::init", [&] { initSuccess = m_pMain->init(); });
if (initSuccess)
{
ProtectedCode("hl::Main::step",
[&]
{
while (m_pMain->step())
{
}
});
}
ProtectedCode("hl::Main::shutdown", [&] { m_pMain->shutdown(); });
m_pMain = nullptr;
}
}
unloadSelf();
}
void hl::StaticInitImpl::notifyDerivedConstructed()
{
std::lock_guard l(m_mutex);
m_derivedConstructed = true;
m_condVar.notify_one();
}