Skip to content

Commit

Permalink
Fix offset consistency after Ssb compilation (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
theCapypara authored Jan 30, 2025
1 parent c53cdcc commit d55766a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
7 changes: 5 additions & 2 deletions skytemple_files/script/ssb/script_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,13 @@ def compile_structured(
# Handle the rest
new_params.append(self._parse_param(param, built_strings, built_constants))
op_len += 2
built_ops.append(SkyTempleSsbOperation(opcode_cursor, op_code, new_params))

actual_stored_offset = int(opcode_cursor / 2)

built_ops.append(SkyTempleSsbOperation(actual_stored_offset, op_code, new_params))

# Create actual offset mapping for this opcode and update source map
opcode_index_mem_offset_mapping[in_op.offset] = int(opcode_cursor / 2)
opcode_index_mem_offset_mapping[in_op.offset] = actual_stored_offset

bytes_written_last_rtn += op_len
opcode_cursor += op_len
Expand Down
103 changes: 103 additions & 0 deletions test/skytemple_files_test/script/ssb/jump_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2020-2025 Capypara and the SkyTemple Contributors
#
# This file is part of SkyTemple.
#
# SkyTemple is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SkyTemple is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SkyTemple. If not, see <https://www.gnu.org/licenses/>.
import unittest

from explorerscript.ssb_converting.ssb_data_types import SsbOperation

from skytemple_files.common.ppmdu_config.xml_reader import Pmd2XmlReader
from skytemple_files.script.ssb.script_compiler import ScriptCompiler

# This is the jump test from ExplorerScript ported to skytemple-files to also test offset rewriting.

LOOPING_SCRIPT = """
def 0 {
@foo;
me_Stop();
jump @foo;
end;
}
"""

SELF_LOOPING_SCRIPT = """
def 0 {
@foo;
jump @foo;
end;
}
"""


class StringTestCase(unittest.TestCase):
"""
Tests compilation of jumps.
"""

def test_looping_exps(self) -> None:
routine_ops = self.compile_exps(LOOPING_SCRIPT)
self.assertEqual(1, len(routine_ops))
routine = routine_ops[0]
self.assertEqual(3, len(routine))

for op in routine_ops[0]:
print(op)

# FIRST OP
op_to_check = routine[0]
self.assertEqual("me_Stop", op_to_check.op_code.name)
self.assertEqual(5, op_to_check.offset)
self.assertEqual(0, len(op_to_check.params))

# SECOND OP
op_to_check = routine[1]
self.assertEqual("Jump", op_to_check.op_code.name)
self.assertEqual(6, op_to_check.offset)
self.assertEqual(1, len(op_to_check.params))
self.assertIs(5, op_to_check.params[0])

# THIRD OP
op_to_check = routine[2]
self.assertEqual("End", op_to_check.op_code.name)
self.assertEqual(8, op_to_check.offset)
self.assertEqual(0, len(op_to_check.params))

def test_self_looping_exps(self) -> None:
routine_ops = self.compile_exps(SELF_LOOPING_SCRIPT)

self.assertEqual(1, len(routine_ops))
routine = routine_ops[0]
self.assertEqual(2, len(routine))

# FIRST OP
op_to_check = routine[0]
self.assertEqual("Jump", op_to_check.op_code.name)
self.assertEqual(5, op_to_check.offset)
self.assertEqual(1, len(op_to_check.params))
self.assertIs(5, op_to_check.params[0])

# SECOND OP
op_to_check = routine[1]
self.assertEqual("End", op_to_check.op_code.name)
self.assertEqual(7, op_to_check.offset)
self.assertEqual(0, len(op_to_check.params))

def compile_exps(self, src: str) -> list[list[SsbOperation]]:
static_data = Pmd2XmlReader.load_default()
compiler = ScriptCompiler(static_data)

ssb_after = compiler.compile_explorerscript(src, "")[0]

return ssb_after.routine_ops

0 comments on commit d55766a

Please sign in to comment.