forked from SymmetricDevs/Supersymmetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialIDRefactorer.py
47 lines (35 loc) · 1.49 KB
/
MaterialIDRefactorer.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
import regex as re # Has to be regex because pythons re lookbehinds need to be fixed length lm
import sys
# incredibly non robust and cursed do not use
# wrote this up in 5 minutes dont expect it to work well
path = sys.argv[1]
with open(path, 'r') as f:
material_strs = f.readlines()
def replace_material_ids(strings):
pattern_builder = r"(?<=Material.Builder\()(\d+)(?=,)"
pattern_generate = r"(?<=\bgenerate\w+\(\w+,\s*)\b(\d+)\b"
pattern_id_range = r"(?<=\bIDs\s+)\d+"
replaced_strings = []
currentId = int(sys.argv[2])
for string in strings:
replaced_string = string
if "//" in string:
found = re.search(pattern_id_range, string)
if found != None:
currentId = int(found.group())
if "Material.Builder" in string:
found = re.search(pattern_builder, string)
if found != None:
replaced_string = re.sub(pattern_builder ,str(currentId), string)
currentId +=1
if "generate" in string:
found = re.search(pattern_generate, string)
if found != None:
replaced_string = re.sub(pattern_generate, str(currentId), string)
currentId +=1
replaced_strings.append(replaced_string)
return replaced_strings
replaced_strings = replace_material_ids(material_strs)
with open(path, "w") as f:
for string in replaced_strings:
f.write(string)