Skip to content

Commit

Permalink
add recovery mechanism for improperly imported levelstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Jul 21, 2024
1 parent 19f727e commit ef3948b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
22 changes: 21 additions & 1 deletion history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,23 @@ def get_file_path(self):
os.mkdir(directory)
return f"{directory}/{self.sha256}"

def write_string(self, level_string):
import base64

self.requires_base64 = False
if level_string[:2] == b'eJ' or level_string[:2] == b'H4':
try:
level_string = base64.b64decode(level_string, altchars='-_')
self.requires_base64 = True
except:
#unable to decode, store levelstring as is
print("unable to decode levelstring")

f = open(self.get_file_path(), "wb")
f.write(level_string)
f.close()
self.calculate_file_size()

def load_file_content(self):
import base64

Expand Down Expand Up @@ -886,7 +903,10 @@ def get_decompressed_sha256(self):
return self.decompressed_sha256

def calculate_file_size(self):
self.file_size = len(self.load_file_content())
content = self.load_file_content()
if content is None: return None

self.file_size = len(content)
if self.calculate_decompressed_string():
self.decompressed_file_size = len(self.calculate_decompressed_string())
self.save()
Expand Down
24 changes: 8 additions & 16 deletions history/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,14 @@ def create_level_string(level_string):
level_string = level_string.encode('windows-1252', errors='xmlcharrefreplace')
sha256 = hashlib.sha256(level_string).hexdigest()
try:
return LevelString.objects.get(sha256=sha256)
except:
requires_base64 = False
if level_string[:2] == b'eJ' or level_string[:2] == b'H4':
try:
level_string = base64.b64decode(level_string, altchars='-_')
requires_base64 = True
except:
#unable to decode, store levelstring as is
print("unable to decode levelstring")

record = LevelString(sha256=sha256, requires_base64=requires_base64)
record.save()
f = open(record.get_file_path(), "wb")
f.write(level_string)
f.close()
level_string_object = LevelString.objects.get(sha256=sha256)
file_size = level_string_object.get_file_size()
if file_size is None or file_size == 0:
record.write_string(level_string)
return level_string_object
except Exception as error:
record = LevelString(sha256=sha256)
record.write_string(level_string)
return record

def robtop_unxor(string, key):
Expand Down

0 comments on commit ef3948b

Please sign in to comment.