Repository navigation
Expand file tree
/
Copy pathadd_base.py
More file actions
126 lines (108 loc) · 5 KB
/
Copy pathadd_base.py
File metadata and controls
126 lines (108 loc) · 5 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
#!/usr/bin/env python3
from __future__ import annotations
import sys
from pathlib import Path
import common
from common import (
print_error,
print_live_status_once,
handle_steam_query_worker_request,
ENTRY_SCRIPTS,
ErrorArgumentParser,
console_title,
install_termination_handlers,
is_steam_manifest_id,
index_update_lock,
load_index,
operation_activity_lock,
operation_lock,
resolve_base_name,
scan_tree,
validate_installation_root_entry,
validate_warframe_installation,
write_index,
)
from update import add_update_arguments, handle_automatic_update, handle_early_update_request
def _existing_base_conflict(index: dict, name: str, manifest_id: int) -> str | None:
try:
existing_name = resolve_base_name(index, name)
except KeyError:
existing_name = None
if existing_name is not None:
return f'Base "{existing_name}" already exists in the index.'
for existing_name, entry in index.items():
if entry["steam_manifest_id"] == manifest_id:
return f'[Steam] Manifest ID {manifest_id} is already indexed as "{existing_name}".'
return None
def main() -> int:
install_termination_handlers()
argv = sys.argv[1:]
steam_worker_result = handle_steam_query_worker_request(argv)
if steam_worker_result is not None:
return steam_worker_result
early_update_result = handle_early_update_request(argv)
if early_update_result is not None:
return early_update_result
parser = ErrorArgumentParser(description="Add a clean, unmodified Steam manifest base to data/index.json.")
parser.add_argument("path", type=Path, help="Path to the clean Steam manifest base")
parser.add_argument("name", help="Warframe version, for example U43.5.1 or Pre-U43.0.0")
parser.add_argument("manifest_id", type=int, help="Steam manifest ID of the base")
add_update_arguments(parser)
parser.add_version_argument()
parser.add_help_argument()
args = parser.parse_args(argv)
try:
with operation_activity_lock():
update_result = handle_automatic_update(args, argv)
if update_result is not None:
return update_result
if not args.path.is_dir():
print_error(f"Base directory does not exist: {args.path}")
return 1
validate_installation_root_entry(args.path)
base = args.path.resolve()
name = args.name.strip()
manifest_id = args.manifest_id
if not validate_warframe_installation(base, "Base"):
return 1
if not name:
print_error("Base name cannot be empty.")
return 1
if not is_steam_manifest_id(manifest_id):
print_error("[Steam] Manifest ID must be a valid unsigned 64-bit number.")
return 1
# Reject conflicts that can be determined from the index before doing a potentially very expensive full-tree hash.
# The same checks are repeated after hashing because another add_base process may update the index meanwhile.
with index_update_lock(common.INDEX_FILE):
conflict = _existing_base_conflict(load_index(), name, manifest_id)
if conflict is not None:
print_error(f"{conflict}\nNo changes were made.")
return 1
print_live_status_once()
with operation_lock("installation", base, "operation using this installation"):
print(f'Hashing base "{name}"...\n' "This may take a while for large installations.")
files, root_hash = scan_tree(base, "Hashing base")
# Keep the installation locked until its verified identity is committed to the index.
with index_update_lock(common.INDEX_FILE, timeout_seconds=5):
index = load_index()
conflict = _existing_base_conflict(index, name, manifest_id)
if conflict is not None:
print_error(f"{conflict}\nNo changes were made.")
return 1
for existing_name, entry in index.items():
if entry["sha256"].lower() == root_hash.lower():
print_error(f'This exact base is already indexed as "{existing_name}".\nNo changes were made.')
return 1
index[name] = {"steam_manifest_id": manifest_id, "sha256": root_hash, "file_count": len(files)}
write_index(index)
print(f'\n[Added] Base "{name}"\n[Steam] Manifest ID: {manifest_id}\nFiles: {len(files):,}\nSHA-256: {root_hash}\nIndex: {common.INDEX_FILE}')
return 0
except KeyboardInterrupt:
print("\nBase addition cancelled.", file=sys.stderr)
return 130
except Exception as exc:
print_error(f"{exc}")
return 1
if __name__ == "__main__":
with console_title(ENTRY_SCRIPTS["add_base.py"]):
raise SystemExit(main())