mzm/tools/extractor.py

90 lines
2.2 KiB
Python
Raw Normal View History

2022-09-24 15:42:57 +00:00
from array import array
from io import BufferedReader
import shutil
2022-09-25 19:05:36 +00:00
import os
2022-09-24 15:42:57 +00:00
2022-09-24 20:19:02 +00:00
DATA_PATH = "data/"
2022-09-24 15:42:57 +00:00
subDirs: array = [
2022-09-25 19:56:27 +00:00
"sprites/",
2022-12-24 16:07:17 +00:00
"hud/",
2023-01-28 14:42:10 +00:00
"weapons/",
2023-01-31 15:44:13 +00:00
"common/",
2023-02-01 13:50:25 +00:00
"intro/",
2023-03-08 23:00:51 +00:00
"animated_tiles/",
2023-03-08 01:05:24 +00:00
"close_up/",
2023-03-12 14:11:24 +00:00
"fusion_gallery/",
2023-05-28 18:28:09 +00:00
"ending/",
2023-05-28 20:10:26 +00:00
"chozodia_escape/",
2023-05-28 21:27:41 +00:00
"tilesets/",
"tilesets/animated_palettes",
2023-02-01 13:50:25 +00:00
"cutscenes/",
"cutscenes/KraidRising/",
"cutscenes/StoryText/",
"cutscenes/MotherBrainCloseUp/",
2023-02-04 17:27:37 +00:00
"cutscenes/RidleyInSpace/",
2023-02-03 17:52:02 +00:00
"cutscenes/RidleyLanding/",
"cutscenes/RidleySpawn/",
"cutscenes/MechaSeesSamus/",
"cutscenes/GettingFullyPoweredSuit/",
2023-02-06 21:56:29 +00:00
"cutscenes/BeforeCharlie/",
2023-02-07 09:39:28 +00:00
"cutscenes/StatueOpening/",
2023-03-14 13:21:20 +00:00
"cutscenes/EnterTourian/",
"cutscenes/SamusInBlueShip/",
2023-02-28 15:30:57 +00:00
"menus/",
2023-02-28 21:03:51 +00:00
"menus/TitleScreen/",
2023-05-25 16:39:25 +00:00
"menus/GameOver/",
2023-02-28 21:03:51 +00:00
"menus/EraseSram/",
"menus/PauseScreen/",
2023-03-14 12:42:20 +00:00
"rooms/",
"rooms/debug/",
2023-05-29 17:41:44 +00:00
"rooms/brinstar/",
"rooms/kraid/",
"rooms/norfair/",
"rooms/ridley/",
"rooms/tourian/",
"rooms/crateria/",
"rooms/chozodia/",
2023-03-18 22:42:03 +00:00
"samus/",
"samus/palettes/",
"samus/graphics/",
"samus/graphics/power_suit/",
"samus/graphics/full_suit/",
"samus/graphics/suitless/",
"samus/graphics/effects/",
2023-03-22 22:29:31 +00:00
"samus/graphics/arm_cannon/",
2022-09-24 15:42:57 +00:00
]
2022-09-25 19:05:36 +00:00
try:
shutil.rmtree(DATA_PATH, ignore_errors=False, onerror=None)
except:
pass
2022-09-24 15:42:57 +00:00
2022-09-25 19:05:36 +00:00
# Create directories
os.mkdir(DATA_PATH)
for dir in subDirs:
os.mkdir(DATA_PATH.__add__(dir))
2022-09-24 15:42:57 +00:00
2022-09-24 20:19:02 +00:00
rom: BufferedReader = open("mzm_us_baserom.gba", "rb")
db: BufferedReader = open("database.txt", "r")
2022-09-24 15:42:57 +00:00
line: str = db.readline()
while line != '':
2022-09-25 19:05:36 +00:00
# Formatted as follows : name;length;address;size
2022-09-24 20:19:02 +00:00
# The symbol # can be used as the first character of a line to make the extractor ignore it
if line[0] != '\n' and line[0] != '#':
info: array = line.split(";")
2022-09-25 19:05:36 +00:00
name: str = info[0]
print("Extracting", name)
2022-09-24 20:19:02 +00:00
rom.seek(int(info[2], 16))
2022-09-25 19:05:36 +00:00
size: int = int(info[3])
output: BufferedReader = open(DATA_PATH.__add__(name), "ab")
for x in range(0, int(info[1])):
output.write(int.from_bytes(rom.read(size), "little").to_bytes(size, "little"))
2022-09-24 20:19:02 +00:00
output.close()
2022-09-24 15:42:57 +00:00
line = db.readline()
rom.close()