import os
import struct

class Resource:
    def __init__(self, seq, attribute, offset, size, filename):
        self.seq = seq
        self.attribute = attribute
        self.offset = offset
        self.size = size
        self.filename = filename

ext_to_attr = {"MP3": 0, "WAV": 1, "ADPCM": 2}

def create_playback_dat(audio_dir):

    resources = []

    files = [f for f in os.listdir(audio_dir) if f.split(".")[-1].upper() in ext_to_attr]

    offset = (len(files) * 16)

    for filename in files:

        ext = filename.split(".")[-1].upper()

        seq = int(filename[:2])

        size = os.path.getsize(os.path.join(audio_dir, filename))

        resources.append(Resource(seq, ext_to_attr[ext], offset, size, filename))

        offset += (((size + 1023) // 1024) * 1024)


    with open(audio_dir + "/playback.dat", "wb") as f:

        for res in resources:
            f.write(struct.pack("IIII", res.seq, res.attribute, res.offset, res.size))

        for res in resources:

            with open(os.path.join(audio_dir, res.filename), "rb") as audiofile:

                f.seek(res.offset)

                content = audiofile.read()
                f.write(content)

                padding = b'\x00' * ((1024 - len(content) % 1024) % 1024)
                f.write(padding)



create_playback_dat(".\wav")