-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathUtils.cpp
More file actions
197 lines (169 loc) · 5.37 KB
/
Copy pathUtils.cpp
File metadata and controls
197 lines (169 loc) · 5.37 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "stdafx.h"
#include <CoreLib/Base/Base.h>
#include <Shlwapi.h>
BEGIN_SE()
STDString ToUTF8(WStringView s)
{
int size = WideCharToMultiByte(CP_UTF8, 0, s.data(), (int)s.size(), NULL, 0, NULL, NULL);
STDString converted;
converted.resize(size);
WideCharToMultiByte(CP_UTF8, 0, s.data(), (int)s.size(), converted.data(), (int)converted.size(), NULL, NULL);
return converted;
}
STDWString FromUTF8(StringView s)
{
int size = MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), NULL, 0);
STDWString converted;
converted.resize(size);
MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), converted.data(), (int)converted.size());
return converted;
}
std::string ToStdUTF8(std::wstring_view s)
{
int size = WideCharToMultiByte(CP_UTF8, 0, s.data(), (int)s.size(), NULL, 0, NULL, NULL);
std::string converted;
converted.resize(size);
WideCharToMultiByte(CP_UTF8, 0, s.data(), (int)s.size(), converted.data(), (int)converted.size(), NULL, NULL);
return converted;
}
std::wstring FromStdUTF8(std::string_view s)
{
int size = MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), NULL, 0);
std::wstring converted;
converted.resize(size);
MultiByteToWideChar(CP_UTF8, 0, s.data(), (int)s.size(), converted.data(), (int)converted.size());
return converted;
}
std::optional<std::string> GetExeResource(int resourceId)
{
auto hResource = FindResource(gCoreLibPlatformInterface.ThisModule, MAKEINTRESOURCE(resourceId), L"SCRIPT_EXTENDER");
if (hResource) {
auto hGlobal = LoadResource(gCoreLibPlatformInterface.ThisModule, hResource);
if (hGlobal) {
auto resourceData = LockResource(hGlobal);
if (resourceData) {
DWORD resourceSize = SizeofResource(gCoreLibPlatformInterface.ThisModule, hResource);
std::string contents;
contents.resize(resourceSize);
memcpy(contents.data(), resourceData, resourceSize);
return contents;
}
}
}
return {};
}
void Debug(DebugMessageType type, _In_z_ _Printf_format_string_ char const* fmt, ...)
{
if (gCoreLibPlatformInterface.GlobalConsole) {
va_list args;
va_start(args, fmt);
char buf[1024];
_vsnprintf_s(buf, std::size(buf), _TRUNCATE, fmt, args);
va_end(args);
gCoreLibPlatformInterface.GlobalConsole->Print(type, buf);
}
}
void DebugLocal(DebugMessageType type, _In_z_ _Printf_format_string_ char const* fmt, ...)
{
if (gCoreLibPlatformInterface.GlobalConsole) {
va_list args;
va_start(args, fmt);
char buf[1024];
_vsnprintf_s(buf, std::size(buf), _TRUNCATE, fmt, args);
va_end(args);
gCoreLibPlatformInterface.GlobalConsole->LocalPrint(type, buf);
}
}
void TryDebugBreak()
{
#if defined(_DEBUG)
if (IsDebuggerPresent() && gCoreLibPlatformInterface.EnableDebugBreak) {
DebugBreak();
}
#endif
}
[[noreturn]]
void Fail(char const * reason)
{
ERR("%s", reason);
TryDebugBreak();
MessageBoxA(NULL, reason, "BG3 Script Extender Error", MB_OK | MB_ICONERROR);
TerminateProcess(GetCurrentProcess(), 1);
}
bool TryCreateDirectory(std::wstring const& path)
{
if (!PathFileExistsW(path.c_str())) {
return CreateDirectoryW(path.c_str(), NULL) == TRUE;
} else {
return true;
}
}
bool SaveFile(std::wstring const& path, std::vector<uint8_t> const& body)
{
std::ofstream f(path, std::ios::binary | std::ios::out);
if (!f.good()) {
return false;
}
f.write(reinterpret_cast<char const*>(body.data()), body.size());
return f.good();
}
bool SaveFile(std::wstring const& path, std::string_view body)
{
std::ofstream f(path, std::ios::binary | std::ios::out);
if (!f.good()) {
return false;
}
f.write(body.data(), body.size());
return f.good();
}
bool LoadFile(std::wstring const& path, std::vector<uint8_t>& body)
{
std::ifstream f(path, std::ios::in | std::ios::binary);
if (f.good()) {
f.seekg(0, std::ios::end);
auto size = f.tellg();
f.seekg(0, std::ios::beg);
body.resize(size);
f.read(reinterpret_cast<char*>(body.data()), size);
return f.good();
}
return false;
}
bool LoadFile(std::wstring const& path, std::string& body)
{
std::ifstream f(path, std::ios::in | std::ios::binary);
if (f.good()) {
f.seekg(0, std::ios::end);
auto size = f.tellg();
f.seekg(0, std::ios::beg);
body.resize(size);
f.read(body.data(), size);
return f.good();
}
return false;
}
void const* ResolveRealFunctionAddress(void const * ptr)
{
auto p = (uint8_t const*)ptr;
// Unconditional jump
if (p[0] == 0xE9) {
int32_t relOffset = *reinterpret_cast<int32_t const *>(p + 1);
return p + relOffset + 5;
}
// Resolve function pointer through relocations
auto end = p + 64;
for (; p < end; p++)
{
// Look for the instruction "cmp qword ptr [rip+xxxxxx], 0"
if (p[0] == 0x48 && p[1] == 0x83 && p[2] == 0x3d && p[6] == 0x00 &&
// Look for the instruction "jmp xxxx"
p[13] == 0xe9)
{
int32_t relOffset = *reinterpret_cast<int32_t const *>(p + 14);
return p + relOffset + 18;
}
}
// Could not find any relocations
return ptr;
}
END_SE()