-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEC.LoadSave.cpp
More file actions
339 lines (281 loc) · 8.32 KB
/
EC.LoadSave.cpp
File metadata and controls
339 lines (281 loc) · 8.32 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "EC.LoadSave.h"
#include "EC.LoadSaveImpl.h"
#include "IH.Initial.h"
#include "IH.InitialService.h"
#include "EC.Misc.h"
//------------------------------------------------------------------------------------------------------------------------------------
void CALLBACK ISF_PointerGotInvalid(AbstractClass* const pObject, bool bRemoved);
void CALLBACK ISF_LoadGame(IStream* pStream);
void CALLBACK ISF_FinalSwizzle(IStream* pStream, SIFinalSwizzleFn SIFinalSwizzle);
void CALLBACK ISF_SaveGame(IStream* pStream);
LoadSaveInterface LocalLSInterface
{
ISF_LoadGame,
ISF_FinalSwizzle,
ISF_SaveGame,
ISF_PointerGotInvalid,
};
void RegisterLoadSaveInterface()
{
InitialLoad::CreateRequestAndSubmit<InitialLoadParam_RegisterObject<LoadSaveInterface>>(
"EC::Internal::LoadSave", Internal_GetLibRegName(), LocalLSInterface);
}
//------------------------------------------------------------------------------------------------------------------------------------
bool EnableStreamLoadCheck{ true };
void StreamReaderLoadCheck(bool bCheck)
{
EnableStreamLoadCheck = bCheck;
}
bool StreamReaderLoadCheck()
{
return EnableStreamLoadCheck;
}
bool EnableStreamSaveCheck{ true };
void StreamWriterSaveCheck(bool bCheck)
{
EnableStreamSaveCheck = bCheck;
}
bool StreamWriterSaveCheck()
{
return EnableStreamSaveCheck;
}
bool ECStreamReader::ExpectEndOfBlock() const
{
if (!this->Success() || this->stream->Size() != this->stream->Offset())
{
if (EnableStreamLoadCheck) this->EmitExpectEndOfBlockWarning();
return false;
}
return true;
}
bool ECStreamReader::Read(ECStreamByte::data_t* Value, size_t Size)
{
if (!this->stream->Read(Value, Size))
{
if(EnableStreamLoadCheck) this->EmitLoadWarning(Size);
this->success = false;
return false;
}
return true;
}
bool ECStreamReader::Expect(unsigned int value)
{
unsigned int buffer = 0;
if (this->Load(buffer))
{
if (buffer == value)
{
return true;
}
if (EnableStreamLoadCheck)this->EmitExpectWarning(buffer, value);
}
return false;
}
bool ECStreamReader::RegisterChange(void* pNew)
{
static_assert(sizeof(long) == sizeof(void*), "EC : long and void* need to be of same size.");
long pOld = 0;
if (this->Load(pOld))
{
if (SUCCEEDED(SwizzleManagerClass::Instance->Here_I_Am(pOld, pNew)))
{
return true;
}
if (EnableStreamLoadCheck)this->EmitSwizzleWarning(pOld, pNew);
}
return false;
}
void ECStreamReader::EmitExpectEndOfBlockWarning() const
{
Internal_DebugLog("[EC] Read %X bytes instead of %X!\n", this->stream->Offset(), this->stream->Size());
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::EmitExpectEndOfBlockWarning);
}
void ECStreamReader::EmitLoadWarning(size_t size) const
{
Internal_DebugLog("[EC] Could not read data of length %u at %X of %X.\n", size, this->stream->Offset() - size, this->stream->Size());
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::EmitLoadWarning);
}
void ECStreamReader::EmitExpectWarning(unsigned int found, unsigned int expect) const
{
Internal_DebugLog("[EC] Found %X, expected %X\n", found, expect);
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::EmitExpectWarning);
}
void ECStreamReader::EmitSwizzleWarning(long id, void* pointer) const
{
Internal_DebugLog("[EC] Could not register change from %X to %p\n", id, pointer);
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::EmitSwizzleWarning);
}
//------------------------------------------------------------------------------------------------------------------------------------
ECStreamErrorHandler ECRS_ErrorHandler{ nullptr };
ECStreamErrorHandler ECWS_ErrorHandler{ nullptr };
void SetStreamReadErrorHandler(ECStreamErrorHandler Handler)
{
ECRS_ErrorHandler = Handler;
}
void SetStreamWriteErrorHandler(ECStreamErrorHandler Handler)
{
ECWS_ErrorHandler = Handler;
}
ECStreamErrorHandler GetStreamReadErrorHandler()
{
return ECRS_ErrorHandler;
}
ECStreamErrorHandler GetStreamWriteErrorHandler()
{
return ECWS_ErrorHandler;
}
//------------------------------------------------------------------------------------------------------------------------------------
void RaiseReadBlockError()
{
if (EnableStreamLoadCheck)
{
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::ReadBlockError);
}
}
void RaiseWriteBlockError()
{
if (EnableStreamSaveCheck)
{
auto Handler = GetStreamWriteErrorHandler();
if (Handler)Handler(ECStreamErrorType::WriteBlockError);
}
}
void RaiseExpectEndOfBlockError()
{
if (EnableStreamLoadCheck)
{
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::EmitExpectEndOfBlockWarning);
}
}
void RaiseEmitLoadWarning()
{
if (EnableStreamLoadCheck)
{
auto Handler = GetStreamReadErrorHandler();
if (Handler)Handler(ECStreamErrorType::EmitLoadWarning);
}
}
SIFinalSwizzleFn SIFinalSwizzleFunc;
bool SIFinalSwizzleImpl(void* pOld, void*& pNew)
{
return SIFinalSwizzleFunc(pOld, pNew);
}
#ifdef EC_NoObjBase
void ECGameClass_PointerGotInvalid(AbstractClass* const pObject, bool bRemoved)
{
}
void ECGameClass_FinalSwizzle()
{
}
void ECGameClass_LoadGame(IStream* pStream)
{
}
void ECGameClass_SaveGame(IStream* pStream)
{
}
#endif
//bRemoved : true=真寄了 false=临时离场
void CALLBACK ISF_PointerGotInvalid(AbstractClass* const pObject, bool bRemoved)
{
ECGameClass_PointerGotInvalid(pObject, bRemoved);
}
void CALLBACK ISF_FinalSwizzle(IStream* pStream, SIFinalSwizzleFn SIFinalSwizzle)
{
if (!SIFinalSwizzle)
{
Internal_DebugLog("[EC] SIFinalSwizzle == nullptr\n");
Internal_DebugLog("[EC] Failed to trigger FinalSwizzle!!\n");
return;
}
SIFinalSwizzleFunc = SIFinalSwizzle;
ECGameClass_FinalSwizzle();
}
void CALLBACK ISF_LoadGame(IStream* pStream)
{
ECStreamByte ReadStm(0);
//auto Version = Internal_GetLibVersion();
//Version Check
ECGameClass_LoadGame(pStream);
}
void CALLBACK ISF_SaveGame(IStream* pStream)
{
ECStreamByte WriteStm;
//auto Version = Internal_GetLibVersion();
//Version Check
ECGameClass_SaveGame(pStream);
}
#if 0
/*
ULONG out = 0;
UINT X = 0;
pStream->Read(&X, sizeof(X), &out);
sprintf_s(bb, "读出 %d, 应 %u 字节, 实 %u 字节", X, sizeof(X), out);
MessageBoxA(Game::hWnd, bb, "万物互通", MB_OK);
*/
char bb[10000];
TechnoClass* test__;
#include "EC.Listener.h"
void CALLBACK AFG()
{
if (test__)sprintf_s(bb, "读出 0x%08p(%s)", test__, UTF16ToGBK(test__->GetUIName()).c_str());
else sprintf_s(bb, "WTF");
MessageBoxA(Game::hWnd, bb, "万物互通", MB_OK);
}
void CALLBACK ISF_LoadGame(IStream* pStream)
{
ULONG out = 0;
pStream->Read(&test__, sizeof(test__), &out);
SwizzleManagerClass::Instance().Swizzle((void**)&test__);
//ECListener::Listen_AfterLoadGame(AFG);
}
void CALLBACK ISF_SaveGame(IStream* pStream)
{
AFG();
ULONG out = 0;
test__ = TechnoClass::Array()->Items[0];
pStream->Write(&test__, sizeof(test__), &out);
sprintf_s(bb, "写入 0x%08p(%s)", test__, UTF16ToGBK(test__->GetUIName()).c_str());
MessageBoxA(Game::hWnd, bb, "万物互通", MB_OK);
}
#include "EC.ObjBase.h"
ECRTTI_ExportRTTI(TestType1)
ECObjType TestType1 : EC_USERTTI, EC_USEUNIQUEID
{
ECRTTI_DefineRTTIFunction(TestType1)
};
ECRTTI_ExportRTTI(TestType2)
ECObjType TestType2 : EC_USERTTI, EC_USEUNIQUEID
{
ECRTTI_DefineRTTIFunction(TestType2)
};
ECRTTI_ExportRTTI(TestType4, TestType2)
ECObjType TestType4 : public TestType2
{
ECRTTI_DefineRTTIFunction(TestType4)
};
ECRTTI_ExportRTTI(TestType4, TestType2)
ECObjType TestType4 : public TestType2
{
ECRTTI_DefineRTTIFunction(TestType4)
};
ECRTTI_ExportRTTI(TestType3, TestType1)
ECObjType TestType3 : public TestType1
{
ECRTTI_DefineRTTIFunction(TestType3)
void Test()
{
char x[10000];
auto V = WhatAmI();
sprintf_s(x, "ID : %d\nType : { %s : 0x%08X }\nBase { %s : 0x%08X }\nthis = %p -> TestType1: %p TestType2: %p",
GetUniqueID(), V->GetName(), V, V->GetFirstBaseClassName(),
V->GetFirstBaseClassInfo(), this, DynamicCast<TestType1>(), DynamicCast<TestType2>());
MessageBoxA(Game::hWnd, x, "万物互通", MB_OK);
}
};
#endif