-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMSF_Serialization.cpp
More file actions
310 lines (293 loc) · 11.1 KB
/
MSF_Serialization.cpp
File metadata and controls
310 lines (293 loc) · 11.1 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
#include "MSF_Serialization.h"
#include "f4se/PluginAPI.h"
namespace MSF_Serialization
{
void RevertCallback(const F4SESerializationInterface * intfc)
{
_MESSAGE("Clearing MSF serialization data.");
MSF_MainData::weaponStateStore.Free();
}
void LoadCallback(const F4SESerializationInterface * intfc)
{
if (!(MSF_MainData::MCMSettingFlags & MSF_MainData::bEnableMetadataSaving))
{
MSF_MainData::weaponStateStore.InvalidateAllIDs();
MSF_MainData::weaponStateStore.ClearInvalidWeaponStates();
_MESSAGE("MSF serialization data loading disabled.");
return;
}
_MESSAGE("Loading MSF serialization data.");
UInt32 type, version, length;
while (intfc->GetNextRecordInfo(&type, &version, &length))
{
switch (type)
{
case 'EXWS':
{
_DEBUG("Load info: %08X %08X %08X", type, version, length);
MSF_Serialization::Load(intfc, version);
}
break;
}
}
MSF_MainData::weaponStateStore.ClearInvalidWeaponStates();
}
bool Load(const F4SESerializationInterface * intfc, UInt32 version)
{
//UInt32 dataCount = 0;
//Serialization::ReadData(intfc, &dataCount);
//for (UInt32 idx = 0; idx < dataCount; idx++)
if (version >= MIN_SUPPORTED_SERIALIZATION_VERSION)
{
StoredExtraWeaponState loadedExtraState(intfc, version);
loadedExtraState.Recover(intfc, version);
}
else
{
WeaponStateID id = 0;
Serialization::ReadData(intfc, &id);
MSF_MainData::weaponStateStore.InvalidateID(id);
_MESSAGE("WARNING: deserialization failure at extra weapon state with ID %08X: unsupported save file version", id);
return false;
}
return true;
}
void SaveCallback(const F4SESerializationInterface * intfc)
{
if (!(MSF_MainData::MCMSettingFlags & MSF_MainData::bEnableMetadataSaving))
{
_MESSAGE("MSF serialization data saving disabled.");
return;
}
_MESSAGE("Saving MSF serialization data.");
//intfc->OpenRecord(StoredExtraWeaponState::dataType, SERIALIZATION_VERSION);
//UInt32 dataCount = MSF_MainData::weaponStateStore.GetCount();
//intfc->WriteRecordData(&dataCount, sizeof(dataCount));
MSF_MainData::weaponStateStore.SaveWeaponStates(MSF_Serialization::Save, intfc, MSF_VERSION);
}
bool Save(const F4SESerializationInterface * intfc, UInt32 version, ExtraWeaponState* extraState)
{
StoredExtraWeaponState storedExtraState(extraState);
storedExtraState.SaveExtra(intfc, version);
return true;
}
}
StoredExtraWeaponState::StoredWeaponState::StoredWeaponState(ExtraWeaponState::WeaponState* weaponState)
{
this->flags = weaponState->flags;
this->ammoCapacity = weaponState->ammoCapacity;
this->chamberSize = weaponState->chamberSize;
this->chamberedCount = weaponState->chamberedCount;
this->shotCount = weaponState->shotCount;
this->loadedAmmo = weaponState->loadedAmmo;
if (weaponState->chamberedAmmo)
this->chamberedAmmo = weaponState->chamberedAmmo->formID;
else
this->chamberedAmmo = 0;
if (weaponState->equippedAmmo)
this->equippedAmmo = weaponState->equippedAmmo->formID;
else
this->equippedAmmo = 0;
for (auto itAmmo = weaponState->BCRammo.begin(); itAmmo != weaponState->BCRammo.end(); itAmmo++)
{
if (*itAmmo)
this->BCRammo.push_back((*itAmmo)->formID);
}
for (auto itMod = weaponState->stateMods.begin(); itMod != weaponState->stateMods.end(); itMod++)
{
if (*itMod)
this->stateMods.push_back((*itMod)->formID);
}
}
StoredExtraWeaponState::StoredExtraWeaponState(ExtraWeaponState* extraWeaponState)
{
_DEBUG("Saving WeaponState %08X", extraWeaponState->ID);
this->ID = extraWeaponState->ID;
this->currentState = 0;
UInt32 idx = 1;
for (auto itState = extraWeaponState->weaponStates.begin(); itState != extraWeaponState->weaponStates.end(); itState++)
{
ExtraWeaponState::WeaponState* weaponState = itState->second;
if (weaponState == extraWeaponState->currentState)
this->currentState = idx;
if (itState->first)
{
this->weaponStates.insert({ itState->first->formID, StoredWeaponState(weaponState) });
}
else
{
this->weaponStates.insert({ 0, StoredWeaponState(weaponState) });
}
idx++;
}
}
StoredExtraWeaponState::StoredWeaponState::StoredWeaponState(const F4SESerializationInterface* intfc, UInt32 version)
{
Serialization::ReadData(intfc, &this->flags);
Serialization::ReadData(intfc, &this->ammoCapacity);
Serialization::ReadData(intfc, &this->chamberSize);
Serialization::ReadData(intfc, &this->shotCount);
Serialization::ReadData(intfc, &this->loadedAmmo);
Serialization::ReadData(intfc, &this->chamberedAmmo);
if (MAKE_EXE_VERSION_EX(1, 0, 0, 0) <= version)
{
Serialization::ReadData(intfc, &this->equippedAmmo);
Serialization::ReadData(intfc, &this->chamberedCount);
}
else
{
this->equippedAmmo = this->chamberedAmmo;
this->chamberedCount = this->chamberSize;
}
UInt32 size = 0;
Serialization::ReadData(intfc, &size);
_DEBUG("Loaded data: v%08X %04X %04X %04X %04X %016X %08X %08X %08X %08X", version, this->flags, this->ammoCapacity, this->chamberSize, this->shotCount, this->loadedAmmo, this->chamberedAmmo, this->equippedAmmo, this->chamberedCount, size);
for (UInt32 dataidx = 0; dataidx < size; dataidx++)
{
UInt32 ammoFormID = 0;
Serialization::ReadData(intfc, &ammoFormID);
this->BCRammo.push_back(ammoFormID);
}
UInt32 modsize = 0;
Serialization::ReadData(intfc, &modsize);
for (UInt32 dataidx = 0; dataidx < modsize; dataidx++)
{
//UInt32 formIdKW = 0;
UInt32 formIdMod = 0;
//Serialization::ReadData(intfc, &formIdKW);
Serialization::ReadData(intfc, &formIdMod);
//this->stateMods[formIdKW] = formIdMod;
this->stateMods.push_back(formIdMod);
}
}
bool StoredExtraWeaponState::StoredWeaponState::SaveState(const F4SESerializationInterface* intfc, UInt32 version)
{
intfc->WriteRecordData(&this->flags, sizeof(this->flags));
intfc->WriteRecordData(&this->ammoCapacity, sizeof(this->ammoCapacity));
intfc->WriteRecordData(&this->chamberSize, sizeof(this->chamberSize));
intfc->WriteRecordData(&this->shotCount, sizeof(this->shotCount));
intfc->WriteRecordData(&this->loadedAmmo, sizeof(this->loadedAmmo));
intfc->WriteRecordData(&this->chamberedAmmo, sizeof(this->chamberedAmmo));
intfc->WriteRecordData(&this->equippedAmmo, sizeof(this->equippedAmmo));
intfc->WriteRecordData(&this->chamberedCount, sizeof(this->chamberedCount));
UInt32 size = this->BCRammo.size();
intfc->WriteRecordData(&size, sizeof(size));
_DEBUG("Saved data: v%08X %04X %04X %04X %04X %016X %08X %08X %08X %08X", version, this->flags, this->ammoCapacity, this->chamberSize, this->shotCount, this->loadedAmmo, this->chamberedAmmo, this->equippedAmmo, this->chamberedCount, size);
for (auto itAmmo = this->BCRammo.begin(); itAmmo != this->BCRammo.end(); itAmmo++)
{
UInt32 formId = *itAmmo;
intfc->WriteRecordData(&formId, sizeof(formId));
}
UInt32 modsize = this->stateMods.size();
intfc->WriteRecordData(&modsize, sizeof(modsize));
for (auto itMod = this->stateMods.begin(); itMod != this->stateMods.end(); itMod++)
{
UInt32 formIdMod = *itMod;
intfc->WriteRecordData(&formIdMod, sizeof(formIdMod));
}
return true;
}
ExtraWeaponState::WeaponState* StoredExtraWeaponState::StoredWeaponState::Recover(const F4SESerializationInterface* intfc, UInt32 version)
{
UInt32 newFormID = 0;
TESAmmo* recoveredChamberedAmmo = nullptr;
if (intfc->ResolveFormId(this->chamberedAmmo, &newFormID))
recoveredChamberedAmmo = (TESAmmo*)LookupFormByID(newFormID);
TESAmmo* recoveredEquippedAmmo = nullptr;
if (intfc->ResolveFormId(this->equippedAmmo, &newFormID))
recoveredEquippedAmmo = (TESAmmo*)LookupFormByID(newFormID);
std::vector<TESAmmo*> recoveredBCRammoVector;
for (auto itAmmo = this->BCRammo.begin(); itAmmo != this->BCRammo.end(); itAmmo++)
{
UInt32 newBCRFormID = 0;
if (!intfc->ResolveFormId(*itAmmo, &newBCRFormID))
continue;
TESAmmo* recoveredBCRAmmo = (TESAmmo*)LookupFormByID(newBCRFormID);
if (!recoveredBCRAmmo)
continue;
recoveredBCRammoVector.push_back(recoveredBCRAmmo);
}
std::vector<BGSMod::Attachment::Mod*> recoveredStateMods;
for (auto itMod = this->stateMods.begin(); itMod != this->stateMods.end(); itMod++)
{
UInt32 newModFormID = 0;
if (!intfc->ResolveFormId(*itMod, &newModFormID))
continue;
BGSMod::Attachment::Mod* recoveredStateMod = (BGSMod::Attachment::Mod*)LookupFormByID(newModFormID);
if (!recoveredStateMod)
continue;
recoveredStateMods.push_back(recoveredStateMod);
}
return new ExtraWeaponState::WeaponState(this->flags, this->ammoCapacity, this->chamberSize, this->chamberedCount, this->shotCount, this->loadedAmmo, recoveredChamberedAmmo, recoveredEquippedAmmo, &recoveredBCRammoVector, &recoveredStateMods);
}
bool StoredExtraWeaponState::SaveExtra(const F4SESerializationInterface* intfc, UInt32 version)
{
intfc->OpenRecord(this->dataType, version);
intfc->WriteRecordData(&this->ID, sizeof(this->ID));
intfc->WriteRecordData(&this->currentState, sizeof(this->currentState));
UInt32 size = this->weaponStates.size();
intfc->WriteRecordData(&size, sizeof(size));
_DEBUG("Saved data: %08X, %08X, %08X", this->ID, this->currentState, size);
for (auto itState = this->weaponStates.begin(); itState != this->weaponStates.end(); itState++)
{
_DEBUG("Saved data: %08X", itState->first);
intfc->WriteRecordData(&itState->first, sizeof(itState->first));
itState->second.SaveState(intfc, version);
}
return true;
}
StoredExtraWeaponState::StoredExtraWeaponState(const F4SESerializationInterface* intfc, UInt32 version)
{
Serialization::ReadData(intfc, &this->ID);
Serialization::ReadData(intfc, &this->currentState);
UInt32 size = 0;
Serialization::ReadData(intfc, &size);
_DEBUG("Loaded data %08X ", this->ID, this->currentState, size);
for (UInt32 dataidx = 0; dataidx < size; dataidx++)
{
UInt32 modFormID = 0;
Serialization::ReadData(intfc, &modFormID);
_DEBUG("Loaded data: %08X", modFormID);
this->weaponStates.insert({ modFormID, StoredWeaponState(intfc, version) });
}
}
bool StoredExtraWeaponState::Recover(const F4SESerializationInterface* intfc, UInt32 version)
{
ExtraRank* holder = MSF_MainData::weaponStateStore.GetForLoad(this->ID);
if (!holder)
{
_MESSAGE("WARNING: data recovery failure at extra weapon state with ID %08X: weapon state holder not found", this->ID);
return false;
}
ExtraWeaponState* recoveredExtraWeaponState = new ExtraWeaponState(holder);
_DEBUG("Loading ExtraRank %p, %08X", holder, holder->rank);
UInt32 idx = 0;
for (auto itStates = this->weaponStates.begin(); itStates != this->weaponStates.end(); itStates++)
{
idx++;
ExtraWeaponState::WeaponState* recoveredWeaponState = nullptr;
if (itStates->first != 0)
{
UInt32 newFormID = 0;
if (!intfc->ResolveFormId(itStates->first, &newFormID))
continue;
BGSMod::Attachment::Mod* mod = (BGSMod::Attachment::Mod*)LookupFormByID(newFormID);
if (!mod)
continue;
recoveredWeaponState = itStates->second.Recover(intfc, version);
if (!recoveredWeaponState)
continue;
recoveredExtraWeaponState->weaponStates[mod] = recoveredWeaponState;
}
else
{
recoveredWeaponState = itStates->second.Recover(intfc, version);
if (!recoveredWeaponState)
continue;
recoveredExtraWeaponState->weaponStates[nullptr] = recoveredWeaponState;
}
if (this->currentState == idx)
recoveredExtraWeaponState->currentState = recoveredWeaponState;
}
return true;
}