Skip to content

Commit 67607c4

Browse files
committed
Add python script for automatic RTS_INTERNAL removals (#1231)
1 parent bc9e235 commit 67607c4

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

scripts/cpp/remove_rts_internal.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Created with python 3.11.4
2+
3+
# This script helps removing RTS_INTERNAL words from the various CPP files.
4+
# Just run it.
5+
6+
import glob
7+
import os
8+
9+
10+
def modifyLine(line: str) -> str:
11+
searchWords = [
12+
"!defined(RTS_DEBUG) && !defined(RTS_INTERNAL)",
13+
"!defined(RTS_INTERNAL) && !defined(RTS_DEBUG)",
14+
"defined(RTS_DEBUG) || defined(RTS_INTERNAL)",
15+
"defined(RTS_INTERNAL) || defined(RTS_DEBUG)",
16+
"defined( RTS_INTERNAL ) || defined( RTS_DEBUG )",
17+
"defined RTS_DEBUG || defined RTS_INTERNAL",
18+
"RTS_DEBUG || RTS_INTERNAL",
19+
]
20+
21+
replaceWords = [
22+
"!defined(RTS_DEBUG)",
23+
"!defined(RTS_DEBUG)",
24+
"defined(RTS_DEBUG)",
25+
"defined(RTS_DEBUG)",
26+
"defined(RTS_DEBUG)",
27+
"defined(RTS_DEBUG)",
28+
"RTS_DEBUG",
29+
]
30+
31+
for searchIdx, searchWord in enumerate(searchWords):
32+
wordBegin = line.find(searchWord)
33+
wordEnd = wordBegin + len(searchWord)
34+
if wordBegin >= 0:
35+
replaceWord = replaceWords[searchIdx]
36+
lineCopy = line[:wordBegin] + replaceWord + line[wordEnd:]
37+
return lineCopy
38+
39+
return line
40+
41+
42+
def main():
43+
current_dir = os.path.dirname(os.path.abspath(__file__))
44+
root_dir = os.path.join(current_dir, "..", "..")
45+
root_dir = os.path.normpath(root_dir)
46+
core_dir = os.path.join(root_dir, "Core")
47+
generals_dir = os.path.join(root_dir, "Generals")
48+
generalsmd_dir = os.path.join(root_dir, "GeneralsMD")
49+
fileNames = []
50+
fileNames.extend(glob.glob(os.path.join(core_dir, '**', '*.h'), recursive=True))
51+
fileNames.extend(glob.glob(os.path.join(core_dir, '**', '*.cpp'), recursive=True))
52+
fileNames.extend(glob.glob(os.path.join(core_dir, '**', '*.inl'), recursive=True))
53+
fileNames.extend(glob.glob(os.path.join(generals_dir, '**', '*.h'), recursive=True))
54+
fileNames.extend(glob.glob(os.path.join(generals_dir, '**', '*.cpp'), recursive=True))
55+
fileNames.extend(glob.glob(os.path.join(generals_dir, '**', '*.inl'), recursive=True))
56+
fileNames.extend(glob.glob(os.path.join(generalsmd_dir, '**', '*.h'), recursive=True))
57+
fileNames.extend(glob.glob(os.path.join(generalsmd_dir, '**', '*.cpp'), recursive=True))
58+
fileNames.extend(glob.glob(os.path.join(generalsmd_dir, '**', '*.inl'), recursive=True))
59+
60+
for fileName in fileNames:
61+
with open(fileName, 'r', encoding="cp1252") as file:
62+
try:
63+
lines = file.readlines()
64+
except UnicodeDecodeError:
65+
continue # Not good.
66+
with open(fileName, 'w', encoding="cp1252") as file:
67+
skipLine = 0
68+
for line in lines:
69+
# Skip RTS_INTERNAL ifdef blocks
70+
if skipLine > 0:
71+
if "#if" in line:
72+
skipLine += 1
73+
elif "#endif" in line:
74+
skipLine -= 1
75+
continue
76+
if skipLine > 0:
77+
continue
78+
if line == "#ifdef RTS_INTERNAL\n" or line == "#if defined(RTS_INTERNAL)\n":
79+
skipLine += 1
80+
continue
81+
82+
line = modifyLine(line)
83+
file.write(line)
84+
85+
return
86+
87+
88+
if __name__ == "__main__":
89+
main()

0 commit comments

Comments
 (0)