| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | import hashlib | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | import Utils | 
					
						
							| 
									
										
										
										
											2022-09-30 00:36:30 +02:00
										 |  |  | from Utils import read_snes_rom | 
					
						
							| 
									
										
										
										
											2024-11-29 21:36:00 -05:00
										 |  |  | from worlds.Files import APProcedurePatch, APPatchExtension, APTokenMixin, APTokenTypes | 
					
						
							|  |  |  | from worlds.smz3.ips import IPS_Patch | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-28 00:43:05 +00:00
										 |  |  | SMJUHASH = '21f3e98df4780ee1c667b84e57d88675' | 
					
						
							|  |  |  | LTTPJPN10HASH = '03a63945398191337e896e5771f77173' | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | ROM_PLAYER_LIMIT = 256 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 21:36:00 -05:00
										 |  |  | world_folder = os.path.dirname(__file__) | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 21:36:00 -05:00
										 |  |  | class SMZ3PatchExtensions(APPatchExtension): | 
					
						
							|  |  |  |     game = "SMZ3" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def apply_basepatch(caller: APProcedurePatch, rom: bytes) -> bytes: | 
					
						
							|  |  |  |         basepatch = IPS_Patch.load(world_folder + "/data/zsm.ips") | 
					
						
							|  |  |  |         return basepatch.apply(rom) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SMZ3ProcedurePatch(APProcedurePatch, APTokenMixin): | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  |     hash = "3a177ba9879e3dd04fb623a219d175b2" | 
					
						
							|  |  |  |     game = "SMZ3" | 
					
						
							| 
									
										
										
										
											2022-04-03 19:42:18 +02:00
										 |  |  |     patch_file_ending = ".apsmz3" | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-29 21:36:00 -05:00
										 |  |  |     procedure = [ | 
					
						
							|  |  |  |         ("apply_basepatch", []), | 
					
						
							|  |  |  |         ("apply_tokens", ["token_data.bin"]), | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def write_tokens(self, patches): | 
					
						
							|  |  |  |         for addr, data in patches.items(): | 
					
						
							|  |  |  |             self.write_token(APTokenTypes.WRITE, addr, bytes(data)) | 
					
						
							|  |  |  |         self.write_file("token_data.bin", self.get_token_binary()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def get_source_data(cls) -> bytes: | 
					
						
							|  |  |  |         return get_base_rom_bytes() | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def get_base_rom_bytes() -> bytes: | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |     base_rom_bytes = getattr(get_base_rom_bytes, "base_rom_bytes", None) | 
					
						
							|  |  |  |     if not base_rom_bytes: | 
					
						
							|  |  |  |         sm_file_name = get_sm_base_rom_path() | 
					
						
							| 
									
										
										
										
											2022-09-30 00:36:30 +02:00
										 |  |  |         sm_base_rom_bytes = bytes(read_snes_rom(open(sm_file_name, "rb"))) | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         basemd5 = hashlib.md5() | 
					
						
							|  |  |  |         basemd5.update(sm_base_rom_bytes) | 
					
						
							| 
									
										
										
										
											2022-06-28 00:43:05 +00:00
										 |  |  |         if SMJUHASH != basemd5.hexdigest(): | 
					
						
							|  |  |  |             raise Exception('Supplied Base Rom does not match known MD5 for SM Japan+US release. ' | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |                             'Get the correct game and version, then dump it') | 
					
						
							|  |  |  |         lttp_file_name = get_lttp_base_rom_path() | 
					
						
							| 
									
										
										
										
											2022-09-30 00:36:30 +02:00
										 |  |  |         lttp_base_rom_bytes = bytes(read_snes_rom(open(lttp_file_name, "rb"))) | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         basemd5 = hashlib.md5() | 
					
						
							|  |  |  |         basemd5.update(lttp_base_rom_bytes) | 
					
						
							| 
									
										
										
										
											2022-06-28 00:43:05 +00:00
										 |  |  |         if LTTPJPN10HASH != basemd5.hexdigest(): | 
					
						
							|  |  |  |             raise Exception('Supplied Base Rom does not match known MD5 for LttP Japan(1.0) release. ' | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |                             'Get the correct game and version, then dump it') | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |         get_base_rom_bytes.base_rom_bytes = bytes(combine_smz3_rom(sm_base_rom_bytes, lttp_base_rom_bytes)) | 
					
						
							|  |  |  |     return get_base_rom_bytes.base_rom_bytes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | def get_sm_base_rom_path(file_name: str = "") -> str: | 
					
						
							|  |  |  |     options = Utils.get_options() | 
					
						
							|  |  |  |     if not file_name: | 
					
						
							|  |  |  |         file_name = options["sm_options"]["rom_file"] | 
					
						
							|  |  |  |     if not os.path.exists(file_name): | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         file_name = Utils.user_path(file_name) | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |     return file_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  | def get_lttp_base_rom_path(file_name: str = "") -> str: | 
					
						
							|  |  |  |     options = Utils.get_options() | 
					
						
							|  |  |  |     if not file_name: | 
					
						
							|  |  |  |         file_name = options["lttp_options"]["rom_file"] | 
					
						
							|  |  |  |     if not os.path.exists(file_name): | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |         file_name = Utils.user_path(file_name) | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |     return file_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 04:53:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def combine_smz3_rom(sm_rom: bytes, lttp_rom: bytes) -> bytearray: | 
					
						
							| 
									
										
										
										
											2022-03-15 08:55:57 -04:00
										 |  |  |     combined = bytearray(0x600000) | 
					
						
							|  |  |  |     # SM hi bank | 
					
						
							|  |  |  |     pos = 0 | 
					
						
							|  |  |  |     srcpos = 0 | 
					
						
							|  |  |  |     for i in range(0x40): | 
					
						
							|  |  |  |         combined[pos + 0x8000:pos + 0x8000 + 0x8000] = sm_rom[srcpos:srcpos + 0x8000] | 
					
						
							|  |  |  |         srcpos += 0x8000 | 
					
						
							|  |  |  |         pos += 0x10000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # SM lo bank | 
					
						
							|  |  |  |     pos = 0 | 
					
						
							|  |  |  |     for i in range(0x20): | 
					
						
							|  |  |  |         combined[pos:pos + 0x8000] = sm_rom[srcpos:srcpos + 0x8000] | 
					
						
							|  |  |  |         srcpos += 0x8000 | 
					
						
							|  |  |  |         pos += 0x10000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Z3 hi bank | 
					
						
							|  |  |  |     pos = 0x400000 | 
					
						
							|  |  |  |     srcpos = 0 | 
					
						
							|  |  |  |     for i in range(0x20): | 
					
						
							|  |  |  |         combined[pos + 0x8000:pos + 0x8000 + 0x8000] = lttp_rom[srcpos:srcpos + 0x8000] | 
					
						
							|  |  |  |         srcpos += 0x8000 | 
					
						
							|  |  |  |         pos += 0x10000 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return combined |