forked from Hazardu/ChampionsOfForest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_interpolated_to_stringFormat.py
62 lines (52 loc) · 2.12 KB
/
replace_interpolated_to_stringFormat.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
import os
import re
# this file converts all interpolated strings in a file to string.Format
path = os.path.dirname(__file__)
#files to look for strings to replace
files = [
# "Player\Main Menu\MainMenu_DifficultySelection.cs",
# "Player\Main Menu\MainMenu.cs",
# "Player\Main Menu\MainMenu_Guide.cs",
# "Player\Main Menu\MainMenu_HUD.cs",
# "Player\Main Menu\MainMenu_Inventory.cs",
# "Player\Main Menu\MainMenu_Perks.cs",
# "Player\Main Menu\MainMenu_Spells.cs",
# "Player\Crafting\Empowering.cs",
# "Player\Crafting\IndividualRerolling.cs",
# "Player\Crafting\Polishing.cs",
# "Player\Crafting\Reforging.cs",
# "Player\Crafting\Rerolling.cs",
"Player\Perks\PerkDatabase.cs",
"Player\Spells\SpellDataBase.cs",
# "Items\ItemDataBase_StatDefinitions.cs",
# "Items\ItemDataBase_ItemDefinitions.cs",
# "Items\Item.cs",
"Res\ResourceLoader.cs",
]
ignore = ['"P"', '"P0"', '"N"', '"N0"', '"\\n"', '""']
pattern2 = re.compile(r'(?<=\$)"([^"]|((?<=\\)"))+"') #find strings that start with $ with \" characters included
pattern3 = re.compile(r'(\{[^}]+\})') # find curly brackets and its content
for file in files:
p = os.path.join(path, file)
f = open(p, "r", encoding="utf-8")
lines = f.readlines()
f.close()
for idx, line in enumerate(lines):
if(pattern2.search(line)):
print("replacing $" + line)
format_params = ""
brackets = list(pattern3.finditer(line))
n = len(brackets) - 1
rev_brackets = reversed(brackets)
for bracket in rev_brackets:
content = bracket.group()[1:-1]
format_params = ", " + content + format_params
line = line[:bracket.start()] + '{'+str(n)+'}' + line[bracket.end():]
n-=1
match2 = pattern2.search(line)
line = line[:match2.start()-1] +"string.Format(" + match2.group() + format_params+ ")" + line[match2.end():]
print("with "+ line+ "\n")
lines[idx] = line
f = open(p,"w",encoding="utf-8")
f.writelines(lines)
f.close()