-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgkupdate.py
More file actions
355 lines (298 loc) · 12.4 KB
/
Copy pathgkupdate.py
File metadata and controls
355 lines (298 loc) · 12.4 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
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
#!/usr/bin/env python3
"""GK-ModUpdater: update your old Gotham Knights .pak mods to the IoStore format
the final (2023) build uses.
gkupdate convert <thing>... an old .pak (or a .zip/.rar/.7z/folder) -> IoStore
gkupdate verify <file.utoc> check a converted mod is structurally sound
gkupdate tools check or install the retoc / repak helpers
gkupdate terms show the terms of use
This is a tool for MOD AUTHORS to update their own mods. It is not a mod manager;
it does not install, enable or launch anything. If you just want to play with old
.pak mods, use GK-ModLoader instead (coming soon).
GK-ModUpdater ships no third-party binaries. `gkupdate tools` will tell you what
is missing and can fetch retoc / repak from their official release pages.
"""
from __future__ import annotations
import argparse
import shutil
import sys
from pathlib import Path
from gkmodkit import archives, convert, gamedir, terms, tools
from gkmodkit.errors import GKModKitError, LooseFileMod, ToolFailed, ToolMissing
C_OK, C_BAD, C_DIM, C_OFF = "\033[32m", "\033[31m", "\033[90m", "\033[0m"
if sys.platform == "win32":
try:
import ctypes
ctypes.windll.kernel32.SetConsoleMode(
ctypes.windll.kernel32.GetStdHandle(-11), 7)
except Exception:
C_OK = C_BAD = C_DIM = C_OFF = ""
def ok(s):
return f"{C_OK}{s}{C_OFF}"
def bad(s):
return f"{C_BAD}{s}{C_OFF}"
def dim(s):
return f"{C_DIM}{s}{C_OFF}"
NO_GAME_NOTE = ("(no game found, so imports were not resolved. Pass --game <Gotham "
"Knights folder> to check your mod against the shipped assets.)")
def resolve_provided(args, remember=False):
"""The set of packages the shipped game provides, or None if we shouldn't
resolve imports (no --game, none found, or --no-game). Warns on a bad --game."""
if args.no_game:
return None
g = gamedir.find_game(args.game)
if not g:
if args.game:
print(bad(f" {args.game}: not a Gotham Knights install; "
f"imports not resolved"))
return None
if remember:
gamedir.remember_game(g)
return gamedir.base_packages(g)
# ------------------------------------------------------------------- commands
def cmd_tools(args):
if args.set:
name, path = args.set
_require_tool_name(name)
print(ok(f"{name} -> {tools.set_tool(name, path)}"))
return 0
if args.fetch:
names = list(tools.TOOLS) if args.fetch == "all" else [args.fetch]
for n in names:
_require_tool_name(n)
for name in names:
print(f"fetching {name}...")
try:
tools.fetch_tool(name, on_progress=lambda m: print(dim(" " + m)))
print(ok(f" {name} ready"))
except GKModKitError as e:
print(bad(f" {name} failed: {e}"))
return 0
print("Helper executables (third-party, not shipped with GK-ModUpdater):\n")
missing = 0
for st in tools.status():
meta = tools.TOOLS[st.name]
if st.path:
print(f" {ok('found')} {st.name:8} {st.version or ''}")
print(dim(f" {st.path}"))
else:
missing += 1
print(f" {bad('missing')} {st.name:8} - {meta['purpose']}")
print(dim(f" {meta['home']}/releases"))
if missing:
print(f"\n Install with: {ok('gkupdate tools --fetch all')}")
print(dim(" or point at copies you have: gkupdate tools --set retoc <path>"))
return 0
def _require_tool_name(name):
if name not in tools.TOOLS:
raise GKModKitError(f"unknown tool {name!r}; expected "
f"{' or '.join(tools.TOOLS)}")
def cmd_convert(args):
out = Path(args.out)
provided = resolve_provided(args, remember=True)
work = out / ".import"
jobs = []
for src in args.inputs:
cands, _ = archives.import_source(Path(src), work)
if not args.all_variants:
cands = archives.pick_best_variants(cands)
jobs += cands
convertible = [c for c in jobs if c.kind not in ("config", "loose", "error",
"iostore")]
if args.name and len(convertible) > 1:
shutil.rmtree(work, ignore_errors=True)
raise GKModKitError("--name only works with a single mod; you gave "
f"{len(convertible)}. Drop --name to keep each mod's "
"own name.")
# only require the helpers once we know there is something to convert
retoc = repak = None
if convertible:
retoc = tools.find_tool("retoc", args.retoc)
repak = tools.find_tool("repak", args.repak)
results, skipped = [], []
helper_failed = False
for c in jobs:
if c.kind in ("config", "loose", "error"):
skipped.append((c.name, c.note))
continue
if c.kind == "iostore":
skipped.append((c.name, "already IoStore, no conversion needed"))
continue
name = convert.normalise_mod_name(args.name or c.name)
print(f"\n=== {name}")
try:
r = convert.convert(c.path, out, name=args.name, retoc=retoc,
repak=repak, provided=provided)
except LooseFileMod as e:
skipped.append((c.name, str(e)))
continue
except ToolFailed as e:
# a broken helper is exit 3, but don't let one bad mod abort the batch
print(bad(f" helper failed: {e}"))
results.append(None)
helper_failed = True
continue
except GKModKitError as e:
print(bad(f" failed: {e}"))
results.append(None)
continue
results.append(r)
print(f" packages : {r.packages}")
print(f" ExportBundlesSize : fixed {r.bundles_size_fixed}")
print(f" export bundles : merged {r.bundles_merged}")
print(f" redirects dropped : {r.redirects_dropped}")
print(f" verify : {ok('OK') if r.ok else bad('PROBLEMS')}")
for p in r.problems:
print(bad(f" !! {p}"))
if skipped:
print("\nskipped:")
for n, why in skipped:
print(dim(f" {n}: {why}"))
shutil.rmtree(work, ignore_errors=True)
good = [r for r in results if r and r.ok]
if not results:
print("\nnothing needed converting")
return 0
print(f"\n{len(good)}/{len(results)} converted and verified -> {out}")
if provided is None and not args.no_game and not args.game:
print(dim(" " + NO_GAME_NOTE))
if helper_failed:
return 3
return 0 if len(good) == len(results) else 1
def cmd_verify(args):
retoc = tools.find_tool("retoc", args.retoc)
work = Path(args.out or tools.app_dir() / "work")
provided = resolve_provided(args)
rc = 0
for f in args.files:
probs = convert.verify_container(Path(f), retoc, work, provided)
print(f"{Path(f).stem:38} {ok('OK') if not probs else bad('PROBLEMS')}")
for p in probs:
print(bad(f" !! {p}"))
rc = 1
if provided is None and not args.no_game and not args.game:
print(dim(NO_GAME_NOTE))
return rc
def cmd_terms(args):
if args.revoke:
terms.forget_acceptance()
print("Acceptance cleared. You'll be asked again on the next run.")
return 0
print("\n Terms of use\n")
print(terms.SUMMARY)
if not terms.has_accepted() and args.accept:
terms.record_acceptance()
print(f"\n Status: {'accepted' if terms.has_accepted() else 'NOT accepted'}\n")
return 0
# ------------------------------------------------------------- the terms gate
def _console_available() -> bool:
try:
return sys.stdin is not None and sys.stdin.isatty()
except Exception:
return False
def _prompt_terms() -> bool:
print("\n Terms of use\n")
print(terms.SUMMARY)
try:
answer = input("\n Type 'agree' to confirm you have the right to the mods "
"you'll process and accept these terms; anything else quits: ")
except (EOFError, KeyboardInterrupt):
print()
return False
if terms.is_acceptance(answer):
terms.record_acceptance()
print("\n Accepted; you won't be asked again.\n")
return True
return False
# Only `terms` stays reachable, since it is how you view and accept them.
# Everything else, including fetching the helpers, requires accepting first.
UNGATED = {"terms"}
def _common_parser():
"""Shared options, valid before or after the subcommand."""
common = argparse.ArgumentParser(add_help=False)
common.add_argument("--retoc", help="path to retoc.exe")
common.add_argument("--repak", help="path to repak.exe")
common.add_argument("--game", help="path to the Gotham Knights folder, used to "
"resolve imports (remembered)")
common.add_argument("--no-game", action="store_true",
help="don't look for the game; check structure only")
common.add_argument("--accept-terms", action="store_true",
help="accept the terms of use without being asked")
return common
def check_terms(argv) -> bool:
"""Gate BEFORE argparse so nothing runs until the terms are accepted, not
even --help. Only `terms` (to read and accept them) stays reachable.
A pre-parse just picks out the subcommand and --accept-terms; it uses
add_help=False so a --help in argv is ignored here rather than acted on.
"""
pre = _common_parser()
pre.add_argument("cmd", nargs="?")
known, _ = pre.parse_known_args(argv)
if known.cmd in UNGATED or terms.has_accepted():
return True
if known.accept_terms:
terms.record_acceptance()
return True
if _console_available():
return _prompt_terms()
print(bad("This tool has terms of use you have not accepted yet."))
print(terms.SUMMARY)
print(dim("\n No console to ask on. Accept with either:\n"
" gkupdate terms --accept\n"
" gkupdate <command> --accept-terms"))
return False
# ---------------------------------------------------------------------- main
def build_parser():
common = _common_parser()
ap = argparse.ArgumentParser(
prog="gkupdate", description=__doc__, parents=[common],
formatter_class=argparse.RawDescriptionHelpFormatter)
sub = ap.add_subparsers(dest="cmd", required=True)
p = sub.add_parser("convert", parents=[common],
help="old .pak / archive / folder -> IoStore")
p.add_argument("inputs", nargs="+")
p.add_argument("--out", default="converted")
p.add_argument("--name", help="override output mod name (single input)")
p.add_argument("--all-variants", action="store_true",
help="convert every variant found, not just the best one")
p.set_defaults(fn=cmd_convert)
p = sub.add_parser("verify", parents=[common],
help="check a converted mod is sound")
p.add_argument("files", nargs="+")
p.add_argument("--out", help="scratch directory")
p.set_defaults(fn=cmd_verify)
p = sub.add_parser("tools", parents=[common],
help="check or install the helper executables")
p.add_argument("--set", nargs=2, metavar=("NAME", "PATH"))
p.add_argument("--fetch", metavar="NAME", help="retoc | repak | all")
p.set_defaults(fn=cmd_tools)
p = sub.add_parser("terms", parents=[common], help="show the terms of use")
p.add_argument("--accept", action="store_true")
p.add_argument("--revoke", action="store_true", help="ask again next run")
p.set_defaults(fn=cmd_terms)
return ap
def main(argv=None) -> int:
argv = list(sys.argv[1:] if argv is None else argv)
if not check_terms(argv):
return 4
args = build_parser().parse_args(argv)
try:
return args.fn(args) or 0
except ToolMissing as e:
print(bad(e.advice()))
return 2
except ToolFailed as e:
print(bad(f"{e.tool} exited {e.code}"))
print(dim(f" {e.cmd}"))
print(e.output.strip()[:1200])
return 3
except GKModKitError as e:
print(bad(f"error: {e}"))
return 1
except KeyboardInterrupt:
print()
return 130
except OSError as e:
print(bad(f"file error: {e}"))
return 1
if __name__ == "__main__":
raise SystemExit(main())