def GenerateSource(self, srcPath, hdrPath): f = filewriter.FileWriter() f.Open(srcPath) f.WriteLine("// NIDL #version:{}#".format(self.version)) head, tail = ntpath.split(hdrPath) hdrInclude = tail or ntpath.basename(head) head, tail = ntpath.split(srcPath) srcFileName = tail or ntpath.basename(head) IDLDocument.WriteSourceHeader(f, srcFileName) IDLDocument.AddInclude(f, hdrInclude) hasMessages = "messages" in self.document if hasMessages: IDLDocument.AddInclude(f, "scripting/bindings.h") if "attributes" in self.document: IDLDocument.BeginNamespaceOverride(f, self.document, "Attr") IDLAttribute.WriteAttributeSourceDefinitions(f, self.document) IDLDocument.EndNamespaceOverride(f, self.document, "Attr") f.WriteLine("") # Add additional dependencies to document. if "dependencies" in self.document: for dependency in self.document["dependencies"]: fstream = open(dependency, 'r') depDocument = sjson.loads(fstream.read()) deps = depDocument["attributes"] # Add all attributes to this document self.document["attributes"].update(deps) fstream.close() hasComponents = "components" in self.document if hasComponents or hasMessages: IDLDocument.BeginNamespace(f, self.document) if hasMessages: IDLProtocol.WriteMessageImplementation(f, self.document) if hasComponents: namespace = IDLDocument.GetNamespace(self.document) for componentName, component in self.document[ "components"].items(): f.WriteLine("") componentWriter = IDLComponent.ComponentClassWriter( f, self.document, component, componentName, namespace) componentWriter.WriteClassImplementation() f.WriteLine("") IDLDocument.EndNamespace(f, self.document) f.Close()
def WriteMessageImplementation(f, document): # We need to set all messages within the same module at the same time. f.WriteLine("PYBIND11_EMBEDDED_MODULE({}, m)".format(f.fileName)) f.WriteLine("{") f.IncreaseIndent() f.WriteLine('m.doc() = "namespace {}";'.format( IDLDocument.GetNamespace(document))) for messageName, message in document["messages"].items(): if not "export" in message or message["export"] == True: messageDescription = "" if "description" in message: messageDescription = message["description"] f.WriteLine( 'm.def("{MSG}", &{MSG}::Send, "{MessageDescription}");'.format( MSG=messageName, MessageDescription=messageDescription)) f.DecreaseIndent() f.WriteLine("}")
def WriteEnumJsonSerializers(f, document): namespace = IDLDocument.GetNamespace(document) for enumName, enum in document["enums"].items(): f.WriteLine( 'template<> void JsonReader::Get<{namespace}::{name}>({namespace}::{name}& ret, const char* attr)' .format(namespace=namespace, name=enumName)) f.WriteLine('{') f.IncreaseIndent() f.WriteLine("const pjson::value_variant* node = this->GetChild(attr);") f.WriteLine("if (node->is_string())") f.WriteLine("{") f.IncreaseIndent() f.WriteLine("Util::String str = node->as_string_ptr();") for value in enum: f.WriteLine( 'if (str == "{val}") {{ ret = {namespace}::{name}::{val}; return; }}' .format(val=value, namespace=namespace, name=enumName)) f.DecreaseIndent() f.WriteLine("}") f.WriteLine("else if (node->is_int())") f.WriteLine("{") f.IncreaseIndent() f.WriteLine('ret = ({namespace}::{name})node->as_int32();'.format( namespace=namespace, name=enumName)) f.WriteLine('return;') f.DecreaseIndent() f.WriteLine("}") f.WriteLine('ret = {namespace}::{name}();'.format(namespace=namespace, name=enumName)) f.DecreaseIndent() f.WriteLine("}") f.WriteLine("") f.WriteLine( 'template<> void JsonWriter::Add<{namespace}::{name}>({namespace}::{name} const& value, Util::String const& attr)' .format(namespace=namespace, name=enumName)) f.WriteLine('{') f.IncreaseIndent() f.WriteLine('this->Add<int>(value, attr);') f.DecreaseIndent() f.WriteLine("}") f.WriteLine("")
def WriteStructJsonSerializers(f, document): namespace = IDLDocument.GetNamespace(document) for prop in properties: if not prop.isStruct: continue f.WriteLine( 'template<> void JsonReader::Get<{namespace}::{name}>({namespace}::{name}& ret, const char* attr)' .format(namespace=namespace, name=prop.propertyName)) f.WriteLine('{') f.IncreaseIndent() f.WriteLine('ret = {namespace}::{name}();'.format( namespace=namespace, name=prop.propertyName)) f.WriteLine("const pjson::value_variant* node = this->GetChild(attr);") f.WriteLine("if (node->is_object())") f.WriteLine("{") f.IncreaseIndent() for var in prop.variables: f.WriteLine( 'if (this->HasAttr("{fieldName}")) this->Get<{type}>(ret.{fieldName}, "{fieldName}");' .format(fieldName=var.name, type=var.type)) f.DecreaseIndent() f.WriteLine("}") f.DecreaseIndent() f.WriteLine("}") f.WriteLine("") f.WriteLine( 'template<> void JsonWriter::Add<{namespace}::{name}>({namespace}::{name} const& value, Util::String const& attr)' .format(namespace=namespace, name=prop.propertyName)) f.WriteLine('{') f.IncreaseIndent() f.WriteLine("this->BeginObject(attr.AsCharPtr());") for var in prop.variables: f.WriteLine( 'this->Add<{type}>(value.{fieldName}, "{fieldName}");'.format( fieldName=var.name, type=var.type)) f.WriteLine("this->End();") f.DecreaseIndent() f.WriteLine("}") f.WriteLine("")
def GenerateHeader(self, hdrPath): f = filewriter.FileWriter() f.Open(hdrPath) f.WriteLine("// NIDL #version:{}#".format(self.version)) attributeLibraries = [] # Add additional dependencies to document. if "dependencies" in self.document: for dependency in self.document["dependencies"]: fileName = '{}.h'.format( os.path.splitext(dependency)[0]).lower() attributeLibraries.append(fileName) attributeLibraries.append("game/entity.h") if "components" in self.document: attributeLibraries.append("game/component/component.h") if "messages" in self.document: attributeLibraries.append("game/messaging/message.h") IDLDocument.WriteIncludeHeader(f) IDLDocument.WriteIncludes(f, self.document) IDLComponent.WriteIncludes(f, attributeLibraries) # Generate attributes include file if "attributes" in self.document: IDLDocument.WriteAttributeLibraryDeclaration(f) if "enums" in self.document: IDLDocument.BeginNamespace(f, self.document) IDLAttribute.WriteEnumeratedTypes(f, self.document) IDLDocument.EndNamespace(f, self.document) f.WriteLine("") if "attributes" in self.document: IDLDocument.BeginNamespaceOverride(f, self.document, "Attr") IDLAttribute.WriteAttributeHeaderDeclarations(f, self.document) IDLDocument.EndNamespaceOverride(f, self.document, "Attr") f.WriteLine("") # Add additional dependencies to document. if "dependencies" in self.document: for dependency in self.document["dependencies"]: fstream = open(dependency, 'r') depDocument = sjson.loads(fstream.read()) deps = depDocument["attributes"] # Add all attributes to this document self.document["attributes"].update(deps) fstream.close() # Generate components base classes headers hasMessages = "messages" in self.document hasComponents = "components" in self.document if hasComponents or hasMessages: IDLDocument.BeginNamespace(f, self.document) if hasMessages: IDLProtocol.WriteMessageDeclarations(f, self.document) if hasComponents: namespace = IDLDocument.GetNamespace(self.document) for componentName, component in self.document[ "components"].items(): componentWriter = IDLComponent.ComponentClassWriter( f, self.document, component, componentName, namespace) componentWriter.WriteClassDeclaration() IDLDocument.EndNamespace(f, self.document) f.Close() return