Skip to content

Instantly share code, notes, and snippets.

@dazjo
Created August 3, 2016 16:13
Show Gist options
  • Save dazjo/22f5751da92421e0023b8a08ab50331f to your computer and use it in GitHub Desktop.
Save dazjo/22f5751da92421e0023b8a08ab50331f to your computer and use it in GitHub Desktop.
old as heck
# Usage: fixsvs.py [path] (optional)
import os, sys
import struct
from binascii import crc32
if len(sys.argv) > 1:
dir = sys.argv[1]
else:
dir = "."
system = os.path.join(dir, "system.sav")
system = open(system, "rb+")
svs = system.read()
entries = []
num_entries = len(svs) / 0x10
# Tag <-> entry index ordering is important (hardcoded)
# [0] = #SVS (system.sav)
# [1] = #PRG (progress.sav)
# [2] = #FIG (figure.sav)
# [3] = #JST (injustic.sav)
for entry in range(num_entries):
entry = struct.unpack_from("<4sIII", svs, entry * 0x10)
entries.append(list(entry))
print entries
for file in os.listdir(dir):
path = os.path.join(dir, file)
if os.path.isdir(path) == True:
continue
name, ext = os.path.splitext(file)
if ext != ".sav": continue
if name == "system": continue
file = open(path, "rb")
buffer = file.read()
file.close()
size = len(buffer)
# Game uses bog standard CRC32 without the final bitwise NOT.
# crc32() will do this NOT so we must undo it.
crc = (~(crc32(buffer) & 0xFFFFFFFF)) & 0xFFFFFFFF
tag = struct.unpack_from("4s", buffer)[0];
tags = [entry[0] for entry in entries]
print (tag, name, hex(crc))
if tag not in tags:
raise ValueError("Tag %s does not exist in SVS." % tag)
index = tags.index(tag)
if index == 0: raise ValueError("Tried to update SVS.")
# Update SVS entry for this file.
entry = entries[index]
entry[2] = size
entry[3] = crc
entries[index] = entry
# With all files done, we need to recalculate the SVS CRC over itself.
# This is done by first setting the CRC field to the contents of the tag field ("#SVS").
svs_entry = entries[0]
svs_entry[3] = struct.unpack("<I", svs_entry[0])[0]
entries[0] = svs_entry
svs = ""
for entry in range(num_entries):
svs += struct.pack("<4sIII", *entries[entry])
crc = (~(crc32(svs) & 0xFFFFFFFF)) & 0xFFFFFFFF
svs_entry[3] = crc
entries[0] = svs_entry
print entries
svs = ""
for entry in range(num_entries):
svs += struct.pack("<4sIII", *entries[entry])
# Write the fixed SVS table.
system.seek(0)
system.write(svs)
system.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment