-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.py
More file actions
208 lines (164 loc) · 8.63 KB
/
editor.py
File metadata and controls
208 lines (164 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#Class to parse and edit ads.inf file
class Editor:
def __read4Bytes(self, file):
return int.from_bytes(file.read(4), "little")
def __readString(self, file, ptr):
file.seek(ptr)
str = b''
ch = file.read(1)
while(ch not in [b'\x00', b'']):
str += ch
ch = file.read(1)
return str.decode('ASCII')
def __extract(self):
file = self.file
file.seek(12)
self.total_tracks = self.__read4Bytes(file)
playlists = []
unkData = {} #Each sone has some block of data; currently unkonwn to us
tracks_added = 0
while(tracks_added < self.total_tracks):
pointer = self.__read4Bytes(file)
trackCount = self.__read4Bytes(file)
playlists.append({'pointer': pointer, 'trackCount': trackCount})
tracks_added += trackCount
#Offset where pointers list of each track data starts
tracksInfoptrs_offset = playlists[0]['pointer']
self.isPAL = True if(file.tell() < tracksInfoptrs_offset) else False
#PAL version have 8 bytes of data with 1st 4 bytes storing pointer to the 1st unk data and later 0s
if(self.isPAL):
file.seek(8, 1)
tracksInfo_offset = self.__read4Bytes(file)
for playlist in playlists:
tracks = []
current_pointer = playlist['pointer']
for i in range(playlist['trackCount']):
file.seek(current_pointer)
basefilename_ptr = self.__read4Bytes(file)
filename_ptr = self.__read4Bytes(file)
trackname_ptr = self.__read4Bytes(file)
artistname_ptr = self.__read4Bytes(file)
unkData_ptr = self.__read4Bytes(file)
current_pointer += 20 #20 bytes for each song
basefilename = self.__readString(file, basefilename_ptr)
filename = self.__readString(file, filename_ptr)
trackname = self.__readString(file, trackname_ptr)
artistname = self.__readString(file, artistname_ptr)
unkData_size = 0
if(playlists.index(playlist) < len(playlists)-1):
file.seek(current_pointer+16) #we only want next song's data pointer, so skip 4*4 bytes
nextunkData_ptr = self.__read4Bytes(file)
unkData_size = nextunkData_ptr - unkData_ptr
else:
unkData_size = tracksInfo_offset - unkData_ptr
file.seek(unkData_ptr)
unkData[basefilename] = file.read(unkData_size)
tracks.append({
'basename': basefilename,
'filename': filename,
'trackname': trackname,
'artist': artistname
})
playlist.pop('pointer')
playlist['tracks'] = tracks
self.playlists = playlists
self.unkData = unkData
#Store unkData of 1st track to use as a unkData of newly added track for we currently don't know how to generate it.
self.temp_unkData = unkData[playlists[2]['tracks'][0]['basename']]
def __init__(self, filepath) -> None:
self.file = open(filepath, "rb")
if(self.__read4Bytes(self.file) != 0x5344414d):
raise Exception("Unsupported File Format")
self.__extract()
def assemble_and_save(self, filename):
with open(filename, "wb") as file:
file.write(bytearray([0x4d, 0x41, 0x44, 0x53]))
file.write(bytearray([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
file.write(self.total_tracks.to_bytes(4, byteorder="little"))
playlists = self.playlists
#Offset where list of all pointers to data of all tracks begins
tracksInfoptrs_offset = 16 + 8*len(playlists)
if(self.isPAL):
tracksInfoptrs_offset += 8
current_ptr = tracksInfoptrs_offset
for playlist in playlists:
trackCount = playlist['trackCount']
file.write( current_ptr.to_bytes(4, byteorder="little"))
file.write( trackCount.to_bytes(4, byteorder="little"))
current_ptr += 20*trackCount
tracksInfoptrs_blocksize = self.total_tracks*20
unkData_blocksize = 0
for playlist in playlists:
for track in playlist['tracks']:
basename = track['basename']
if(basename in self.unkData):
unkData_blocksize += len(self.unkData[basename])
else:
unkData_blocksize += len(self.temp_unkData)
tracksInfo_offset = tracksInfoptrs_offset + tracksInfoptrs_blocksize + unkData_blocksize
unkData_offset = tracksInfoptrs_offset + tracksInfoptrs_blocksize
#PAL exclusive
if(self.isPAL):
file.write((file.tell() + tracksInfoptrs_blocksize + 8).to_bytes(4, byteorder="little"))
file.write(bytearray([0x00, 0x00, 0x00, 0x00]))
'''
Writing trackdata pointers block.
In orginal file, if multiple track have same artists, artists name are not stored seperately
rather, other track points to the address where artist name is already localted.
So to emulate this, we maintain a dict of artists, to store whether their name have been already written or not,
if yes, we point to previous location of name, else we write the name in file and update the dict.
This goes for track's name too.
We write the info of tracks (name, artistname etc) at at the end of the file but
we need the pointers of those data while writing the tracksInfoptrs block,
so we first write everyting in a bytearray and
get the real location by adding the tracksInfo offset to the current length of the bytearray.
'''
artists = {}
tracks = {}
tracksInfo = b''
unkData_ptr = unkData_offset
for playlist in playlists:
for track in playlist['tracks']:
file.write((tracksInfo_offset + len(tracksInfo)).to_bytes(4, byteorder="little"))
tracksInfo += bytes(track['basename'], 'ascii') + b'\x00'
file.write((tracksInfo_offset + len(tracksInfo)).to_bytes(4, byteorder="little"))
tracksInfo += bytes(track['filename'], 'ascii') + b'\x00'
trackname = track['trackname']
if trackname not in tracks:
tracks[trackname] = tracksInfo_offset + len(tracksInfo)
tracksInfo += bytes(trackname, 'ascii') + b'\x00'
file.write(tracks[trackname].to_bytes(4, byteorder="little"))
artist = track['artist']
if artist not in artists:
artists[artist] = tracksInfo_offset + len(tracksInfo)
tracksInfo += bytes(artist, 'ascii') + b'\x00'
file.write(artists[artist].to_bytes(4, byteorder="little"))
file.write(unkData_ptr.to_bytes(4, byteorder="little"))
if track['basename'] in self.unkData:
unkData_ptr += len(self.unkData[track['basename']])
else:
unkData_ptr += len(self.temp_unkData)
for playlist in playlists:
for track in playlist['tracks']:
basename = track['basename']
if basename in self.unkData:
file.write(self.unkData[basename])
else:
file.write(self.temp_unkData)
file.write(tracksInfo)
def add_track(self, basename, filename, trackname, artist):
self.playlists[2]['tracks'].append({
'basename': basename,
'filename': filename,
'trackname': trackname,
'artist': artist
})
self.playlists[2]['trackCount'] += 1
self.total_tracks += 1
def remove_track(self, index):
basename = self.playlists[2]['tracks'][index]['basename']
del self.playlists[2]['tracks'][index]
if basename in self.unkData:
del self.unkData[basename]
self.total_tracks -= 1
self.playlists[2]['trackCount'] -= 1