forked from n0k0m3/al-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
265 lines (232 loc) · 9.89 KB
/
main.py
File metadata and controls
265 lines (232 loc) · 9.89 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
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
#!/usr/bin/python3
#
# Copyright (C) 2019 k0np4ku
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
import os
import ntpath
import argparse
import k0np4ku.utils.binaryReader as binaryReader
import k0np4ku.lua.crypt as luacrypt
import k0np4ku.ab.crypt as ab
# File Types
_UNKNOWN = "Unknown"
_LUA = "Lua"
_BUNDLE = "AssetBundle"
# Tasks
_ENCRYPT = "Encrypt"
_DECRYPT = "Decrypt"
_COMPILE = "Compile"
_DECOMPILE = "Decompile"
_REPACK = "Repackage"
_UNPACK = "Unpackage"
class Main():
def initialize(self):
parser = argparse.ArgumentParser(description="-x- al-tools v1.0.0 -x-")
parser.add_argument("--encrypt", nargs="+",
help = "Encrypt Lua(s) or Asset Bundle(s). Can be a directory(s) that contains Lua(s) or Asset Bundle(s)")
parser.add_argument("--decrypt", nargs="+",
help = "Decrypt Lua(s) or Asset Bundle(s). Can be a directory(s) that contains Lua(s) or Asset Bundle(s)")
parser.add_argument("--compile", nargs="+",
help = "Compile Lua(s). Can be a directory(s) that contains Lua(s)")
parser.add_argument("--decompile", nargs="+",
help = "Decompile Lua(s). Can be a directory(s) that contains Lua(s)")
parser.add_argument("--repack", nargs="+",
help = "Repack Asset Bundle(s)")
parser.add_argument("--unpack", nargs="+",
help = "Unpack Asset Bundle(s)")
parser.add_argument("--dir", default=None,
help = "Output directory. Will save to input's directory if not specified")
args = parser.parse_args()
if len(sys.argv) < 2:
parser.print_help()
return 1
self.files = None
self.mode = _UNKNOWN
self.outDir = args.dir
if args.encrypt:
self.files = args.encrypt
self.mode = _ENCRYPT
if args.decrypt:
self.files = args.decrypt
self.mode = _DECRYPT
if args.compile:
self.files = args.compile
self.mode = _COMPILE
if args.decompile:
self.files = args.decompile
self.mode = _DECOMPILE
if args.repack:
self.files = args.repack
self.mode = _REPACK
if args.unpack:
self.files = args.unpack
self.mode = _UNPACK
if self.files is not None and self.mode is not _UNKNOWN:
self.validatePaths()
if len(self.files) > 0:
for f in self.files:
reader = binaryReader.BinaryReader()
reader.open(f)
fileType = self.determineFileType(reader)
reader.close()
if self.mode == _ENCRYPT:
if fileType == _LUA: self.encryptLua(f)
elif fileType == _BUNDLE: self.encryptBundle(f)
elif self.mode == _DECRYPT:
if fileType == _LUA: self.decryptLua(f)
elif fileType == _BUNDLE: self.decryptBundle(f)
elif self.mode == _COMPILE:
self.compileLua(f)
elif self.mode == _DECOMPILE:
if fileType == _LUA: self.decompileLua(f)
else: self.logWarning("Invalid file type for task", _DECOMPILE)
elif self.mode == _REPACK:
if fileType == _BUNDLE: self.repackBundle(f)
else: self.logWarning("Invalid file type for task", _REPACK)
elif self.mode == _UNPACK:
if fileType == _BUNDLE: self.unpackBundle(f)
else: self.logWarning("Invalid file type for task", _UNPACK)
else:
self.logCompleted("No valid file path found, tasks completed without doing anything...")
return 0
@staticmethod
def logInfo(*message):
print("[INFO]>", *message)
@staticmethod
def logWarning(*message):
print("[WARNING]>", *message)
@staticmethod
def logCompleted(*message):
print("[COMPLETED]>", *message)
def validatePaths(self):
validPaths = []
for path in self.files:
name = path
path = os.path.abspath(name)
if os.path.exists(path):
isFile = os.path.isfile(path)
if not isFile:
directory = os.listdir(path)
for f in directory:
p = os.path.join(path, f)
if os.path.isfile(p):
validPaths.append(p)
else: self.logWarning("Skipping", f, "inside folder", name, "because it's not a file")
else: validPaths.append(path)
else: self.logWarning("Skipping", ntpath.basename(path), "because it's either doesn't exists or invalid")
self.files = validPaths
@staticmethod
def getHeader(reader, size = 16):
path = None
if isinstance(reader, str):
path = reader
reader = binaryReader.BinaryReader()
reader.open(path)
lastPos = reader.position
if lastPos is not 0:
reader.changePosition(0)
header = reader.readBytes(size).hex()
reader.changePosition(lastPos)
if path is not None:
reader.close()
return header
def determineFileType(self, reader):
header = self.getHeader(reader)
fileType = _UNKNOWN
if luacrypt.isLuaEncrypted(header) or luacrypt.isLuaDecrypted(header):
fileType = _LUA
elif ab.isBundleEncrypted(header) or ab.isBundleDecrypted(header):
fileType = _BUNDLE
return fileType
def compileLua(self, path):
lua = luacrypt.Crypt(path, self.outDir, False)
lua.compile()
def decompileLua(self, path):
lua = luacrypt.Crypt(path, self.outDir, True)
header = self.getHeader(lua.reader)
if luacrypt.isLuaEncrypted(header):
self.logWarning("Lua is encrypted, decrypting...")
#lua.decrypt()
elif not luacrypt.isLuaEncrypted(header) and not luacrypt.isLuaDecrypted(header):
self.logWarning("Skipping because it has an unknown header")
return
lua.decompile()
def encryptLua(self, path):
lua = luacrypt.Crypt(path, self.outDir, True)
header = self.getHeader(lua.reader)
if not luacrypt.isLuaDecrypted(header):
self.logWarning("Skipping", lua.path, "because it's already encrypted")
return
elif not luacrypt.isLuaEncrypted(header) and not luacrypt.isLuaDecrypted(header):
self.logWarning("Skipping", lua.path, "because it has an unknown header")
return
lua.encrypt()
lua.reader.close()
def decryptLua(self, path):
lua = luacrypt.Crypt(path, self.outDir, True)
header = self.getHeader(lua.reader)
if not luacrypt.isLuaEncrypted(header):
self.logWarning("Skipping", lua.path, "because it's already decrypted")
return
elif not luacrypt.isLuaEncrypted(header) and not luacrypt.isLuaDecrypted(header):
self.logWarning("Skipping", lua.path, "because it has an unknown header")
return
lua.decrypt()
lua.reader.close()
def encryptBundle(self, path):
bundle = ab.Crypt(path, self.outDir)
header = self.getHeader(path)
if not ab.isBundleDecrypted(header):
self.logWarning("Skipping", bundle.path, "because it's already encrypted")
return
elif not ab.isBundleEncrypted(header) and not ab.isBundleDecrypted(header):
self.logWarning("Skipping", bundle.path, "because it has an unknown header")
return
bundle.encrypt()
def decryptBundle(self, path):
bundle = ab.Crypt(path, self.outDir)
header = self.getHeader(path)
if not ab.isBundleEncrypted(header):
self.logWarning("Skipping", bundle.path, "because it's already decrypted")
return
elif not ab.isBundleEncrypted(header) and not ab.isBundleDecrypted(header):
self.logWarning("Skipping", bundle.path, "because it has an unknown header")
return
bundle.decrypt()
def repackBundle(self, path):
bundle = ab.Crypt(path, self.outDir)
header = self.getHeader(path)
if not ab.isBundleDecrypted(header) and ab.isBundleEncrypted(header):
self.logWarning("Bundle is not decrypted, decrypting...")
# bundle.decrypt()
elif not ab.isBundleEncrypted(header) and not ab.isBundleDecrypted(header):
self.logWarning("Skipping because it has an unknown header")
return
bundle.repack()
def unpackBundle(self, path):
bundle = ab.Crypt(path, self.outDir)
header = self.getHeader(path)
if not ab.isBundleDecrypted(header) and ab.isBundleEncrypted(header):
self.logWarning("Bundle is not decrypted, decrypting...")
# bundle.decrypt()
elif not ab.isBundleEncrypted(header) and not ab.isBundleDecrypted(header):
self.logWarning("Skipping because it has an unknown header")
return
bundle.unpack()
if __name__ == "__main__":
main = Main()
ret = main.initialize()
sys.exit(ret)