Skip to content

Load the original's saves that have signs or a Great Wall - #154

Open
MortenLyhr wants to merge 4 commits into
bssth:masterfrom
MortenLyhr:fix/save-compat
Open

MortenLyhr wants to merge 4 commits into
bssth:masterfrom
MortenLyhr:fix/save-compat

Conversation

@MortenLyhr

@MortenLyhr MortenLyhr commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

The original engine keeps its own data after a save's pickled Python data: the map's signs and the lines it laid
(the Great Wall), then the MD5 string. SaveFile::read skipped a fixed 33 bytes there (the size of the section when it
is empty), so any original save with a sign or a wall failed to load with "save file truncated". One example is a
scenario with landmarks.

The reader now takes the whole section, up to the MD5 string, and keeps it in SaveFile::engineData. The section's size
isn't stored, so the MD5 string (int 32, then 32 hex digits) is found by its form: the first one after the Python data.
The section before it holds only ints, floats and UTF-16 text. GameSession keeps a loaded save's section, and
saveGame writes it back unchanged, so re-saving an original save keeps its signs and wall. New games and scenarios
write the empty section, as before. OpenCiv4 doesn't read the section's contents: signs placed in OpenCiv4 aren't saved
yet, and a re-saved wall is the one from the turn the save was made. The layout was found by comparing the original's
saves (a 140-save Wine game and the saves in testgames) with OpenCiv4's.

Not in this PR: the original still rejects OpenCiv4's saves. That comes from the salted MD5 at the end, not from this
section, and its algorithm is known only from the exe, so the clean-room rule rules it out.

New ctest save_file_test: round-trips an empty engine section and one laid out like the original's (a sign, a wall
line, the tail that holds a 32), and checks that a file cut short anywhere from inside the engine section to the last
byte of the MD5 throws.

Tested:

  • Nine original saves (unmodded and BUG Mod: the TestMap QuickSave with 439 signs and a Great Wall, W-antium,
    AutoSave_Initial, and empty-section ones) load, save, load again and save again. Both re-saves keep the engine
    section byte for byte.
  • ctest passes (16/16).
  • The sync checksums of --turns 100 --map Fractal --size STANDARD --seed 7 are unchanged (D7D43800 at turn 100).

🤖 Generated with Claude Code

@bssth

bssth commented Nov 23, 2026

Copy link
Copy Markdown
Owner

saveGame always builds a fresh SaveFile, so engineData is empty on every real save: re-saving an original save writes 33 zeros and drops the signs and the Great Wall. Either carry engineData from the loaded save, or drop the write branch and say so in the description.

The comment at SaveFile.cpp:198 ("OpenCiv4 may keep blocks of its own") describes nothing that exists: please drop it and the test cases that append GWAL. Please also add a test where a file truncated before the MD5 throws.

MortenLyhr and others added 4 commits November 23, 2026 09:11
The original stores signs and engine-made lines (the Great Wall) after the
pickled Python data; reading a fixed 33 bytes there failed on such saves
("save file truncated"). Keep the section's bytes so they can be written back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A save may carry more data after the MD5 string (OpenCiv4's own Great Wall block), so taking the
last 36 bytes as the MD5 broke loading of such saves with "save file truncated". The reader now
looks for the first int 32 followed by 32 hex digits after the Python data; the engine's section
before it holds only ints, floats and UTF-16 text. Adds save_file_test, which round-trips an empty
engine section, one laid out like the original's, and both with a block after the MD5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
saveGame built a fresh SaveFile, so re-saving an original save wrote the
empty section (33 zeros) and dropped its signs and Great Wall lines.
GameSession now keeps the section from loadGame and writes it back as it
was; a new game or scenario clears it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r it

Drop the comment and the test cases about blocks after the MD5 string:
nothing writes one. New cases cut the file 1 to 50 bytes short of its
end (inside the MD5 string, just before it, inside the engine section)
and expect read to throw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MortenLyhr

Copy link
Copy Markdown
Contributor Author

Thanks, all three are done:

  1. Carrying engineData. GameSession now keeps the section from loadGame and saveGame writes it back as it
    was. A new game or a scenario clears it (33 zeros, as before). Before doing it I checked what the original puts there
    in its own saves: 182 of them (a 140-save Wine game and the saves in testgames). 77 have the empty section. The
    others hold the Great Wall's line group, and the TestMap saves also hold 437 to 442 signs. The wall's lines change
    from turn to turn in the Wine game (256 to 1006 points), so the original redraws them as borders move. What a re-save
    writes is the section as loaded, which is a snapshot: OpenCiv4 doesn't read it or update it. Signs placed in OpenCiv4
    still aren't saved (it has no store for them yet), and the wall's lines are the ones from the turn the save was made.
    Today only OpenCiv4 reads these files back, since the original rejects OpenCiv4's saves because of the MD5 (see the
    description).
    I checked it on 9 original saves (unmodded and BUG Mod, with and without signs and a wall). I loaded each one, saved
    it, loaded the result and saved it again. Both times the engine section matched the original's byte for byte (439
    signs and the wall in the TestMap QuickSave, for example), and every re-saved file loaded.
  2. The comment and the GWAL cases are gone. You're right that nothing upstream writes a block after the MD5. The
    reader still finds the MD5 string by its form instead of taking the last 36 bytes, because the section's size isn't
    stored. If you'd rather it check that the last 36 bytes are the MD5 string, I can change that.
  3. Truncated files. save_file_test now cuts the file short by 1, 20, 32, 36, 37 and 50 bytes (inside the MD5,
    just before it, inside the engine section), with an empty section and with an original-style one, and checks that
    read throws each time.

ctest passes (16/16). The sync checksum at turn 100 (--turns 100 --map Fractal --size STANDARD --seed 7) is still D7D43800.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants