Example #1
0
        public static void GenerateCode(object obj, StreamWriter stream, string indent, string var, object parent, string paramName)
        {
            Debug.Check(obj != null);

            Type type = obj.GetType();

            Debug.Check(Plugin.IsCustomClassType(type));

            MethodDef method = parent as MethodDef;
            IList <DesignerPropertyInfo> properties = DesignerProperty.GetDesignerProperties(type);

            foreach (DesignerPropertyInfo property in properties)
            {
                if (!property.Attribute.HasFlags(DesignerProperty.DesignerFlags.NoSave))
                {
                    object member = property.GetValue(obj);
                    if (property.Attribute is DesignerStruct)
                    {
                        GenerateCode(member, stream, indent, var + "." + property.Property.Name, parent, paramName);
                    }
                    else
                    {
                        bool bStructProperty = false;
                        if (method != null)
                        {
                            MethodDef.Param param = method.GetParam(paramName, property);
                            if (param != null)
                            {
                                bStructProperty = true;
                                ParameterCsExporter.GenerateCode(param, stream, indent, string.Empty, var + "." + property.Property.Name, string.Empty);
                            }
                        }

                        if (!bStructProperty)
                        {
                            DataCsExporter.GenerateCode(member, stream, indent, string.Empty, var + "." + property.Property.Name, string.Empty);
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Generate code for the given value object.
        /// </summary>
        /// <param name="obj">The given object.</param>
        /// <param name="stream">The file stream for generating the codes.</param>
        /// <param name="indent">The indent string when generating the line of codes.</param>
        /// <param name="typename">The native type of the variable.</param>
        /// <param name="var">The variable for the given object when generating the codes.</param>
        /// <param name="caller">The caller for the method or the agent.</param>
        /// <returns>Returns the string generated value.</returns>
        public static string GenerateCode(object obj, StreamWriter stream, string indent, string typename, string var, string caller, string setValue = null)
        {
            string retStr = string.Empty;

            if (obj != null)
            {
                Type type = obj.GetType();

                if (obj is Behaviac.Design.MethodDef)
                {
                    Behaviac.Design.MethodDef method = obj as Behaviac.Design.MethodDef;
                    retStr = MethodCsExporter.GenerateCode(method, stream, indent, typename, var, caller);
                }
                else if (obj is Behaviac.Design.MethodDef.Param)
                {
                    Behaviac.Design.MethodDef.Param param = obj as Behaviac.Design.MethodDef.Param;
                    retStr = ParameterCsExporter.GenerateCode(param, stream, indent, typename, var, caller);
                }
                else if (obj is Behaviac.Design.ParInfo)
                {
                    Behaviac.Design.ParInfo par = obj as Behaviac.Design.ParInfo;
                    retStr = ParInfoCsExporter.GenerateCode(par, stream, indent, typename, var, caller);
                }
                else if (obj is Behaviac.Design.PropertyDef)
                {
                    Behaviac.Design.PropertyDef property = obj as Behaviac.Design.PropertyDef;
                    retStr = PropertyCsExporter.GenerateCode(property, stream, indent, typename, var, caller, setValue);
                }
                else if (obj is Behaviac.Design.VariableDef)
                {
                    Behaviac.Design.VariableDef variable = obj as Behaviac.Design.VariableDef;
                    retStr = VariableCsExporter.GenerateCode(variable, stream, indent, typename, var, caller);
                }
                else if (obj is Behaviac.Design.RightValueDef)
                {
                    Behaviac.Design.RightValueDef rightValue = obj as Behaviac.Design.RightValueDef;
                    retStr = RightValueCsExporter.GenerateCode(rightValue, stream, indent, typename, var, caller);
                }
                // Array type
                else if (Plugin.IsArrayType(type))
                {
                    retStr = var;

                    if (!string.IsNullOrEmpty(typename))
                    {
                        stream.WriteLine("{0}{1} {2};", indent, typename, var);
                    }
                    else
                    {
                        typename = DataCsExporter.GetGeneratedNativeType(type);
                    }

                    int    startIndex = typename.IndexOf('<');
                    int    endIndex   = typename.LastIndexOf('>');
                    string itemType   = typename.Substring(startIndex + 1, endIndex - startIndex - 1);

                    ArrayCsExporter.GenerateCode(obj, stream, indent, itemType, var);
                }
                // Struct type
                else if (Plugin.IsCustomClassType(type))
                {
                    retStr = var;

                    if (!string.IsNullOrEmpty(typename))
                    {
                        stream.WriteLine("{0}{1} {2};", indent, typename, var);
                    }

                    StructCsExporter.GenerateCode(obj, stream, indent, var, null, "");
                }
                // Other types
                else
                {
                    retStr = obj.ToString();

                    if (Plugin.IsStringType(type)) // string
                    {
                        retStr = string.Format("\"{0}\"", retStr);
                    }
                    else if (Plugin.IsCharType(type)) // char
                    {
                        char c = 'A';
                        if (retStr.Length >= 1)
                        {
                            c = retStr[0];
                        }

                        retStr = string.Format("\'{0}\'", c);
                    }
                    else if (Plugin.IsBooleanType(type)) // bool
                    {
                        retStr = retStr.ToLowerInvariant();
                    }
                    else if (Plugin.IsEnumType(type)) // enum
                    {
                        retStr = EnumCsExporter.GeneratedCode(obj);
                    }
                    else if (type == typeof(float)) // float
                    {
                        retStr += "f";
                    }

                    if (!string.IsNullOrEmpty(var))
                    {
                        if (string.IsNullOrEmpty(typename))
                        {
                            stream.WriteLine("{0}{1} = {2};", indent, var, retStr);
                        }
                        else
                        {
                            stream.WriteLine("{0}{1} {2} = {3};", indent, typename, var, retStr);
                        }
                    }
                }
            }

            return(retStr);
        }
Example #3
0
        public static string GenerateCode(Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string typename, string var, string caller)
        {
            Debug.Check(!string.IsNullOrEmpty(var) || !string.IsNullOrEmpty(caller));

            string allParams  = string.Empty;
            string paramsName = (method.Params.Count == 0) ? "null" : getParamsName(var, caller);

            for (int i = 0; i < method.Params.Count; ++i)
            {
                string nativeType = DataCsExporter.GetGeneratedNativeType(method.Params[i].NativeType);
                string param      = string.Empty;
                if (method.IsPublic)
                {
                    param = getParamName(var, caller, i);
                }
                else
                {
                    param = string.Format("{0}[{1}]", paramsName, i);
                }

                if (method.Params[i].IsProperty) // property
                {
                    if (method.IsPublic)
                    {
                        param = ParameterCsExporter.GenerateCode(method.Params[i], stream, indent, nativeType, "", param);
                    }
                    else
                    {
                        ParameterCsExporter.GenerateCode(method.Params[i], stream, indent, "", param, caller);
                    }
                }
                else
                {
                    if (method.Params[i].IsPar) // par
                    {
                        ParameterCsExporter.GenerateCode(method.Params[i], stream, indent, method.IsPublic ? nativeType : "", param, caller);
                    }
                    else // const value
                    {
                        object obj = method.Params[i].Value;
                        if (obj != null)
                        {
                            Type type = obj.GetType();
                            if (Plugin.IsCustomClassType(type) && !DesignerStruct.IsPureConstDatum(obj, method, method.Params[i].Name))
                            {
                                string paramName = getParamName(var, caller, i);
                                StructCsExporter.GenerateCode(obj, stream, indent, paramName, method, method.Params[i].Name);
                                if (!method.IsPublic)
                                {
                                    stream.WriteLine("{0}{1} = {2};", indent, param, paramName);
                                }
                            }
                        }
                    }
                }

                if (i > 0)
                {
                    allParams += ", ";
                }

                //if (method.Params[i].NativeType.EndsWith("&"))
                if (method.Params[i].NativeType.IndexOf("&") >= 0)
                {
                    param = "ref " + param;
                }

                allParams += param;
            }

            string agentName = "pAgent";

            if (method.Owner != Behaviac.Design.VariableDef.kSelf &&
                (!method.IsPublic || !method.IsStatic))
            {
                agentName = "pAgent_" + caller;

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

            string retStr = string.Empty;

            if (method.IsPublic)
            {
                string className = method.ClassName.Replace("::", ".");
                if (method.IsStatic)
                {
                    retStr = string.Format("{0}.{1}({2})", className, method.BasicName, allParams);
                }
                else
                {
                    retStr = string.Format("(({0}){1}).{2}({3})", className, agentName, method.BasicName, allParams);
                }
            }
            else
            {
                retStr = string.Format("AgentExtra_Generated.ExecuteMethod({0}, \"{1}\", {2})", agentName, method.BasicName, paramsName);
            }

            if (!string.IsNullOrEmpty(var))
            {
                string nativeReturnType = DataCsExporter.GetGeneratedNativeType(method.NativeReturnType);
                string typeConvertStr   = (nativeReturnType == "void") ? string.Empty : "(" + nativeReturnType + ")";

                stream.WriteLine("{0}{1} {2} = {3}{4};", indent, nativeReturnType, var, typeConvertStr, retStr);
            }

            return(retStr);
        }
Example #4
0
        public static void GenerateCode(object obj, DefaultObject defaultObj, StreamWriter stream, string indent, string var, string typename, object parent, string paramName)
        {
            Debug.Check(obj != null);

            Type type = obj.GetType();

            Debug.Check(Plugin.IsCustomClassType(type));

            if (!Plugin.IsRefType(type))
            {
                stream.WriteLine("{0}{1} = new {2}();", indent, var, typename);
            }

            MethodDef method = parent as MethodDef;
            IList <DesignerPropertyInfo> properties = DesignerProperty.GetDesignerProperties(type);

            foreach (DesignerPropertyInfo property in properties)
            {
                if (!property.Attribute.HasFlags(DesignerProperty.DesignerFlags.NoSave))
                {
                    object member = property.GetValue(obj);

                    Type memberType = member.GetType();

                    if (Plugin.IsArrayType(memberType))
                    {
                        string memberNativeType = Plugin.GetNativeTypeName(memberType);
                        string nativeTypeStr    = DataCsExporter.GetGeneratedNativeType(memberNativeType);
                        int    startIndex       = nativeTypeStr.IndexOf('<');
                        int    endIndex         = nativeTypeStr.LastIndexOf('>');
                        string itemType         = nativeTypeStr.Substring(startIndex + 1, endIndex - startIndex - 1);

                        ArrayCsExporter.GenerateCode(member, defaultObj, stream, indent, itemType, var + "." + property.Property.Name);
                    }
                    else
                    {
                        if (property.Attribute is DesignerStruct)
                        {
                            string memberTypeStr = DataCsExporter.GetGeneratedNativeType(member.GetType());
                            GenerateCode(member, defaultObj, stream, indent, var + "." + property.Property.Name, memberTypeStr, parent, paramName);
                        }
                        else
                        {
                            bool bStructProperty = false;
                            if (method != null)
                            {
                                MethodDef.Param param = method.GetParam(paramName, property);
                                if (param != null)
                                {
                                    bStructProperty = true;
                                    ParameterCsExporter.GenerateCode(defaultObj, param, stream, indent, string.Empty, var + "." + property.Property.Name, string.Empty);
                                }
                            }

                            if (!bStructProperty)
                            {
                                DataCsExporter.GenerateCode(member, defaultObj, stream, indent, string.Empty, var + "." + property.Property.Name, string.Empty);
                            }
                        }
                    }
                }
            }
        }
Example #5
0
        public static string GenerateCode(DefaultObject defaultObj, Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string typename, string var, string caller)
        {
            Debug.Check(!string.IsNullOrEmpty(var) || !string.IsNullOrEmpty(caller));

            string allParams  = string.Empty;
            string paramsName = getParamsName(var, caller);

            for (int i = 0; i < method.Params.Count; ++i)
            {
                string nativeType = DataCsExporter.GetGeneratedNativeType(method.Params[i].NativeType);
                string param      = string.Empty;
                if (method.IsPublic)
                {
                    param = getParamName(var, caller, i);
                }
                else
                {
                    param = string.Format("{0}[{1}]", paramsName, i);
                }

                if (method.Params[i].IsProperty || method.Params[i].IsLocalVar) // property
                {
                    if ((method.Params[i].Property != null && method.Params[i].Property.IsCustomized) || method.Params[i].IsLocalVar)
                    {
                        ParameterCsExporter.GenerateCode(defaultObj, method.Params[i], stream, indent, method.IsPublic ? nativeType : "", param, caller);
                    }
                    else
                    {
                        if (method.IsPublic)
                        {
                            param = ParameterCsExporter.GenerateCode(defaultObj, method.Params[i], stream, indent, nativeType, "", param);
                        }
                        else
                        {
                            ParameterCsExporter.GenerateCode(defaultObj, method.Params[i], stream, indent, "", param, caller);
                        }
                    }

                    VariableDef v = method.Params[i].Value as VariableDef;
                    if (v != null && v.ArrayIndexElement != null)
                    {
                        PropertyDef prop = method.Params[i].Property;
                        if (prop != null && prop.IsArrayElement)
                        {
                            ParameterCsExporter.GenerateCode(defaultObj, v.ArrayIndexElement, stream, indent, "int", param + "_index", param + caller);

                            if (string.IsNullOrEmpty(param))
                            {
                                string property = PropertyCsExporter.GetProperty(defaultObj, prop, v.ArrayIndexElement, stream, indent, param, caller);
                                param = string.Format("({0})[{1}_index]", property, param);
                            }
                            else
                            {
                                param = string.Format("{0}[{0}_index]", param);
                            }
                        }
                    }
                }
                else // const value
                {
                    object obj = method.Params[i].Value;
                    if (obj != null)
                    {
                        Type type = obj.GetType();
                        if (Plugin.IsCustomClassType(type) && !DesignerStruct.IsPureConstDatum(obj, method, method.Params[i].Name))
                        {
                            string paramName = getParamName(var, caller, i);
                            string paramType = DataCsExporter.GetGeneratedNativeType(method.Params[i].NativeType);

                            StructCsExporter.GenerateCode(obj, defaultObj, stream, indent, paramName, paramType, method, method.Params[i].Name);
                            if (!method.IsPublic)
                            {
                                stream.WriteLine("{0}{1} = {2};", indent, param, paramName);
                            }
                        }
                    }
                }

                if (i > 0)
                {
                    allParams += ", ";
                }

                if (method.Params[i].IsRef)
                {
                    param = "ref " + param;
                }
                else if (method.Params[i].IsOut)
                {
                    param = "out " + param;
                }

                allParams += param;
            }

            string agentName = "pAgent";

            if (method.Owner != Behaviac.Design.VariableDef.kSelf && (!method.IsPublic || !method.IsStatic))
            {
                string instanceName = method.Owner.Replace("::", ".");
                agentName = "pAgent_" + caller;

                bool        isGlobal      = Plugin.IsInstanceName(instanceName, null);
                PropertyDef ownerProperty = null;

                if (!isGlobal)
                {
                    Debug.Check(defaultObj != null && defaultObj.Behavior != null && defaultObj.Behavior.AgentType != null);
                    ownerProperty = defaultObj.Behavior.AgentType.GetPropertyByName(instanceName);
                }

                if (isGlobal || ownerProperty == null || ownerProperty.IsCustomized) // global or customized instance
                {
                    stream.WriteLine("{0}behaviac.Agent {1} = behaviac.Utils.GetParentAgent(pAgent, \"{2}\");", indent, agentName, instanceName);
                }
                else // member instance
                {
                    string prop = "";

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

                        if (ownerProperty.IsStatic)
                        {
                            prop = string.Format("{0}.{1}", className, instanceName);
                        }
                        else
                        {
                            prop = string.Format("(({0})pAgent).{1}", className, instanceName);
                        }
                    }
                    else
                    {
                        string nativeType = DataCsExporter.GetGeneratedNativeType(ownerProperty.NativeType);
                        prop = string.Format("({0})AgentMetaVisitor.GetProperty(pAgent, \"{1}\")", nativeType, instanceName);
                    }

                    stream.WriteLine("{0}behaviac.Agent {1} = {2};", indent, agentName, prop);
                }
                stream.WriteLine("{0}Debug.Check({1} != null || Utils.IsStaticClass(\"{2}\"));", indent, agentName, instanceName);
            }

            string retStr = string.Empty;

            if (method.IsPublic)
            {
                string className = method.ClassName.Replace("::", ".");
                if (method.IsStatic)
                {
                    retStr = string.Format("{0}.{1}({2})", className, method.BasicName, allParams);
                }
                else
                {
                    retStr = string.Format("(({0}){1}).{2}({3})", className, agentName, method.BasicName, allParams);
                }
            }
            else
            {
                retStr = string.Format("AgentMetaVisitor.ExecuteMethod({0}, \"{1}\", {2})", agentName, method.BasicName, paramsName);
            }

            if (!string.IsNullOrEmpty(var))
            {
                string nativeReturnType = DataCsExporter.GetGeneratedNativeType(method.NativeReturnType);
                string typeConvertStr   = (nativeReturnType == "void") ? string.Empty : "(" + nativeReturnType + ")";

                stream.WriteLine("{0}{1} {2} = {3}{4};", indent, nativeReturnType, var, typeConvertStr, retStr);
            }

            return(retStr);
        }