2019-12-08 22:43:49 +01:00
|
|
|
import struct
|
|
|
|
from typing import Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
class TwoDX:
|
|
|
|
"""
|
|
|
|
Packer/unpacker class for a bytestream representing a `.2dx` file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, data: Optional[bytes] = None) -> None:
|
|
|
|
self.__name: Optional[str] = None
|
|
|
|
self.__files: Dict[str, bytes] = {}
|
|
|
|
if data is not None:
|
|
|
|
self.__parse_file(data)
|
|
|
|
|
|
|
|
def __parse_file(self, data: bytes) -> None:
|
|
|
|
# Parse file header
|
2022-10-15 20:56:30 +02:00
|
|
|
(name, headerSize, numfiles) = struct.unpack("<16sII", data[0:24])
|
|
|
|
self.__name = name.split(b"\x00")[0].decode("ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if headerSize != (72 + (4 * numfiles)):
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unrecognized 2dx file header!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
fileoffsets = struct.unpack(
|
|
|
|
"<" + "".join(["I" for _ in range(numfiles)]),
|
|
|
|
data[72 : (72 + (4 * numfiles))],
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
fileno = 1
|
|
|
|
|
|
|
|
for offset in fileoffsets:
|
2022-10-15 20:56:30 +02:00
|
|
|
(
|
|
|
|
magic,
|
|
|
|
headerSize,
|
|
|
|
wavSize,
|
|
|
|
_,
|
|
|
|
track,
|
|
|
|
_,
|
|
|
|
attenuation,
|
|
|
|
loop,
|
|
|
|
) = struct.unpack(
|
|
|
|
"<4sIIhhhhi",
|
|
|
|
data[offset : (offset + 24)],
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if magic != b"2DX9":
|
|
|
|
raise Exception("Unrecognized entry in file!")
|
2019-12-08 22:43:49 +01:00
|
|
|
if headerSize != 24:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unrecognized subheader in file!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
wavOffset = offset + headerSize
|
2022-10-15 20:56:30 +02:00
|
|
|
wavData = data[wavOffset : (wavOffset + wavSize)]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
self.__files[f"{self.__name}_{fileno}.wav"] = wavData
|
2019-12-08 22:43:49 +01:00
|
|
|
fileno = fileno + 1
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2021-05-31 20:13:43 +02:00
|
|
|
if self.__name is None:
|
2024-01-02 03:46:24 +01:00
|
|
|
raise Exception("Logic error, tried to get name of 2dx file before setting it or parsing file!")
|
2019-12-08 22:43:49 +01:00
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def set_name(self, name: str) -> None:
|
|
|
|
if len(name) <= 16:
|
|
|
|
self.__name = name
|
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Name of archive too long!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def filenames(self) -> List[str]:
|
|
|
|
return [f for f in self.__files]
|
|
|
|
|
|
|
|
def read_file(self, filename: str) -> bytes:
|
|
|
|
return self.__files[filename]
|
|
|
|
|
|
|
|
def write_file(self, filename: str, data: bytes) -> None:
|
|
|
|
self.__files[filename] = data
|
|
|
|
|
|
|
|
def get_new_data(self) -> bytes:
|
|
|
|
if not self.__files:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("No files to write!")
|
2019-12-08 22:43:49 +01:00
|
|
|
if not self.__name:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("2dx archive name not set!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
name = self.__name.encode("ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
while len(name) < 16:
|
2022-10-15 20:56:30 +02:00
|
|
|
name = name + b"\x00"
|
2019-12-08 22:43:49 +01:00
|
|
|
filedata = [self.__files[x] for x in self.__files]
|
|
|
|
|
|
|
|
# Header length is also the base offset for the first file
|
|
|
|
baseoffset = 72 + (4 * len(filedata))
|
2022-10-15 20:56:30 +02:00
|
|
|
data = [struct.pack("<16sII", name, baseoffset, len(filedata)) + (b"\x00" * 48)]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Calculate offset this will go to
|
|
|
|
for bytedata in filedata:
|
|
|
|
# Add where this file will go, then calculate the length
|
2022-10-15 20:56:30 +02:00
|
|
|
data.append(struct.pack("<I", baseoffset))
|
2019-12-08 22:43:49 +01:00
|
|
|
baseoffset = baseoffset + 24 + len(bytedata)
|
|
|
|
|
|
|
|
# Now output the headers and files
|
|
|
|
for bytedata in filedata:
|
2022-10-15 20:56:30 +02:00
|
|
|
data.append(
|
|
|
|
struct.pack(
|
|
|
|
"<4sIIhhhhi",
|
|
|
|
b"2DX9",
|
|
|
|
24,
|
|
|
|
len(bytedata),
|
|
|
|
0x3231,
|
|
|
|
-1,
|
|
|
|
64,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
data.append(bytedata)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
return b"".join(data)
|