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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
# -*- coding: utf-8 -*-
#
# Copyright 2010 Andreas Löscher
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# @author Andreas Löscher
# @author Matthias Ableitner <spam@abma.de>
#
from helper import normstring, joinstrings
def parse_enums(filename):
""" String without comments and defines
"""
f=open(filename, "r")
line = "" # line to be processed
skipnext=False # are we in an unfinished multiline comment?
retval=[] # return value
events = {}
inenum = False
inenum = False
indefine = False
structname =""
structs = {} # Eventname -> (Structname, {membername->membertype})
pairs = {}
for actline in f.readlines():
# ignore exter "c"{
if 'extern "C"' in actline:
continue
# ignore lines starting with defines
if ("#" in actline):
if "\\" in actline:
indefine = True
continue
if indefine:
if "\\" in actline:
continue
else:
indefine = False
continue
# skip /* */ comments
cont=False # continue after while loop?
while (1): # for cases where "/* com */ /* com */"in one line
if not skipnext and "/*" in actline:
skipnext=True
cont=True
break
if skipnext and "*/" in actline:
skipnext=False
i = actline.find("*/")
actline=actline[(i+2):]
if skipnext:
cont=True
break
break
if cont:
cont=False
continue
# remove // comments
if ("//" in actline) and not ("};" in actline):
i = actline.find("//")
actline = actline[:i]
if (not inenum) and ("enum" in actline):
# store structname
inenum=True
continue
if inenum:
if "};" in actline:
vallist = normstring(line).split(",")
for val in vallist:
tmp = val.split("=")
if len(tmp)>1:
pairs[tmp[0].strip()] = int(tmp[-1])
break
else:
line = line + actline
f.close()
return pairs
def parse_structs(filename):
""" String without comments and defines
"""
f=open(filename, "r")
line = "" # line to be processed
skipnext=False # are we in an unfinished multiline comment?
retval=[] # return value
events = {}
inenum = False
instruct = False
indefine = False
structname =""
structs = {} # Eventname -> (Structname, {membername->membertype})
for actline in f.readlines():
# ignore exter "c"{
if 'extern "C"' in actline:
continue
# ignore lines starting with defines
if ("#" in actline):
if "\\" in actline:
indefine = True
continue
if indefine:
if "\\" in actline:
continue
else:
indefine = False
continue
# skip /* */ comments
cont=False # continue after while loop?
while (1): # for cases where "/* com */ /* com */"in one line
if not skipnext and "/*" in actline:
skipnext=True
cont=True
break
if skipnext and "*/" in actline:
skipnext=False
i = actline.find("*/")
actline=actline[(i+2):]
if skipnext:
cont=True
break
break
if cont:
cont=False
continue
# remove // comments
if ("//" in actline) and not ("};" in actline):
i = actline.find("//")
actline = actline[:i]
if (not instruct) and ("struct" in actline):
# store structname
structname = normstring(actline).split(" ")[1]
instruct=True
continue
if instruct:
if "};" in actline:
event = (normstring(actline).split(" "))[-1]
# process struct
memberline = normstring(line).split(";")
memberline.pop()
members = []
for member in memberline:
member=normstring(member).split(" ")
members.append((member[-1], joinstrings(member[:-1])))
structs[event] = (structname, members)
line = ""
instruct=False
continue
else:
line = line + actline
f.close()
return structs
def getevents(filename):
# Eventname -> (Structname, {membername->membertype})
events = parse_structs(filename)
for event in events:
sname, members = events[event]
argstring = ""
formatstring = "{"
for member, mtype in members:
if "int*" in mtype:
# special case, it is EVENT_PLAYER_COMMAND
# make this as generic as possible
nummember = "num"+member[0].upper()+member[1:]
formatstring+="sO"
argstring+="\""+member+"\","
argstring+="convert_intarray(((struct "+sname+"*)data)->"+member+", ((struct "+sname+"*)data)->"+nummember+"),"
elif "int" in mtype:
formatstring+="si"
argstring+="\""+member+"\","
argstring+="((struct "+sname+"*)data)->"+member+","
elif "char" in mtype:
formatstring+="ss"
argstring+="\""+member+"\","
argstring+="((struct "+sname+"*)data)->"+member+","
elif "float" in mtype:
formatstring+="sf"
argstring+="\""+member+"\","
argstring+="((struct "+sname+"*)data)->"+member+","
elif "bool" in mtype:
formatstring+="si"
argstring+="\""+member+"\","
argstring+="((struct "+sname+"*)data)->"+member+","
elif "SAIFloat3" in mtype:
formatstring+="sO"
argstring+="\""+member+"\","
argstring+="convert_SAIFloat3(&(((struct "+sname+"*)data)->"+member+")),"
elif "SSkirmishAICallback" in mtype:
formatstring+="sO"
argstring+="\""+member+"\","
argstring+="PyAICallback_New(((struct "+sname+"*)data)->"+member+"),"
formatstring = formatstring+ "}"
argstring = argstring[:-1]
buildstring = "Py_BuildValue(\""+formatstring+"\","+argstring+")"
events[event] = buildstring
return events
def getcommands(filename):
# Commandname -> (Structname, {membername->membertype})
commands = parse_structs(filename)
return commands
|