#pragma once #include #include #include #include #include #include #include constexpr int ACHIEVEMENT_COUNT = 642; // The achievements the game checks by id. constexpr int ACHIEVEMENT_THE_WOMB = 4; constexpr int ACHIEVEMENT_IT_LIVES = 34; constexpr int ACHIEVEMENT_THE_POLAROID = 57; constexpr int ACHIEVEMENT_THE_NEGATIVE = 78; constexpr int ACHIEVEMENT_KRAMPUS = 142; constexpr int ACHIEVEMENT_THE_VOID = 320; constexpr int BOSS_COUNT = 104; constexpr int CHALLENGE_COUNT = 46; constexpr int COLLECTIBLE_SEEN_COUNT = 733; constexpr int CUTSCENE_COUNTER_COUNT = 27; constexpr int EVENT_COUNTER_COUNT = 523; constexpr int LEVEL_COUNTER_COUNT = 14; constexpr int MINIBOSS_COUNT = 7; constexpr int SPECIAL_SEED_COUNT = 80; // How a Checksum folds bytes in: 4-byte words added to the state rotated right by 1, or CRC-32. enum ChecksumMode { CHECKSUM_ROTATE_ADD, CHECKSUM_CRC32 }; // The running checksum KAGE closes its save files with. Saves use CRC-32 started from SEED. struct Checksum { static constexpr uint32_t SEED = 0xfedcba76; ChecksumMode mode = CHECKSUM_CRC32; uint32_t state = SEED; uint32_t word = 0; uint8_t wordSize = 0; void Update(std::span data); uint32_t value_get(); }; // The slots of a character's row in COMPLETION_MARKS, as REPENTOGON's CompletionType. // GREED_DONATION holds a counter only, HARD and the TAINTED ones an achievement only. enum CompletionType { COMPLETION_MOMS_HEART, COMPLETION_ISAAC, COMPLETION_SATAN, COMPLETION_BOSS_RUSH, COMPLETION_BLUE_BABY, COMPLETION_LAMB, COMPLETION_MEGA_SATAN, COMPLETION_ULTRA_GREED, COMPLETION_GREED_DONATION, COMPLETION_HUSH, COMPLETION_HARD, COMPLETION_ULTRA_GREEDIER, COMPLETION_DELIRIUM, COMPLETION_MOTHER, COMPLETION_BEAST, COMPLETION_TAINTED, COMPLETION_TAINTED_GROUP1, COMPLETION_TAINTED_GROUP2, COMPLETION_TYPE_COUNT }; // A completion mark: the event counter holding its difficulty bits and the achievement it unlocks. struct CompletionMark { int counter; int achievement; }; constexpr int COMPLETION_PLAYER_TYPE_COUNT = 41; // Every character's completion marks, by player type (0c35ed0 in the exe). inline constexpr CompletionMark COMPLETION_MARKS[COMPLETION_PLAYER_TYPE_COUNT][COMPLETION_TYPE_COUNT] = { {{27, 167}, {41, 106}, {55, 43}, {69, 70}, {83, 49}, {97, 149}, {116, 205}, {130, 192}, {159, 0}, {144, 179}, {0, 253}, {0, 296}, {173, 282}, {423, 440}, {457, 441}, {0, 474}, {0, 0}, {0, 0}}, {{28, 168}, {42, 20}, {56, 45}, {70, 109}, {84, 50}, {98, 71}, {117, 206}, {131, 193}, {160, 0}, {145, 180}, {0, 254}, {0, 297}, {174, 283}, {424, 442}, {458, 443}, {0, 475}, {0, 0}, {0, 0}}, {{29, 171}, {43, 21}, {57, 46}, {71, 110}, {85, 75}, {99, 51}, {118, 207}, {132, 194}, {161, 0}, {146, 181}, {0, 261}, {0, 298}, {175, 284}, {425, 444}, {459, 445}, {0, 476}, {0, 0}, {0, 0}}, {{30, 170}, {44, 107}, {58, 72}, {72, 108}, {86, 77}, {100, 52}, {119, 208}, {133, 195}, {162, 0}, {147, 182}, {0, 263}, {0, 299}, {176, 285}, {426, 446}, {460, 447}, {0, 477}, {0, 0}, {0, 0}}, {{31, 174}, {45, 29}, {59, 48}, {73, 114}, {87, 113}, {101, 73}, {120, 209}, {134, 196}, {163, 0}, {148, 183}, {0, 252}, {0, 300}, {177, 286}, {427, 448}, {461, 449}, {0, 478}, {0, 0}, {0, 0}}, {{32, 169}, {46, 76}, {60, 44}, {74, 112}, {88, 53}, {102, 111}, {121, 210}, {135, 197}, {164, 0}, {149, 184}, {0, 255}, {0, 302}, {178, 288}, {428, 450}, {462, 451}, {0, 479}, {0, 0}, {0, 0}}, {{33, 177}, {47, 54}, {61, 56}, {75, 115}, {89, 55}, {103, 74}, {122, 211}, {136, 198}, {165, 0}, {150, 185}, {0, 262}, {0, 301}, {179, 287}, {429, 452}, {463, 453}, {0, 480}, {0, 0}, {0, 0}}, {{34, 173}, {48, 126}, {62, 127}, {76, 9}, {90, 128}, {104, 47}, {123, 212}, {137, 199}, {166, 0}, {151, 186}, {0, 259}, {0, 304}, {180, 290}, {430, 454}, {464, 455}, {0, 481}, {0, 0}, {0, 0}}, {{35, 172}, {49, 116}, {63, 117}, {77, 105}, {91, 118}, {105, 119}, {124, 213}, {138, 200}, {167, 0}, {152, 187}, {0, 257}, {0, 305}, {181, 291}, {431, 456}, {465, 457}, {0, 482}, {0, 0}, {0, 0}}, {{36, 176}, {50, 121}, {64, 122}, {78, 125}, {92, 123}, {106, 124}, {125, 214}, {139, 201}, {168, 0}, {153, 188}, {0, 256}, {0, 303}, {182, 289}, {432, 458}, {466, 459}, {0, 483}, {0, 0}, {0, 0}}, {{37, 175}, {51, 129}, {65, 130}, {79, 133}, {93, 131}, {107, 132}, {126, 215}, {140, 202}, {169, 0}, {154, 189}, {0, 156}, {0, 307}, {183, 293}, {433, 460}, {467, 461}, {0, 484}, {0, 0}, {0, 0}}, {}, {}, {{38, 223}, {52, 218}, {66, 220}, {80, 222}, {94, 219}, {108, 221}, {127, 216}, {141, 203}, {170, 0}, {155, 190}, {0, 260}, {0, 306}, {184, 292}, {434, 462}, {468, 463}, {0, 485}, {0, 0}, {0, 0}}, {{39, 241}, {53, 236}, {67, 237}, {81, 240}, {95, 238}, {109, 239}, {128, 217}, {142, 204}, {171, 0}, {156, 191}, {0, 264}, {0, 308}, {185, 294}, {435, 464}, {469, 465}, {0, 486}, {0, 0}, {0, 0}}, {{40, 318}, {54, 310}, {68, 311}, {82, 314}, {96, 312}, {110, 313}, {129, 317}, {143, 316}, {172, 0}, {157, 315}, {0, 319}, {0, 309}, {186, 295}, {436, 466}, {470, 467}, {0, 487}, {0, 0}, {0, 0}}, {{203, 392}, {204, 393}, {205, 394}, {206, 397}, {207, 395}, {208, 396}, {209, 403}, {210, 399}, {212, 0}, {211, 398}, {0, 402}, {0, 400}, {213, 401}, {437, 468}, {471, 469}, {0, 488}, {0, 0}, {0, 0}}, {}, {{214, 416}, {233, 417}, {252, 418}, {271, 421}, {290, 419}, {309, 420}, {328, 427}, {347, 422}, {385, 0}, {366, 423}, {0, 426}, {0, 424}, {404, 425}, {438, 470}, {472, 471}, {0, 489}, {0, 0}, {0, 0}}, {{215, 428}, {234, 429}, {253, 430}, {272, 433}, {291, 431}, {310, 432}, {329, 439}, {348, 434}, {386, 0}, {367, 435}, {0, 438}, {0, 436}, {405, 437}, {439, 472}, {473, 473}, {0, 490}, {0, 0}, {0, 0}}, {}, {{216, 0}, {235, 0}, {254, 0}, {273, 0}, {292, 0}, {311, 0}, {330, 601}, {349, 0}, {387, 0}, {368, 0}, {0, 0}, {0, 541}, {406, 584}, {440, 549}, {474, 491}, {0, 0}, {0, 548}, {0, 618}}, {{217, 0}, {236, 0}, {255, 0}, {274, 0}, {293, 0}, {312, 0}, {331, 602}, {350, 0}, {388, 0}, {369, 0}, {0, 0}, {0, 530}, {407, 585}, {441, 551}, {475, 492}, {0, 0}, {0, 550}, {0, 619}}, {{218, 0}, {237, 0}, {256, 0}, {275, 0}, {294, 0}, {313, 0}, {332, 603}, {351, 0}, {389, 0}, {370, 0}, {0, 0}, {0, 534}, {408, 586}, {442, 553}, {476, 493}, {0, 0}, {0, 552}, {0, 620}}, {{219, 0}, {238, 0}, {257, 0}, {276, 0}, {295, 0}, {314, 0}, {333, 604}, {352, 0}, {390, 0}, {371, 0}, {0, 0}, {0, 525}, {409, 587}, {443, 555}, {477, 494}, {0, 0}, {0, 554}, {0, 621}}, {{220, 0}, {239, 0}, {258, 0}, {277, 0}, {296, 0}, {315, 0}, {334, 605}, {353, 0}, {391, 0}, {372, 0}, {0, 0}, {0, 528}, {410, 588}, {444, 557}, {478, 495}, {0, 0}, {0, 556}, {0, 622}}, {{221, 0}, {240, 0}, {259, 0}, {278, 0}, {297, 0}, {316, 0}, {335, 606}, {354, 0}, {392, 0}, {373, 0}, {0, 0}, {0, 527}, {411, 589}, {445, 559}, {479, 496}, {0, 0}, {0, 558}, {0, 623}}, {{222, 0}, {241, 0}, {260, 0}, {279, 0}, {298, 0}, {317, 0}, {336, 607}, {355, 0}, {393, 0}, {374, 0}, {0, 0}, {0, 535}, {412, 590}, {446, 561}, {480, 497}, {0, 0}, {0, 560}, {0, 624}}, {{223, 0}, {242, 0}, {261, 0}, {280, 0}, {299, 0}, {318, 0}, {337, 608}, {356, 0}, {394, 0}, {375, 0}, {0, 0}, {0, 539}, {413, 591}, {447, 563}, {481, 498}, {0, 0}, {0, 562}, {0, 625}}, {{224, 0}, {243, 0}, {262, 0}, {281, 0}, {300, 0}, {319, 0}, {338, 609}, {357, 0}, {395, 0}, {376, 0}, {0, 0}, {0, 543}, {414, 592}, {448, 565}, {482, 499}, {0, 0}, {0, 564}, {0, 626}}, {{225, 0}, {244, 0}, {263, 0}, {282, 0}, {301, 0}, {320, 0}, {339, 610}, {358, 0}, {396, 0}, {377, 0}, {0, 0}, {0, 544}, {415, 593}, {449, 567}, {483, 500}, {0, 0}, {0, 566}, {0, 627}}, {{226, 0}, {245, 0}, {264, 0}, {283, 0}, {302, 0}, {321, 0}, {340, 611}, {359, 0}, {397, 0}, {378, 0}, {0, 0}, {0, 524}, {416, 594}, {450, 569}, {484, 501}, {0, 0}, {0, 568}, {0, 628}}, {{227, 0}, {246, 0}, {265, 0}, {284, 0}, {303, 0}, {322, 0}, {341, 612}, {360, 0}, {398, 0}, {379, 0}, {0, 0}, {0, 526}, {417, 595}, {451, 571}, {485, 502}, {0, 0}, {0, 570}, {0, 629}}, {{228, 0}, {247, 0}, {266, 0}, {285, 0}, {304, 0}, {323, 0}, {342, 613}, {361, 0}, {399, 0}, {380, 0}, {0, 0}, {0, 536}, {418, 596}, {452, 573}, {486, 503}, {0, 0}, {0, 572}, {0, 630}}, {{229, 0}, {248, 0}, {267, 0}, {286, 0}, {305, 0}, {324, 0}, {343, 614}, {362, 0}, {400, 0}, {381, 0}, {0, 0}, {0, 540}, {419, 597}, {453, 575}, {487, 504}, {0, 0}, {0, 574}, {0, 631}}, {{230, 0}, {249, 0}, {268, 0}, {287, 0}, {306, 0}, {325, 0}, {344, 615}, {363, 0}, {401, 0}, {382, 0}, {0, 0}, {0, 537}, {420, 598}, {454, 577}, {488, 505}, {0, 0}, {0, 576}, {0, 632}}, {{231, 0}, {250, 0}, {269, 0}, {288, 0}, {307, 0}, {326, 0}, {345, 616}, {364, 0}, {402, 0}, {383, 0}, {0, 0}, {0, 529}, {421, 599}, {455, 579}, {489, 506}, {0, 0}, {0, 578}, {0, 633}}, {{232, 0}, {251, 0}, {270, 0}, {289, 0}, {308, 0}, {327, 0}, {346, 617}, {365, 0}, {403, 0}, {384, 0}, {0, 0}, {0, 542}, {422, 600}, {456, 581}, {490, 507}, {0, 0}, {0, 580}, {0, 634}}, {}, {}, {}, }; // Whether Clear also restarts the count of saves written. enum ClearMode { CLEAR_KEEP_SAVE_COUNT, CLEAR_ALL }; struct SaveImport; // The bestiary's counts per entity, keyed by type << 20 | variant << 8. // Construction and destruction (00926020, 00926120) are the defaults. struct PersistentGameData_Bestiary { std::map deaths; std::map encounters; std::map hits; std::map kills; }; // A save slot's progress (persistentgamedata1-3.dat): achievements, event counters, the collection, bosses, challenges, // cutscenes seen and the bestiary, kept in the original binary format so a Windows save loads as it is. // Unlocking re-checks the unlocks that depend on others. Read-only mode freezes it during runs that cannot unlock. // Copies, assignment and destruction (00925f80, 009261a0, 00929010) are the defaults. // Steam Cloud (00926cf0, 00928ee0, 0092bd20, 0092bf00, 0092c0e0) is dropped, since Steam is offline. class PersistentGameData { public: std::string filePath; PersistentGameData_Bestiary bestiary; std::array achievements{}; std::array bosses{}; std::array challenges{}; std::array itemsCollection{}; std::array miniBosses{}; std::array specialSeeds{}; std::array cutsceneCounters{}; std::array eventCounters{}; std::array levelCounters{}; int characterMenuShown = 0; int lastDifficulty = 0; uint32_t gameStateChecksum = 0; uint32_t saveCount = 0; bool isChangesMade = false; bool isFileLoaded = true; bool isReadOnly = false; bool isSteamAchievementsEnabled = true; static void copy_shared_progress(PersistentGameData& destination, PersistentGameData& source); void Clear(ClearMode mode); bool Load(std::string_view path); void Save(); void write_file(std::string_view path); void BackupSaveFile(); std::string get_save_file_name(); void SetReadOnly(bool isEnabled); bool TryUnlock(int achievementID); bool Unlocked(int achievementID); void UnlockSteamAchievement(int achievementID); void IncreaseEventCounter(int eventCounter, int num); void AddToCollection(int collectibleID); void AddChallenge(int challengeID); void AddBoss(int bossId); void AddMiniBoss(int miniBossId); void set_run_mask(uint64_t mask); bool AddBestiaryKill(int type, int variant); bool TryAddBestiaryEncounter(int type, int variant); int GetBestiaryDeathCount(int type, int variant); int GetBestiaryEncounterCount(int type, int variant); int GetBestiaryKillCount(int type, int variant); private: bool read(std::span file); std::vector write(); bool stream_load(std::span file); void backfill_cutscene_counters(); void check_counter_unlocks(); void check_challenge_unlocks(); void check_collection_unlocks(); void check_collection_set_unlocks(); bool TryImportLocalSave(std::string_view path, const SaveImport& import); }; // Loads a whole file, false when it cannot be opened. bool file_load(std::string_view path, std::vector& data);