| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1save.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par Licence: |
| 9 | * Copyright (c) 2015-2024 Carsten Teibes |
| 10 | * |
| 11 | * OpenJazz is distributed under the terms of |
| 12 | * the GNU General Public License, version 2.0 |
| 13 | * |
| 14 | * @par Description: |
| 15 | * Deals with savegames. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "jj1save.h" |
| 20 | #include "util.h" |
| 21 | #include "io/log.h" |
| 22 | |
| 23 | /* This algorithm has been found out by CYBERDEViL by analysing the Assembly in |
| 24 | * DoxBox-X. For more information see: https://codeberg.org/CYBERDEV/JJSave/ |
| 25 | */ |
| 26 | |
| 27 | namespace { |
| 28 | constexpr int multiplicatorField = 256; // 0x100 |
| 29 | } |
| 30 | |
| 31 | static void keyIterate(unsigned short &key1, unsigned short &key2) { |
| 32 | // multiplicators |
| 33 | constexpr int m1 = 33797; // 0x8405 |
| 34 | constexpr int m2 = 2056; // 0x808 |
| 35 | constexpr int m3 = 5; |
| 36 | constexpr int m4 = 4; |
| 37 | |
| 38 | //LOG_TRACE("FROM key1, key2: 0x%04X, 0x%04X\n", key1, key2); |
| 39 | |
| 40 | // save value |
| 41 | unsigned short oldKey2 = key2; |
| 42 | |
| 43 | // calculate keys |
| 44 | unsigned int keyBuffer = key1 * m1; |
| 45 | key2 = keyBuffer + 1; // use lower bits |
| 46 | unsigned short part1 = keyBuffer >> 16; // use upper bits |
| 47 | unsigned short part2 = key1 * m2; |
| 48 | unsigned short part3 = oldKey2 * m3; |
| 49 | unsigned short val1 = part1 + part2 + part3; |
| 50 | part1 = val1 >> 8; // use upper bit |
| 51 | part2 = (unsigned char)oldKey2 + (oldKey2 << m3); |
| 52 | unsigned short val2 = part1 + part2 * m4; |
| 53 | key1 = val2 * multiplicatorField | (unsigned char)val1; // add lower bit |
| 54 | |
| 55 | //LOG_TRACE("TO key1, key2: 0x%04X, 0x%04X\n", key1, key2); |
| 56 | } |
| 57 | |
| 58 | static unsigned short dataOffset(unsigned short key, bool skipGarbage = false) { |
| 59 | // multiplicators |
| 60 | constexpr int mData = 2000; // 0x7D0 |
| 61 | |
| 62 | unsigned short offset; |
| 63 | |
| 64 | if(skipGarbage) { |
| 65 | // return offset to data |
| 66 | offset = (key * mData) >> 16; |
| 67 | } else { |
| 68 | // return next data field |
| 69 | offset = (key * multiplicatorField) >> 16; |
| 70 | } |
| 71 | //LOG_TRACE("SKIP offset: 0x%04X\n", offset); |
| 72 | |
| 73 | return offset; |
| 74 | } |
| 75 | |
| 76 | JJ1Save::JJ1Save (const char* fileName) : |
| 77 | valid(false) { |
| 78 | |
| 79 | FilePtr file; |
| 80 | LOG_TRACE("Save: %s", fileName); |
| 81 | |
| 82 | try { |
| 83 | file = std::make_unique<File>(fileName, PATH_TYPE_GAME); |
| 84 | } catch (int e) { |
| 85 | name = createString("empty"); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | // First load player's name |
| 90 | |
| 91 | name = file->loadString(16); |
| 92 | lowercaseString(name); |
| 93 | |
| 94 | // Use termination marker as magic |
| 95 | |
| 96 | if (file->loadChar() != 0x1A) { |
| 97 | LOG_WARN("Wrong save magic."); |
| 98 | delete[] name; |
| 99 | name = createString("invalid"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Read creation time for initial key generation |
| 104 | |
| 105 | int m = file->loadChar(); |
| 106 | int h = file->loadChar(); |
| 107 | int t = file->loadChar(); |
| 108 | int s = file->loadChar(); |
| 109 | LOG_MAX("Save creation time: %02d:%02d:%02d:%02d.", h, m, s, t); |
| 110 | |
| 111 | unsigned short key1 = m | (h << 8); |
| 112 | unsigned short key2 = t | (s << 8); |
| 113 | |
| 114 | // Calculate offset from keys |
| 115 | keyIterate(key1, key2); |
| 116 | |
| 117 | // Seek to data offset |
| 118 | file->seek(dataOffset(key1, true)); |
| 119 | |
| 120 | // Read keys for data entries |
| 121 | key1 = file->loadShort(); |
| 122 | key2 = file->loadShort(); |
| 123 | |
| 124 | // Read XOR'ed values |
| 125 | auto readValue = [&]() -> unsigned short { |
| 126 | keyIterate(key1, key2); |
| 127 | unsigned short bit = dataOffset(key1); |
| 128 | unsigned short val = file->loadShort(); |
| 129 | |
| 130 | // Switch keys |
| 131 | auto tmp = key2; |
| 132 | key2 = key1; |
| 133 | key1 = tmp; |
| 134 | |
| 135 | return val ^ bit; |
| 136 | }; |
| 137 | |
| 138 | level = readValue(); |
| 139 | planet = readValue(); |
| 140 | difficulty = static_cast<difficultyType>(readValue()); |
| 141 | |
| 142 | LOG_TRACE("Saved planet = %d, level = %d, difficulty = %d", planet, level, +difficulty); |
| 143 | |
| 144 | // TODO: Find out about the next 6 unknown fields |
| 145 | for(int i = 0; i < 6; i++) |
| 146 | unknown[i] = readValue(); |
| 147 | |
| 148 | LOG_MAX("Unknown save fields: %d %d %d %d %d %d", unknown[0], unknown[1], unknown[2], unknown[3], unknown[4], unknown[5]); |
| 149 | |
| 150 | valid = true; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | JJ1Save::~JJ1Save () { |
| 155 | delete[] name; |
| 156 | } |