Files
tp/tools/extract_game_assets.py
T

170 lines
4.7 KiB
Python
Raw Normal View History

2021-06-13 19:07:06 -04:00
import os
import sys
"""
Extracts the game assets and stores them in the game folder
Usage: `python tools/extract_game_assets.py`
"""
fstInfoPosition = 0x424
numFileEntries = 0
"""
Returns the offset address and size of fst.bin
"""
2021-12-02 23:38:37 +01:00
2021-06-13 19:07:06 -04:00
def getFstInfo(handler, fstOffsetPosition):
2021-12-02 23:38:37 +01:00
fstOffset = int.from_bytes(bytearray(handler.read(4)), byteorder="big")
handler.seek(
fstOffsetPosition + 4
) # Get the size which is 4 bytes after the offset
fstSize = int.from_bytes(bytearray(handler.read(4)), byteorder="big")
2021-06-13 19:07:06 -04:00
return fstOffset, fstSize
2021-12-02 23:38:37 +01:00
2021-06-13 19:07:06 -04:00
"""
Parses the fst.bin into a list of dictionaries containing
2021-06-13 19:47:12 -04:00
the file entry type, the file/folder name, the ISO file offset/parent file entry, the file size/last file entry
2021-06-13 19:07:06 -04:00
"""
2021-12-02 23:38:37 +01:00
2021-06-13 19:07:06 -04:00
def parseFstBin(fstBinBytes):
currentByte = 0
2021-12-02 23:38:37 +01:00
numFileEntries = int.from_bytes(
fstBinBytes[10:12], byteorder="big"
) # fst.bin offset
2021-06-13 19:07:06 -04:00
stringTableOffset = numFileEntries * 0xC
ret = []
2021-12-02 23:38:37 +01:00
while currentByte != (numFileEntries * 12):
2021-06-13 19:07:06 -04:00
currentByte += 12
# lazy
2021-12-02 23:38:37 +01:00
if currentByte == (numFileEntries * 12):
2021-06-13 19:07:06 -04:00
break
fileFolder = fstBinBytes[currentByte]
2021-12-02 23:38:37 +01:00
filenameOffset = int.from_bytes(
fstBinBytes[currentByte + 1 : currentByte + 4], byteorder="big"
)
fileOffsetOrParentEntryNum = int.from_bytes(
fstBinBytes[currentByte + 4 : currentByte + 8], byteorder="big"
)
fileSizeOrLastEntryNum = int.from_bytes(
fstBinBytes[currentByte + 8 : currentByte + 12], byteorder="big"
)
currentFilenameOffset = stringTableOffset + filenameOffset
2021-06-13 19:07:06 -04:00
# Figure out the filename by checking for null string terminator
i = 0
2021-12-02 23:38:37 +01:00
while fstBinBytes[currentFilenameOffset + i] != 0:
2021-06-13 19:07:06 -04:00
i += 1
2021-12-02 23:38:37 +01:00
fileName = (
fstBinBytes[currentFilenameOffset : currentFilenameOffset + i]
).decode()
2021-06-13 19:07:06 -04:00
if fileFolder == 0:
2021-12-02 23:38:37 +01:00
ret.append(
{
"type": "File",
"fileName": fileName,
"fileOffset": fileOffsetOrParentEntryNum,
"fileSize": fileSizeOrLastEntryNum,
}
)
2021-06-13 19:07:06 -04:00
else:
2021-12-02 23:38:37 +01:00
ret.append(
{
"type": "Folder",
"folderName": fileName,
"parentFolderEntryNumber": fileOffsetOrParentEntryNum,
"lastEntryNumber": fileSizeOrLastEntryNum,
}
)
2021-06-13 19:07:06 -04:00
return ret
2021-12-02 23:38:37 +01:00
2021-06-13 19:07:06 -04:00
"""
Write the current folder to disk and return it's name/last entry number
"""
2021-12-02 23:38:37 +01:00
def writeFolder(parsedFstBin, i):
folderPath = i["folderName"] + "/"
2021-06-13 19:07:06 -04:00
lastEntryNumber = i["lastEntryNumber"]
if i["parentFolderEntryNumber"] == 0:
if not os.path.exists(folderPath):
os.makedirs(folderPath)
else:
2021-12-02 23:38:37 +01:00
parentFolderEntry = parsedFstBin[i["parentFolderEntryNumber"] - 1]
2021-06-13 19:07:06 -04:00
while True:
folderPath = parentFolderEntry["folderName"] + "/" + folderPath
if parentFolderEntry["parentFolderEntryNumber"] == 0:
break
nextParentFolderEntryNumber = parentFolderEntry["parentFolderEntryNumber"]
2021-12-02 23:38:37 +01:00
parentFolderEntry = parsedFstBin[nextParentFolderEntryNumber - 1]
2021-06-13 19:07:06 -04:00
if not os.path.exists(folderPath):
os.makedirs(folderPath)
return folderPath, lastEntryNumber
2021-12-02 23:38:37 +01:00
2021-06-13 19:07:06 -04:00
"""
Use the parsed fst.bin contents to write assets to file
"""
2021-12-02 23:38:37 +01:00
2021-06-13 19:07:06 -04:00
def writeAssets(parsedFstBin, handler):
# Write the folder structure and files to disc
j = 0
folderStack = []
folderStack.append({"folderName": "./", "lastEntryNumber": numFileEntries})
for i in parsedFstBin:
j += 1
if i["type"] == "Folder":
2021-12-02 23:38:37 +01:00
currentFolder, lastEntryNumber = writeFolder(parsedFstBin, i)
folderStack.append(
{"folderName": currentFolder, "lastEntryNumber": lastEntryNumber}
)
2021-06-13 19:07:06 -04:00
else:
handler.seek(i["fileOffset"])
2021-12-02 23:38:37 +01:00
with open(
(folderStack[-1]["folderName"] + i["fileName"]), "wb"
) as currentFile:
2021-06-13 19:07:06 -04:00
currentFile.write(bytearray(handler.read(i["fileSize"])))
2021-12-02 23:38:37 +01:00
while folderStack[-1]["lastEntryNumber"] == j + 1:
2021-06-13 19:07:06 -04:00
folderStack.pop()
2021-12-02 23:38:37 +01:00
def extract(path):
with open(path, "rb") as f:
2021-06-13 19:07:06 -04:00
# Seek to fst offset information and retrieve it
f.seek(fstInfoPosition)
2021-12-02 23:38:37 +01:00
fstOffset, fstSize = getFstInfo(f, fstInfoPosition)
2021-06-13 19:07:06 -04:00
# Seek to fst.bin and retrieve it
f.seek(fstOffset)
fstBinBytes = bytearray(f.read(fstSize))
# Parse fst.bin
parsedFstBin = parseFstBin(fstBinBytes)
# Write assets to file
writeAssets(parsedFstBin, f)
2021-12-02 23:38:37 +01:00
def main():
extract(sys.argv[1], "rb")
2021-06-13 19:07:06 -04:00
if __name__ == "__main__":
2021-12-02 23:38:37 +01:00
main()