-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversor.py
153 lines (146 loc) · 4.96 KB
/
conversor.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
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
#!/usr/bin/env python3
# %%
import os
import os.path
import re
import shutil
dirname = "" # Example: C:/DIR/Scripts/
matched_exts = [".xml", ".lua"]
ignored_files = []
ignored_prefix_patterns = [
"-",
" - ",
" -",
"interval = ",
"chance = ",
"minDamage = ",
"maxDamage = ",
"maxDamage = -",
"speedChange = ",
"speedChange = -",
"duration = ",
"monster.raceId = ",
"totalDamage = ",
"lookType = ",
"monster.speed = ",
"monster.experience = ",
"monster.health = ",
"monster.maxHealth = ",
"SecondUnlock = ",
"toKill = ",
"monster.manaCost = ",
"runHealth = ",
"outfitItem = ",
'<attribute key="duration" value="',
'<attribute key="leveldoor" value="',
'<attribute key="weight" value="',
'<attribute key="ticks" value="',
'<attribute key="count" value="',
'<attribute key="damage" value="',
'<attribute key="charges" value="',
'<attribute key="armor" value="',
'<attribute key="defense" value="',
'<attribute key="attack" value="',
'<attribute key="maxHitChance" value="',
'<attribute key="healthTicks" value="',
'<attribute key="manaTicks" value="',
'<attribute key="range" value="',
'<attribute key="healthGain" value="',
'<attribute key="manaGain" value="',
'<attribute key="speed" value="',
'<attribute key="levelDoor" value="',
'<attribute key="maxtextlen" value="',
'<attribute key="writeonceitemid" value="',
'<attribute key="criticalhitdamage" value="',
'<attribute key="fromDamage" value="',
'<attribute key="toDamage" value="',
'<attribute key="level" value="',
]
ignored_line_regexes = []
ignored_ids = []
regex = re.compile(r"\b([1-9][0-9]{2,4})\b")
processed_files = []
replacemap = {}
with open("cid_sid_mapping.txt", "r") as fi:
for line in fi:
line = line.strip()
sid, cid = line.split(",")
replacemap[sid] = cid
# %%
for root, dirs, files in os.walk(dirname):
for file in files:
for ext in matched_exts:
if file.endswith(ext):
fullname = root + os.sep + file
for ignored in ignored_files:
if fullname.endswith(ignored):
break
else:
processed_files.append(root + os.sep + file)
# %%
for file in processed_files:
with open(file, "r") as fi, open(file + ".tmp", "w") as fo:
i = 0
for line in fi:
offset = 0
skipLine = False
for ignore_regex in ignored_line_regexes:
if type(ignore_regex) is list:
if ignore_regex[0].match(file):
ignore_regex = ignore_regex[1]
else:
continue
if ignore_regex.match(line):
skipLine = True
break
if skipLine:
fo.write(line)
continue
outline = line
i += 1
matches = regex.finditer(line)
skipPosition = 0
for match in matches:
skipMatch = False
if skipPosition > 0:
skipPosition -= 1
skipMatch = True
for prefix in ignored_prefix_patterns:
if type(prefix) is list:
if prefix[0].match(file):
prefix = prefix[1]
else:
continue
if match.start() >= len(prefix):
if line[match.start() - len(prefix) : match.start()] == prefix:
skipMatch = True
break
id = int(match.group())
# Skip common numbers for intervals and values
if id in ignored_ids:
skipMatch = True
# Detect positions starting at 30000 < X < 35000
if id > 30000 and id < 35000:
if line[match.start() - 1] == "(":
skipPosition = 2
skipMatch = True
if not skipMatch and match.group() in replacemap:
print(
"{}:{}[{}] - match {} in '{}'".format(
file, i, match.start(), match.group(), line
)
)
print(
"replacing {} with {}".format(
match.group(), replacemap[match.group()]
)
)
outline = (
outline[: match.start() + offset]
+ replacemap[match.group()]
+ outline[match.end() + offset :]
)
offset += len(replacemap[match.group()]) - len(match.group())
fo.write(outline)
shutil.move(file + ".tmp", file)
# os.remove(file+'.tmp')