Skip to content

Commit 781e7e1

Browse files
committed
Add python script for automatic RTS_INTERNAL removals
1 parent 9584719 commit 781e7e1

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

scripts/cpp/remove_rts_internal.py

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

0 commit comments

Comments
 (0)