-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_assets.py
299 lines (270 loc) · 11.4 KB
/
load_assets.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Loads a .assets file (or multiple .assets.split* files) and parses them at given offsets.
import argparse
import struct
import json
from helper import *
import beatmapDifficultyData_to_from_UABE as difficultyData
import beatmapLevelData_to_from_UABE as levelData
import audioClip_to_from_UABE as audioClip
import levelCollection_to_from_UABE as levelCollection
import levelPack_to_from_UABE as levelPack
import texture2D_to_from_UABE as texture2d
# This audio clip has fileID = 0, pathID = 28
memOffsetForAudioClip = 0x00684a38
# This audio clip has fileID = 0, pathID = 29
memOffsetForAudioClip2 = 0x00684a98
# d = "../UABE Dumps/sharedassets17.assets"
# outp = "../UABE Dumps/sharedassets17.assets.json"
def readHeader(fs):
o = {}
o['MetadataSize'] = readUInt32(fs, '>')
o['FileSize'] = readUInt32(fs, '>')
o['Generation'] = readUInt32(fs, '>')
o['DataOffset'] = readUInt32(fs, '>')
o['IsBigEndian'] = readUInt32(fs, '>')
return o
def writeHeader(fs, o):
writeUInt32(fs, o['MetadataSize'], '>')
writeUInt32(fs, o['FileSize'], '>')
writeUInt32(fs, o['Generation'], '>')
writeUInt32(fs, o['DataOffset'], '>')
writeUInt32(fs, o['IsBigEndian'], '>')
def readType(fs):
o = {}
o["ClassID"] = readUInt32(fs)
o["IsStripped"] = readUInt8(fs)
o["ScriptTypeIndex"] = readInt16(fs)
if o['ClassID'] == 114:
o['ScriptID'] = readHex(fs)
o['TypeHash'] = readHex(fs)
return o
def writeType(fs, o):
writeUInt32(fs, o["ClassID"])
writeUInt8(fs, o["IsStripped"])
writeInt16(fs, o["ScriptTypeIndex"])
if o['ClassID'] == 114:
writeHex(fs, o['ScriptID'])
writeHex(fs, o['TypeHash'])
def readTypes(fs, count):
o = {}
# readAlign(fs, fs.tell())
for i in range(count):
o[i] = readType(fs)
return o
def writeTypes(fs, o):
for item in o.keys():
writeType(fs, o[item])
def readObjects(fs, count, types):
a = []
for _ in range(count):
o = {}
readAlign(fs, fs.tell())
o['PathID'] = readUInt64(fs)
o['Offset'] = readUInt32(fs)
o['ByteSize'] = readUInt32(fs)
o['TypeID'] = readUInt32(fs)
o['ClassID'] = types[o['TypeID']]['ClassID']
a.append(o)
return a
def writeObjects(fs, a):
for o in a:
writeAlign(fs, fs.tell())
writeUInt64(fs, o['PathID'])
writeUInt32(fs, o['Offset'])
writeUInt32(fs, o['ByteSize'])
writeUInt32(fs, o['TypeID'])
def readScripts(fs, count):
a = []
for _ in range(count):
o = {}
o['FileIndex'] = readUInt32(fs)
readAlign(fs, fs.tell())
o['LocalID'] = readUInt64(fs)
a.append(o)
return a
def writeScripts(fs, a):
for o in a:
writeUInt32(fs, o['FileIndex'])
writeAlign(fs, fs.tell())
writeUInt64(fs, o['LocalID'])
def readExternals(fs, count):
a = []
for _ in range(count):
o = {}
o['Temp'] = readCString(fs, False)
o['GUID'] = readHex(fs)
o['Type'] = readUInt32(fs)
o['PathName'] = readCString(fs, False)
read(fs, 1)
a.append(o)
return a
def writeExternals(fs, a):
for o in a:
writeCString(fs, o['Temp'], False)
writeHex(fs, o['GUID'])
writeUInt32(fs, o['Type'])
writeCString(fs, o['PathName'], False)
def readMetadata(fs):
o = {}
o['UnityVersion'] = readCString(fs)
o['TargetPlatform'] = readUInt32(fs)
o['EnableTypeTree'] = readUInt8(fs)
# readAlign(fs, fs.tell())
o['TypeCount'] = readUInt32(fs)
o['Types'] = readTypes(fs, o['TypeCount'])
o['ObjectCount'] = readUInt32(fs)
o['Objects'] = readObjects(fs, o['ObjectCount'], o['Types'])
o['ScriptCount'] = readUInt32(fs)
o['Scripts'] = readScripts(fs, o['ScriptCount'])
o['ExternalsCount'] = readUInt32(fs)
o['Externals'] = readExternals(fs, o['ExternalsCount'])
return o
def writeMetadata(fs, o):
writeCString(fs, o['UnityVersion'])
writeUInt32(fs, o['TargetPlatform'])
writeUInt8(fs, o['EnableTypeTree'])
writeUInt32(fs, o['TypeCount'])
writeTypes(fs, o['Types'])
writeUInt32(fs, o['ObjectCount'])
writeObjects(fs, o['Objects'])
writeUInt32(fs, o['ScriptCount'])
writeScripts(fs, o['Scripts'])
writeUInt32(fs, o['ExternalsCount'])
writeExternals(fs, o['Externals'])
def readBehaviour(fs):
o = {}
o['GameObject'] = readPtr(fs)
o['Enabled'] = readUInt32(fs)
o['MonoScript'] = readPtr(fs)
# o['ScriptExportID'] = readUInt32(fs)
# o['Kind'] = readUInt64(fs)
o['Name'] = readAlignedString(fs)
if o['MonoScript']['PathID'] == 644:
print("Attempting to read beatmap level...")
o = levelData.readBeatmapLevel(fs, o)
elif o['MonoScript']['PathID'] == 1552:
print("Attempting to read Beatmap Data...")
o = difficultyData.readMonoBehaviour(fs, o)
elif o['MonoScript']['PathID'] == 762:
print("Attempting to read BeatmapLevel Collection...")
o = levelCollection.readMonoBehaviour(fs, o)
elif o['MonoScript']['PathID'] == 1480:
print("Attempting to read levelPack...")
o = levelPack.readMonoBehaviour(fs, o)
return o
def writeBehaviour(fs, o):
if o['MonoScript']['PathID'] == 644:
print("Attempting to write beatmap level...")
levelData.writeBeatmapLevel(fs, o)
return True
elif o['MonoScript']['PathID'] == 1552:
print("Attempting to write Beatmap Data...")
difficultyData.writeMonoBehaviour(fs, o)
return True
elif o['MonoScript']['PathID'] == 762:
print("Attempting to write BeatmapLevel Collection...")
levelCollection.writeMonoBehaviour(fs, o)
return True
elif o['MonoScript']['PathID'] == 1480:
print("Attempting to write levelPack...")
levelPack.writeMonoBehaviour(fs, o)
return True
else:
# writePtr(fs, o['GameObject'])
# writeUInt32(fs, o['Enabled'])
# writePtr(fs, o['MonoScript'])
# writeAlignedString(fs, o['Name'])
return False
def readAsset(fs):
o = {}
o['Header'] = readHeader(fs)
o['Metadata'] = readMetadata(fs)
print("Current: " + str(fs.tell()) + " Metadata end: " + str(o['Header']['MetadataSize'] + 20))
print("Going to data offset: " + str(o['Header']['DataOffset']))
fs.seek(o['Header']['DataOffset'])
o['DataLength'] = o['Header']['FileSize'] - o['Header']['DataOffset']
o['Objects'] = []
for obj in o['Metadata']['Objects']:
fs.seek(o['Header']['DataOffset'] + obj['Offset'])
dat = {}
if obj['ClassID'] == 114:
dat = readBehaviour(fs)
print("Interpretting data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as MonoBehaviour")
elif obj['ClassID'] == 83:
dat = audioClip.readAudioClip(fs)
print("Interpretting data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as AudioClip")
elif obj['ClassID'] == 28:
print("Interpretting data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as Texture2D")
dat = texture2d.readTexture2D(fs)
dat['Offset'] = obj['Offset']
dat['PathID'] = obj['PathID']
dat['ClassID'] = obj['ClassID']
dat['ByteSize'] = obj['ByteSize']
o['Objects'].append(dat)
return o
def writeMiddleData(fs, fr, offset, length, readOffset=0):
fr.seek(offset - readOffset)
fs.write(fr.read(length))
def copyData(fs, fr, offset, length, obj):
if 'ReadOffset' in obj.keys():
writeMiddleData(fs, fr, offset, length, obj['ReadOffset'])
else:
writeMiddleData(fs, fr, offset, length)
def writeAsset(fs, fr, o):
writeHeader(fs, o['Header'])
writeMetadata(fs, o['Metadata'])
# write0(fs, o['Header']['DataOffset'] - fs.tell())
writeMiddleData(fs, fr, fs.tell(), o['Header']['DataOffset'] - fs.tell())
objects = sorted(o['Objects'], key=lambda ob: ob['Offset'])
for i in range(len(objects)):
obj = objects[i]
# Need to somehow do proper seeking (write the other info too) but don't just fill it with 0s
# We want to write the old data in order of increasing offset (hopefully this works?)
if i == 0:
print("Writing skip data: " + str(o['Header']['DataOffset']) + " with length: " + str(obj['Offset']))
copyData(fs, fr, o['Header']['DataOffset'], obj['Offset'], obj)
else:
if objects[i]['Offset'] < objects[i-1]['Offset'] + objects[i-1]['ByteSize']:
print(str(objects[i-1]))
print("Writing skip data: " + str(o['Header']['DataOffset'] + objects[i-1]['Offset'] + objects[i-1]['ByteSize']) + " with length: " + str(obj['Offset'] - objects[i-1]['Offset'] - objects[i-1]['ByteSize']))
copyData(fs, fr, o['Header']['DataOffset'] + objects[i-1]['Offset'] + objects[i-1]['ByteSize'], obj['Offset'] - objects[i-1]['Offset'] - objects[i-1]['ByteSize'], obj)
if obj['ClassID'] == 114:
print("Writing data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as MonoBehaviour with size: " + str(obj['ByteSize']))
if not writeBehaviour(fs, obj):
print("Writing data at: " + str(obj['ByteSize'] + obj['Offset'] + o['Header']['DataOffset']) + " as unknown MonoBehaviour with size: " + str(obj['ByteSize']))
copyData(fs, fr, fs.tell(), obj['ByteSize'], obj)
else:
end = obj['ByteSize'] + obj['Offset'] + o['Header']['DataOffset']
# print("Expected end: " + str(end) + " actual end: " + str(fs.tell()))
if end - fs.tell() > 4:
print("Delta expected detected! delta: " + str(end - fs.tell()) + " obj ByteSize: " + str(obj['ByteSize']))
copyData(fs, fr, fs.tell(), obj['ByteSize'] + obj['Offset'] + o['Header']['DataOffset'] - fs.tell(), obj)
elif obj['ClassID'] == 83:
print("Writing data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as AudioClip with size: " + str(obj['ByteSize']))
audioClip.writeAudioClip(fs, obj)
elif obj['ClassID'] == 28:
print("Writing data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as Texture2D with size: " + str(obj['ByteSize']))
texture2d.writeTexture2D(fs, obj)
else:
print("Writing data at: " + str(o['Header']['DataOffset'] + obj['Offset']) + " as unknown raw copy with size: " + str(obj['ByteSize']))
copyData(fs, fr, obj['Offset'] + o['Header']['DataOffset'], obj['ByteSize'], obj)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A script for loading and writing .assets files into legible json.")
parser.add_argument("path", type=str, help="The path to a packaged (full) .assets file.")
parser.add_argument("output", type=str, help="The path to the resulting .json file")
parser.add_argument("--load", help="Load the .json and overwrite the .assets at this path.")
args = parser.parse_args()
if args.load:
o = deserialize(args.output)
print("Deserialized!")
print("Writing...")
with open(args.load, 'wb') as fs:
with open(args.path, 'rb') as fr:
writeAsset(fs, fr, o)
print("Wrote asset to: " + args.load)
else:
with open(args.path, 'rb') as fs:
print("Reading...")
o = readAsset(fs)
serialize(o, args.output)
print("Serialized to: " + args.output)