Example #1
0
        public static void GenerateOperand(DefaultObject defaultObj, StringWriter stream, string indent, RightValueDef operand, string operandName, string nodeName)
        {
            if (operand != null)
            {
                string typeName = DataCsExporter.GetGeneratedNativeType(operand.ValueType);
                typeName = typeName.Replace("::", ".");

                if (operand.IsMethod) // method
                {
                    RightValueCsExporter.GenerateCode(defaultObj, operand, stream, indent, typeName, operandName, string.Empty);
                    RightValueCsExporter.PostGenerateCode(operand, stream, indent, typeName, operandName, string.Empty);
                }
                else
                {
                    VariableDef var = operand.Var;

                    if (var != null)
                    {
                        if (var.IsProperty) // property
                        {
                            PropertyDef prop = var.Property;

                            if (prop != null)
                            {
                                string property = PropertyCsExporter.GetProperty(defaultObj, prop, var.ArrayIndexElement, stream, indent, operandName, nodeName);
                                string propName = prop.BasicName.Replace("[]", "");

                                if (prop.IsArrayElement && var.ArrayIndexElement != null)
                                {
                                    ParameterCsExporter.GenerateCode(defaultObj, var.ArrayIndexElement, stream, indent, "int", operandName + "_index", nodeName + "_opl");
                                    property = string.Format("({0})[{1}_index]", property, operandName);
                                }

                                stream.WriteLine("{0}{1} {2} = {3};", indent, typeName, operandName, property);
                            }
                        }
                        else if (var.IsConst) // const
                        {
                            RightValueCsExporter.GenerateCode(defaultObj, operand, stream, indent, typeName, operandName, string.Empty);
                        }
                    }
                }
            }
        }
Example #2
0
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Assignment assignment = node as Assignment;

            Debug.Check(assignment != null);

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tEBTStatus result = EBTStatus.BT_SUCCESS;", indent);

            if (assignment.Opl != null && assignment.Opr != null)
            {
                if (assignment.Opl.IsPar)
                {
                    ParInfo par = assignment.Opl.Value as ParInfo;
                    if (par != null)
                    {
                        RightValueCsExporter.GenerateCode(assignment.Opr, stream, indent + "\t\t\t", assignment.Opr.NativeType.Replace("::", "."), "opr", "opr");
                        uint   id       = Behaviac.Design.CRC32.CalcCRC(par.Name);
                        string typename = DataCsExporter.GetGeneratedNativeType(par.NativeType);
                        stream.WriteLine("{0}\t\t\tDebug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, par.Name, id);
                        stream.WriteLine("{0}\t\t\tpAgent.SetVariable<{1}>(\"{2}\", opr, {3}u);", indent, typename, par.Name, id);
                    }
                }
                else
                {
                    Debug.Check(assignment.Opl.IsProperty);
                    RightValueCsExporter.GenerateCode(assignment.Opr, stream, indent + "\t\t\t", assignment.Opr.NativeType.Replace("::", "."), "opr", "opr");
                    //VariableCsExporter.GenerateCode(assignment.Opl, stream, indent + "\t\t\t", string.Empty, string.Empty, "opl", "opr");
                    VariableCsExporter.PostGenerateCode(assignment.Opl, stream, indent + "\t\t\t", assignment.Opl.NativeType.Replace("::", "."), "opl", string.Empty, null, "", "opr");
                }

                if (assignment.Opr.IsMethod)
                {
                    RightValueCsExporter.PostGenerateCode(assignment.Opr, stream, indent + "\t\t\t", assignment.Opr.NativeType.Replace("::", "."), "opr", string.Empty);
                }
            }

            stream.WriteLine("{0}\t\t\treturn result;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
        protected override void GenerateMethod(Node node, StringWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Assignment assignment = node as Assignment;

            if (assignment == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tEBTStatus result = EBTStatus.BT_SUCCESS;", indent);

            if (assignment.Opl != null && assignment.Opr != null)
            {
                PropertyDef prop = assignment.Opl.Property;

                if (prop != null)
                {
                    RightValueCsExporter.GenerateCode(node, assignment.Opr, stream, indent + "\t\t\t", assignment.Opr.NativeType.Replace("::", "."), "opr", "opr");

                    string property = PropertyCsExporter.GetProperty(node, prop, assignment.Opl.ArrayIndexElement, stream, indent + "\t\t\t", "opl", "assignment");
                    string propName = prop.BasicName.Replace("[]", "");

                    if (prop.IsArrayElement && assignment.Opl.ArrayIndexElement != null)
                    {
                        ParameterCsExporter.GenerateCode(node, assignment.Opl.ArrayIndexElement, stream, indent + "\t\t\t", "int", "opl_index", "assignment_opl");
                        property = string.Format("({0})[opl_index]", property);
                    }

                    string oprStr = "opr";

                    if (!Plugin.IsArrayType(prop.Type))
                    {
                        if (assignment.Opr.Var != null && assignment.Opr.Var.ArrayIndexElement != null)
                        {
                            ParameterCsExporter.GenerateCode(node, assignment.Opr.Var.ArrayIndexElement, stream, indent + "\t\t\t", "int", "opr_index", "assignment_opr");
                            oprStr = string.Format("({0})[opr_index]", oprStr);
                        }
                    }

                    if (!prop.IsArrayElement && (!prop.IsPublic || prop.IsPar || prop.IsCustomized))
                    {
                        string propBasicName = prop.BasicName.Replace("[]", "");
                        uint   id            = Behaviac.Design.CRC32.CalcCRC(propBasicName);
                        string agentName     = PropertyCsExporter.GetGenerateAgentName(prop, "opl", "assignment");
                        string typename      = DataCsExporter.GetGeneratedNativeType(prop.NativeType);

                        stream.WriteLine("{0}\t\t\t{1}.SetVariable<{2}>(\"{3}\", {4}u, {5});", indent, agentName, typename, propBasicName, id, oprStr);
                    }
                    else
                    {
                        if (assignment.IsCasting)
                        {
                            stream.WriteLine("{0}\t\t\t{1} = ({2}){3};", indent, property, DataCsExporter.GetGeneratedNativeType(assignment.Opl.ValueType), oprStr);
                        }
                        else
                        {
                            stream.WriteLine("{0}\t\t\t{1} = {2};", indent, property, oprStr);
                        }
                    }
                }

                if (assignment.Opr.IsMethod)
                {
                    RightValueCsExporter.PostGenerateCode(assignment.Opr, stream, indent + "\t\t\t", assignment.Opr.NativeType.Replace("::", "."), "opr", string.Empty);
                }
            }

            stream.WriteLine("{0}\t\t\treturn result;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
Example #4
0
        private void ExportCustomizedTypes(string agentFolder)
        {
            if (CustomizedTypeManager.Instance.Enums.Count > 0 || CustomizedTypeManager.Instance.Structs.Count > 0)
            {
                string   filename    = Path.Combine(agentFolder, "customizedtypes.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("// ---------------------------------------------------------------------\n");

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

                    //file.WriteLine("namespace behaviac");
                    //file.WriteLine("{");

                    file.WriteLine("// -------------------");
                    file.WriteLine("// Customized enums");
                    file.WriteLine("// -------------------\n");

                    for (int e = 0; e < CustomizedTypeManager.Instance.Enums.Count; ++e)
                    {
                        if (e > 0)
                        {
                            file.WriteLine();
                        }

                        CustomizedEnum customizedEnum = CustomizedTypeManager.Instance.Enums[e];
                        file.WriteLine("[behaviac.TypeMetaInfo(\"{0}\", \"{1}\")]", customizedEnum.DisplayName, customizedEnum.Description);

                        file.WriteLine("public enum {0}", customizedEnum.Name);
                        file.WriteLine("{");

                        for (int m = 0; m < customizedEnum.Members.Count; ++m)
                        {
                            if (m > 0)
                            {
                                file.WriteLine();
                            }

                            CustomizedEnum.CustomizedEnumMember member = customizedEnum.Members[m];
                            if (member.DisplayName != member.Name || !string.IsNullOrEmpty(member.Description))
                            {
                                file.WriteLine("\t[behaviac.MemberMetaInfo(\"{0}\", \"{1}\")]", member.DisplayName, member.Description);
                            }

                            if (member.Value >= 0)
                            {
                                file.WriteLine("\t{0} = {1},", member.Name, member.Value);
                            }
                            else
                            {
                                file.WriteLine("\t{0},", member.Name);
                            }
                        }

                        file.WriteLine("}");
                    }

                    if (CustomizedTypeManager.Instance.Enums.Count > 0)
                    {
                        file.WriteLine();
                    }

                    file.WriteLine("// -------------------");
                    file.WriteLine("// Customized structs");
                    file.WriteLine("// -------------------\n");

                    for (int s = 0; s < CustomizedTypeManager.Instance.Structs.Count; s++)
                    {
                        if (s > 0)
                        {
                            file.WriteLine();
                        }

                        CustomizedStruct customizedStruct = CustomizedTypeManager.Instance.Structs[s];
                        file.WriteLine("[behaviac.TypeMetaInfo(\"{0}\", \"{1}\")]", customizedStruct.DisplayName, customizedStruct.Description);

                        file.WriteLine("public struct {0}", customizedStruct.Name);
                        file.WriteLine("{");

                        for (int m = 0; m < customizedStruct.Properties.Count; ++m)
                        {
                            if (m > 0)
                            {
                                file.WriteLine();
                            }

                            PropertyDef member = customizedStruct.Properties[m];
                            if (member.DisplayName != member.Name || !string.IsNullOrEmpty(member.BasicDescription))
                            {
                                file.WriteLine("\t[behaviac.MemberMetaInfo(\"{0}\", \"{1}\")]", member.DisplayName, member.BasicDescription);
                            }

                            file.WriteLine("\tpublic {0} {1};", DataCsExporter.GetGeneratedNativeType(member.NativeType), member.BasicName);
                        }

                        file.WriteLine("}");
                    }

                    //file.WriteLine("}");
                }
            }
        }
Example #5
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 #6
0
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            PluginBehaviac.Nodes.Condition condition = node as PluginBehaviac.Nodes.Condition;
            if (condition == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);

            string typeName = DataCsExporter.GetGeneratedNativeType(condition.Opl.ValueType);

            // opl
            ConditionCsExporter.GenerateOperand(stream, indent + "\t\t\t", condition.Opl, "opl", "condition");

            // opr
            ConditionCsExporter.GenerateOperand(stream, indent + "\t\t\t", condition.Opr, "opr", "condition");

            // Operator
            switch (condition.Operator)
            {
            case OperatorType.Equal:
                stream.WriteLine("{0}\t\t\tbool op = opl == opr;", indent);
                break;

            case OperatorType.NotEqual:
                stream.WriteLine("{0}\t\t\tbool op = opl != opr;", indent);
                break;

            case OperatorType.Greater:
                stream.WriteLine("{0}\t\t\tbool op = opl > opr;", indent);
                break;

            case OperatorType.GreaterEqual:
                stream.WriteLine("{0}\t\t\tbool op = opl >= opr;", indent);
                break;

            case OperatorType.Less:
                stream.WriteLine("{0}\t\t\tbool op = opl < opr;", indent);
                break;

            case OperatorType.LessEqual:
                stream.WriteLine("{0}\t\t\tbool op = opl <= opr;", indent);
                break;

            case OperatorType.And:
                stream.WriteLine("{0}\t\t\tbool op = opl && opr;", indent);
                break;

            case OperatorType.Or:
                stream.WriteLine("{0}\t\t\tbool op = opl || opr;", indent);
                break;

            default:
                stream.WriteLine("{0}\t\t\tbool op = false;", indent);
                break;
            }

            stream.WriteLine("{0}\t\t\treturn op ? EBTStatus.BT_SUCCESS : EBTStatus.BT_FAILURE;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
Example #7
0
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Compute compute = node as Compute;

            if (compute == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tEBTStatus result = EBTStatus.BT_SUCCESS;", indent);

            if (compute.Opl != null && compute.Opr1 != null && compute.Opr2 != null)
            {
                string typeName = Plugin.GetNativeTypeName(compute.Opr1.ValueType);
                typeName = typeName.Replace("::", ".");

                RightValueCsExporter.GenerateCode(compute.Opr1, stream, indent + "\t\t\t", typeName, "opr1", "opr1");
                RightValueCsExporter.GenerateCode(compute.Opr2, stream, indent + "\t\t\t", typeName, "opr2", "opr2");

                string oprStr = string.Empty;
                switch (compute.Operator)
                {
                case ComputeOperator.Add:
                    oprStr = "opr1 + opr2";
                    break;

                case ComputeOperator.Sub:
                    oprStr = "opr1 - opr2";
                    break;

                case ComputeOperator.Mul:
                    oprStr = "opr1 * opr2";
                    break;

                case ComputeOperator.Div:
                    oprStr = "opr1 / opr2";
                    break;

                default:
                    Debug.Check(false, "The operator is wrong!");
                    break;
                }

                oprStr = string.Format("({0})({1})", typeName, oprStr);

                PropertyDef prop = compute.Opl.Property;
                if (prop != null)
                {
                    string property = PropertyCsExporter.GetProperty(prop, compute.Opl.ArrayIndexElement, stream, indent + "\t\t\t", "opl", "compute");
                    string propName = prop.BasicName.Replace("[]", "");

                    if (prop.IsArrayElement && compute.Opl.ArrayIndexElement != null)
                    {
                        ParameterCsExporter.GenerateCode(compute.Opl.ArrayIndexElement, stream, indent + "\t\t\t", "int", "opl_index", "compute_opl");
                        property = string.Format("({0})[opl_index]", property);
                    }

                    if (!prop.IsArrayElement && (prop.IsPar || prop.IsCustomized))
                    {
                        string propBasicName = prop.BasicName.Replace("[]", "");
                        uint   id            = Behaviac.Design.CRC32.CalcCRC(propBasicName);

                        string typename = DataCsExporter.GetGeneratedNativeType(prop.NativeType);
                        stream.WriteLine("{0}\t\t\tDebug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, propBasicName, id);
                        stream.WriteLine("{0}\t\t\tpAgent.SetVariable<{1}>(\"{2}\", {3}, {4}u);", indent, typename, propBasicName, oprStr, id);
                    }
                    else
                    {
                        stream.WriteLine("{0}\t\t\t{1} = {2};", indent, property, oprStr);
                    }
                }

                if (compute.Opr1.IsMethod)
                {
                    RightValueCsExporter.PostGenerateCode(compute.Opr1, stream, indent + "\t\t\t", typeName, "opr1", string.Empty);
                }

                if (compute.Opr2.IsMethod)
                {
                    RightValueCsExporter.PostGenerateCode(compute.Opr2, stream, indent + "\t\t\t", typeName, "opr2", string.Empty);
                }
            }

            stream.WriteLine("{0}\t\t\treturn result;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Compute compute = node as Compute;

            Debug.Check(compute != null);

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);
            stream.WriteLine("{0}\t\t\tEBTStatus result = EBTStatus.BT_SUCCESS;", indent);

            if (compute.Opl != null && compute.Opr1 != null && compute.Opr2 != null)
            {
                RightValueCsExporter.GenerateCode(compute.Opr1, stream, indent + "\t\t\t", compute.Opr1.NativeType, "opr1", "opr1");
                RightValueCsExporter.GenerateCode(compute.Opr2, stream, indent + "\t\t\t", compute.Opr2.NativeType, "opr2", "opr2");

                string oprStr = string.Empty;
                switch (compute.Operator)
                {
                case ComputeOperator.Add:
                    oprStr = "opr1 + opr2";
                    break;

                case ComputeOperator.Sub:
                    oprStr = "opr1 - opr2";
                    break;

                case ComputeOperator.Mul:
                    oprStr = "opr1 * opr2";
                    break;

                case ComputeOperator.Div:
                    oprStr = "opr1 / opr2";
                    break;

                default:
                    Debug.Check(false, "The operator is wrong!");
                    break;
                }

                string basicType = DataCsExporter.GetGeneratedNativeType(compute.Opl.NativeType);
                stream.WriteLine("{0}\t\t\t{1} opr = ({1})({2});", indent, basicType, oprStr);

                if (compute.Opl.IsPar)
                {
                    ParInfo par = compute.Opl.Value as ParInfo;
                    if (par != null)
                    {
                        uint id = Behaviac.Design.CRC32.CalcCRC(par.Name);
                        stream.WriteLine("{0}\t\t\tDebug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, par.Name, id);
                        stream.WriteLine("{0}\t\t\tpAgent.SetVariable(\"{1}\", opr, {2}u);", indent, par.Name, id);
                    }
                }
                else
                {
                    //VariableCsExporter.GenerateCode(compute.Opl, stream, indent + "\t\t\t", string.Empty, string.Empty, "opl", "opr");
                    VariableCsExporter.PostGenerateCode(compute.Opl, stream, indent + "\t\t\t", compute.Opl.NativeType, "opl", string.Empty, null, "", "opr");
                }

                if (compute.Opr1.IsMethod)
                {
                    RightValueCsExporter.PostGenerateCode(compute.Opr1, stream, indent + "\t\t\t", compute.Opr1.NativeType, "opr1", string.Empty);
                }

                if (compute.Opr2.IsMethod)
                {
                    RightValueCsExporter.PostGenerateCode(compute.Opr2, stream, indent + "\t\t\t", compute.Opr2.NativeType, "opr2", string.Empty);
                }
            }

            stream.WriteLine("{0}\t\t\treturn result;", indent);
            stream.WriteLine("{0}\t\t}}", indent);
        }
Example #9
0
        protected override void GenerateMethod(Node node, StringWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Nodes.Action action = node as Nodes.Action;

            if (action == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);

            string resultStatus = getResultOptionStr(action.ResultOption);

            if (action.Method != null && !isNullMethod(action.Method))
            {
                string nativeReturnType = DataCsExporter.GetGeneratedNativeType(action.Method.NativeReturnType);
                string method           = MethodCsExporter.GenerateCode(node, action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");

                if ("behaviac.EBTStatus" == nativeReturnType)
                {
                    resultStatus = "result";

                    stream.WriteLine("{0}\t\t\t{1} result = {2};", indent, nativeReturnType, method);
                    MethodCsExporter.PostGenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");
                }
                else
                {
                    if (("void" == nativeReturnType) || (EBTStatus.BT_INVALID != action.ResultOption) || action.ResultFunctor == null)
                    {
                        stream.WriteLine("{0}\t\t\t{1};", indent, method);
                    }
                    else
                    {
                        stream.WriteLine("{0}\t\t\t{1} result = {2};", indent, nativeReturnType, method);
                    }

                    MethodCsExporter.PostGenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");

                    if (EBTStatus.BT_INVALID != action.ResultOption)
                    {
                        resultStatus = getResultOptionStr(action.ResultOption);
                    }
                    else if (Plugin.IsMatchedStatusMethod(action.Method, action.ResultFunctor))
                    {
                        if ("void" == nativeReturnType)
                        {
                            resultStatus = MethodCsExporter.GenerateCode(node, action.ResultFunctor, stream, indent + "\t\t\t", string.Empty, string.Empty, "functor");
                        }
                        else
                        {
                            string agentName = "pAgent";

                            if (action.ResultFunctor.Owner != VariableDef.kSelf &&
                                (!action.ResultFunctor.IsPublic || !action.ResultFunctor.IsStatic))
                            {
                                string instanceName = action.ResultFunctor.Owner.Replace("::", ".");
                                agentName = "pAgent_functor";

                                stream.WriteLine("{0}behaviac.Agent {1} = behaviac.Utils.GetParentAgent(pAgent, \"{2}\");", indent, agentName, instanceName);
                                //stream.WriteLine("{0}Debug.Check(!System.Object.ReferenceEquals({1}, null) || Utils.IsStaticClass(\"{2}\"));", indent, agentName, instanceName);
                            }

                            if (action.ResultFunctor.IsPublic)
                            {
                                string className = action.ResultFunctor.ClassName.Replace("::", ".");

                                if (action.ResultFunctor.IsStatic)
                                {
                                    resultStatus = string.Format("{0}.{1}(result)", className, action.ResultFunctor.BasicName);
                                }
                                else
                                {
                                    resultStatus = string.Format("(({0}){1}).{2}(result)", className, agentName, action.ResultFunctor.BasicName);
                                }
                            }
                            else
                            {
                                resultStatus = string.Format("AgentMetaVisitor.ExecuteMethod({0}, \"{1}\", new object[] {{ result }})", agentName, action.ResultFunctor.BasicName);
                            }
                        }

                        resultStatus = string.Format("(EBTStatus){0}", resultStatus);
                    }
                }
            }

            stream.WriteLine("{0}\t\t\treturn {1};", indent, resultStatus);
            stream.WriteLine("{0}\t\t}}", indent);
        }
Example #10
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();
                    }
                }
            }
        }
Example #11
0
        protected override void GenerateMethod(Node node, StreamWriter stream, string indent)
        {
            base.GenerateMethod(node, stream, indent);

            Action action = node as Action;

            if (action == null)
            {
                return;
            }

            stream.WriteLine("{0}\t\tprotected override EBTStatus update_impl(behaviac.Agent pAgent, behaviac.EBTStatus childStatus)", indent);
            stream.WriteLine("{0}\t\t{{", indent);

            string resultStatus = getResultOptionStr(action.ResultOption);

            if (action.Method != null && !isNullMethod(action.Method))
            {
                string nativeReturnType = DataCsExporter.GetGeneratedNativeType(action.Method.NativeReturnType);
                string typeConvertStr   = (nativeReturnType == "void") ? string.Empty : "(" + nativeReturnType + ")";
                string method           = MethodCsExporter.GenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");

                if ("behaviac.EBTStatus" == nativeReturnType)
                {
                    resultStatus = "result";

                    stream.WriteLine("{0}\t\t\t{1} result = {2}{3};", indent, nativeReturnType, typeConvertStr, method);
                    MethodCsExporter.PostGenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");
                }
                else
                {
                    if (("void" == nativeReturnType) || (EBTStatus.BT_INVALID != action.ResultOption) || action.ResultFunctor == null)
                    {
                        stream.WriteLine("{0}\t\t\t{1};", indent, method);
                    }
                    else
                    {
                        stream.WriteLine("{0}\t\t\t{1} result = {2}{3};", indent, nativeReturnType, typeConvertStr, method);
                    }
                    MethodCsExporter.PostGenerateCode(action.Method, stream, indent + "\t\t\t", string.Empty, string.Empty, "method");

                    if (EBTStatus.BT_INVALID != action.ResultOption)
                    {
                        resultStatus = getResultOptionStr(action.ResultOption);
                    }
                    else if (Plugin.IsMatchedStatusMethod(action.Method, action.ResultFunctor))
                    {
                        if ("void" == nativeReturnType)
                        {
                            resultStatus = MethodCsExporter.GenerateCode(action.ResultFunctor, stream, indent + "\t\t\t", string.Empty, string.Empty, "functor");
                        }
                        else
                        {
                            string agentName = "pAgent";
                            if (action.ResultFunctor.Owner != VariableDef.kSelf &&
                                (!action.ResultFunctor.IsPublic || !action.ResultFunctor.IsStatic))
                            {
                                agentName = "pAgent_functor";

                                stream.WriteLine("{0}behaviac.Agent {1} = behaviac.Agent.GetInstance(\"{2}\", pAgent.GetContextId());", indent, agentName, action.ResultFunctor.Owner.Replace("::", "."));
                                stream.WriteLine("{0}Debug.Check({1} != null);", indent, agentName);
                            }

                            if (action.ResultFunctor.IsPublic)
                            {
                                string className = action.ResultFunctor.ClassName.Replace("::", ".");
                                if (action.ResultFunctor.IsStatic)
                                {
                                    resultStatus = string.Format("{0}.{1}(result)", className, action.ResultFunctor.BasicName);
                                }
                                else
                                {
                                    resultStatus = string.Format("(({0}){1}).{2}(result)", className, agentName, action.ResultFunctor.BasicName);
                                }
                            }
                            else
                            {
                                resultStatus = string.Format("AgentExtra_Generated.ExecuteMethod({0}, \"{1}\", new object[] {{ result }})", agentName, action.ResultFunctor.BasicName);
                            }
                        }

                        resultStatus = string.Format("(EBTStatus){0}", resultStatus);
                    }
                }
            }

            stream.WriteLine("{0}\t\t\treturn {1};", indent, resultStatus);
            stream.WriteLine("{0}\t\t}}", indent);
        }