-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSerialize.cpp
More file actions
297 lines (241 loc) · 6.84 KB
/
Serialize.cpp
File metadata and controls
297 lines (241 loc) · 6.84 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
#include "Serialize.h"
#include <sstream>
#include "skse64/GameAPI.h"
#include "skse64/PluginAPI.h"
#include "skse64/GameTypes.h"
#include "skse64/PapyrusVM.h"
#include "skse64/PapyrusArgs.h"
#include "Forms.h"
#include "Data.h"
#include "External.h"
#include "PackageData.h"
//#include <fstream>
//#include <boost/archive/text_oarchive.hpp>
//#include <boost/archive/text_iarchive.hpp>
namespace Data {
const UInt32 kSerializationDataVersion = 1;
// Flat obj=>key=>value storage
extern intv* intValues;
extern flov* floatValues;
extern strv* stringValues;
extern forv* formValues;
// Vector obj=>key=>vector[i] storage
extern intl* intLists;
extern flol* floatLists;
extern strl* stringLists;
extern forl* formLists;
// Overrides
extern forl* packageLists;
//extern aniv* animValues;
template <class T> void Load(T *Data, SKSESerializationInterface *intfc, UInt32 &version, UInt32 &length) {
if (version == kSerializationDataVersion && length > 5) {
char *buf = new char[length + 1];
intfc->ReadRecordData(buf, length);
buf[length] = 0;
std::stringstream ss(buf);
Data->LoadStream(ss);
delete[] buf;
_MESSAGE("\t- objects: %d", Data->Data.size());
}
}
template <class T> void Save(T *Data, SKSESerializationInterface* intfc, UInt32 type) {
if (!Data || Data->Data.empty())
_MESSAGE("\t- empty!");
else{
_MESSAGE("\t- objects: %d", Data->Data.size());
std::stringstream ss;
Data->SaveStream(ss);
if (intfc->OpenRecord(type, kSerializationDataVersion)) {
const std::string &str = ss.str();
const char *cstr = str.c_str();
intfc->WriteRecordData(cstr, strlen(cstr));
}
}
}
void FormDelete(UInt64 handle){
if (intValues){
_MESSAGE("Form Delete Handle: %llu", handle);
intValues->RemoveForm(handle);
floatValues->RemoveForm(handle);
stringValues->RemoveForm(handle);
formValues->RemoveForm(handle);
intLists->RemoveForm(handle);
floatLists->RemoveForm(handle);
stringLists->RemoveForm(handle);
formLists->RemoveForm(handle);
}
}
void Serialization_Load(SKSESerializationInterface *intfc) {
UInt32 type;
UInt32 version;
UInt32 length;
InitLists();
//Forms::LoadCurrentMods();
//Forms::LoadModList(intfc);
_MESSAGE("Storage Loading...");
while (intfc->GetNextRecordInfo(&type, &version, &length)) {
switch (type) {
case 'MODS':
_MESSAGE("\tMODS Load (old)");
Forms::LoadModList(intfc);
break;
case 'PLGN':
_MESSAGE("\tPLGN Load");
Forms::LoadPluginList(intfc);
break;
case 'INTV':
_MESSAGE("\tINTV Load");
Load(intValues, intfc, version, length);
break;
case 'FLOV':
_MESSAGE("\tFLOV Load");
Load(floatValues, intfc, version, length);
break;
case 'STRV':
_MESSAGE("\tSTRV Load");
Load(stringValues, intfc, version, length);
break;
case 'FORV':
_MESSAGE("\tFORV Load");
Load(formValues, intfc, version, length);
break;
case 'INTL':
_MESSAGE("\tINTL Load");
Load(intLists, intfc, version, length);
break;
case 'FLOL':
_MESSAGE("\tFLOL Load");
Load(floatLists, intfc, version, length);
break;
case 'STRL':
_MESSAGE("\tSTRL Load");
Load(stringLists, intfc, version, length);
break;
case 'FORL':
_MESSAGE("\tFORL Load");
Load(formLists, intfc, version, length);
break;
case 'PKGO':
_MESSAGE("\tPKGO Load");
Load(PackageData::GetPackages(), intfc, version, length);
break;
/*case 'PACK':
_MESSAGE("PACK Load");
Load(packageLists, intfc, version, length);
break;*/
/*case 'DATA':
if (version == kSerializationDataVersion && length > 0) {
char *buf = new char[length + 1];
intfc->ReadRecordData(buf, length);
buf[length] = 0;
std::stringstream ss(buf);
const std::string &tmp = ss.str();
_MESSAGE("DATA: %d", strlen(tmp.c_str()));
int ver;
ss >> ver;
//Forms::LoadPreviousMods(ss);
if (ver == -1) {
_MESSAGE("-- Legacy Data");
intValues->LoadStream(ss);
floatValues->LoadStream(ss);
stringValues->LoadStream(ss);
formValues->LoadStream(ss);
intLists->LoadStream(ss);
floatLists->LoadStream(ss);
stringLists->LoadStream(ss);
formLists->LoadStream(ss);
}
delete[] buf;
}
break;*/
default:
_MESSAGE("unhandled type %08X", type);
break;
}
}
//Forms::ClearPreviousMods();
_MESSAGE("Done!\n");
}
void Serialization_Save(SKSESerializationInterface *intfc) {
_MESSAGE("Storage Saving...");
//Forms::LoadCurrentMods();
//Forms::ClearPreviousMods();
// Init lists if for some weird reason unset
InitLists();
/*if (intfc->OpenRecord('DATA', kSerializationDataVersion)) {
std::stringstream ss;
ss << (int)1;
Forms::SaveCurrentMods(ss);
std::string str = ss.str();
const char *cstr = str.c_str();
intfc->WriteRecordData(cstr, strlen(cstr));
}*/
// Save load order
//Forms::SaveModList(intfc);
Forms::SavePluginsList(intfc);
_MESSAGE("\tPLGN Saved");
// Cleanup removed forms
/*int cleaned = 0;
cleaned += intValues->Cleanup();
cleaned += floatValues->Cleanup();
cleaned += stringValues->Cleanup();
cleaned += formValues->Cleanup();
cleaned += intLists->Cleanup();
cleaned += floatLists->Cleanup();
cleaned += stringLists->Cleanup();
cleaned += formLists->Cleanup();
if (cleaned > 0)
_MESSAGE("- discarded: %d", cleaned);*/
// Save value storage
Save(intValues, intfc, 'INTV');
_MESSAGE("\tINTV Saved");
Save(floatValues, intfc, 'FLOV');
_MESSAGE("\tFLOV Saved");
Save(stringValues, intfc, 'STRV');
_MESSAGE("\tSTRV Saved");
Save(formValues, intfc, 'FORV');
_MESSAGE("\tFORV Saved");
// Save list storage
Save(intLists, intfc, 'INTL');
_MESSAGE("\tINTL Saved");
Save(floatLists, intfc, 'FLOL');
_MESSAGE("\tFLOL Saved");
Save(stringLists, intfc, 'STRL');
_MESSAGE("\tSTRL Saved");
Save(formLists, intfc, 'FORL');
_MESSAGE("\tFORL Saved");
// Overrides
Save(PackageData::GetPackages(), intfc, 'PKGO');
_MESSAGE("\tPKGO Saved");
// Save external files
External::SaveFiles();
_MESSAGE("\tExternal JSON Saved");
_MESSAGE("Save Done!\n");
}
void Serialization_Revert(SKSESerializationInterface* intfc) {
_MESSAGE("Storage Reverting...");
_MESSAGE("\t - Mod List");
Forms::ClearModList();
//Forms::LoadCurrentMods();
//Forms::ClearPreviousMods();
_MESSAGE("\t - StorageUtil: Values");
intValues->Revert();
floatValues->Revert();
stringValues->Revert();
formValues->Revert();
_MESSAGE("\t - StorageUtil: Lists");
intLists->Revert();
floatLists->Revert();
stringLists->Revert();
formLists->Revert();
_MESSAGE("\t - Package Overrides");
PackageData::GetPackages()->Revert();
// Revert external files
_MESSAGE("\t - JSON Files");
External::RevertFiles();
_MESSAGE("\t - Re-Init");
InitLists();
_MESSAGE("Done!\n");
//_MESSAGE("Revert skip!\n");
}
}