sm64-binds 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sm64_binds/Sm64Game.py ADDED
@@ -0,0 +1,174 @@
1
+ import struct
2
+ import zipfile
3
+ import io
4
+ import os
5
+ from wasmtime import Engine, Store, Module, Linker, WasiConfig
6
+
7
+ # ---------------------------------------------------------
8
+ # GamePad and GameState Python versions
9
+ # ---------------------------------------------------------
10
+
11
+ class GamePad:
12
+ def __init__(self, button=0, stick_x=0, stick_y=0):
13
+ self.button = button
14
+ self.stick_x = stick_x
15
+ self.stick_y = stick_y
16
+
17
+ @staticmethod
18
+ def from_bytes(b):
19
+ button = (b[0] << 8) | b[1]
20
+ stick_x = struct.unpack("b", b[2:3])[0]
21
+ stick_y = struct.unpack("b", b[3:4])[0]
22
+ return GamePad(button, stick_x, stick_y)
23
+
24
+ class GameState:
25
+ def __init__(self, data):
26
+ # Rust reads 60 bytes: first 24 bytes are pos/vel floats
27
+ b_data = bytes(data)
28
+ floats = [struct.unpack("f", b_data[i:i+4])[0] for i in range(0, 24, 4)]
29
+ self.pos = floats[0:3]
30
+ self.vel = floats[3:6]
31
+
32
+ # ---------------------------------------------------------
33
+ # Utility: unzip and XOR like Rust
34
+ # ---------------------------------------------------------
35
+
36
+ def unzip_bytes(zipped_bytes):
37
+ with zipfile.ZipFile(io.BytesIO(zipped_bytes), 'r') as z:
38
+ return z.read(z.namelist()[0])
39
+
40
+ import hashlib
41
+
42
+ ROM_HASH = "9bef1128717f958171a4afac3ed78ee2bb4e86ce"
43
+ def check_hash(data: bytes) -> bool:
44
+ sha1 = hashlib.sha1()
45
+ sha1.update(data)
46
+ hash_result = sha1.hexdigest()
47
+ return hash_result == ROM_HASH
48
+
49
+ # ---------------------------------------------------------
50
+ # RngConfig class matching Rust
51
+ # ---------------------------------------------------------
52
+
53
+ class RngConfig:
54
+ def __init__(self, window_length, random_amount, random_burst_length,
55
+ a_prob, b_prob, z_prob):
56
+ self.window_length = window_length
57
+ self.random_amount = random_amount
58
+ self.random_burst_length = random_burst_length
59
+ self.a_prob = a_prob
60
+ self.b_prob = b_prob
61
+ self.z_prob = z_prob
62
+
63
+ # ---------------------------------------------------------
64
+ # SM64GameGenerator (Python match of Rust version)
65
+ # ---------------------------------------------------------
66
+
67
+ class SM64GameGenerator:
68
+ def __init__(self, wasm_bytes):
69
+ self.wasm_bytes = wasm_bytes
70
+
71
+ @staticmethod
72
+ def new(rom_bytes):
73
+ if not check_hash(rom_bytes):
74
+ raise ValueError("Invalid ROM. Should be US z64 (8MB).")
75
+
76
+ wasm_bytes = SM64GameGenerator.rom_to_wasm_bytes(rom_bytes)
77
+ return SM64GameGenerator(wasm_bytes)
78
+
79
+ @staticmethod
80
+ def from_file(path):
81
+ with open(path, "rb") as f:
82
+ return SM64GameGenerator.new(f.read())
83
+
84
+ @staticmethod
85
+ def rom_to_wasm_bytes(rom_bytes):
86
+ rom_len = len(rom_bytes)
87
+
88
+ # load XOR bytes exactly like Rust include_bytes!
89
+
90
+ base_dir = os.path.dirname(__file__)
91
+ xor_path = os.path.join(base_dir, "..", "..", "pkg", "sm64_headless.us.wasm.zip.xor")
92
+
93
+ with open(xor_path, "rb") as f:
94
+ xor_bytes = f.read()
95
+
96
+ wasm_zip_bytes = bytes([xor_bytes[i] ^ rom_bytes[i % rom_len]
97
+ for i in range(len(xor_bytes))])
98
+
99
+ return unzip_bytes(wasm_zip_bytes)
100
+
101
+ def create_game(self):
102
+ return SM64Game(self.wasm_bytes)
103
+
104
+ # ---------------------------------------------------------
105
+ # SM64Game (Python match of Rust version)
106
+ # ---------------------------------------------------------
107
+
108
+ class SM64Game:
109
+ def __init__(self, wasm_bytes):
110
+ self.engine = Engine()
111
+ self.module = Module(self.engine, wasm_bytes)
112
+
113
+ self.store = Store(self.engine)
114
+ self.linker = Linker(self.engine)
115
+ self.linker.define_wasi()
116
+
117
+ wasi = WasiConfig()
118
+ wasi.inherit_stdout()
119
+ self.store.set_wasi(wasi)
120
+
121
+ self.instance = self.linker.instantiate(self.store, self.module)
122
+
123
+ self.using_rng_flag = False
124
+
125
+ # call main_func like Rust does
126
+ self.instance.exports(self.store)["main_func"](self.store)
127
+
128
+ self.memory = self.instance.exports(self.store)["memory"]
129
+
130
+ # -----------------------------------------------------
131
+ # RNG functions
132
+ # -----------------------------------------------------
133
+
134
+ def set_rng_config(self, cfg: RngConfig):
135
+ self.using_rng_flag = True
136
+ func = self.instance.exports(self.store)["set_rng_config"]
137
+ func(self.store,
138
+ cfg.window_length,
139
+ cfg.random_amount,
140
+ cfg.random_burst_length,
141
+ cfg.a_prob,
142
+ cfg.b_prob,
143
+ cfg.z_prob
144
+ )
145
+
146
+ def set_rng_seed(self, seed):
147
+ self.using_rng_flag = True
148
+ func = self.instance.exports(self.store)["set_rng_seed"]
149
+ func(self.store, seed)
150
+
151
+ # -----------------------------------------------------
152
+ # Game stepping + state
153
+ # -----------------------------------------------------
154
+
155
+ def step_game(self, pad: GamePad):
156
+ func = self.instance.exports(self.store)["step_game"]
157
+ func(self.store, pad.button, pad.stick_x, pad.stick_y)
158
+
159
+ def get_game_state(self):
160
+ func = self.instance.exports(self.store)["get_game_state"]
161
+ ptr = func(self.store)
162
+
163
+ raw = self.memory.data_ptr(self.store)[ptr:ptr+60]
164
+ return GameState(raw)
165
+
166
+ def rng_pad(self, pad: GamePad):
167
+ func = self.instance.exports(self.store)["rng_pad"]
168
+ ptr = func(self.store, pad.button, pad.stick_x, pad.stick_y)
169
+
170
+ raw = self.memory.data_ptr(self.store)[ptr:ptr+4]
171
+ return GamePad.from_bytes(raw)
172
+
173
+ def using_rng(self):
174
+ return self.using_rng_flag
sm64_binds/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .Sm64Game import SM64Game, SM64GameGenerator, GamePad
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.4
2
+ Name: sm64-binds
3
+ Version: 0.0.1
4
+ Summary: Mario 64 using WASM. Requires a US .z64 version ROM to work (8.00MB)
5
+ Author-email: Gumbo64 <gumbo64isme@proton.me>
6
+ License-Expression: MIT
7
+ Project-URL: homepage, https://github.com/Gumbo64/sm64-binds
8
+ Project-URL: repository, https://github.com/Gumbo64/sm64-binds
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ https://github.com/Gumbo64/sm64-binds
@@ -0,0 +1,7 @@
1
+ sm64_binds/Sm64Game.py,sha256=FnOGq6IuywuS3pRfKfsILZVbS94vyN_qDpW7Tw1J_Ns,5569
2
+ sm64_binds/__init__.py,sha256=sBCVjr1X7aW5JridH0Fq8K7Utxy8RE_sxpPYTSePiZY,58
3
+ sm64_binds-0.0.1.dist-info/licenses/LICENSE,sha256=-D5uzDRNu6hQDCvdej1BkHLkZnzrW_ttnaXueY_VH1s,1063
4
+ sm64_binds-0.0.1.dist-info/METADATA,sha256=Apn4Q9qio98S8Hhu8zPb9X-WS8d3SLXFiIhlmFyOHVA,567
5
+ sm64_binds-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
+ sm64_binds-0.0.1.dist-info/top_level.txt,sha256=kk-xW6AVg0mC6SsiUKfTuIeLCihTv8ivLgn8K0cnyNE,11
7
+ sm64_binds-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Gumbo64
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ sm64_binds