Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/User/Core/UserStorage.hpp
1163 views
1
#ifndef USER_STORAGE_H
2
#define USER_STORAGE_H
3
4
namespace RSDK
5
{
6
namespace SKU
7
{
8
9
#if RETRO_REV02
10
11
#define RETRO_USERDB_MAX (8)
12
#define RETRO_USERDB_COL_MAX (8)
13
#define RETRO_USERDB_ROW_MAX (0x400)
14
15
// no clue what this is...
16
// most signatures are recognisable like "SCN" for scenes
17
// but as far as I can tell, this one is just random numbers?
18
#define RETRO_USERDB_SIGNATURE (0x80074B1E)
19
20
// This is the base struct, it serves as the base for any API-specific stats
21
// This struct should never be removed
22
struct UserStorage {
23
virtual ~UserStorage() = default;
24
25
virtual void FrameInit()
26
{
27
if (!saveStatus) {
28
if (authStatus == STATUS_ERROR || storageStatus == STATUS_ERROR)
29
saveStatus = STATUS_ERROR;
30
else if (storageStatus == STATUS_OK)
31
saveStatus = STATUS_OK;
32
}
33
}
34
virtual void StageLoad() { saveStatus = STATUS_NONE; }
35
virtual void OnUnknownEvent() {}
36
virtual int32 TryAuth() { return 0; }
37
virtual int32 TryInitStorage() { return 0; }
38
virtual bool32 GetUsername(String *userName) { return false; }
39
virtual bool32 TryLoadUserFile(const char *filename, void *buffer, uint32 size, void (*callback)(int32 status)) { return false; }
40
virtual bool32 TrySaveUserFile(const char *filename, void *buffer, uint32 size, void (*callback)(int32 status), bool32 compress) { return false; }
41
virtual bool32 TryDeleteUserFile(const char *filename, void (*callback)(int32 status)) { return false; }
42
virtual void ClearPrerollErrors() {}
43
44
int32 authStatus = STATUS_NONE;
45
int32 storageStatus = STATUS_NONE;
46
int32 saveStatus = STATUS_NONE;
47
int32 noSaveActive = false;
48
};
49
50
static char dbVarTypes[18][16] = {
51
"unknown", // unused (in Sonic Mania)
52
"bool", // unused (in Sonic Mania)
53
"uint8", // used (in Sonic Mania)
54
"uint16", // unused (in Sonic Mania)
55
"uint32", // used (in Sonic Mania)
56
"uint64", // unimplemented in RSDKv5
57
"int8", // unused (in Sonic Mania)
58
"int16", // unused (in Sonic Mania)
59
"int32", // unused (in Sonic Mania)
60
"int64", // unimplemented in RSDKv5
61
"float", // unused (in Sonic Mania)
62
"double", // unimplemented in RSDKv5
63
"Vector2", // unimplemented in RSDKv5
64
"Vector3", // unimplemented in RSDKv5
65
"Vector4", // unimplemented in RSDKv5
66
"Color", // unused (in Sonic Mania)
67
"String", // unused (in Sonic Mania)
68
"HashMD5" // unimplemented in RSDKv5
69
};
70
71
enum DBVarTypes {
72
DBVAR_UNKNOWN,
73
DBVAR_BOOL,
74
DBVAR_UINT8,
75
DBVAR_UINT16,
76
DBVAR_UINT32,
77
DBVAR_UINT64, // unimplemented in RSDKv5
78
DBVAR_INT8,
79
DBVAR_INT16,
80
DBVAR_INT32,
81
DBVAR_INT64, // unimplemented in RSDKv5
82
DBVAR_FLOAT,
83
DBVAR_DOUBLE, // unimplemented in RSDKv5
84
DBVAR_VECTOR2, // unimplemented in RSDKv5
85
DBVAR_VECTOR3, // unimplemented in RSDKv5
86
DBVAR_VECTOR4, // unimplemented in RSDKv5
87
DBVAR_COLOR,
88
DBVAR_STRING,
89
DBVAR_HASHMD5, // unimplemented in RSDKv5
90
};
91
92
extern UserStorage *userStorage;
93
94
struct UserDB;
95
struct UserDBRow;
96
97
struct UserDBValue {
98
UserDBValue() { memset(data, 0, sizeof(data)); }
99
~UserDBValue() {}
100
101
bool32 CheckMatch(int32 row, int32 column);
102
void Set(int32 type, void *data);
103
void Get(int32 type, void *data);
104
105
UserDBRow *parent = NULL;
106
uint8 size = 0;
107
uint8 data[0x10];
108
};
109
110
struct UserDBRow {
111
UserDBRow()
112
{
113
memset(&createTime, 0, sizeof(createTime));
114
memset(&changeTime, 0, sizeof(changeTime));
115
memset(values, 0, sizeof(values));
116
}
117
~UserDBRow() {}
118
119
bool32 AddValue(int32 type, char *name, void *value);
120
bool32 GetValue(int32 type, char *name, void *value);
121
122
bool32 Compare(UserDBRow *other, int32 type, char *name, bool32 sortAscending);
123
124
UserDB *parent = NULL;
125
uint32 uuid = 0;
126
tm createTime;
127
tm changeTime;
128
UserDBValue values[8];
129
};
130
131
struct UserDB {
132
UserDB()
133
{
134
MEM_ZERO(sortedRowIDs);
135
MEM_ZERO(columnTypes);
136
MEM_ZERO(columnNames);
137
MEM_ZERO(columnUUIDs);
138
MEM_ZERO(rows);
139
}
140
~UserDB() { sortedRowList.Clear(true); }
141
142
void Init(va_list list);
143
bool32 Load(uint8 *buffer);
144
void Save(int32 totalSize, uint8 *buffer);
145
void Clear();
146
147
int32 GetColumnID(const char *name);
148
149
int32 AddRow();
150
uint16 GetRowByID(uint32 uuid);
151
bool32 RemoveRow(uint32 row);
152
bool32 RemoveAllRows();
153
154
void FilterValues(UserDBValue *value, int32 column);
155
void AddSortFilter(const char *name, void *value);
156
157
void SetupRowSortIDs();
158
void RefreshSortList();
159
void SortRows(int32 type, char *name, bool32 sortAscending);
160
161
uint32 CreateRowUUID();
162
void Refresh();
163
size_t GetSize();
164
165
const char *name = "";
166
uint32 uuid = 0;
167
uint8 loaded = false;
168
uint8 active = false;
169
uint8 valid = false;
170
UserDB *parent = NULL;
171
uint8 rowsChanged = false;
172
List<int32> sortedRowList;
173
int32 sortedRowIDs[RETRO_USERDB_ROW_MAX];
174
int32 sortedRowCount = 0;
175
int32 columnCount = 0;
176
int32 columnTypes[RETRO_USERDB_COL_MAX];
177
char columnNames[RETRO_USERDB_COL_MAX][0x10];
178
uint32 columnUUIDs[RETRO_USERDB_COL_MAX];
179
uint16 rowCount = 0;
180
UserDBRow rows[RETRO_USERDB_ROW_MAX];
181
};
182
183
struct UserDBStorage {
184
UserDBStorage();
185
186
uint16 InitUserDB(const char *name, va_list list);
187
uint16 LoadUserDB(const char *filename, void (*callback)(int32 status));
188
bool32 SaveUserDB(uint16 tableID, void (*callback)(int32 status));
189
void ClearUserDB(uint16 tableID);
190
void ClearAllUserDBs();
191
192
static bool32 LoadCB(uint16 tableID, int32 status);
193
static bool32 SaveCB(uint16 tableID, int32 status);
194
195
UserDB userDB[RETRO_USERDB_MAX];
196
uint8 unknown;
197
int32 *writeBuffer[RETRO_USERDB_MAX];
198
void *readBuffer[RETRO_USERDB_MAX];
199
void (*loadCallback[RETRO_USERDB_MAX])(int32 status);
200
void (*saveCallback[RETRO_USERDB_MAX])(int32 status);
201
bool32 (*dbLoadCB[RETRO_USERDB_MAX])(uint16 table, int32 status);
202
bool32 (*dbSaveCB[RETRO_USERDB_MAX])(uint16 table, int32 status);
203
void (*callbacks[RETRO_USERDB_MAX])(int32);
204
int32 unused[RETRO_USERDB_MAX];
205
};
206
207
extern UserDBStorage *userDBStorage;
208
209
// ====================
210
// API Cores
211
// ====================
212
213
// Dummy API
214
#if RETRO_USERCORE_DUMMY
215
#include "RSDK/User/Dummy/DummyStorage.hpp"
216
#endif
217
218
// Steam API
219
#if RETRO_USERCORE_STEAM
220
#include "RSDK/User/Steam/SteamStorage.hpp"
221
#endif
222
223
// Epic Games API
224
#if RETRO_USERCORE_EOS
225
#include "RSDK/User/EOS/EOSStorage.hpp"
226
#endif
227
228
// Switch API
229
#if RETRO_USERCORE_NX
230
#include "RSDK/User/NX/NXStorage.hpp"
231
#endif
232
233
// =======================
234
// USER STORAGE
235
// =======================
236
237
inline int32 TryAuth() { return userStorage->TryAuth(); }
238
inline int32 TryInitStorage() { return userStorage->TryInitStorage(); }
239
inline bool32 GetUsername(String *userName) { return userStorage->GetUsername(userName); }
240
inline bool32 TryLoadUserFile(const char *filename, void *buffer, uint32 size, void (*callback)(int32 status))
241
{
242
return userStorage->TryLoadUserFile(filename, buffer, size, callback);
243
}
244
inline bool32 TrySaveUserFile(const char *filename, void *buffer, uint32 size, void (*callback)(int32 status), bool32 compressed)
245
{
246
return userStorage->TrySaveUserFile(filename, buffer, size, callback, compressed);
247
}
248
inline bool32 TryDeleteUserFile(const char *filename, void (*callback)(int32 status)) { return userStorage->TryDeleteUserFile(filename, callback); }
249
inline void ClearPrerollErrors() { userStorage->ClearPrerollErrors(); }
250
251
inline int32 GetUserAuthStatus() { return userStorage->authStatus; }
252
inline int32 GetUserStorageStatus()
253
{
254
if (userStorage->saveStatus == STATUS_ERROR)
255
return STATUS_ERROR;
256
else
257
return userStorage->storageStatus;
258
}
259
260
inline int32 GetSaveStatus()
261
{
262
if (userStorage->noSaveActive)
263
return STATUS_OK;
264
else
265
return userStorage->saveStatus;
266
}
267
inline void SetSaveStatusOK()
268
{
269
if (userStorage->saveStatus == STATUS_CONTINUE)
270
userStorage->saveStatus = STATUS_OK;
271
}
272
inline void SetSaveStatusForbidden()
273
{
274
if (userStorage->saveStatus == STATUS_CONTINUE)
275
userStorage->saveStatus = STATUS_FORBIDDEN;
276
}
277
inline void SetSaveStatusError()
278
{
279
if (userStorage->saveStatus == STATUS_CONTINUE)
280
userStorage->saveStatus = STATUS_ERROR;
281
}
282
inline void ClearSaveStatus() { userStorage->saveStatus = STATUS_NONE; }
283
inline void SetSaveStatusContinue() { userStorage->saveStatus = STATUS_CONTINUE; }
284
inline void SetUserStorageNoSave(bool32 noSave) { userStorage->noSaveActive = noSave; }
285
inline bool32 GetUserStorageNoSave() { return userStorage->noSaveActive; }
286
287
// =======================
288
// USER DB API
289
// =======================
290
291
// UserDB management
292
inline uint16 InitUserDB(const char *name, ...)
293
{
294
va_list list;
295
va_start(list, name);
296
uint16 id = userDBStorage->InitUserDB(name, list);
297
va_end(list);
298
return id;
299
}
300
inline uint16 LoadUserDB(const char *filename, void (*callback)(int32 status)) { return userDBStorage->LoadUserDB(filename, callback); }
301
inline bool32 SaveUserDB(uint16 tableID, void (*callback)(int32 status)) { return userDBStorage->SaveUserDB(tableID, callback); }
302
inline void ClearUserDB(uint16 tableID) { userDBStorage->ClearUserDB(tableID); }
303
inline void ClearAllUserDBs() { userDBStorage->ClearAllUserDBs(); }
304
305
// UserDB rows
306
inline int32 AddUserDBRow(uint16 tableID)
307
{
308
if (tableID == (uint16)-1)
309
return -1;
310
UserDB *userDB = &userDBStorage->userDB[tableID];
311
if (!userDB->active)
312
return -1;
313
314
return userDB->AddRow();
315
}
316
inline bool32 RemoveDBRow(uint16 tableID, uint16 rowID)
317
{
318
if (tableID == (uint16)-1 || rowID == (uint16)-1)
319
return false;
320
321
UserDB *userDB = &userDBStorage->userDB[tableID];
322
if (!userDB->active)
323
return false;
324
325
return userDB->RemoveRow(rowID);
326
}
327
inline bool32 RemoveAllDBRows(uint16 tableID)
328
{
329
if (tableID == (uint16)-1)
330
return false;
331
332
UserDB *userDB = &userDBStorage->userDB[tableID];
333
if (!userDB->active)
334
return false;
335
336
return userDB->RemoveAllRows();
337
}
338
inline uint16 GetUserDBRowByID(uint16 tableID, uint32 uuid)
339
{
340
if (tableID == (uint16)-1 || !uuid)
341
return -1;
342
343
UserDB *userDB = &userDBStorage->userDB[tableID];
344
if (!userDB->active)
345
return -1;
346
347
return userDB->GetRowByID(uuid);
348
}
349
350
inline uint32 GetDBRowUUID(uint16 tableID, uint16 rowID)
351
{
352
if (tableID == (uint16)-1 || rowID == (uint16)-1)
353
return 0;
354
355
UserDB *userDB = &userDBStorage->userDB[tableID];
356
if (!userDB->active)
357
return 0;
358
359
return userDB->rows[rowID].uuid;
360
}
361
362
// UserDB Row Sorting
363
uint16 SetupUserDBRowSorting(uint16 tableID);
364
int32 AddUserDBRowSortFilter(uint16 tableID, int32 type, const char *name, void *value);
365
int32 SortUserDBRows(uint16 tableID, int32 type, const char *name, bool32 sortAscending);
366
int32 GetSortedUserDBRowCount(uint16 tableID);
367
int32 GetSortedUserDBRowID(uint16 tableID, uint16 entryID);
368
369
// UserDB Values
370
bool32 GetUserDBValue(uint16 tableID, uint32 rowID, int32 type, char *name, void *value);
371
bool32 SetUserDBValue(uint16 tableID, uint32 rowID, int32 type, char *name, void *value);
372
373
// UserDB Misc
374
bool32 GetUserDBRowsChanged(uint16 tableID);
375
void GetUserDBRowCreationTime(uint16 tableID, uint16 rowID, char *buf, size_t size, char *format);
376
377
// =======================
378
// USER DB CALLBACKS
379
// =======================
380
381
void UserDBStorage_LoadCB1(int32 status);
382
void UserDBStorage_LoadCB2(int32 status);
383
void UserDBStorage_LoadCB3(int32 status);
384
void UserDBStorage_LoadCB4(int32 status);
385
void UserDBStorage_LoadCB5(int32 status);
386
void UserDBStorage_LoadCB6(int32 status);
387
void UserDBStorage_LoadCB7(int32 status);
388
void UserDBStorage_LoadCB8(int32 status);
389
390
void UserDBStorage_SaveCB1(int32 status);
391
void UserDBStorage_SaveCB2(int32 status);
392
void UserDBStorage_SaveCB3(int32 status);
393
void UserDBStorage_SaveCB4(int32 status);
394
void UserDBStorage_SaveCB5(int32 status);
395
void UserDBStorage_SaveCB6(int32 status);
396
void UserDBStorage_SaveCB7(int32 status);
397
void UserDBStorage_SaveCB8(int32 status);
398
#endif
399
400
extern void (*preLoadSaveFileCB)();
401
extern void (*postLoadSaveFileCB)();
402
extern char userFileDir[0x100];
403
404
inline void SetUserFileCallbacks(const char *userDir, void (*preCB)(), void (*postCB)())
405
{
406
preLoadSaveFileCB = preCB;
407
postLoadSaveFileCB = postCB;
408
strcpy(userFileDir, userDir);
409
}
410
411
bool32 LoadUserFile(const char *filename, void *buffer, uint32 bufSize);
412
bool32 SaveUserFile(const char *filename, void *buffer, uint32 bufSize);
413
bool32 DeleteUserFile(const char *filename);
414
415
#if !RETRO_REV02
416
bool32 TryLoadUserFile(const char *filename, void *buffer, uint32 size, void (*callback)(int32 status));
417
bool32 TrySaveUserFile(const char *filename, void *buffer, uint32 size, void (*callback)(int32 status));
418
#endif
419
420
void InitUserDirectory();
421
422
} // namespace SKU
423
} // namespace RSDK
424
425
#endif
426