Skip to content

Commit

Permalink
Add checksum stripping functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maikelwever committed Mar 4, 2021
1 parent c88d966 commit d6f16aa
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions slimseditor/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from slimseditor.game import Game


ChecksumStructLE = struct.Struct('<II')
ChecksumStructBE = struct.Struct('>II')


class AbstractBackend:
def __init__(self, path, game=Game.ERROR):
self.data = bytearray()
Expand All @@ -29,6 +33,9 @@ def read_data(self):
def write_data(self):
pass

def strip_checksums(self):
pass

def write_all_items(self, items):
for section, section_items in items.items():
for item in section_items:
Expand All @@ -46,6 +53,15 @@ def read_data(self):
with open(self.path, 'rb') as f:
self.data = bytearray(f.read())

self.strip_checksums()

def strip_checksums(self):
pos = 0x08
while pos < len(self.data):
bytecount, checksum = ChecksumStructLE.unpack_from(self.data, pos)
ChecksumStructLE.pack_into(self.data, pos, bytecount, 0xFFFFFFFF)
pos = pos + 8 + bytecount

def write_data(self):
with open(self.path, 'wb') as f:
f.write(self.data)
Expand Down Expand Up @@ -105,6 +121,7 @@ def read_data(self):
card_file = self.wrapper.card.open(self.path, 'rb')
self.data = bytearray(card_file.read())
card_file.close()
self.strip_checksums()

def write_data(self):
self.calculate_checksum()
Expand Down Expand Up @@ -179,6 +196,7 @@ def read_data(self):
data_file = os.path.join(self.path, self.get_filename())
with open(data_file, 'rb') as f:
self.data = bytearray(f.read())
self.strip_checksums()

def read_item(self, item):
struct_def = '>{0}'.format(item.struct_type)
Expand Down Expand Up @@ -218,3 +236,14 @@ def detect_game(self):
region_sfo = f.read(9).decode('ascii')

self.match_region_to_game(region_sfo)

def strip_checksums(self):
if self.game not in HD_REMASTER_GAMES:
return

pos = 0x08
while pos < len(self.data):
bytecount, checksum = ChecksumStructBE.unpack_from(self.data, pos)
ChecksumStructBE.pack_into(self.data, pos, bytecount, 0xFFFFFFFF)
pos = pos + 8 + bytecount

0 comments on commit d6f16aa

Please sign in to comment.