Files
neoLegacy-mac/tests/test_reconstructed_swf_math.py
2026-07-13 03:45:26 -05:00

339 lines
13 KiB
Python

import ctypes
import math
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "iggy-decompiled/src/reconstructed/swf_math.c"
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
VARIANTS = {
"durango": "IGGY_RECONSTRUCT_SWF_MATH_DURANGO",
"orbis": "IGGY_RECONSTRUCT_SWF_MATH_ORBIS",
"ps3": "IGGY_RECONSTRUCT_SWF_MATH_PS3",
"vita": "IGGY_RECONSTRUCT_SWF_MATH_VITA",
}
GLOBALS = """
#include <math.h>
double as3vm_P_Infinity = INFINITY;
double as3vm_N_Infinity = -INFINITY;
"""
class ReconstructedSwfMathTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.temporary_directory = tempfile.TemporaryDirectory()
cls.libraries = {}
temporary_path = Path(cls.temporary_directory.name)
globals_path = temporary_path / "swf_math_globals.c"
globals_path.write_text(GLOBALS, encoding="utf-8")
for platform, define in VARIANTS.items():
library_path = temporary_path / f"libswf_math_{platform}.dylib"
result = subprocess.run(
[
"clang",
"-dynamiclib",
"-std=c11",
"-O2",
"-Wall",
"-Wextra",
"-Werror",
"-fno-stack-protector",
f"-D{define}=1",
"-I",
str(RECONSTRUCTED),
str(SOURCE),
str(globals_path),
"-o",
str(library_path),
],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise AssertionError(result.stdout + result.stderr)
library = ctypes.CDLL(str(library_path))
float_pointer = ctypes.POINTER(ctypes.c_float)
library.iggy_invert_matrix.argtypes = [float_pointer, float_pointer]
library.iggy_invert_matrix.restype = ctypes.c_int
if platform == "durango":
transform_args = [
float_pointer,
float_pointer,
float_pointer,
ctypes.c_float,
ctypes.c_float,
]
else:
transform_args = [
ctypes.c_float,
ctypes.c_float,
float_pointer,
float_pointer,
float_pointer,
]
library.iggy_transform.argtypes = transform_args
library.iggy_transform.restype = None
library.iggy_inverse_transform.argtypes = transform_args
library.iggy_inverse_transform.restype = ctypes.c_int
cls.libraries[platform] = library
@classmethod
def tearDownClass(cls):
cls.temporary_directory.cleanup()
@staticmethod
def matrix(values):
return (ctypes.c_float * 6)(*values)
def transform(self, platform, x, y, matrix, inverse=False, shared=False):
library = self.libraries[platform]
function = (
library.iggy_inverse_transform if inverse else library.iggy_transform
)
out_x = ctypes.c_float(1234.0)
out_y = out_x if shared else ctypes.c_float(-5678.0)
if platform == "durango":
result = function(
ctypes.byref(out_x), ctypes.byref(out_y), matrix, x, y
)
else:
result = function(
x, y, ctypes.byref(out_x), ctypes.byref(out_y), matrix
)
return result, out_x.value, out_y.value
def test_transform_uses_the_recovered_affine_layout_and_store_order(self):
fixtures = [
([1, 0, 0, 1, 0, 0], 17, 19, (17, 19)),
([1, 0, 0, 1, 11, 13], 17, 19, (28, 32)),
([2, 3, 5, 7, 11, 13], 17, 19, (102, 231)),
([0, -1, 1, 0, 0, 0], 4, 9, (-9, 4)),
]
for platform in VARIANTS:
for values, x, y, expected in fixtures:
with self.subTest(platform=platform, matrix=values):
result, out_x, out_y = self.transform(
platform, x, y, self.matrix(values)
)
self.assertIsNone(result)
self.assertEqual((out_x, out_y), expected)
_, shared_x, shared_y = self.transform(
platform, 17, 19, self.matrix([2, 3, 5, 7, 11, 13]), shared=True
)
self.assertEqual(shared_x, 231)
self.assertEqual(shared_y, 231)
def test_invert_matrix_matches_layout_and_rejects_without_writing(self):
invalid_matrices = [
[1, 2, 2, 4, 11, 13],
[1, 0, 0, -0.0, 11, 13],
[math.inf, 0, 0, 1, 11, 13],
[-math.inf, 0, 0, 1, 11, 13],
]
sentinels = [101, 102, 103, 104, 105, 106]
for platform, library in self.libraries.items():
with self.subTest(platform=platform, case="general"):
output = self.matrix([0] * 6)
result = library.iggy_invert_matrix(
output, self.matrix([2, 3, 5, 7, 11, 13])
)
self.assertEqual(result, 1)
self.assertEqual(list(output), [-7, 3, 5, -2, 38, -29])
for values in invalid_matrices:
with self.subTest(platform=platform, matrix=values):
output = self.matrix(sentinels)
result = library.iggy_invert_matrix(output, self.matrix(values))
self.assertEqual(result, 0)
self.assertEqual(list(output), sentinels)
output = self.matrix([0] * 6)
result = library.iggy_invert_matrix(
output, self.matrix([1.40129846e-45, 0, 0, 1, 0, 0])
)
self.assertEqual(result, 1)
self.assertTrue(math.isinf(output[0]))
if platform == "ps3":
self.assertTrue(math.isinf(output[3]))
self.assertTrue(
all(math.isnan(output[index]) for index in (1, 2, 4, 5))
)
else:
self.assertEqual(output[3], 1)
self.assertEqual(output[1], 0)
self.assertEqual(output[2], 0)
self.assertTrue(math.isnan(output[4]))
self.assertEqual(output[5], 0)
def test_nan_determinant_policy_matches_each_platform_object(self):
matrix = self.matrix([math.nan, 0, 0, 1, 5, -2])
sentinels = [101, 102, 103, 104, 105, 106]
for platform, library in self.libraries.items():
with self.subTest(platform=platform):
output = self.matrix(sentinels)
result = library.iggy_invert_matrix(output, matrix)
if platform == "ps3":
self.assertEqual(result, 1)
self.assertTrue(all(math.isnan(value) for value in output))
else:
self.assertEqual(result, 0)
self.assertEqual(list(output), sentinels)
def test_inverse_transform_round_trips_and_reproduces_singular_fallbacks(self):
fixtures = [
([0, 0, 0, 0, 5, -2], 9, 8, (0, 0)),
([0, 0, 0, 2, 5, -2], 9, 8, (0, 5)),
([2, 0, 0, 0, 5, -2], 9, 8, (2, 0)),
([1, 2, 2, 4, 5, -2], 9, 8, (4, 10)),
([0, 1, 0, 0, 5, -2], 9, 8, (-6, 10)),
]
for platform in VARIANTS:
matrix = self.matrix([2, 3, 1, 4, 5, -2])
_, transformed_x, transformed_y = self.transform(platform, 7, -3, matrix)
result, out_x, out_y = self.transform(
platform, transformed_x, transformed_y, matrix, inverse=True
)
self.assertEqual(result, 1)
self.assertAlmostEqual(out_x, 7, places=5)
self.assertAlmostEqual(out_y, -3, places=5)
for values, x, y, expected in fixtures:
with self.subTest(platform=platform, matrix=values):
result, out_x, out_y = self.transform(
platform, x, y, self.matrix(values), inverse=True
)
self.assertEqual(result, 1)
self.assertEqual((out_x, out_y), expected)
def test_inverse_nan_fallback_preserves_object_specific_unordered_comparisons(self):
matrix = self.matrix([math.nan, 0, 0, 1, 5, -2])
for platform in VARIANTS:
with self.subTest(platform=platform):
result, out_x, out_y = self.transform(
platform, 9, 8, matrix, inverse=True
)
self.assertEqual(result, 1)
if platform == "ps3":
self.assertTrue(math.isnan(out_x))
self.assertTrue(math.isnan(out_y))
elif platform == "orbis":
self.assertEqual((out_x, out_y), (0, 10))
else:
self.assertEqual((out_x, out_y), (4, 10))
def test_platform_abis_compile_as_c_and_cpp_and_export_only_three_functions(self):
temporary_path = Path(self.temporary_directory.name)
expected_exports = {
"iggy_transform",
"iggy_invert_matrix",
"iggy_inverse_transform",
}
for platform, define in VARIANTS.items():
with self.subTest(platform=platform):
object_path = temporary_path / f"swf_math_{platform}.o"
compile_result = subprocess.run(
[
"clang",
"-c",
"-std=c11",
"-O2",
"-Wall",
"-Wextra",
"-Werror",
"-fno-stack-protector",
f"-D{define}=1",
"-I",
str(RECONSTRUCTED),
str(SOURCE),
"-o",
str(object_path),
],
capture_output=True,
text=True,
)
self.assertEqual(
compile_result.returncode,
0,
compile_result.stdout + compile_result.stderr,
)
nm_result = subprocess.run(
["nm", "-g", str(object_path)], capture_output=True, text=True
)
self.assertEqual(nm_result.returncode, 0, nm_result.stderr)
defined = {
line.split()[-1].removeprefix("_")
for line in nm_result.stdout.splitlines()
if " T " in line
}
undefined = {
line.split()[-1].removeprefix("_")
for line in nm_result.stdout.splitlines()
if " U " in line
}
self.assertEqual(defined, expected_exports)
self.assertEqual(
undefined, {"as3vm_P_Infinity", "as3vm_N_Infinity"}
)
syntax_result = subprocess.run(
[
"clang",
"-target",
"i386-unknown-linux-gnu",
"-ffreestanding",
"-fsyntax-only",
"-std=c11",
"-Wall",
"-Wextra",
"-Werror",
f"-D{define}=1",
"-I",
str(RECONSTRUCTED),
str(SOURCE),
],
capture_output=True,
text=True,
)
self.assertEqual(
syntax_result.returncode,
0,
syntax_result.stdout + syntax_result.stderr,
)
cpp_path = temporary_path / "swf_math_cpp_linkage.cpp"
cpp_path.write_text(
'#include "swf_math.h"\n'
"void use_api(float *out, const float *matrix) {\n"
" iggy_transform(1.0f, 2.0f, out, out + 1, matrix);\n"
"}\n",
encoding="utf-8",
)
cpp_result = subprocess.run(
[
"clang++",
"-fsyntax-only",
"-std=c++17",
"-Wall",
"-Wextra",
"-Werror",
"-DIGGY_RECONSTRUCT_SWF_MATH_ORBIS=1",
"-I",
str(RECONSTRUCTED),
str(cpp_path),
],
capture_output=True,
text=True,
)
self.assertEqual(
cpp_result.returncode, 0, cpp_result.stdout + cpp_result.stderr
)