summaryrefslogtreecommitdiff
path: root/ModSlots/ModSlots.py
blob: 164fb0e2d2903c31c144a14457280f7ecc4bd313 (plain)
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
#! /usr/bin/env python3

import sys
sys.dont_write_bytecode = True
import os.path
import hashlib
from enum import Enum
from zipfile import ZipFile
#from unrardll import extract, names
#from py7zr import SevenZipFile
from ArmorIDS import ArmorIDS

USAGE = f'Usage: {sys.argv[0]} <Slots.txt> <Path_to_Mods> <Path_to_Game>'

if len(sys.argv) < 3:
    print(USAGE)
    sys.exit(1)

Slots = sys.argv[1]
Mods_Directory = sys.argv[2]
Game_Directory = sys.argv[3]
Extract_Directory = f'{Mods_Directory}/extract'
Hash_Dir = f'{Game_Directory}/mod_slots_hash.txt'
List_Dir = f'{Game_Directory}/mod_slots.txt'

if not os.path.isfile(Slots):
    print(f'{Slots} not a file.')
    sys.exit(1)

if not os.path.isdir(Mods_Directory):
    print(f'Directory {Mods_Directory} doesn\'t exist.')
    sys.exit(1)

if not os.path.isdir(Extract_Directory):
    os.mkdir(Extract_Directory)

if not os.path.isdir(Game_Directory):
    print(f'Directory {Game_Directory} doesn\'t exist.')
    sys.exit(1)

ArmorIDS_Inverted = { v: k for k, v in ArmorIDS.items() }

class ArchiveType(Enum):
    ZIP = 0
    RAR = 1
    SEVEN_ZIP = 2

class Mod():
    def __init__(self, path):
        m = hashlib.sha256()
        m.update(open(path, 'rb').read())
        self.hash = m.hexdigest()
        ext = path.split('.')[-1].lower()
        if ext == 'zip':
            self.type = ArchiveType.ZIP
            self.obj = ZipFile(path, 'r')
            self.files = []
            for file in self.obj.infolist():
                if file.filename[-1] == '/':
                    continue
                self.files.append(file.filename)
        elif ext == 'rar':
            self.type = ArchiveType.RAR
            pass
        elif ext == '7z':
            self.type = ArchiveType.SEVEN_ZIP
            pass
        self.toplevels = []
        self.roots = []
        self.ignored = []
        self.overrides = {}
        self.map = {}

    def get_id_from_subpath(self, sub):
        for armor_id in ArmorIDS.keys():
            armor_id = armor_id[2:]
            search = sub.find(armor_id)
            if search >= 0:
                return sub[search:search + 8]

    def substitute_override_name(self, name):
        if name[0] == '[':
            name = name[1:-1]
            if name in ArmorIDS_Inverted.keys():
                name = ArmorIDS_Inverted[name][2:]
        return name

    def file_sort_key(self, x):
        for i in range(0, len(self.roots)):
            if x.startswith(self.roots[i]):
                return i
        return 0

    def do_map(self, all_files):
        for file in sorted(self.files, key=self.file_sort_key):
            ignore_this = False
            for ig in self.ignored:
                if file.startswith(ig):
                    ignore_this = True
                    break
            if ignore_this:
                continue
            path = file # Absolute path within archive.
            for root in self.roots:
                if file.startswith(root):
                    nativePC = file.find('nativePC')
                    if nativePC >= 0:
                        file = file[nativePC:]
                        break
            if file in self.overrides.keys():
                targets = self.overrides[file]
            elif not (file.startswith('nativePC') or file in self.toplevels):
                continue
            else:
                targets = [file]
            for i in range(0, len(targets)):
                target = targets[i]
                # Substitute armor names with their IDs.
                for key in self.overrides.keys():
                    if target.startswith(key):
                        name = self.substitute_override_name(self.overrides[key][0]) # Sub has to be first element.
                        target = target.replace(self.get_id_from_subpath(target), name)
                if target in all_files:
                    print(f'DUPLICATE MAPPING: {target}.')
                    del targets[i]
                    continue
                targets[i] = target
            all_files.extend(targets)
            print(path)
            for target in targets:
                print(' -> ' + target)
            self.map[path] = targets

    def add_to_hash(self, m):
        output = f'{os.path.join(Extract_Directory, self.hash)}'
        if not os.path.isdir(output):
            os.mkdir(output)
            if self.type == ArchiveType.ZIP:
                self.obj.extractall(path=output)
        for key in self.map:
            m.update(open(os.path.join(output, key), 'rb').read())

    def write(self, old_list):
        output = f'{os.path.join(Extract_Directory, self.hash)}'
        with open(List_Dir, 'a+') as l:
            for key in self.map:
                targets = self.map[key]
                for target in targets:
                    if target in old_list:
                        old_list.remove(target)
                    l.write(target + '\n')
                    inc = Game_Directory
                    for s in target.split('/')[:-1]:
                        inc = os.path.join(inc, s)
                        if not os.path.isdir(inc):
                            os.mkdir(inc)
                    target = os.path.join(Game_Directory, target)
                    if os.path.isfile(target):
                        os.remove(target)
                    os.symlink(f'{os.path.join(output, key)}', f'{target}')

    def delete(self):
        for key in self.map:
            targets = self.map[key]
            for target in targets:
                target = os.path.join(Game_Directory, target)
                if os.path.isfile(target):
                    os.remove(target)

def is_armor_override(line):
    sp = line.split('/')
    if len(sp) >= 3 and sp[2] == 'slg':
        return True
    if sp[-1] in ['helm', 'body', 'arm', 'wst', 'leg']:
        return True
    return False

Mods = []
Current_Mod = None
Current_Override = None
with open(Slots, 'r') as f:
    for line in f.read().splitlines():
        if len(line) == 0 or line[0] == '#':
            continue
        if line[0] == ';':
            if Current_Mod:
                Mods.append(Current_Mod)
                Current_Mod = None
            path = os.path.join(Mods_Directory, line[1:])
            if os.path.isfile(path):
                Current_Mod = Mod(path)
            else:
                print(f'FILE NOT FOUND: {path}')
            continue
        cmd = ''
        if line[0] == '*':
            cmd = 'root'
        elif line[0] == '<':
            cmd = 'ignore'
        elif line[0] == ':':
            cmd = 'override'
        elif line[0] == '>':
            cmd = 'value'
        if cmd:
            line = line[1:]
            if line.startswith('pl/'):
                line = 'nativePC/' + line
            if line.startswith('wp/'):
                line = 'nativePC/' + line
        if cmd == 'root' and Current_Mod:
            Current_Mod.roots.append(line)
        elif cmd == 'ignore' and Current_Mod:
            Current_Mod.ignored.append(line)
        elif cmd == 'override' and Current_Mod:
            if not (is_armor_override(line) or line in Current_Mod.files):
                print(f'FILE NOT FOUND: {line}')
                Current_Mod = None
                continue
            Current_Override = line
            Current_Mod.overrides[Current_Override] = []
        elif cmd == 'value' and Current_Mod:
            if Current_Override:
                Current_Mod.overrides[Current_Override].append(line)
            else:
                Current_Mod.toplevels.append(line)
    if Current_Mod:
        Mods.append(Current_Mod)
        Current_Mod = None

Files = []
M = hashlib.sha256()
for mod in Mods:
    mod.do_map(Files)
    mod.add_to_hash(M)

with open(List_Dir, 'a+') as l:
    l.seek(0)
    old_list = l.read().splitlines()
    l.truncate(0)

Mode = 'Write'
if Mode == 'Write':
    for mod in Mods:
        mod.write(old_list)
    for old in old_list:
        old_target = os.path.join(Game_Directory, old)
        if os.path.islink(old_target):
            print(f'Removing old link: {old_target}')
            os.remove(old_target)
    with open(Hash_Dir, 'w+') as f:
        f.write(M.hexdigest())
if Mode == 'Verify':
    if os.path.isfile(Hash_Dir):
        with open(Hash_Dir, 'r') as f:
            if f.read() == M.hexdigest():
                print('Verification passed')
            else:
                print('Verification failed')
elif Mode == 'Delete':
    for mod in Mods:
        mod.delete()