private static bool generateCsFile(string csFilename, string xmlFilename) { try { XmlDocument xmlDoc = new XmlDocument(); Encoding utf8WithoutBom = new UTF8Encoding(false); using (StreamReader fileStream = new StreamReader(xmlFilename, utf8WithoutBom)) { xmlDoc.Load(fileStream); } XmlNode rootNode_ = xmlDoc.DocumentElement; XmlNode versionNode = rootNode_.Attributes["version"]; bool bInvalid = false; if (versionNode == null) { bInvalid = true; } else { float version = float.Parse(versionNode.Value); if (version < 2.0) { bInvalid = true; } } if (bInvalid) { string msg = string.Format("{0}: \n{1}", Resources.InvalidMeta, xmlFilename); System.Windows.Forms.MessageBox.Show(msg, Resources.LoadError, MessageBoxButtons.OK); return(false); } XmlNode languageNode = rootNode_.Attributes["language"]; Plugin.SourceLanguage = (languageNode != null) ? languageNode.Value : ""; Stream s = File.Open(csFilename, FileMode.Create); StreamWriter wrtr = new StreamWriter(s); wrtr.WriteLine("using System;"); wrtr.WriteLine("using System.Collections;"); wrtr.WriteLine("using System.Collections.Generic;"); wrtr.WriteLine("using System.Text;"); wrtr.WriteLine("using Behaviac.Design;"); wrtr.WriteLine("using Behaviac.Design.Attributes;"); wrtr.WriteLine(""); // types XmlNode typesNode = null; foreach (XmlNode c in rootNode_.ChildNodes) { if (c.Name == "types") { typesNode = c; break; } } List <XmlNode> typesNodes = new List <XmlNode>(); if (typesNode != null) { foreach (XmlNode typeNode in typesNode.ChildNodes) { typesNodes.Add(typeNode); } } if (Workspace.CustomizedTypesXMLNode != null) { foreach (XmlNode typeNode in Workspace.CustomizedTypesXMLNode.ChildNodes) { typesNodes.Add(typeNode); } } Plugin.AllMetaTypes.Clear(); if (typesNodes.Count > 0) { wrtr.WriteLine("namespace XMLPluginBehaviac"); wrtr.WriteLine("{"); enumNodeDict.Clear(); structNodeDict.Clear(); foreach (XmlNode typeNode in typesNodes) { if (typeNode.Name == "enumtype") { writeEnumNode(typeNode, enumNodeDict, wrtr); } else if (typeNode.Name == "struct") { writeStructNode(typeNode, structNodeDict, wrtr); } } wrtr.WriteLine("}"); } // agents XmlNode agentsNode = null; foreach (XmlNode c in rootNode_.ChildNodes) { if (c.Name == "agents") { agentsNode = c; break; } } if (agentsNode != null) { wrtr.WriteLine("namespace XMLPluginBehaviac"); wrtr.WriteLine("{"); foreach (XmlNode agentNode in agentsNode.ChildNodes) { XmlNode classfullnameNode = agentNode.Attributes["classfullname"]; string classfullname = (classfullnameNode != null && classfullnameNode.Value.Length > 0) ? classfullnameNode.Value : ""; string className = HandleBaseName(classfullname); XmlAttribute baseNode = agentNode.Attributes["base"]; string baseName = (baseNode != null && baseNode.Value.Length > 0) ? baseNode.Value : "Agent"; // Then write out this class itself. XmlAttribute inheritedNode = agentNode.Attributes["inherited"]; string isinherited = (inheritedNode != null) ? inheritedNode.Value : "false"; XmlNode isRefTypeNode = agentNode.Attributes["IsRefType"]; string isRefType = (isRefTypeNode != null) ? isRefTypeNode.Value : "false"; XmlNode displayNameNode = agentNode.Attributes["DisplayName"]; string displayName = (displayNameNode != null && displayNameNode.Value.Length > 0) ? displayNameNode.Value : classfullname; XmlNode descNode = agentNode.Attributes["Desc"]; string desc = (descNode != null && descNode.Value.Length > 0) ? descNode.Value : displayName; wrtr.WriteLine("\t[Behaviac.Design.ClassDesc(\"{0}\", \"{1}\", {2}, {3}, \"{4}\", \"{5}\")]", classfullname, baseName, isinherited, isRefType, displayName, desc); wrtr.WriteLine("\tpublic class {0} : {1}", HandleHierarchyName(classfullname), (baseName == "Agent") ? "Behaviac.Design.Agent" : HandleHierarchyName(baseName)); wrtr.WriteLine("\t{"); { writeMembers(agentNode, className, wrtr, false); wrtr.WriteLine(""); writeMethods(agentNode, className, wrtr); //string methodDesc = "\t\t[Behaviac.Design.MethodDesc(\"Agent\", true, true, false, false, \"int\", \"VectorLength\", \"VectorLength\")]"; //string delegateStr = "\t\tpublic delegate int VectorLength(System.Collections.IList list);"; //wrtr.WriteLine(methodDesc); //wrtr.WriteLine(delegateStr); } wrtr.WriteLine("\t}\n"); } wrtr.WriteLine("}"); } // instances XmlNode instancesNode = null; foreach (XmlNode c in rootNode_.ChildNodes) { if (c.Name == "instances") { instancesNode = c; break; } } if (instancesNode != null) { foreach (XmlNode instanceNode in instancesNode.ChildNodes) { if (instanceNode.Name == "instance") { XmlAttribute nameNode = instanceNode.Attributes["name"]; XmlAttribute classNode = instanceNode.Attributes["class"]; XmlAttribute displayNameNode = instanceNode.Attributes["DisplayName"]; XmlAttribute descNode = instanceNode.Attributes["Desc"]; Plugin.AddInstanceName(nameNode.Value, classNode.Value, displayNameNode.Value, descNode.Value); } } } wrtr.Close(); s.Close(); return(true); } catch (XmlException ex) { string msg = string.Format("{0}:{1}", ex.SourceUri, ex.Message); System.Windows.Forms.MessageBox.Show(msg, Resources.InvalidMeta, MessageBoxButtons.OK); } return(false); }
private static bool generateCsFile(string csFilename, string metaFile) { try { XmlNode rootNode = null; XmlNode languageNode = null; if (!string.IsNullOrEmpty(metaFile) && File.Exists(metaFile)) { XmlDocument xmlDoc = new XmlDocument(); Encoding utf8WithoutBom = new UTF8Encoding(false); using (StreamReader fileStream = new StreamReader(metaFile, utf8WithoutBom)) { xmlDoc.Load(fileStream); } rootNode = xmlDoc.DocumentElement; if (rootNode.Name != "metas") { return(false); } XmlNode versionNode = rootNode.Attributes["version"]; bool bValid = false; if (versionNode != null) { try { int version = int.Parse(versionNode.Value); if (version >= 5) { bValid = true; } } catch (Exception) { } } if (!bValid) { string msg = string.Format(Resources.InvalidMetaInfo, metaFile); System.Windows.Forms.MessageBox.Show(msg, Resources.LoadError, MessageBoxButtons.OK); return(false); } languageNode = rootNode.Attributes["language"]; } Workspace.Current.Language = (languageNode != null) ? languageNode.Value : ""; Stream s = File.Open(csFilename, FileMode.Create); StreamWriter wrtr = new StreamWriter(s); wrtr.WriteLine("using System;"); wrtr.WriteLine("using System.Collections;"); wrtr.WriteLine("using System.Collections.Generic;"); wrtr.WriteLine("using System.Text;"); wrtr.WriteLine("using Behaviac.Design;"); wrtr.WriteLine("using Behaviac.Design.Attributes;"); wrtr.WriteLine(""); // types XmlNode typesNode = null; if (rootNode != null) { foreach (XmlNode c in rootNode.ChildNodes) { if (c.Name == "types") { typesNode = c; break; } } } List <XmlNode> typesNodes = new List <XmlNode>(); if (typesNode != null) { foreach (XmlNode typeNode in typesNode.ChildNodes) { typesNodes.Add(typeNode); } } if (Workspace.CustomizedTypesXMLNode != null) { foreach (XmlNode typeNode in Workspace.CustomizedTypesXMLNode.ChildNodes) { typesNodes.Add(typeNode); } } if (typesNodes.Count > 0) { wrtr.WriteLine("namespace XMLPluginBehaviac"); wrtr.WriteLine("{"); enumNodeDict.Clear(); structNodeDict.Clear(); foreach (XmlNode typeNode in typesNodes) { if (typeNode.Name == "enumtype") { writeEnumNode(typeNode, enumNodeDict, wrtr); } else if (typeNode.Name == "struct") { writeStructNode(typeNode, structNodeDict, wrtr); } } wrtr.WriteLine("}"); } // agents XmlNode agentsNode = null; if (rootNode != null) { foreach (XmlNode c in rootNode.ChildNodes) { if (c.Name == "agents") { agentsNode = c; break; } } } List <XmlNode> agentsNodes = new List <XmlNode>(); if (agentsNode != null) { foreach (XmlNode agentNode in agentsNode.ChildNodes) { agentsNodes.Add(agentNode); } } Dictionary <string, bool> customizedAgentNames = new Dictionary <string, bool>(); if (Workspace.CustomizedAgentsXMLNode != null) { foreach (XmlNode agentNode in Workspace.CustomizedAgentsXMLNode.ChildNodes) { string customizedClass = getAgentClassFullName(agentNode); bool bFound = false; foreach (XmlNode node in agentsNodes) { string classname = getAgentClassFullName(node); if (classname == customizedClass) { bFound = true; break; } } if (!bFound) { customizedAgentNames[customizedClass] = true; agentsNodes.Add(agentNode); } } } if (agentsNodes.Count > 0) { wrtr.WriteLine(); wrtr.WriteLine("namespace XMLPluginBehaviac"); wrtr.WriteLine("{"); for (int i = 0; i < agentsNodes.Count; ++i) { XmlNode agentNode = agentsNodes[i]; string classfullname = getAgentClassFullName(agentNode); string className = HandleBasicName(classfullname); if (i == 0 && classfullname != "behaviac::Agent") { wrtr.WriteLine("\t[Behaviac.Design.ClassDesc(\"behaviac::Agent\", \"Agent\", false, true, false, \"\", \"\", false)]"); wrtr.WriteLine("\tpublic class behaviac_Agent : Behaviac.Design.Agent"); wrtr.WriteLine("\t{"); wrtr.WriteLine("\t}\n"); } XmlAttribute baseNode = agentNode.Attributes["base"]; string baseName = (baseNode != null && baseNode.Value.Length > 0) ? baseNode.Value : "Agent"; // Then write out this class itself. XmlAttribute inheritedNode = agentNode.Attributes["inherited"]; string isinherited = (inheritedNode != null) ? inheritedNode.Value : "true"; XmlNode isRefTypeNode = agentNode.Attributes["IsRefType"]; string isRefType = (isRefTypeNode != null) ? isRefTypeNode.Value : "true"; XmlNode isStaticNode = agentNode.Attributes["IsStatic"]; string isStatic = (isStaticNode != null) ? isStaticNode.Value : "false"; XmlNode displayNameNode = agentNode.Attributes["DisplayName"]; if (displayNameNode == null) { displayNameNode = agentNode.Attributes["disp"]; } string displayName = (displayNameNode != null && displayNameNode.Value.Length > 0) ? displayNameNode.Value : classfullname; XmlNode descNode = agentNode.Attributes["Desc"]; if (descNode == null) { descNode = agentNode.Attributes["desc"]; } string desc = (descNode != null && descNode.Value.Length > 0) ? descNode.Value : displayName; XmlAttribute isCustomizedNode = agentNode.Attributes["IsCustomized"]; string isCustomized = (isCustomizedNode != null) ? isCustomizedNode.Value : "false"; wrtr.WriteLine("\t[Behaviac.Design.ClassDesc(\"{0}\", \"{1}\", {2}, {3}, {4}, \"{5}\", \"{6}\", {7})]", classfullname, baseName, isinherited, isRefType, isStatic, displayName, desc, isCustomized); wrtr.WriteLine("\tpublic class {0} : {1}", HandleHierarchyName(classfullname), (baseName == "Agent") ? "Behaviac.Design.Agent" : HandleHierarchyName(baseName)); wrtr.WriteLine("\t{"); { writeMembers(agentNode, className, wrtr, null); wrtr.WriteLine(); writeMethods(agentNode, className, wrtr); } wrtr.WriteLine("\t}\n"); } wrtr.WriteLine("}"); } // instances XmlNode instancesNode = null; if (rootNode != null) { foreach (XmlNode c in rootNode.ChildNodes) { if (c.Name == "instances") { instancesNode = c; break; } } } if (instancesNode != null) { foreach (XmlNode instanceNode in instancesNode.ChildNodes) { if (instanceNode.Name == "instance") { XmlAttribute nameNode = instanceNode.Attributes["name"]; XmlAttribute classNode = instanceNode.Attributes["class"]; XmlAttribute displayNameNode = instanceNode.Attributes["DisplayName"]; XmlAttribute descNode = instanceNode.Attributes["Desc"]; Plugin.AddInstanceName(nameNode.Value, classNode.Value, displayNameNode.Value, descNode.Value); } } } wrtr.Close(); s.Close(); return(true); } catch (XmlException ex) { string msg = string.Format("{0}:{1}", ex.SourceUri, ex.Message); System.Windows.Forms.MessageBox.Show(msg, Resources.InvalidMeta, MessageBoxButtons.OK); } return(false); }