Skip to content

Commit 939e3b0

Browse files
committed
adapt new DBLoadCtx vtable in bo7 handler
1 parent d474a37 commit 939e3b0

File tree

4 files changed

+70
-34
lines changed

4 files changed

+70
-34
lines changed

src/acts/decryptutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace acts::decryptutils {
3737
};
3838

3939
for (auto& cfg : knownScans) {
40-
std::vector<hook::library::ScanResult> res = lib.Scan(cfg.pattern);
40+
std::vector<hook::library::ScanResult> res = lib.Scan(cfg.pattern, false, cfg.id);
4141

4242
if (res.size() != 1) {
4343
if (res.size() > 1) {

src/acts/tools/fastfile/handlers/handler_game_bo7.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,39 @@ namespace fastfile::handlers::bo7 {
5454
Asset* assets;
5555
};
5656

57+
enum XFileBlock : int {
58+
XFILE_BLOCK_MEMMAPPED = 11,
59+
XFILE_BLOCK_VIRTUAL = 15,
60+
XFILE_BLOCK_COUNT = 16,
61+
};
62+
5763
struct DBLoadCtx;
5864
struct DBLoadCtxVT {
65+
void (*unk0)(DBLoadCtx* ctx, byte a1);
5966
void* (__fastcall* AllocStreamPos)(DBLoadCtx* ctx, int align);
60-
void(__fastcall* PushStreamPos)(DBLoadCtx* ctx, int type);
67+
void(__fastcall* PushStreamPos)(DBLoadCtx* ctx, XFileBlock type);
6168
void(__fastcall* PopStreamPos)(DBLoadCtx* ctx);
6269
void(__fastcall* PreAssetRead)(DBLoadCtx* ctx, HandlerAssetType type);
6370
void(__fastcall* PostAssetRead)(DBLoadCtx* ctx);
6471
};
72+
struct XZoneTemporaryLoadData {
73+
// todo instead of in vector patch
74+
};
75+
76+
struct DBLoadCtxData {
77+
byte __pad[392];
78+
XFileBlock streamPosIndex;
79+
uint32_t unk194;
80+
byte __pad198[1080];
81+
XZoneTemporaryLoadData* tempData;
82+
byte __pad5d8[56];
83+
void* unk608;
84+
byte unk610;
85+
};
6586

6687
struct DBLoadCtx {
6788
DBLoadCtxVT* __vtb;
68-
// DBLoadCtxData data;
89+
DBLoadCtxData data;
6990
};
7091

7192
union LoadStreamObjectData;
@@ -109,15 +130,18 @@ namespace fastfile::handlers::bo7 {
109130
void ErrorStub(LoadStreamObjectData* that) {
110131
throw std::runtime_error(std::format("Error loadstream {}", hook::library::CodePointer{ _ReturnAddress() }));
111132
}
133+
void DBLoadCtxUnk0(DBLoadCtx* ctx, byte a1) {
134+
throw std::runtime_error(std::format("DBLoadCtxUnk0 {}", hook::library::CodePointer{ _ReturnAddress() }));
135+
}
112136
void* AllocStreamPos(DBLoadCtx* ctx, int align);
113-
void PushStreamPos(DBLoadCtx* ctx, int type);
137+
void PushStreamPos(DBLoadCtx* ctx, XFileBlock type);
114138
void PopStreamPos(DBLoadCtx* ctx);
115139
void PreAssetRead(DBLoadCtx* ctx, HandlerAssetType type);
116140
void PostAssetRead(DBLoadCtx* ctx);
117141

118142

119143
LoadStreamObjectVtable dbLoadStreamVTable{ LoadStreamImpl, ErrorStub, ErrorStub, ErrorStub , ErrorStub };
120-
DBLoadCtxVT dbLoadCtxVTable{ AllocStreamPos, PushStreamPos, PopStreamPos, PreAssetRead, PostAssetRead };
144+
DBLoadCtxVT dbLoadCtxVTable{ DBLoadCtxUnk0, AllocStreamPos, PushStreamPos, PopStreamPos, PreAssetRead, PostAssetRead };
121145

122146
struct XStringOutCTX {
123147
std::filesystem::path path;
@@ -134,7 +158,7 @@ namespace fastfile::handlers::bo7 {
134158
core::bytebuffer::ByteBuffer* reader{};
135159
HandlerHashedAssetType assetLoadStack[0x100];
136160
size_t assetLoadStackTop{};
137-
int streamPosStack[64]{};
161+
XFileBlock streamPosStack[64]{};
138162
int streamPosStackIndex{};
139163
size_t loaded{};
140164
core::memory_allocator::MemoryAllocator allocator{};
@@ -184,9 +208,9 @@ namespace fastfile::handlers::bo7 {
184208
}
185209
if (!gcx.streamPosStackIndex) throw std::runtime_error("empty streampos stack");
186210

187-
int block{ gcx.streamPosStack[gcx.streamPosStackIndex - 1] };
211+
XFileBlock block{ gcx.streamPosStack[gcx.streamPosStackIndex - 1] };
188212

189-
if (block == 11) {
213+
if (block == XFILE_BLOCK_MEMMAPPED) {
190214
return false;
191215
}
192216
else {
@@ -242,8 +266,8 @@ namespace fastfile::handlers::bo7 {
242266
}
243267
}
244268

245-
void PushStreamPos(DBLoadCtx* ctx, int type) {
246-
LOG_TRACE("PushStreamPos({}) {}", type, hook::library::CodePointer{ _ReturnAddress() });
269+
void PushStreamPos(DBLoadCtx* ctx, XFileBlock type) {
270+
LOG_TRACE("PushStreamPos({}) {}", (int)type, hook::library::CodePointer{ _ReturnAddress() });
247271
gcx.streamPosStack[gcx.streamPosStackIndex++] = type;
248272
}
249273

@@ -445,6 +469,15 @@ namespace fastfile::handlers::bo7 {
445469
Red(scan.ScanSingle("48 89 5C 24 ?? 48 89 6C 24 ?? 48 89 4C 24 ?? 56 57 41 54 41 56 41 57 48 83 EC ?? 45 33", "EmptyStub<20>").location, EmptyStub<20>); // dlogschema
446470
Red(scan.ScanSingle("49 8B C0 4C 8B 02", "EmptyStub<7>").location, EmptyStub<21>); // model
447471

472+
void (*ErrorStub)(uint32_t code, int a2, const char* msg, int a4) {
473+
[](uint32_t code, int a2, const char* msg, int a4) -> void {
474+
475+
throw std::runtime_error(std::format("Error 0x{:x} : {}", code, msg ? msg : "no message"));
476+
}
477+
};
478+
479+
Red(scan.ScanSingle("40 55 53 57 41 54 41 55 41 56 48 8D AC 24 58", "Error").location, ErrorStub);
480+
448481

449482
if (scan.foundMissing) {
450483
throw std::runtime_error("Can't find some patterns");

src/shared/hook/library.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace hook::library {
106106
loc = "";
107107
return nullptr;
108108
}
109-
std::vector<ScanResult> ScanLibraryString(HMODULE hmod, const char* pattern, bool single) {
109+
std::vector<ScanResult> ScanLibraryString(HMODULE hmod, const char* pattern, bool single, const char* name) {
110110
size_t pl{ std::strlen(pattern) };
111111
std::string patStr{};
112112
patStr.resize((pl + 1) * 3 + 1);
@@ -117,10 +117,10 @@ namespace hook::library {
117117
std::snprintf(&patStrPtr[3 * i], 4, "%02x ", (uint32_t)pattern[i]);
118118
}
119119

120-
return ScanLibrary(hmod, patStrPtr, single);
120+
return ScanLibrary(hmod, patStrPtr, single, name);
121121
}
122122

123-
std::vector<ScanResult> ScanLibrary(HMODULE hmod, const char* pattern, bool single) {
123+
std::vector<ScanResult> ScanLibrary(HMODULE hmod, const char* pattern, bool single, const char* name) {
124124
std::vector<ScanResult> res{};
125125

126126

@@ -143,7 +143,7 @@ namespace hook::library {
143143

144144
if (c == '?') {
145145
if (mid) {
146-
throw std::runtime_error(utils::va("Wildcard pattern in half byte! %s", pattern));
146+
throw std::runtime_error(std::format("Wildcard pattern in half byte! {} ({})", pattern, name ? name : "no name"));
147147
}
148148
if (str[0] == '?') {
149149
// test if we are in a packed context
@@ -172,7 +172,7 @@ namespace hook::library {
172172

173173
// reversed because we set it by default to 0
174174
if (!mid) {
175-
throw std::runtime_error(utils::va("Scan pattern has half byte! %s", pattern));
175+
throw std::runtime_error(std::format("Scan pattern has half byte! {} ({})", pattern, name ? name : "no name"));
176176
}
177177

178178
auto it1 = mask.begin();
@@ -188,7 +188,7 @@ namespace hook::library {
188188
}
189189

190190
if (!mask.size()) {
191-
throw std::runtime_error(utils::va("Empty pattern! %s", pattern));
191+
throw std::runtime_error(std::format("Empty pattern! {} ({})", pattern, name ? name : "no name"));
192192
}
193193

194194
// clear end
@@ -203,19 +203,19 @@ namespace hook::library {
203203
}
204204

205205
if (!mask.size()) {
206-
throw std::runtime_error(utils::va("Empty pattern! %s", pattern));
206+
throw std::runtime_error(std::format("Empty pattern! {} ({})", pattern, name ? name : "no name"));
207207
}
208208

209209
library::Library lib{ hmod };
210210

211211
constexpr size_t lazySize = 0x10000000;
212212

213213
if (lazySize < mask.size()) {
214-
throw std::runtime_error(utils::va("Pattern too big! %s", pattern)); // wtf?
214+
throw std::runtime_error(utils::va("Pattern too big! {} ({})", pattern, pattern, name ? name : "no name")); // wtf?
215215
}
216216

217217

218-
LOG_TRACE("Start searching of pattern {}", pattern);
218+
LOG_TRACE("Start searching of pattern {} ({})", pattern, name ? name : "no name");
219219
byte* start{ (byte*)*lib };
220220
byte* end{ (byte*)lib[lazySize - mask.size()] };
221221

src/shared/hook/library.hpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,28 +148,31 @@ namespace hook::library {
148148
* @param hmod module
149149
* @param pattern pattern
150150
* @param single single search
151+
* @param name name
151152
* @return matches
152153
*/
153-
std::vector<ScanResult> ScanLibrary(HMODULE hmod, const char* pattern, bool single = false);
154+
std::vector<ScanResult> ScanLibrary(HMODULE hmod, const char* pattern, bool single = false, const char* name = nullptr);
154155

155156
/*
156157
* Scan the memory to find a string
157158
* @param hmod module
158159
* @param pattern pattern
159160
* @param single single search
161+
* @param name name
160162
* @return matches
161163
*/
162-
std::vector<ScanResult> ScanLibraryString(HMODULE hmod, const char* pattern, bool single = false);
164+
std::vector<ScanResult> ScanLibraryString(HMODULE hmod, const char* pattern, bool single = false, const char* name = nullptr);
163165

164166
/*
165167
* Scan the memory to find data
166168
* @param hmod module
167169
* @param pattern pattern
168170
* @param single single search
171+
* @param name name
169172
* @return matches
170173
*/
171174
template<typename T>
172-
std::vector<ScanResult> ScanLibraryNumber(HMODULE hmod, T val, bool single = false) {
175+
std::vector<ScanResult> ScanLibraryNumber(HMODULE hmod, T val, bool single = false, const char* name = nullptr) {
173176
char buff[sizeof(T) * 3 + 1]{};
174177

175178
byte* ptr{ (byte*)&val };
@@ -179,7 +182,7 @@ namespace hook::library {
179182
patStrPtr += std::snprintf(patStrPtr, 4, "%02x ", (int)ptr[i]);
180183
}
181184

182-
return ScanLibrary(hmod, buff, single);
185+
return ScanLibrary(hmod, buff, single, name);
183186
}
184187
class LibraryModule;
185188
class Detour;
@@ -288,13 +291,13 @@ namespace hook::library {
288291
return process::PImageDosHeader(hmod);
289292
}
290293

291-
inline std::vector<ScanResult> Scan(const char* pattern, bool single = false) const {
292-
return ScanLibrary(hmod, pattern, single);
294+
inline std::vector<ScanResult> Scan(const char* pattern, bool single = false, const char* name = nullptr) const {
295+
return ScanLibrary(hmod, pattern, single, name);
293296
}
294297

295298

296299
ScanResult ScanSingle(const char* pattern, const char* name = nullptr) const {
297-
auto res = Scan(pattern);
300+
auto res = Scan(pattern, false, name);
298301

299302
if (res.empty()) {
300303
throw std::runtime_error(utils::va("Can't find pattern %s", name ? name : pattern));
@@ -308,7 +311,7 @@ namespace hook::library {
308311

309312

310313
ScanResult ScanAny(const char* pattern, const char* name = nullptr) const {
311-
auto res = Scan(pattern, true);
314+
auto res = Scan(pattern, true, name);
312315

313316
if (res.empty()) {
314317
throw std::runtime_error(utils::va("Can't find pattern %s", name ? name : pattern));
@@ -368,28 +371,28 @@ namespace hook::library {
368371
return FindAnyScan(name, args...);
369372
}
370373

371-
inline std::vector<ScanResult> ScanString(const char* str, bool single = false) const {
372-
return ScanLibraryString(hmod, str, single);
374+
inline std::vector<ScanResult> ScanString(const char* str, bool single = false, const char* name = nullptr) const {
375+
return ScanLibraryString(hmod, str, single, name);
373376
}
374377

375378
ScanResult ScanStringSingle(const char* str, const char* name = nullptr) const {
376-
auto res = ScanString(str);
379+
auto res = ScanString(str, false, name);
377380

378381
if (res.empty()) {
379-
throw std::runtime_error(utils::va("Can't find string %s", name ? name : str));
382+
throw std::runtime_error(std::format("Can't find string {}", name ? name : str));
380383
}
381384
if (res.size() != 1) {
382-
throw std::runtime_error(utils::va("Too many finds for string %s", name ? name : str));
385+
throw std::runtime_error(std::format("Too many finds for string {}", name ? name : str));
383386
}
384387

385388
return res[0];
386389
}
387390

388391
ScanResult ScanStringAny(const char* str, const char* name = nullptr) const {
389-
auto res = ScanString(str, true);
392+
auto res = ScanString(str, true, name);
390393

391394
if (res.empty()) {
392-
throw std::runtime_error(utils::va("Can't find string %s", name ? name : str));
395+
throw std::runtime_error(std::format("Can't find string {}", name ? name : str));
393396
}
394397

395398
return res[0];

0 commit comments

Comments
 (0)