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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
import os
import sys
import shutil
import extract_game_assets
from pathlib import Path
import libyaz0
import libarc
import libstage
from datetime import datetime
import libbti
import assets_config
def getMaxDateFromDir(path):
maxTime = 0
for root, dirs, files in os.walk(str(path)):
for file in files:
time = os.path.getmtime(Path(root + "/" + file))
if time > maxTime:
maxTime = time
return maxTime
convertDefinitions = {
"arc": {
"destExtension": ".arc",
"convertFunction": libarc.convert_dir_to_arc,
"exceptions": ["game/files/res/Object/HomeBtn.c.arc/archive/dat/speakerse.arc"],
"sourceIsDir": True
},
"dzs.json": {
"destExtension": ".dzs",
"convertFunction": libstage.package_from_json,
},
"dzr.json": {
"destExtension": ".dzr",
"convertFunction": libstage.package_from_json,
},
"png": {
"destExtension": ".bti",
"convertFunction": libbti.png_to_bti
},
"bti.json": {
"destExtension": None,
"convertFunction": None
}
}
yaz0CompressFunction = libyaz0.compress
def convertEntry(file, path, destPath, returnData):
split = str(file).split(".")
mustBeCompressed = False
destFileName = str(file)
if len(split)>1 and split[1] == "c":
destFileName = ".".join([split[0]] + split[2:])
mustBeCompressed = True
# print(destFileName)
sourceExtension = ".".join(destFileName.split(".")[1:])
data = None
extractDef = None
if sourceExtension in convertDefinitions:
extractDef = convertDefinitions[sourceExtension]
if extractDef["destExtension"] == None and extractDef["convertFunction"] == None:
extractDef = None
elif "exceptions" in extractDef and str(path/file) in extractDef["exceptions"]:
extractDef = None
elif "sourceIsDir" in extractDef and extractDef["sourceIsDir"] == True and os.path.isfile(path/file):
extractDef = None
if extractDef != None:
destFileName = destFileName.split(".")[0] + extractDef["destExtension"]
targetTime = 0
if destPath != None and os.path.exists(destPath / destFileName):
targetTime = os.path.getmtime(destPath / destFileName)
sourceTime = 0
if targetTime != 0:
if os.path.isdir(path / file):
sourceTime = getMaxDateFromDir(path / file)
else:
sourceTime = os.path.getmtime(path / file)
if returnData == False and sourceTime < targetTime:
return destFileName
if extractDef != None:
data = extractDef["convertFunction"](path / file, convertEntry)
if mustBeCompressed == True:
if data == None:
data = open(path / file, "rb").read()
data = yaz0CompressFunction(data)
if returnData == True:
if data == None and returnData == True:
data = open(path / file, "rb").read()
return destFileName, data
else:
print(str(path / file) + " -> " + str(destPath / destFileName))
if data != None:
open(destPath / destFileName, "wb").write(data)
else:
shutil.copy(path / file, destPath / destFileName)
return destFileName
def copy(path, destPath):
for file in os.listdir(path):
split = os.path.splitext(file)
if len(split[1]) == 0 and os.path.isdir(path / file):
# is a standard directory
if not Path(destPath / file).exists():
os.mkdir(destPath / file)
copy(path / file, destPath / file)
else:
# either a file or directory that needs to be converted
convertEntry(file, path, destPath, False)
aMemRels = """d_a_alldie.rel
d_a_andsw2.rel
d_a_bd.rel
d_a_canoe.rel
d_a_cstaF.rel
d_a_demo_item.rel
d_a_door_bossL1.rel
d_a_econt.rel
d_a_e_dn.rel
d_a_e_fm.rel
d_a_e_ga.rel
d_a_e_hb.rel
d_a_e_nest.rel
d_a_e_rd.rel
d_a_fr.rel
d_a_grass.rel
d_a_kytag05.rel
d_a_kytag10.rel
d_a_kytag11.rel
d_a_kytag14.rel
d_a_mg_fish.rel
d_a_npc_besu.rel
d_a_npc_fairy_seirei.rel
d_a_npc_fish.rel
d_a_npc_henna.rel
d_a_npc_kakashi.rel
d_a_npc_kkri.rel
d_a_npc_kolin.rel
d_a_npc_maro.rel
d_a_npc_taro.rel
d_a_npc_tkj.rel
d_a_obj_bhashi.rel
d_a_obj_bkdoor.rel
d_a_obj_bosswarp.rel
d_a_obj_cboard.rel
d_a_obj_digplace.rel
d_a_obj_eff.rel
d_a_obj_fmobj.rel
d_a_obj_gpTaru.rel
d_a_obj_hhashi.rel
d_a_obj_kanban2.rel
d_a_obj_kbacket.rel
d_a_obj_kgate.rel
d_a_obj_klift00.rel
d_a_obj_ktOnFire.rel
d_a_obj_ladder.rel
d_a_obj_lv2Candle.rel
d_a_obj_magne_arm.rel
d_a_obj_metalbox.rel
d_a_obj_mgate.rel
d_a_obj_nameplate.rel
d_a_obj_ornament_cloth.rel
d_a_obj_rope_bridge.rel
d_a_obj_stick.rel
d_a_obj_stoneMark.rel
d_a_obj_sWallShutter.rel
d_a_obj_swpropeller.rel
d_a_obj_swpush5.rel
d_a_obj_yobikusa.rel
d_a_scene_exit2.rel
d_a_shop_item.rel
d_a_sq.rel
d_a_swc00.rel
d_a_tag_ajnot.rel
d_a_tag_attack_item.rel
d_a_tag_CstaSw.rel
d_a_tag_gstart.rel
d_a_tag_hinit.rel
d_a_tag_hjump.rel
d_a_tag_hstop.rel
d_a_tag_lv2prchk.rel
d_a_tag_magne.rel
d_a_tag_mhint.rel
d_a_tag_mstop.rel
d_a_tag_spring.rel
d_a_tag_statue_evt.rel
d_a_ykgr.rel"""
mMemRels = """d_a_andsw.rel
d_a_arrow.rel
d_a_bg.rel
d_a_bg_obj.rel
d_a_boomerang.rel
d_a_crod.rel
d_a_demo00.rel
d_a_disappear.rel
d_a_dmidna.rel
d_a_door_dbdoor00.rel
d_a_door_knob00.rel
d_a_door_shutter.rel
d_a_door_spiral.rel
d_a_dshutter.rel
d_a_ep.rel
d_a_hitobj.rel
d_a_kytag00.rel
d_a_kytag04.rel
d_a_kytag17.rel
d_a_mg_rod.rel
d_a_midna.rel
d_a_nbomb.rel
d_a_obj_brakeeff.rel
d_a_obj_burnbox.rel
d_a_obj_carry.rel
d_a_obj_ito.rel
d_a_obj_life_container.rel
d_a_obj_movebox.rel
d_a_obj_swpush.rel
d_a_obj_timer.rel
d_a_obj_yousei.rel
d_a_path_line.rel
d_a_scene_exit.rel
d_a_set_bgobj.rel
d_a_spinner.rel
d_a_suspend.rel
d_a_swhit0.rel
d_a_tag_allmato.rel
d_a_tag_attention.rel
d_a_tag_camera.rel
d_a_tag_chkpoint.rel
d_a_tag_event.rel
d_a_tag_evt.rel
d_a_tag_evtarea.rel
d_a_tag_evtmsg.rel
d_a_tag_howl.rel
d_a_tag_kmsg.rel
d_a_tag_lantern.rel
d_a_tag_mist.rel
d_a_tag_msg.rel
d_a_tag_push.rel
d_a_tag_telop.rel
d_a_tbox.rel
d_a_tbox2.rel
d_a_vrbox.rel
d_a_vrbox2.rel
f_pc_profile_lst.rel"""
def copyRelFiles(gamePath, buildPath, aMemList, mMemList):
relArcPaths = []
for root, dirs, files in os.walk(str(buildPath / "dolzel2/rel")):
for file in files:
if file.find(".rel") != -1:
relArcFound = False
for rel in aMemList:
if rel == file:
relArcFound = True
for rel in mMemList:
if rel == file:
relArcFound = True
fullPath = Path(root + "/" + file)
if relArcFound == False:
print(
str(fullPath)
+ " -> "
+ str(buildPath / "dolzel2/game/files/rel/Final/Release" / file)
)
relSource = open(fullPath, "rb")
data = relSource.read()
relSource.close()
data = yaz0CompressFunction(data)
relNew = open(
buildPath / "dolzel2/game/files/rel/Final/Release" / file, "wb"
)
relNew.write(data)
relNew.truncate()
relNew.close()
else:
relArcPaths.append(fullPath)
if os.path.exists(buildPath / "RELS.arc") == False:
os.mkdir(buildPath / "RELS.arc")
os.mkdir(buildPath / "RELS.arc/rels")
os.mkdir(buildPath / "RELS.arc/rels/mmem")
os.mkdir(buildPath / "RELS.arc/rels/amem")
filesTxtData = (
"Folder:rels/amem/\nFolder:rels/mmem/\nFolder:rels/./\nFolder:rels/../\n"
)
for i, rel in enumerate(aMemRels.splitlines()):
filesTxtData = filesTxtData + str(i + 4) + ":rels/amem/" + rel.lower() + ":0xa500\n"
filesTxtData = filesTxtData + "Folder:rels/amem/./\nFolder:rels/amem/../\n"
for i, rel in enumerate(mMemRels.splitlines()):
filesTxtData = filesTxtData + str(i + 83) + ":rels/mmem/" + rel.lower() + ":0xa500\n"
filesTxtData = filesTxtData + "Folder:rels/mmem/./\nFolder:rels/mmem/../\n"
open(buildPath / "RELS.arc/_files.txt", "w").write(filesTxtData)
for rel in relArcPaths:
for rel2 in aMemRels.splitlines():
if str(rel).find(rel2) != -1:
sourceRel = open(rel, "rb").read()
open(buildPath / "RELS.arc/rels/amem/" / rel2.lower(), "wb").write(
yaz0CompressFunction(sourceRel)
)
break
for rel2 in mMemRels.splitlines():
if str(rel).find(rel2) != -1:
sourceRel = open(rel, "rb").read()
open(buildPath / "RELS.arc/rels/mmem/" / rel2.lower(), "wb").write(
yaz0CompressFunction(sourceRel)
)
break
print("Creating RELS.arc")
open(buildPath / "dolzel2/game/files/RELS.arc", "wb").write(
libarc.convert_dir_to_arc(buildPath / "RELS.arc", convertEntry)
)
def postprocessMapFile(inputMapString):
outputMap = ""
textFound = False
for line in inputMapString.splitlines():
if len(line)==0:
continue
elif textFound == True and line[0:3] == " 0" and line[39] != '.':
split = line.split(" ")
outputMap += ' '.join(split[0:5])+' '+' '.join(split[6:])+'\n'
elif line[0]=='.':
if line == '.text section layout' or textFound == True:
textFound = True
outputMap += line+'\n'
return outputMap
def copyMapFiles(buildPath):
open(buildPath/"dolzel2/game/files/map/Final/Release/frameworkF.map","w").write(postprocessMapFile(open(buildPath/"dolzel2/dolzel2.map","r").read()))
for map in (buildPath/"dolzel2/rel/").rglob("*.map"):
open(buildPath/"dolzel2/game/files/map/Final/Release/"/map.name,"w").write(postprocessMapFile(open(map,"r").read()))
def main(gamePath, buildPath, copyCode, config_file):
global config
config = assets_config.getConfig(config_file, update = True)
if config["oead_yaz0"]:
try:
from oead import yaz0
global yaz0CompressFunction
yaz0CompressFunction = yaz0.compress
except:
print("Package: oead isn't installed, falling back to native yaz0")
if not gamePath.exists():
gamePath.mkdir(parents=True, exist_ok=True)
iso = Path("gz2e01.iso")
if not iso.exists() or not iso.is_file():
print("gz2e01.iso doesn't exist in project directory!")
sys.exit(1)
if not (gamePath / "files").exists() or not (gamePath / "sys").exists():
print("ISO is not extracted; extracting...")
extract_game_assets.extract(iso.absolute(),gamePath.absolute(),config_file)
print("Copying game files...")
if os.path.exists(buildPath / "dolzel2") == False:
os.mkdir(buildPath / "dolzel2")
if os.path.exists(buildPath / "dolzel2" / "game") == False:
os.mkdir(buildPath / "dolzel2/game")
copy(gamePath, Path(buildPath / "dolzel2/game").absolute())
if copyCode != "noCopyCode":
print(
str(buildPath / "dolzel2/main_shift.dol")
+ " -> "
+ str(buildPath / "dolzel2/game/sys/main.dol")
)
shutil.copyfile(
buildPath / "dolzel2/main_shift.dol",
buildPath / "dolzel2/game/sys/main.dol",
)
copyRelFiles(gamePath, buildPath, aMemRels.splitlines(), mMemRels.splitlines())
if config["package_maps"]:
shutil.copy(buildPath/"dolzel2/frameworkF.str",buildPath/"dolzel2/game/files/str/Final/Release/frameworkF.str")
copyMapFiles(buildPath)
if config["update_copydate"]:
now = datetime.now()
copydate = str(now.year)+"/"+str(now.month).zfill(2)+"/"+str(now.day).zfill(2)+" "+str(now.hour).zfill(2)+":"+str(now.minute).zfill(2)+"\n"
open(buildPath/"dolzel2/game/files/str/Final/Release/COPYDATE","w").write(copydate)
if __name__ == "__main__":
main(Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3], sys.argv[4])
|