summaryrefslogtreecommitdiff
path: root/devtools/search_and_replace_template.py
diff options
context:
space:
mode:
authorFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
committerFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
commit3bf9df6b2785fa6d951086978a3e66f49427166a (patch)
tree2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /devtools/search_and_replace_template.py
downloadarchived-source-engine-2018-hl2-src-master.tar.xz
archived-source-engine-2018-hl2-src-master.zip
Diffstat (limited to 'devtools/search_and_replace_template.py')
-rw-r--r--devtools/search_and_replace_template.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/devtools/search_and_replace_template.py b/devtools/search_and_replace_template.py
new file mode 100644
index 0000000..e714493
--- /dev/null
+++ b/devtools/search_and_replace_template.py
@@ -0,0 +1,58 @@
+
+# This file can be used as a template for search-and-replace operations across a bunch of files.
+# It's fairly slow, but it's still a lot faster than doing it by hand.
+# You can use foreach.exe or batch commands to run the script on a bunch of files at once.
+# It's a good idea to test your regular expressions beforehand though. To do this, just set g_bTest to 1.
+
+import sys
+import re
+
+
+g_bTest = 0
+
+
+# Read the file in.
+f = open( sys.argv[1], "rt" )
+lines = f.readlines()
+f.close()
+
+
+# First entry is the regular expression to look for.
+# Second entry is the text to replace it with.
+myREList = [
+ [re.compile( r"m_vecRenderOrigin" ), r"GetAbsOrigin()"],
+ [re.compile( r"m_vecRenderAngles" ), r"GetAbsAngles()"],
+ [re.compile( r"GetAbsOrigin\(\)\s*\=\s*(?P<setTo>.+);" ), r"SetAbsOrigin( \g<setTo> );" ],
+ [re.compile( r"GetAbsAngles\(\)\s*\=\s*(?P<setTo>.+);" ), r"SetAbsAngles( \g<setTo> );" ],
+ [re.compile( r"SetAbsOrigin\( GetAbsOrigin\(\) \);" ), r"" ],
+ [re.compile( r"SetAbsAngles\( GetAbsAngles\(\) \);" ), r"" ],
+ [re.compile( r"GetAbsOrigin\(\)\.Init\(\)" ), r"SetAbsOrigin( Vector( 0, 0, 0 ) )"],
+ [re.compile( r"GetAbsAngles\(\)\.Init\(\)" ), r"SetAbsAngles( Vector( 0, 0, 0 ) )"],
+]
+
+
+# Now replace occurrences of it.
+i = 1
+if g_bTest:
+ # If g_bTest is set, just print out the matches.
+ for x in lines:
+ startX = x
+ for curRE in myREList:
+ x = curRE[0].sub( curRE[1], x )
+
+ if x != startX:
+ print "%d: %s" % ( i, x )
+ i += 1
+else:
+ f = open( sys.argv[1], "wt" )
+
+ for x in lines:
+ for curRE in myREList:
+ x = curRE[0].sub( curRE[1], x )
+
+ f.write( x )
+
+ f.close()
+
+
+print sys.argv[1]