-
Notifications
You must be signed in to change notification settings - Fork 0
/
unvivtool_script.py
executable file
·126 lines (105 loc) · 4.66 KB
/
unvivtool_script.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
# unvivtool Copyright (C) 2020-2024 Benjamin Futasz <https://github.com/bfut>
#
# Portions copyright, see each source file for more information.
#
# You may not redistribute this program without its source code.
# README.md may not be removed or altered from any unvivtool redistribution.
#
# 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/>.
"""
unvivtool_script.py - decode or encode VIV/BIG archives
USAGE
Run decoder:
python unvivtool_script.py </path/to/archive.viv>|</path/to/archive.big> [</path/to/output_folder>]
Run encoder:
python unvivtool_script.py </path/to/folder> [</path/to/archive.viv>|</path/to/archive.big>]
Print info only:
python unvivtool_script.py i </path/to/archive.viv>|</path/to/archive.big>
python unvivtool_script.py </path/to/archive.viv>|</path/to/archive.big>
REQUIRES
python -m pip install unvivtool
this installs unvivtool <https://github.com/bfut/unvivtool>
"""
import argparse
import os
import pathlib
import re
import time
import unvivtool as uvt
CONFIG = {
"opt_requestfmt" : "BIGF", # viv() only
"opt_direnlenfixed" : 0,
"opt_overwrite" : 1,
}
def main():
# Parse command (or print module help)
parser = argparse.ArgumentParser()
parser.add_argument("cmd", nargs="+", help= "<path/to/file>, <path/to/folder>, i <path/to/file>")
args = parser.parse_args()
inpath = pathlib.Path(args.cmd[0])
print(f"inpath: '{inpath}'")
# Decode
if inpath.is_file():
print(f"#{args.cmd[:]}#")
outdir = None
if len(args.cmd) > 1:
outdir = pathlib.Path(args.cmd[1])
if not isinstance(outdir, pathlib.Path) or not outdir.is_dir():
outdir = inpath.parent / (inpath.stem + "_" + inpath.suffix[1:])
ptn = time.process_time_ns()
if uvt.unviv(str(inpath), str(outdir), direnlen=CONFIG["opt_direnlenfixed"], overwrite=CONFIG["opt_overwrite"]): # extract all files in archive "inpath"
print(f"unvivtool took {(float(time.process_time_ns() - ptn) / 1e6):.2f} ms")
# Encode
elif inpath.is_dir():
print(f"#{args.cmd[:]}#")
if len(args.cmd) > 1:
vivfile = pathlib.Path(args.cmd[1])
else:
suffix1 = re.compile(r"(\w+)_[vV][iI][vV]$", re.IGNORECASE)
suffix2 = re.compile(r"(\w+)_[bB][iI][gG]$", re.IGNORECASE)
if suffix1.match(inpath.stem):
vivfile = (inpath.parent / inpath.stem[:-4]).with_suffix(".viv")
elif suffix2.match(inpath.stem):
vivfile = (inpath.parent / inpath.stem[:-4]).with_suffix(".big")
else:
vivfile = (inpath.parent / inpath.stem).with_suffix(".viv")
infiles = os.listdir(inpath)
for i in range(len(infiles)):
infiles[i] = str(inpath / infiles[i])
infiles = sorted(infiles)
print(infiles)
ptn = time.process_time_ns()
if uvt.viv(str(vivfile), infiles, format=CONFIG["opt_requestfmt"], direnlen=CONFIG["opt_direnlenfixed"]): # encode all files in path/to/infiles
print(f"unvivtool took {(float(time.process_time_ns() - ptn) / 1e6):.2f} ms")
# Print archive info (dry run)
elif args.cmd[0] == "i" and len(args.cmd) > 1:
print(f"#{args.cmd[1:]}#")
print( " ".join(args.cmd[1:]) , pathlib.Path(" ".join(args.cmd[1:])) )
if len(args.cmd) > 1:
vivfile = pathlib.Path(args.cmd[1])
vivfile = pathlib.Path(" ".join(args.cmd[1:]))
else:
vivfile = pathlib.Path(__file__).parent / "car.viv" # all paths can be absolute or relative
if not vivfile.is_file():
raise FileExistsError(f"{vivfile}")
ptn = time.process_time_ns()
if uvt.unviv(str(vivfile), dir=".", direnlen=CONFIG["opt_direnlenfixed"], dry=True, overwrite=CONFIG["opt_overwrite"]):
print(f"unvivtool took {(float(time.process_time_ns() - ptn) / 1e6):.2f} ms")
#
else:
print("Invalid command (expects {<path/to/file>, <path/to/folder>, i <path/to/file>}):", args.cmd[0])
# help(uvt)
if __name__ == "__main__":
# print(CONFIG)
main()