Example #1
0
        private void ExportAgents(string agentFolder)
        {
            foreach (AgentType agent in Plugin.AgentTypes)
            {
                bool hasCustomizedProperty = false;
                foreach (PropertyDef prop in agent.GetProperties())
                {
                    if (prop.IsCustomized && !prop.IsPar && !prop.IsArrayElement)
                    {
                        hasCustomizedProperty = true;
                        break;
                    }
                }

                IList <MethodDef> methods = agent.GetMethods();
                bool hasCustomizedMethod  = false;
                foreach (MethodDef method in methods)
                {
                    if (method.IsCustomized && !method.IsNamedEvent)
                    {
                        hasCustomizedMethod = true;
                        break;
                    }
                }

                if (hasCustomizedProperty || hasCustomizedMethod)
                {
                    string   filename    = Path.Combine(agentFolder, agent.AgentTypeName + ".cs");
                    Encoding utf8WithBom = new UTF8Encoding(true);

                    using (StreamWriter file = new StreamWriter(filename, false, utf8WithBom))
                    {
                        // write comments
                        file.WriteLine("// ---------------------------------------------------------------------");
                        file.WriteLine("// This file is auto-generated by behaviac designer, so please don't modify it by yourself!");
                        file.WriteLine("// ---------------------------------------------------------------------\r\n");

                        file.WriteLine("using System.Collections.Generic;");
                        file.WriteLine();

                        if (!string.IsNullOrEmpty(agent.Namespace))
                        {
                            file.WriteLine("namespace {0}", agent.Namespace);
                            file.WriteLine("{");
                        }

                        file.WriteLine("\tpartial class {0}", agent.AgentTypeName);
                        file.WriteLine("\t{");

                        if (hasCustomizedProperty)
                        {
                            foreach (PropertyDef prop in agent.GetProperties())
                            {
                                if (prop.IsCustomized && !prop.IsPar && !prop.IsArrayElement)
                                {
                                    string isStatic     = prop.IsStatic ? "static " : "";
                                    string propType     = DataCsExporter.GetGeneratedNativeType(prop.Type);
                                    string defaultValue = DataCsExporter.GetGeneratedPropertyDefaultValue(prop, propType);
                                    if (!string.IsNullOrEmpty(defaultValue))
                                    {
                                        defaultValue = " = " + defaultValue;
                                    }

                                    file.WriteLine("\t\tpublic {0}{1} {2}{3};", isStatic, propType, prop.BasicName, defaultValue);
                                }
                            }
                        }

                        if (hasCustomizedMethod)
                        {
                            if (hasCustomizedProperty)
                            {
                                file.WriteLine();
                            }

                            file.WriteLine("\t\t// ----------------------------------------------------------------------------------");
                            file.WriteLine("\t\t// The following methods should be copied to your partial class file to implement it.");
                            file.WriteLine("\t\t// ----------------------------------------------------------------------------------\n");

                            foreach (MethodDef method in methods)
                            {
                                if (method.IsCustomized && !method.IsNamedEvent)
                                {
                                    string allParams = "";
                                    foreach (MethodDef.Param param in method.Params)
                                    {
                                        if (!string.IsNullOrEmpty(allParams))
                                        {
                                            allParams += ", ";
                                        }

                                        allParams += DataCsExporter.GetGeneratedNativeType(param.NativeType) + " " + param.Name;
                                    }

                                    file.WriteLine("\t\t//[behaviac.MethodMetaInfo(\"{0}\", \"{1}\")]", method.DisplayName, method.BasicDescription);
                                    file.WriteLine("\t\t//public partial {0} {1}({2})", DataCsExporter.GetGeneratedNativeType(method.ReturnType), method.BasicName, allParams);
                                }
                            }
                        }

                        //end of class
                        file.WriteLine("\t}");

                        if (!string.IsNullOrEmpty(agent.Namespace))
                        {
                            //end of namespace
                            file.WriteLine("}");
                        }

                        file.Close();
                    }
                }
            }
        }
Example #2
0
        private void ExportAgents(string agentFolder)
        {
            foreach (AgentType agent in Plugin.AgentTypes)
            {
                if (!agent.IsCustomized)
                {
                    continue;
                }

                IList <PropertyDef> properties = agent.GetProperties();
                bool hasCustomizedProperty     = false;
                foreach (PropertyDef prop in properties)
                {
                    if (prop.IsCustomized && !prop.IsPar && !prop.IsArrayElement)
                    {
                        hasCustomizedProperty = true;
                        break;
                    }
                }

                IList <MethodDef> methods = agent.GetMethods();
                bool hasCustomizedMethod  = false;
                foreach (MethodDef method in methods)
                {
                    if (method.IsCustomized && !method.IsNamedEvent)
                    {
                        hasCustomizedMethod = true;
                        break;
                    }
                }

                if (hasCustomizedProperty || hasCustomizedMethod)
                {
                    string   filename    = Path.Combine(agentFolder, agent.BasicClassName + ".cs");
                    Encoding utf8WithBom = new UTF8Encoding(true);

                    using (StreamWriter file = new StreamWriter(filename, false, utf8WithBom))
                    {
                        // write comments
                        file.WriteLine("// ---------------------------------------------------------------------");
                        file.WriteLine("// This agent file is auto-generated by behaviac designer, but you should");
                        file.WriteLine("// implement the methods of the agent class if necessary!");
                        file.WriteLine("// ---------------------------------------------------------------------\r\n");

                        file.WriteLine("using System.Collections.Generic;");
                        file.WriteLine();

                        string indent = "";
                        if (!string.IsNullOrEmpty(agent.Namespace))
                        {
                            indent = "\t";

                            file.WriteLine("namespace {0}", agent.Namespace);
                            file.WriteLine("{");
                        }

                        string agentDisplayName = string.IsNullOrEmpty(agent.DisplayName) ? agent.BasicClassName : agent.DisplayName;
                        string agentDescription = string.IsNullOrEmpty(agent.Description) ? "" : agent.Description;
                        file.WriteLine("{0}[behaviac.TypeMetaInfo(\"{1}\", \"{2}\")]", indent, agentDisplayName, agentDescription);
                        file.WriteLine("{0}public class {1} : {2}", indent, agent.BasicClassName, agent.Base.AgentTypeName.Replace("::", "."));
                        file.WriteLine("{0}{{", indent);

                        if (hasCustomizedProperty)
                        {
                            file.WriteLine("{0}\t// properties", indent);
                            file.WriteLine();

                            foreach (PropertyDef prop in properties)
                            {
                                if (prop.IsCustomized && !prop.IsPar && !prop.IsArrayElement)
                                {
                                    string publicStr    = prop.IsPublic ? "public " : "private ";
                                    string staticStr    = prop.IsStatic ? "static " : "";
                                    string propType     = DataCsExporter.GetGeneratedNativeType(prop.Type);
                                    string defaultValue = DataCsExporter.GetGeneratedPropertyDefaultValue(prop, propType);
                                    if (defaultValue != null)
                                    {
                                        defaultValue = " = " + defaultValue;
                                    }

                                    file.WriteLine("{0}\t[behaviac.MemberMetaInfo(\"{1}\", \"{2}\")]", indent, prop.DisplayName, prop.BasicDescription);
                                    file.WriteLine("{0}\t{1}{2}{3} {4}{5};", indent, publicStr, staticStr, propType, prop.BasicName, defaultValue);
                                    file.WriteLine();
                                }
                            }
                        }

                        if (hasCustomizedMethod)
                        {
                            file.WriteLine("{0}\t// methods", indent);
                            file.WriteLine();

                            foreach (MethodDef method in methods)
                            {
                                if (method.IsCustomized && !method.IsNamedEvent)
                                {
                                    string publicStr = method.IsPublic ? "public " : "private ";
                                    string staticStr = method.IsStatic ? "static " : "";

                                    string allParams = "";
                                    foreach (MethodDef.Param param in method.Params)
                                    {
                                        if (!string.IsNullOrEmpty(allParams))
                                        {
                                            allParams += ", ";
                                        }

                                        allParams += DataCsExporter.GetGeneratedNativeType(param.NativeType) + " " + param.Name;
                                    }

                                    string returnValue = DataCsExporter.GetGeneratedDefaultValue(method.ReturnType, method.NativeReturnType);

                                    file.WriteLine("{0}\t[behaviac.MethodMetaInfo(\"{1}\", \"{2}\")]", indent, method.DisplayName, method.BasicDescription);
                                    file.WriteLine("{0}\t{1}{2}{3} {4}({5})", indent, publicStr, staticStr, DataCsExporter.GetGeneratedNativeType(method.ReturnType), method.BasicName, allParams);
                                    file.WriteLine("{0}\t{{", indent);
                                    file.WriteLine("{0}\t\t// Write your logic codes here.", indent);
                                    if (returnValue != null)
                                    {
                                        file.WriteLine();
                                        file.WriteLine("{0}\t\treturn {1};", indent, returnValue);
                                    }
                                    file.WriteLine("{0}\t}}", indent);
                                    file.WriteLine();
                                }
                            }
                        }

                        //end of class
                        file.WriteLine("{0}}}", indent);

                        if (!string.IsNullOrEmpty(agent.Namespace))
                        {
                            //end of namespace
                            file.WriteLine("}");
                        }

                        file.Close();
                    }
                }
            }
        }