| 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 |
#include "autosave.h" |
| 30 |
|
| 31 |
#include <errno.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
#include <json/json.h> |
| 36 |
|
| 37 |
#include <cdogs/campaign_entry.h> |
| 38 |
#include <cdogs/json_utils.h> |
| 39 |
#include <cdogs/log.h> |
| 40 |
#include <cdogs/sys_specifics.h> |
| 41 |
#include <cdogs/utils.h> |
| 42 |
|
| 43 |
#ifdef __EMSCRIPTEN__ |
| 44 |
#include <emscripten.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#define VERSION 3 |
| 48 |
|
| 49 |
Autosave gAutosave; |
| 50 |
|
| 51 |
static void PlayerSaveInit(PlayerSave *ps) |
| 52 |
{ |
| 53 |
memset(ps, 0, sizeof *ps); |
| 54 |
CArrayInit(&ps->ammo, sizeof(int)); |
| 55 |
} |
| 56 |
static void PlayerSaveTerminate(PlayerSave *ps) |
| 57 |
{ |
| 58 |
for (int i = 0; i < MAX_WEAPONS; i++) |
| 59 |
{ |
| 60 |
CFREE(ps->Guns[i]); |
| 61 |
} |
| 62 |
CArrayTerminate(&ps->ammo); |
| 63 |
} |
| 64 |
static void PlayerSavesTerminate(CArray *a) |
| 65 |
{ |
| 66 |
CA_FOREACH(PlayerSave, ps, *a) |
| 67 |
PlayerSaveTerminate(ps); |
| 68 |
CA_FOREACH_END() |
| 69 |
CArrayTerminate(a); |
| 70 |
} |
| 71 |
|
| 72 |
void CampaignSaveInit(CampaignSave *ms) |
| 73 |
{ |
| 74 |
memset(ms, 0, sizeof *ms); |
| 75 |
ms->IsValid = true; |
| 76 |
CArrayInit(&ms->MissionsCompleted, sizeof(int)); |
| 77 |
CArrayInit(&ms->Players, sizeof(PlayerSave)); |
| 78 |
} |
| 79 |
|
| 80 |
bool CampaignSaveIsValid(const CampaignSave *cs) |
| 81 |
{ |
| 82 |
return cs != NULL && cs->IsValid && strlen(cs->Campaign.Path) > 0; |
| 83 |
} |
| 84 |
|
| 85 |
void AutosaveInit(Autosave *autosave) |
| 86 |
{ |
| 87 |
memset(autosave, 0, sizeof *autosave); |
| 88 |
autosave->LastCampaignIndex = -1; |
| 89 |
CArrayInit(&autosave->Campaigns, sizeof(CampaignSave)); |
| 90 |
} |
| 91 |
void AutosaveTerminate(Autosave *autosave) |
| 92 |
{ |
| 93 |
CA_FOREACH(CampaignSave, m, autosave->Campaigns) |
| 94 |
CampaignEntryTerminate(&m->Campaign); |
| 95 |
CArrayTerminate(&m->MissionsCompleted); |
| 96 |
PlayerSavesTerminate(&m->Players); |
| 97 |
CA_FOREACH_END() |
| 98 |
CArrayTerminate(&autosave->Campaigns); |
| 99 |
} |
| 100 |
|
| 101 |
static void LoadCampaignNode(CampaignEntry *c, json_t *node) |
| 102 |
{ |
| 103 |
const char *path = json_find_first_label(node, "Path")->child->text; |
| 104 |
if (path != NULL) |
| 105 |
{ |
| 106 |
c->Path = json_unescape(path); |
| 107 |
} |
| 108 |
c->Mode = GAME_MODE_NORMAL; |
| 109 |
} |
| 110 |
static void AddCampaignNode(CampaignEntry *c, json_t *root) |
| 111 |
{ |
| 112 |
json_t *subConfig = json_new_object(); |
| 113 |
|
| 114 |
char path[CDOGS_PATH_MAX] = ""; |
| 115 |
RelPathFromCWD(path, c->Path); |
| 116 |
json_insert_pair_into_object( |
| 117 |
subConfig, "Path", json_new_string(json_escape(path))); |
| 118 |
json_insert_pair_into_object(root, "Campaign", subConfig); |
| 119 |
} |
| 120 |
|
| 121 |
static void LoadPlayersNode(CArray *players, json_t *node) |
| 122 |
{ |
| 123 |
json_t *playersNode = json_find_first_label(node, "Players"); |
| 124 |
if (playersNode == NULL) |
| 125 |
{ |
| 126 |
return; |
| 127 |
} |
| 128 |
for (json_t *child = playersNode->child->child; child; child = child->next) |
| 129 |
{ |
| 130 |
PlayerSave ps; |
| 131 |
PlayerSaveInit(&ps); |
| 132 |
|
| 133 |
json_t *gunsNode = json_find_first_label(child, "Guns")->child; |
| 134 |
int i = 0; |
| 135 |
for (json_t *gunNode = gunsNode->child; |
| 136 |
gunNode != NULL && i < MAX_WEAPONS; gunNode = gunNode->next, i++) |
| 137 |
{ |
| 138 |
char *gun = json_unescape(gunNode->text); |
| 139 |
if (strlen(gun) > 0) |
| 140 |
{ |
| 141 |
ps.Guns[i] = gun; |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
CFREE(gun); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
LoadIntArray(&ps.ammo, child, "Ammo"); |
| 150 |
|
| 151 |
LoadInt(&ps.Lives, child, "Lives"); |
| 152 |
LoadInt(&ps.HP, child, "HP"); |
| 153 |
LoadInt(&ps.Score, child, "Score"); |
| 154 |
|
| 155 |
CArrayPushBack(players, &ps); |
| 156 |
} |
| 157 |
} |
| 158 |
static void AddPlayersNode(CArray *players, json_t *root) |
| 159 |
{ |
| 160 |
json_t *playersNode = json_new_array(); |
| 161 |
|
| 162 |
CA_FOREACH(const PlayerSave, ps, *players) |
| 163 |
json_t *playerNode = json_new_object(); |
| 164 |
|
| 165 |
json_t *gunsNode = json_new_array(); |
| 166 |
for (int i = 0; i < MAX_WEAPONS; i++) |
| 167 |
{ |
| 168 |
json_insert_child( |
| 169 |
gunsNode, json_new_string(ps->Guns[i] != NULL ? ps->Guns[i] : "")); |
| 170 |
} |
| 171 |
json_insert_pair_into_object(playerNode, "Guns", gunsNode); |
| 172 |
AddIntArray(playerNode, "Ammo", &ps->ammo); |
| 173 |
AddIntPair(playerNode, "Lives", ps->Lives); |
| 174 |
AddIntPair(playerNode, "HP", ps->HP); |
| 175 |
AddIntPair(playerNode, "Score", ps->Score); |
| 176 |
|
| 177 |
json_insert_child(playersNode, playerNode); |
| 178 |
CA_FOREACH_END() |
| 179 |
|
| 180 |
json_insert_pair_into_object(root, "Players", playersNode); |
| 181 |
} |
| 182 |
|
| 183 |
static void LoadMissionNode(CampaignSave *m, json_t *node, const int version) |
| 184 |
{ |
| 185 |
CampaignSaveInit(m); |
| 186 |
LoadCampaignNode( |
| 187 |
&m->Campaign, json_find_first_label(node, "Campaign")->child); |
| 188 |
LoadInt(&m->NextMission, node, "NextMission"); |
| 189 |
if (version < 3) |
| 190 |
{ |
| 191 |
int missionsCompleted = 0; |
| 192 |
LoadInt(&missionsCompleted, node, "MissionsCompleted"); |
| 193 |
for (int i = 0; i < missionsCompleted; i++) |
| 194 |
{ |
| 195 |
CArrayPushBack(&m->MissionsCompleted, &i); |
| 196 |
} |
| 197 |
m->NextMission = missionsCompleted; |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
LoadIntArray(&m->MissionsCompleted, node, "MissionsCompleted"); |
| 202 |
} |
| 203 |
|
| 204 |
char buf[CDOGS_PATH_MAX]; |
| 205 |
GetDataFilePath(buf, m->Campaign.Path); |
| 206 |
m->IsValid = access(buf, F_OK | R_OK) != -1; |
| 207 |
LoadPlayersNode(&m->Players, node); |
| 208 |
} |
| 209 |
static json_t *CreateMissionNode(CampaignSave *m) |
| 210 |
{ |
| 211 |
json_t *subConfig = json_new_object(); |
| 212 |
AddCampaignNode(&m->Campaign, subConfig); |
| 213 |
AddIntPair(subConfig, "NextMission", m->NextMission); |
| 214 |
AddIntArray(subConfig, "MissionsCompleted", &m->MissionsCompleted); |
| 215 |
AddPlayersNode(&m->Players, subConfig); |
| 216 |
return subConfig; |
| 217 |
} |
| 218 |
|
| 219 |
static void LoadMissionNodes( |
| 220 |
Autosave *a, json_t *root, const char *nodeName, const int version) |
| 221 |
{ |
| 222 |
json_t *child; |
| 223 |
if (json_find_first_label(root, nodeName) == NULL) |
| 224 |
{ |
| 225 |
return; |
| 226 |
} |
| 227 |
child = json_find_first_label(root, nodeName)->child->child; |
| 228 |
while (child != NULL) |
| 229 |
{ |
| 230 |
CampaignSave m; |
| 231 |
LoadMissionNode(&m, child, version); |
| 232 |
AutosaveAddCampaign(a, &m); |
| 233 |
child = child->next; |
| 234 |
} |
| 235 |
} |
| 236 |
static void AddMissionNodes(Autosave *a, json_t *root, const char *nodeName) |
| 237 |
{ |
| 238 |
json_t *missions = json_new_array(); |
| 239 |
CA_FOREACH(CampaignSave, m, a->Campaigns) |
| 240 |
json_insert_child(missions, CreateMissionNode(m)); |
| 241 |
CA_FOREACH_END() |
| 242 |
json_insert_pair_into_object(root, nodeName, missions); |
| 243 |
} |
| 244 |
|
| 245 |
static CampaignSave *FindCampaign( |
| 246 |
Autosave *autosave, const char *path, int *missionIndex); |
| 247 |
|
| 248 |
void AutosaveLoad(Autosave *autosave, const char *filename) |
| 249 |
{ |
| 250 |
FILE *f = fopen(filename, "r"); |
| 251 |
json_t *root = NULL; |
| 252 |
|
| 253 |
if (f == NULL) |
| 254 |
{ |
| 255 |
LOG(LM_MAIN, LL_WARN, "Error loading autosave '%s'", filename); |
| 256 |
goto bail; |
| 257 |
} |
| 258 |
|
| 259 |
if (json_stream_parse(f, &root) != JSON_OK) |
| 260 |
{ |
| 261 |
LOG(LM_MAIN, LL_ERROR, "Error parsing autosave '%s'", filename); |
| 262 |
goto bail; |
| 263 |
} |
| 264 |
int version = 2; |
| 265 |
LoadInt(&version, root, "Version"); |
| 266 |
LoadMissionNodes(autosave, root, "Missions", version); |
| 267 |
if (version < 3) |
| 268 |
{ |
| 269 |
if (json_find_first_label(root, "LastMission")) |
| 270 |
{ |
| 271 |
json_t *lastMission = |
| 272 |
json_find_first_label(root, "LastMission")->child; |
| 273 |
json_t *campaign = |
| 274 |
json_find_first_label(lastMission, "Campaign")->child; |
| 275 |
char *path = NULL; |
| 276 |
LoadStr(&path, campaign, "Path"); |
| 277 |
if (path != NULL) |
| 278 |
{ |
| 279 |
FindCampaign(autosave, path, &autosave->LastCampaignIndex); |
| 280 |
CFREE(path); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
LoadInt(&autosave->LastCampaignIndex, root, "LastCampaignIndex"); |
| 287 |
} |
| 288 |
|
| 289 |
bail: |
| 290 |
json_free_value(&root); |
| 291 |
if (f != NULL) |
| 292 |
{ |
| 293 |
fclose(f); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
void AutosaveSave(Autosave *autosave, const char *filename) |
| 298 |
{ |
| 299 |
char *text = NULL; |
| 300 |
json_t *root; |
| 301 |
|
| 302 |
root = json_new_object(); |
| 303 |
AddIntPair(root, "Version", VERSION); |
| 304 |
AddIntPair(root, "LastCampaignIndex", autosave->LastCampaignIndex); |
| 305 |
AddMissionNodes(autosave, root, "Missions"); |
| 306 |
|
| 307 |
json_tree_to_string(root, &text); |
| 308 |
char *formatText = json_format_string(text); |
| 309 |
|
| 310 |
FILE *f = fopen(filename, "w"); |
| 311 |
if (f == NULL) |
| 312 |
{ |
| 313 |
printf("Error saving autosave '%s'\n", filename); |
| 314 |
return; |
| 315 |
} |
| 316 |
fputs(formatText, f); |
| 317 |
fclose(f); |
| 318 |
|
| 319 |
|
| 320 |
CFREE(formatText); |
| 321 |
CFREE(text); |
| 322 |
json_free_value(&root); |
| 323 |
|
| 324 |
#ifdef __EMSCRIPTEN__ |
| 325 |
EM_ASM( |
| 326 |
|
| 327 |
FS.syncfs( |
| 328 |
false, function(err) { assert(!err); });); |
| 329 |
#endif |
| 330 |
} |
| 331 |
|
| 332 |
static CampaignSave *FindCampaign( |
| 333 |
Autosave *autosave, const char *path, int *missionIndex) |
| 334 |
{ |
| 335 |
if (path == NULL || strlen(path) == 0) |
| 336 |
{ |
| 337 |
return NULL; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
char relPath[CDOGS_PATH_MAX] = ""; |
| 343 |
RelPathFromCWD(relPath, path); |
| 344 |
CA_FOREACH(CampaignSave, m, autosave->Campaigns) |
| 345 |
const char *campaignPath = m->Campaign.Path; |
| 346 |
if (campaignPath != NULL && strcmp(campaignPath, relPath) == 0) |
| 347 |
{ |
| 348 |
if (missionIndex != NULL) |
| 349 |
{ |
| 350 |
*missionIndex = _ca_index; |
| 351 |
} |
| 352 |
return m; |
| 353 |
} |
| 354 |
CA_FOREACH_END() |
| 355 |
return NULL; |
| 356 |
} |
| 357 |
|
| 358 |
void AutosaveAdd( |
| 359 |
Autosave *a, const CampaignEntry *ce, const int missionIndex, |
| 360 |
const int nextMission, const CArray *playerDatas) |
| 361 |
{ |
| 362 |
CampaignSave ms; |
| 363 |
CampaignSaveInit(&ms); |
| 364 |
CampaignEntryCopy(&ms.Campaign, ce); |
| 365 |
CArrayPushBack(&ms.MissionsCompleted, &missionIndex); |
| 366 |
ms.NextMission = nextMission; |
| 367 |
CA_FOREACH(const PlayerData, pd, *playerDatas) |
| 368 |
PlayerSave ps; |
| 369 |
PlayerSaveInit(&ps); |
| 370 |
for (int i = 0; i < MAX_WEAPONS; i++) |
| 371 |
{ |
| 372 |
if (pd->guns[i]) |
| 373 |
{ |
| 374 |
CSTRDUP(ps.Guns[i], pd->guns[i]->name); |
| 375 |
} |
| 376 |
} |
| 377 |
CArrayCopy(&ps.ammo, &pd->ammo); |
| 378 |
ps.Lives = pd->Lives; |
| 379 |
ps.HP = pd->HP; |
| 380 |
ps.Score = pd->Totals.Score; |
| 381 |
CArrayPushBack(&ms.Players, &ps); |
| 382 |
CA_FOREACH_END() |
| 383 |
AutosaveAddCampaign(a, &ms); |
| 384 |
} |
| 385 |
|
| 386 |
void AutosaveAddCampaign(Autosave *autosave, CampaignSave *cs) |
| 387 |
{ |
| 388 |
CampaignSave *existing = FindCampaign( |
| 389 |
autosave, cs->Campaign.Path, &autosave->LastCampaignIndex); |
| 390 |
if (existing != NULL) |
| 391 |
{ |
| 392 |
CampaignEntryTerminate(&existing->Campaign); |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
CArrayPushBack(&autosave->Campaigns, cs); |
| 397 |
autosave->LastCampaignIndex = (int)autosave->Campaigns.size - 1; |
| 398 |
existing = |
| 399 |
CArrayGet(&autosave->Campaigns, autosave->LastCampaignIndex); |
| 400 |
CampaignSaveInit(existing); |
| 401 |
} |
| 402 |
|
| 403 |
existing->NextMission = cs->NextMission; |
| 404 |
|
| 405 |
CA_FOREACH(const int, missionIndex, cs->MissionsCompleted) |
| 406 |
bool found = false; |
| 407 |
for (int i = 0; i < (int)existing->MissionsCompleted.size; i++) |
| 408 |
{ |
| 409 |
if (*(int *)CArrayGet(&existing->MissionsCompleted, i) == |
| 410 |
*missionIndex) |
| 411 |
{ |
| 412 |
found = true; |
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
if (!found) |
| 417 |
{ |
| 418 |
CArrayPushBack(&existing->MissionsCompleted, missionIndex); |
| 419 |
} |
| 420 |
CA_FOREACH_END() |
| 421 |
|
| 422 |
qsort( |
| 423 |
existing->MissionsCompleted.data, existing->MissionsCompleted.size, |
| 424 |
existing->MissionsCompleted.elemSize, CompareIntsAsc); |
| 425 |
CArrayUnique(&existing->MissionsCompleted, IntsEqual); |
| 426 |
CArrayTerminate(&cs->MissionsCompleted); |
| 427 |
|
| 428 |
PlayerSavesTerminate(&existing->Players); |
| 429 |
memcpy(&existing->Players, &cs->Players, sizeof cs->Players); |
| 430 |
|
| 431 |
memcpy(&existing->Campaign, &cs->Campaign, sizeof existing->Campaign); |
| 432 |
} |
| 433 |
|
| 434 |
const CampaignSave *AutosaveGetCampaign(Autosave *autosave, const char *path) |
| 435 |
{ |
| 436 |
return FindCampaign(autosave, path, NULL); |
| 437 |
} |
| 438 |
|
| 439 |
const CampaignSave *AutosaveGetLastCampaign(const Autosave *a) |
| 440 |
{ |
| 441 |
if (a->LastCampaignIndex < 0 || a->LastCampaignIndex >= (int)a->Campaigns.size) |
| 442 |
{ |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
return CArrayGet(&a->Campaigns, a->LastCampaignIndex); |
| 446 |
} |
| 447 |
|
| 448 |
void PlayerSavesApply(const CArray *playerSaves, const bool weaponPersist) |
| 449 |
{ |
| 450 |
for (int i = 0, idx = 0; |
| 451 |
i < (int)gPlayerDatas.size && idx < (int)playerSaves->size; |
| 452 |
i++, idx++) |
| 453 |
{ |
| 454 |
PlayerData *p = CArrayGet(&gPlayerDatas, i); |
| 455 |
if (!p->IsLocal) |
| 456 |
{ |
| 457 |
idx--; |
| 458 |
continue; |
| 459 |
} |
| 460 |
const PlayerSave *ps = CArrayGet(playerSaves, idx); |
| 461 |
if (weaponPersist) |
| 462 |
{ |
| 463 |
for (int j = 0; j < MAX_WEAPONS; j++) |
| 464 |
{ |
| 465 |
if (ps->Guns[j] != NULL) |
| 466 |
{ |
| 467 |
PlayerAddWeapon(p, StrWeaponClass(ps->Guns[j])); |
| 468 |
} |
| 469 |
} |
| 470 |
PlayerAddMinimalWeapons(p); |
| 471 |
CArrayCopy(&p->ammo, &ps->ammo); |
| 472 |
} |
| 473 |
if (ps->Lives > 0) |
| 474 |
{ |
| 475 |
PlayerSetLives(p, ps->Lives); |
| 476 |
} |
| 477 |
if (ps->HP > 0) |
| 478 |
{ |
| 479 |
p->HP = ps->HP; |
| 480 |
} |
| 481 |
if (ps->Score > 0) |
| 482 |
{ |
| 483 |
p->Totals.Score = ps->Score; |
| 484 |
} |
| 485 |
} |
| 486 |
} |