2019-12-08 22:43:49 +01:00
|
|
|
import copy
|
|
|
|
import struct
|
|
|
|
from typing import Dict, List, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
class IIDXSong:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
songid: int,
|
|
|
|
title: str,
|
|
|
|
english_title: str,
|
|
|
|
genre: str,
|
|
|
|
artist: str,
|
|
|
|
difficulties: List[int],
|
|
|
|
folder: int,
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Initialize a IIDX Song. Everything is self-explanatory except difficulties, which
|
|
|
|
is a list of integers representing the difficulty for SPN, SPH, SPA, DPN, DPH, DPA.
|
2023-07-29 20:41:14 +02:00
|
|
|
For IIDX 27 and above, there are 4 additional charts in the difficulties list for
|
|
|
|
B7, L7, B14 and L14.
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
self.id = songid
|
|
|
|
self.title = title
|
|
|
|
self.english_title = english_title
|
|
|
|
self.genre = genre
|
|
|
|
self.artist = artist
|
|
|
|
self.difficulties = difficulties
|
|
|
|
self.folder = folder
|
|
|
|
|
|
|
|
|
|
|
|
class IIDXMusicDB:
|
|
|
|
def __init__(self, data: bytes) -> None:
|
|
|
|
self.__songs: Dict[int, Tuple[IIDXSong, int]] = {}
|
|
|
|
self.__data = data
|
2023-07-29 20:41:14 +02:00
|
|
|
self.__version = self.__parse_db(data)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
def get_new_db(self) -> bytes:
|
|
|
|
# Write out a new music DB based on any possible changes to songs
|
|
|
|
data = copy.deepcopy(self.__data)
|
|
|
|
|
|
|
|
def format_string(string: str) -> bytes:
|
2022-10-15 20:56:30 +02:00
|
|
|
bdata = string.encode("shift-jis")
|
2019-12-08 22:43:49 +01:00
|
|
|
if len(bdata) < 64:
|
2022-10-15 20:56:30 +02:00
|
|
|
bdata = bdata + (b"\0" * (64 - len(bdata)))
|
2019-12-08 22:43:49 +01:00
|
|
|
return bdata
|
|
|
|
|
|
|
|
def copy_over(dst: bytes, src: bytes, base: int, offset: int) -> bytes:
|
2022-10-15 20:56:30 +02:00
|
|
|
return dst[: (base + offset)] + src + dst[(base + offset + len(src)) :]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
for mid in self.__songs:
|
|
|
|
song, offset = self.__songs[mid]
|
|
|
|
data = copy_over(data, format_string(song.title), offset, 0)
|
|
|
|
data = copy_over(data, format_string(song.english_title), offset, 64)
|
|
|
|
data = copy_over(data, format_string(song.genre), offset, 128)
|
|
|
|
data = copy_over(data, format_string(song.artist), offset, 192)
|
|
|
|
data = copy_over(data, bytes([song.folder]), offset, 280)
|
2023-07-29 20:41:14 +02:00
|
|
|
if self.__version < 27:
|
|
|
|
# This is easy.
|
|
|
|
data = copy_over(data, bytes(song.difficulties), offset, 288)
|
|
|
|
elif self.__version >= 27:
|
|
|
|
# This is gross, but I'm too lazy to do it right.
|
|
|
|
data = copy_over(data, bytes([song.difficulties[6]]), offset, 288)
|
|
|
|
data = copy_over(data, bytes(song.difficulties[0:3]), offset, 289)
|
|
|
|
data = copy_over(data, bytes(song.difficulties[7:9]), offset, 292)
|
|
|
|
data = copy_over(data, bytes(song.difficulties[3:6]), offset, 294)
|
|
|
|
data = copy_over(data, bytes([song.difficulties[9]]), offset, 297)
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
return data
|
|
|
|
|
2023-07-29 20:41:14 +02:00
|
|
|
def __parse_string(self, string: bytes) -> str:
|
|
|
|
for i in range(len(string)):
|
|
|
|
if string[i] == 0:
|
|
|
|
string = string[:i]
|
|
|
|
break
|
|
|
|
|
|
|
|
return string.decode("shift-jis")
|
|
|
|
|
|
|
|
def __parse_db(self, data: bytes) -> int:
|
2019-12-08 22:43:49 +01:00
|
|
|
# Verify the signature
|
2023-07-29 20:41:14 +02:00
|
|
|
magic, gameversion, songcount, indexcount = struct.unpack_from(
|
|
|
|
"<4sBxxxHHxxxx",
|
2019-12-08 22:43:49 +01:00
|
|
|
data,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
|
2023-07-29 20:41:14 +02:00
|
|
|
if magic != b"IIDX":
|
|
|
|
raise Exception(f"Invalid signature '{magic}' found!")
|
|
|
|
|
|
|
|
# Stride lookup, which appears unfortunately hardcoded in the game DLL.
|
|
|
|
leap = {
|
|
|
|
20: 0x320,
|
|
|
|
21: 0x320,
|
|
|
|
22: 0x340,
|
|
|
|
23: 0x340,
|
|
|
|
24: 0x340,
|
|
|
|
25: 0x340,
|
|
|
|
26: 0x344,
|
|
|
|
27: 0x52C,
|
|
|
|
28: 0x52C,
|
|
|
|
}.get(gameversion)
|
|
|
|
if leap is None:
|
|
|
|
raise Exception(f"Unsupported game version {gameversion} found!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2023-07-29 20:41:14 +02:00
|
|
|
# Skip past index nodes, which are all 16-bit integers, and past 16 byte header.
|
|
|
|
offset = (indexcount * 2) + 0x10
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Load songs
|
2023-07-29 20:41:14 +02:00
|
|
|
for songid in range(songcount):
|
|
|
|
songoffset = offset + (songid * leap)
|
|
|
|
if gameversion < 27:
|
2019-12-08 22:43:49 +01:00
|
|
|
songdata = struct.unpack_from(
|
|
|
|
"<64s64s64s64s24xB7xBBBBBB162xH",
|
|
|
|
data,
|
2023-07-29 20:41:14 +02:00
|
|
|
songoffset,
|
|
|
|
)
|
|
|
|
song = IIDXSong(
|
|
|
|
songid=songdata[11],
|
|
|
|
title=self.__parse_string(songdata[0]),
|
|
|
|
english_title=self.__parse_string(songdata[1]),
|
|
|
|
genre=self.__parse_string(songdata[2]),
|
|
|
|
artist=self.__parse_string(songdata[3]),
|
|
|
|
difficulties=[
|
|
|
|
songdata[5],
|
|
|
|
songdata[6],
|
|
|
|
songdata[7],
|
|
|
|
songdata[8],
|
|
|
|
songdata[9],
|
|
|
|
songdata[10],
|
|
|
|
],
|
|
|
|
folder=songdata[4],
|
|
|
|
)
|
|
|
|
elif gameversion >= 27:
|
|
|
|
# Heroic Verse and above have a completely different structure for song entries
|
|
|
|
songdata = struct.unpack_from(
|
|
|
|
"<64s64s64s64s24xB7x10B646xH",
|
|
|
|
data,
|
|
|
|
songoffset,
|
|
|
|
)
|
|
|
|
song = IIDXSong(
|
|
|
|
songid=songdata[15],
|
|
|
|
title=self.__parse_string(songdata[0]),
|
|
|
|
english_title=self.__parse_string(songdata[1]),
|
|
|
|
genre=self.__parse_string(songdata[2]),
|
|
|
|
artist=self.__parse_string(songdata[3]),
|
|
|
|
difficulties=[
|
|
|
|
songdata[6],
|
|
|
|
songdata[7],
|
|
|
|
songdata[8],
|
|
|
|
songdata[11],
|
|
|
|
songdata[12],
|
|
|
|
songdata[13],
|
|
|
|
songdata[5],
|
|
|
|
songdata[9],
|
|
|
|
songdata[10],
|
|
|
|
songdata[14],
|
|
|
|
],
|
|
|
|
folder=songdata[4],
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if song.artist == "event_data" and song.genre == "event_data":
|
2019-12-08 22:43:49 +01:00
|
|
|
continue
|
2023-07-29 20:41:14 +02:00
|
|
|
|
|
|
|
self.__songs[song.id] = (song, songoffset)
|
|
|
|
|
|
|
|
return gameversion
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def songs(self) -> List[IIDXSong]:
|
2022-10-15 20:56:30 +02:00
|
|
|
return sorted(
|
|
|
|
[self.__songs[mid][0] for mid in self.__songs], key=lambda song: song.id
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def songids(self) -> List[int]:
|
|
|
|
return sorted([mid for mid in self.__songs])
|
|
|
|
|
|
|
|
def song(self, songid: int) -> IIDXSong:
|
|
|
|
return self.__songs[songid][0]
|