forked from itslunaranyo/copper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qc2def.py
79 lines (67 loc) · 1.94 KB
/
qc2def.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os, sys
# gtkradiant doesn't add skill flags by default and I'd rather keep the source
# clean by adding them here instead, because what else is python for?
def addSkillFlags(deftxt):
deflines = deftxt.split("\n")
init = deflines[0]
initbits = init.split()
cname = initbits[1]
if (cname == "func_group" or cname.startswith("func_detail") or cname == "worldspawn" or cname == "misc_external_map"):
return deftxt
if (initbits[5][0] == '('):
# point ent
while (len(initbits) < 19):
initbits.append("?")
else:
# brush ent
while (len(initbits) < 14):
initbits.append("?")
initbits += ["NotEasy", "NotNormal", "NotHard", "NotDeathmatch", "CoopOnly", "NotCoop"]
if (cname.startswith("monster") or ("Alt Target" in deftxt) or ("alt target" in deftxt)):
initbits += ["AltTarget"]
deflines[0] = " ".join(initbits)
deftxt = "\n".join(deflines)
return deftxt.expandtabs(4)
def getQuakeds(qc):
deftxt = ""
defOut = ""
numDefs = 0
start = 0
end = 0
while 1:
start = qc.find("/*QUAKED",start)
if start == -1:
return defOut, numDefs
#print( "adding ", qc[start + 9 : qc.find(" ",start + 9)] )
end = qc.find("*/",start) + 2
deftxt = addSkillFlags(qc[start : end])
defOut += "\n" + deftxt + "\n"
numDefs += 1
start = end
def go():
cwd = os.path.dirname(os.path.realpath(__file__))
defDir = os.path.normpath(os.path.join(cwd, ".."))
defName = ""
if len(sys.argv) > 1:
defName = sys.argv[1]
else:
defName = os.path.basename(defDir)
defFile = defName + ".def"
print("Scanning *.qc for def comments...")
defOut = ""
numDefs = 0
for qcfn in os.listdir(cwd):
if not qcfn.endswith(".qc"):
continue
with open(os.path.join(cwd, qcfn), "r") as qcfile:
qc = qcfile.read()
defs, n = getQuakeds(qc)
defOut += defs
numDefs += n
dfn = os.path.join(defDir, defFile)
print("Writing to",dfn)
with open(dfn, "w") as df:
df.write(defOut)
print("Completed, found",numDefs)
if __name__ == "__main__":
go()