]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/tools/unc-macro-fix.py
script to fix macros
[xonotic/xonotic-data.pk3dir.git] / qcsrc / tools / unc-macro-fix.py
diff --git a/qcsrc/tools/unc-macro-fix.py b/qcsrc/tools/unc-macro-fix.py
new file mode 100755 (executable)
index 0000000..c2ada26
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+# removes redundant { and } from macros because uncrustify is too dumb to indent them properly (or even not touch them)
+
+# all those fancy unix utilities like grep, sed and awk are either woefully inadequate for this or just completely
+# unreadable with their arcane syntaxes - and yes, the plural is intentional because why extend existing tools
+# when you can write your own with incompatible flags, commands and regex flavors, pass strings between them
+# and call it the unix "philosophy"
+
+import re
+import glob
+
+all_files = set(glob.glob("**/*.qc", recursive=True) + glob.glob("**/*.qh", recursive=True))
+all_files = {f for f in all_files if not f.startswith('qcsrc/dpdefs')}
+
+for file_name in all_files:
+       with open(file_name, "r+") as f:
+               s = f.read()
+               s = re.sub(r"MACRO_BEGIN *(?:\\\s*)?{((?:.|\n)*?)} *(?:\\\s*)?MACRO_END", r"MACRO_BEGIN\1MACRO_END", s)
+               f.seek(0)
+               f.truncate()
+               f.write(s)