forked from itslunaranyo/copper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qc2fgd.py
84 lines (71 loc) · 2.01 KB
/
qc2fgd.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
80
81
82
83
84
import os, datetime, sys
# TODO: put all the baseclasses first
def getQuakeds(qc):
defOut = ""
numDefs = 0
start = 0
end = 0
while 1:
start = qc.find("/*FGD",start)
if start == -1:
return defOut, numDefs
#print( "adding ", qc[start + 9 : qc.find(" ",start + 9)] )
end = qc.find("*/",start)
defOut += qc[start + 5 : end].strip() + "\n"
numDefs += 1
start = end
def getQCs():
# load every file listed in progs.src except the header
with open("progs.src",'r') as progsfile:
progslines = list(progsfile)[1:] # first line is output filename, skip it
qcfiles = []
for line in progslines:
qcfilename = line.partition("//")[0].strip()
if (len(qcfilename) < 4):
continue
if qcfilename[-3:] != ".qc":
continue
qcfiles.append(qcfilename)
return qcfiles
def go():
cwd = os.path.dirname(os.path.realpath(__file__))
qcDir = cwd + "\\"
fgdDir = os.path.normpath(cwd + "\\..\\")
fgdName = ""
if len(sys.argv) > 1:
fgdName = sys.argv[1]
else:
fgdName = os.path.basename(fgdDir)
fgdFile = fgdName + ".fgd"
fgdDir += "\\"
print("Scanning *.qc for FGD comments...")
defOut = """// Copper Quake game definition file (.fgd)
// for Worldcraft 1.6 and above
// If something is wrong with this file, check
// > http://lunaran.com/files/copper.fgd <
// for the very latest version before anything else
// Based heavily on quake.fgd by autolycus/czg/et al
// Generated from Copper QuakeC source comments on """
numDefs = 0
defOut += datetime.datetime.now().strftime("%m.%d.%Y")
defOut += "\n"
# TODO: open quakec in progs.src order for better fgd order control
qcfiles = getQCs()
for qcfn in qcfiles:
if not qcfn.endswith(".qc"):
continue
with open(qcDir+qcfn, "r") as qcfile:
qc = qcfile.read()
defs, n = getQuakeds(qc)
if (n):
defOut += "\n//\n// " + qcfn + "\n//\n"
defOut += defs
numDefs += n
defOut += "\n"
dfn = fgdDir + fgdFile
print("Writing to",dfn)
with open(dfn, "w") as df:
df.write(defOut)
print("Completed, found",numDefs)
if __name__ == "__main__":
go()