Last active
June 27, 2016 04:45
-
-
Save RoadrunnerWMC/536a315d911824a5aeb635235b66109d to your computer and use it in GitHub Desktop.
A little script I ported from C that will recalculate hashes of Super Mario Galaxy save files (GameData.bin).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 6/26/16 | |
# A little script I ported from C that will recalculate hashes of Super Mario | |
# Galaxy save files (GameData.bin). Thanks to MasterF0x and Mathew_Wi (Matt) | |
# for the original C script. Thanks to Marionumber1 for putting the checksum | |
# algorithm on the wiibrew wiki, where they found it | |
# (http://wiibrew.org/wiki/Super_Mario_Galaxy_savefile). Thanks to Treeki/Ninji | |
# for reverse-engineering the algorithm in the first place. | |
# Running with no arguments will cause it to scan for all .bin files in the | |
# same folder as itself (non-recursively) and fix all of their hashes. | |
# Running with one or more arguments will cause it to fix the hash of the | |
# file(s) given. | |
# Licensed under the MIT License: | |
# Copyright (c) 2016 RoadrunnerWMC | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import sys | |
def generate_checksum(data): | |
c1 = c2 = 0 | |
for i in range(0, len(data), 2): | |
val = data[i] << 8 | data[i+1] | |
c1 = (c1 + val) & 0xFFFF | |
c2 = (c2 + ~val) & 0xFFFF | |
return (c1 << 16) | c2 | |
def fixSavegame(data): | |
checksum = generate_checksum(data[4:]) | |
data = bytearray(data) | |
data[0] = checksum >> 24 | |
data[1] = (checksum >> 16) & 0xFF | |
data[2] = (checksum >> 8) & 0xFF | |
data[3] = checksum & 0xFF | |
return bytes(data) | |
def main(argv): | |
if len(argv) > 1: | |
print('Fixing the files passed as arguments:') | |
for file in argv[1:]: | |
print('Fixing ' + file) | |
with open(file, 'rb') as f: | |
data = f.read() | |
with open(file, 'wb') as f: | |
f.write(fixSavegame(data)) | |
else: | |
print('Fixing all .bin files in this folder:') | |
for fn in os.listdir('.'): | |
if not fn.endswith('.bin'): continue | |
print('Fixing ' + file) | |
with open(fn, 'rb') as f: | |
data = f.read() | |
with open(fn, 'wb') as f: | |
f.write(fixSavegame(data)) | |
if __name__ == '__main__': main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment