Skip to content

Commit

Permalink
fix gmdv2 description decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Mar 23, 2024
1 parent 8be5f25 commit 22f9673
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions gdlevelconverter/gjobjects/gjdictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ def from_dict_str(cls, string: str):
root = xml.etree.ElementTree.fromstring(string)

# correct new gmd files into old gmd format
gmd_version = 1
if root.tag == "plist":
root = root[0]
if root.tag == "dict":
gmd_version = 2
root.tag = "d"

if not root.tag == "d":
Expand All @@ -152,6 +154,10 @@ def from_dict_str(cls, string: str):
element_dict = {
k.text: cls._parse_xml_value(v) for k, v in zip(*[iter(root)] * 2)
}

# ah! i don't like that this is neccessary
element_dict["_data_version"] = gmd_version

return element_dict

@staticmethod
Expand Down
8 changes: 5 additions & 3 deletions gdlevelconverter/gjobjects/gjgamelevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class GJGameLevel(gjdictionary.GJDictionary):
level_id: int
name: str
description: str
"base64 encoded description of the level"
level_string: GJLevelString
version: int
audio_track: int
Expand Down Expand Up @@ -72,12 +73,13 @@ def from_gmd(cls, file: str):
instance.level_id = int(element_dict.get("k1", 0))
instance.name = element_dict.get("k2", "Unknown")

description = element_dict.get("k3", None)
if description:
description = element_dict.get("k3", "")
is_old_gmd = element_dict.get("_data_version", 1) <= 1
if description and is_old_gmd:
# gmd files are double decoded, for some reason
instance.description = base64.urlsafe_b64decode(description).decode()
else:
instance.description = ""
instance.description = description

# if this doesn't exist this should error
instance.level_string = GJLevelString.from_encoded(element_dict["k4"])
Expand Down
2 changes: 1 addition & 1 deletion tests/kanpyo_roll.gmd

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/maki_roll.gmd

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions tests/testgjgamelevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest
import pathlib
from gdlevelconverter.gjobjects import GJGameLevel
import base64


class TestGJGameLevel(unittest.TestCase):
Expand All @@ -30,6 +31,11 @@ def test_from_gmd(self):
self.assertEqual(level.name, "maki roll")
self.assertEqual(len(level.level_string.objects), 19999)

# validate description is in proper base64
level_description = "she just won't stop rolling... | collab with zyl"
test_description = base64.b64decode(level.description).decode()
self.assertEqual(test_description, level_description)

def test_from_gmdv2(self):
"""
Test loading from the gmdv2 format.
Expand All @@ -46,6 +52,11 @@ def test_from_gmdv2(self):
self.assertEqual(level.name, "Kanpyo Roll")
self.assertEqual(len(level.level_string.objects), 19604)

# gmdv2 files encode descriptions slightly differently, making the test necessary
level_description = "exclusive !!! "
test_description = base64.b64decode(level.description).decode()
self.assertEqual(test_description, level_description)

def test_to_gmd(self):
"""
Test saving level to gmd file
Expand Down

0 comments on commit 22f9673

Please sign in to comment.