-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply_mod.py
49 lines (41 loc) · 1.8 KB
/
apply_mod.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
import os
import xml.etree.ElementTree as ET
from lib.paths import PACK, MOD, BACKUP
from lib.crypt import encrypt, decrypt, pack, extract
from shutil import copy
import io
def main():
# os.makedirs(OVERRIDE, exist_ok=True)
os.makedirs(BACKUP, exist_ok=True)
# 1. load original xml
index_fp = os.path.join(PACK, "index.xml")
ori_index_fp = os.path.join(PACK, "index")
raw_xml = decrypt(ori_index_fp).rstrip(b"\00")
# parse index.xml
tree = ET.parse(io.BytesIO(raw_xml))
root = tree.getroot()
item_map = {item.get("original"): item for item in root}
# pack mod files
for root, dirs, files in os.walk(MOD):
for fp in files:
fp = os.path.join(root, fp)
ifp = fp[len(MOD) + 1 :]
item = item_map.get(ifp, None)
if item != None:
pack_path = os.path.join(PACK, *item.get("pack").split("/"))
backup_path = os.path.join(BACKUP, *item.get("pack").split("/"))
os.makedirs(os.path.dirname(backup_path), exist_ok=True)
# only create a backup if the file size fits, otherwise the backup might be replaced by an old mod
if os.path.getsize(pack_path) == item.get("csize"
) and (not os.path.exists(backup_path) or os.path.getsize(backup_path) != item.get("csize")):
copy(pack_path, backup_path)
pack(fp, pack_path, item)
# item.set("pack", f"..\\{os.path.basename(OVERRIDE)}\\{item.get('pack')}")
print("Packed", ifp)
else:
print("Couldn't find", ifp, "in the original index.")
print("Import of new assets isn't implemented yet.")
# save index
# copy(ori_index_fp, os.path.join(BACKUP, "index"))
if __name__ == "__main__":
main()