0HMyC / p3cpk

A Python script that allows for extracting and packing Persona 3's CPK files.
MIT License
2 stars 0 forks source link

Account for missing .bin header file [Enhancement] #3

Closed 0HMyC closed 1 year ago

0HMyC commented 1 year ago

Currently, the pack.py script doesn't account for the scenario of someone attempting to pack files into a CPK without also providing a .bin file containing the necessary header data for the file in the Headers directory.

(The relevant part of that script:)

cFile = os.path.join(wDir, fil)
with open(os.path.join(whDir, fil + '.bin'), "rb") as cHed:
    #File headers will always be 0xEE bytes long
    #if we have one that isn't... too bad!
    cpkBytes += cHed.read(0xEE)
#append actual file size to header (as LE)
cFileSize = os.path.getsize(cFile)
cpkBytes += struct.pack('<I', cFileSize)

The script always assumes that the needed header file exists, and as such attempting to read data from that non-existent file causes an exception that isn't as immediately helpful as a plain-english error.

While it would be possible to make the script force generate a blank header as needed (which would allow the file to pack,) that's a terrible idea because then the resulting .CPK just wouldn't work at all in-game most likely.

Therefore, I feel that the best solution is to simply add a check for whether the file exists, print a simple error message telling the user they need to have a header file at the location the script is attempting to load from, and stop the script early. This prevents potentially faulty output files while making things simpler and easier for the average user with practically no compromise.

0HMyC commented 1 year ago

Nevermind, I was just a sleep deprived dumbass who missed this part of the script:

headersDir = os.path.join(input, "Headers")
    if os.path.isdir(filesDir):
        if os.path.isdir(headersDir):
            print("Packing", input, "into CPK file...")
            #pack files into cpk
            cpkData = packFiles(filesDir, headersDir)
            #write file data to disk
            with open(outputFolder, "wb") as cpkOut:
                cpkOut.write(cpkData)
        else:
            #TODO: Add ability to generate generic header, if that's even a good idea
            print("Warning! Could not locate Headers directory in", headersDir + "! Folder will not be packed into CPK!")

Meaning the script has already been doing this, making this issue completely pointless. Oops.