-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepack.py
More file actions
121 lines (93 loc) · 3.15 KB
/
repack.py
File metadata and controls
121 lines (93 loc) · 3.15 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
import os
import sys
import time
try:
import SarcLib
except ImportError:
print("SarcLib is not installed!")
ans = input("Do you want to install it now? (y/n)\t")
if ans.lower() == 'y':
import pip
pip.main(['install', 'SarcLib==0.3'])
del pip
import SarcLib
else:
sys.exit(1)
try:
import libyaz0
except ImportError:
print("libyaz0 is not installed!")
ans = input("Do you want to install it now? (y/n)\t")
if ans.lower() == 'y':
import pip
pip.main(['install', 'libyaz0==0.5'])
del pip
import libyaz0
else:
sys.exit(1)
def pack_folder_to_blarc(folder_path, output_file):
"""
Pack the files and folders in the folder_path to a .blarc output_file.
"""
root = os.path.abspath(folder_path)
endianness = '>'
level = -1
pack(root, endianness, level, output_file)
def pack(root, endianness, level, outname):
"""
Pack the files and folders in the root folder.
"""
if "\\" in root:
root = "/".join(root.split("\\"))
if root[-1] == "/":
root = root[:-1]
arc = SarcLib.SARC_Archive(endianness=endianness)
lenroot = len(root.split("/"))
for path, dirs, files in os.walk(root):
if "\\" in path:
path = "/".join(path.split("\\"))
lenpath = len(path.split("/"))
if lenpath == lenroot:
path = ""
else:
path = "/".join(path.split("/")[lenroot - lenpath:])
for file in files:
if path:
filename = ''.join([path, "/", file])
else:
filename = file
print(f"Repacking {filename}")
if filename == "timg/__Combined.bntx":
print("Writing file. Please wait. This step takes the longest (5-10 seconds).")
fullname = ''.join([root, "/", filename])
i = 0
for folder in filename.split("/")[:-1]:
if not i:
exec("folder%i = SarcLib.Folder(folder + '/'); arc.addFolder(folder%i)".replace('%i', str(i)))
else:
exec("folder%i = SarcLib.Folder(folder + '/'); folder%m.addFolder(folder%i)".replace('%i', str(i)).replace('%m', str(i - 1)))
i += 1
with open(fullname, "rb") as f:
inb = f.read()
hasFilename = True
if file[:5] == "hash_":
hasFilename = False
if not i:
arc.addFile(SarcLib.File(file, inb, hasFilename))
else:
exec("folder%m.addFile(SarcLib.File(file, inb, hasFilename))".replace('%m', str(i - 1)))
data, maxAlignment = arc.save()
if level != -1:
outData = libyaz0.compress(data, maxAlignment, level)
del data
if not outname:
outname = ''.join([root, ".szs"])
print(f"Writing {outname}")
else:
outData = data
if not outname:
outname = ''.join([root, ".sarc"])
# print(f"Writing {outname}")
with open(outname, "wb+") as output:
# print(f"Writing {outname}")
output.write(outData)