2008-04-27 05:59:38 +00:00
|
|
|
|
2008-06-12 10:51:48 +00:00
|
|
|
|
|
|
|
GlobalIdCounter = 0
|
|
|
|
def GetID():
|
|
|
|
global GlobalIdCounter
|
|
|
|
GlobalIdCounter += 1
|
|
|
|
return GlobalIdCounter
|
|
|
|
def GetUID():
|
|
|
|
return "x%d"%GetID()
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
def FixCasing(Str):
|
|
|
|
NewStr = ""
|
|
|
|
NextUpperCase = True
|
|
|
|
for c in Str:
|
|
|
|
if NextUpperCase:
|
|
|
|
NextUpperCase = False
|
|
|
|
NewStr += c.upper()
|
|
|
|
else:
|
|
|
|
if c == "_":
|
|
|
|
NextUpperCase = True
|
|
|
|
else:
|
|
|
|
NewStr += c.lower()
|
|
|
|
return NewStr
|
|
|
|
|
|
|
|
def FormatName(type, name):
|
|
|
|
if "*" in type:
|
|
|
|
return "m_p" + FixCasing(name)
|
|
|
|
if "[]" in type:
|
|
|
|
return "m_a" + FixCasing(name)
|
|
|
|
return "m_" + FixCasing(name)
|
|
|
|
|
2008-06-12 10:51:48 +00:00
|
|
|
class BaseType:
|
|
|
|
def __init__(self, type_name):
|
|
|
|
self._type_name = type_name
|
|
|
|
self._target_name = "INVALID"
|
|
|
|
self._id = GetID() # this is used to remeber what order the members have in structures etc
|
|
|
|
|
|
|
|
def Identifyer(self): return "x"+str(self._id)
|
|
|
|
def TargetName(self): return self._target_name
|
|
|
|
def TypeName(self): return self._type_name
|
|
|
|
def ID(self): return self._id;
|
|
|
|
|
|
|
|
def EmitDeclaration(self, name):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["%s %s;"%(self.TypeName(), FormatName(self.TypeName(), name))]
|
2008-06-12 10:51:48 +00:00
|
|
|
def EmitPreDefinition(self, target_name):
|
|
|
|
self._target_name = target_name
|
|
|
|
return []
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
return []
|
|
|
|
|
|
|
|
class MemberType:
|
|
|
|
def __init__(self, name, var):
|
|
|
|
self.name = name
|
|
|
|
self.var = var
|
|
|
|
|
|
|
|
class Struct(BaseType):
|
|
|
|
def __init__(self, type_name):
|
|
|
|
BaseType.__init__(self, type_name)
|
|
|
|
def Members(self):
|
|
|
|
def sorter(a,b):
|
|
|
|
return a.var.ID()-b.var.ID()
|
|
|
|
m = []
|
|
|
|
for name in self.__dict__:
|
|
|
|
if name[0] == "_":
|
|
|
|
continue
|
|
|
|
m += [MemberType(name, self.__dict__[name])]
|
|
|
|
try:
|
|
|
|
m.sort(sorter)
|
|
|
|
except:
|
|
|
|
for v in m:
|
|
|
|
print v.name, v.var
|
|
|
|
sys.exit(-1)
|
|
|
|
return m
|
|
|
|
|
|
|
|
def EmitTypeDeclaration(self, name):
|
|
|
|
lines = []
|
|
|
|
lines += ["struct " + self.TypeName()]
|
|
|
|
lines += ["{"]
|
|
|
|
for member in self.Members():
|
|
|
|
lines += ["\t"+l for l in member.var.EmitDeclaration(member.name)]
|
|
|
|
lines += ["};"]
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def EmitPreDefinition(self, target_name):
|
|
|
|
BaseType.EmitPreDefinition(self, target_name)
|
|
|
|
lines = []
|
|
|
|
for member in self.Members():
|
|
|
|
lines += member.var.EmitPreDefinition(target_name+"."+member.name)
|
|
|
|
return lines
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
lines = ["/* %s */ {" % self.TargetName()]
|
|
|
|
for member in self.Members():
|
|
|
|
lines += ["\t" + " ".join(member.var.EmitDefinition("")) + ","]
|
|
|
|
lines += ["}"]
|
|
|
|
return lines
|
|
|
|
|
|
|
|
class Array(BaseType):
|
|
|
|
def __init__(self, type):
|
|
|
|
BaseType.__init__(self, type.TypeName())
|
|
|
|
self.type = type
|
|
|
|
self.items = []
|
|
|
|
def Add(self, instance):
|
|
|
|
if instance.TypeName() != self.type.TypeName():
|
|
|
|
error("bah")
|
|
|
|
self.items += [instance]
|
|
|
|
def EmitDeclaration(self, name):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["int m_Num%s;"%(FixCasing(name)),
|
|
|
|
"%s *%s;"%(self.TypeName(), FormatName("[]", name))]
|
2008-06-12 10:51:48 +00:00
|
|
|
def EmitPreDefinition(self, target_name):
|
|
|
|
BaseType.EmitPreDefinition(self, target_name)
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
i = 0
|
|
|
|
for item in self.items:
|
|
|
|
lines += item.EmitPreDefinition("%s[%d]"%(self.Identifyer(), i))
|
|
|
|
i += 1
|
|
|
|
|
2008-09-01 17:30:20 +00:00
|
|
|
if len(self.items):
|
|
|
|
lines += ["static %s %s[] = {"%(self.TypeName(), self.Identifyer())]
|
|
|
|
for item in self.items:
|
|
|
|
itemlines = item.EmitDefinition("")
|
|
|
|
lines += ["\t" + " ".join(itemlines).replace("\t", " ") + ","]
|
|
|
|
lines += ["};"]
|
|
|
|
else:
|
|
|
|
lines += ["static %s *%s = 0;"%(self.TypeName(), self.Identifyer())]
|
|
|
|
|
2008-06-12 10:51:48 +00:00
|
|
|
return lines
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
return [str(len(self.items))+","+self.Identifyer()]
|
|
|
|
|
|
|
|
# Basic Types
|
|
|
|
|
|
|
|
class Int(BaseType):
|
|
|
|
def __init__(self, value):
|
|
|
|
BaseType.__init__(self, "int")
|
|
|
|
self.value = value
|
|
|
|
def Set(self, value):
|
|
|
|
self.value = value
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
return ["%d"%self.value]
|
|
|
|
#return ["%d /* %s */"%(self.value, self._target_name)]
|
|
|
|
|
|
|
|
class Float(BaseType):
|
|
|
|
def __init__(self, value):
|
|
|
|
BaseType.__init__(self, "float")
|
|
|
|
self.value = value
|
|
|
|
def Set(self, value):
|
|
|
|
self.value = value
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
return ["%f"%self.value]
|
|
|
|
#return ["%d /* %s */"%(self.value, self._target_name)]
|
|
|
|
|
|
|
|
class String(BaseType):
|
|
|
|
def __init__(self, value):
|
|
|
|
BaseType.__init__(self, "const char*")
|
|
|
|
self.value = value
|
|
|
|
def Set(self, value):
|
|
|
|
self.value = value
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
return ['"'+self.value+'"']
|
|
|
|
|
|
|
|
class Pointer(BaseType):
|
|
|
|
def __init__(self, type, target):
|
|
|
|
BaseType.__init__(self, "%s*"%type().TypeName())
|
|
|
|
self.target = target
|
|
|
|
def Set(self, target):
|
|
|
|
self.target = target
|
|
|
|
def EmitDefinition(self, name):
|
|
|
|
return ["&"+self.target.TargetName()]
|
|
|
|
|
|
|
|
# helper functions
|
|
|
|
|
|
|
|
def EmitTypeDeclaration(root):
|
|
|
|
for l in root().EmitTypeDeclaration(""):
|
|
|
|
print l
|
|
|
|
|
|
|
|
def EmitDefinition(root, name):
|
|
|
|
for l in root.EmitPreDefinition(name):
|
|
|
|
print l
|
|
|
|
print "%s %s = " % (root.TypeName(), name)
|
|
|
|
for l in root.EmitDefinition(name):
|
|
|
|
print l
|
|
|
|
print ";"
|
|
|
|
|
|
|
|
# Network stuff after this
|
|
|
|
|
2008-04-27 05:59:38 +00:00
|
|
|
class Object:
|
|
|
|
pass
|
|
|
|
|
2008-10-19 16:50:57 +00:00
|
|
|
class Enum:
|
2008-04-27 05:59:38 +00:00
|
|
|
def __init__(self, name, values):
|
|
|
|
self.name = name
|
|
|
|
self.values = values
|
|
|
|
|
2008-10-19 16:50:57 +00:00
|
|
|
class Flags:
|
2008-08-27 15:51:09 +00:00
|
|
|
def __init__(self, name, values):
|
|
|
|
self.name = name
|
|
|
|
self.values = values
|
|
|
|
|
2008-04-27 05:59:38 +00:00
|
|
|
class NetObject:
|
|
|
|
def __init__(self, name, variables):
|
|
|
|
l = name.split(":")
|
2010-05-29 07:25:38 +00:00
|
|
|
self.name = l[0]
|
2008-04-27 05:59:38 +00:00
|
|
|
self.base = ""
|
|
|
|
if len(l) > 1:
|
|
|
|
self.base = l[1]
|
2010-05-29 07:25:38 +00:00
|
|
|
self.base_struct_name = "CNetObj_%s" % self.base
|
|
|
|
self.struct_name = "CNetObj_%s" % self.name
|
2008-04-27 05:59:38 +00:00
|
|
|
self.enum_name = "NETOBJTYPE_%s" % self.name.upper()
|
|
|
|
self.variables = variables
|
|
|
|
def emit_declaration(self):
|
|
|
|
if self.base:
|
|
|
|
lines = ["struct %s : public %s"%(self.struct_name,self.base_struct_name), "{"]
|
|
|
|
else:
|
|
|
|
lines = ["struct %s"%self.struct_name, "{"]
|
|
|
|
for v in self.variables:
|
|
|
|
lines += ["\t"+line for line in v.emit_declaration()]
|
|
|
|
lines += ["};"]
|
|
|
|
return lines
|
|
|
|
def emit_validate(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
lines = ["case %s:" % self.enum_name]
|
2008-04-27 05:59:38 +00:00
|
|
|
lines += ["{"]
|
2010-05-29 07:25:38 +00:00
|
|
|
lines += ["\t%s *pObj = (%s *)pData;"%(self.struct_name, self.struct_name)]
|
|
|
|
lines += ["\tif(sizeof(*pObj) != Size) return -1;"]
|
2008-04-27 05:59:38 +00:00
|
|
|
for v in self.variables:
|
|
|
|
lines += ["\t"+line for line in v.emit_validate()]
|
|
|
|
lines += ["\treturn 0;"]
|
|
|
|
lines += ["}"]
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
class NetEvent(NetObject):
|
|
|
|
def __init__(self, name, variables):
|
|
|
|
NetObject.__init__(self, name, variables)
|
|
|
|
self.base_struct_name = "NETEVENT_%s" % self.base.upper()
|
|
|
|
self.struct_name = "NETEVENT_%s" % self.name.upper()
|
|
|
|
self.enum_name = "NETEVENTTYPE_%s" % self.name.upper()
|
|
|
|
|
|
|
|
class NetMessage(NetObject):
|
|
|
|
def __init__(self, name, variables):
|
|
|
|
NetObject.__init__(self, name, variables)
|
2010-05-29 07:25:38 +00:00
|
|
|
self.base_struct_name = "CNetMsg_%s" % self.base
|
|
|
|
self.struct_name = "CNetMsg_%s" % self.name
|
2008-04-27 05:59:38 +00:00
|
|
|
self.enum_name = "NETMSGTYPE_%s" % self.name.upper()
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_unpack(self):
|
|
|
|
lines = []
|
2010-05-29 07:25:38 +00:00
|
|
|
lines += ["case %s:" % self.enum_name]
|
2008-06-12 10:51:48 +00:00
|
|
|
lines += ["{"]
|
2010-05-29 07:25:38 +00:00
|
|
|
lines += ["\t%s *pMsg = (%s *)m_aMsgData;" % (self.struct_name, self.struct_name)]
|
|
|
|
lines += ["\t(void)pMsg;"]
|
2008-06-12 10:51:48 +00:00
|
|
|
for v in self.variables:
|
|
|
|
lines += ["\t"+line for line in v.emit_unpack()]
|
|
|
|
for v in self.variables:
|
|
|
|
lines += ["\t"+line for line in v.emit_unpack_check()]
|
2010-05-29 07:25:38 +00:00
|
|
|
lines += ["} break;"]
|
2008-06-12 10:51:48 +00:00
|
|
|
return lines
|
|
|
|
def emit_declaration(self):
|
|
|
|
extra = []
|
2010-05-29 07:25:38 +00:00
|
|
|
extra += ["\tint MsgID() const { return %s; }" % self.enum_name]
|
|
|
|
extra += ["\t"]
|
|
|
|
extra += ["\tbool Pack(CMsgPacker *pPacker)"]
|
2008-06-12 10:51:48 +00:00
|
|
|
extra += ["\t{"]
|
2010-05-29 07:25:38 +00:00
|
|
|
#extra += ["\t\tmsg_pack_start(%s, flags);"%self.enum_name]
|
2008-06-12 10:51:48 +00:00
|
|
|
for v in self.variables:
|
|
|
|
extra += ["\t\t"+line for line in v.emit_pack()]
|
2010-05-29 07:25:38 +00:00
|
|
|
extra += ["\t\treturn pPacker->Error() != 0;"]
|
2008-06-12 10:51:48 +00:00
|
|
|
extra += ["\t}"]
|
|
|
|
|
|
|
|
|
|
|
|
lines = NetObject.emit_declaration(self)
|
|
|
|
lines = lines[:-1] + extra + lines[-1:]
|
|
|
|
return lines
|
|
|
|
|
2008-04-27 05:59:38 +00:00
|
|
|
|
|
|
|
class NetVariable:
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
def emit_declaration(self):
|
|
|
|
return []
|
|
|
|
def emit_validate(self):
|
|
|
|
return []
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_pack(self):
|
|
|
|
return []
|
|
|
|
def emit_unpack(self):
|
|
|
|
return []
|
|
|
|
def emit_unpack_check(self):
|
|
|
|
return []
|
2008-04-27 05:59:38 +00:00
|
|
|
|
|
|
|
class NetString(NetVariable):
|
|
|
|
def emit_declaration(self):
|
|
|
|
return ["const char *%s;"%self.name]
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_unpack(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["pMsg->%s = pUnpacker->GetString();" % self.name]
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_pack(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["pPacker->AddString(%s, -1);" % self.name]
|
2008-04-27 05:59:38 +00:00
|
|
|
|
|
|
|
class NetIntAny(NetVariable):
|
|
|
|
def emit_declaration(self):
|
|
|
|
return ["int %s;"%self.name]
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_unpack(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["pMsg->%s = pUnpacker->GetInt();" % self.name]
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_pack(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["pPacker->AddInt(%s);" % self.name]
|
2008-04-27 05:59:38 +00:00
|
|
|
|
|
|
|
class NetIntRange(NetIntAny):
|
|
|
|
def __init__(self, name, min, max):
|
|
|
|
NetIntAny.__init__(self,name)
|
|
|
|
self.min = str(min)
|
|
|
|
self.max = str(max)
|
|
|
|
def emit_validate(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["ClampInt(\"%s\", pObj->%s, %s, %s);"%(self.name,self.name, self.min, self.max)]
|
2008-06-12 10:51:48 +00:00
|
|
|
def emit_unpack_check(self):
|
2010-05-29 07:25:38 +00:00
|
|
|
return ["if(pMsg->%s < %s || pMsg->%s > %s) { m_pMsgFailedOn = \"%s\"; break; }" % (self.name, self.min, self.name, self.max, self.name)]
|
2008-04-27 05:59:38 +00:00
|
|
|
|
|
|
|
class NetBool(NetIntRange):
|
|
|
|
def __init__(self, name):
|
|
|
|
NetIntRange.__init__(self,name,0,1)
|
|
|
|
|
|
|
|
class NetTick(NetIntRange):
|
|
|
|
def __init__(self, name):
|
|
|
|
NetIntRange.__init__(self,name,0,'max_int')
|