Ejemplo n.º 1
0
        public static void GenerateClassConstructor(Behaviac.Design.VariableDef variable, StreamWriter stream, string indent, string var)
        {
            if (variable.ValueClass == Behaviac.Design.VariableDef.kConst)
            {
                Type type = variable.Value.GetType();
                if (Plugin.IsArrayType(type) || Plugin.IsCustomClassType(type) || Plugin.IsStringType(type))
                {
                    if (Plugin.IsArrayType(type))
                    {
                        string nativeType = DataCsExporter.GetGeneratedNativeType(variable.NativeType);
                        int    startIndex = nativeType.IndexOf('<');
                        int    endIndex   = nativeType.LastIndexOf('>');
                        string itemType   = nativeType.Substring(startIndex + 1, endIndex - startIndex - 1);

                        ArrayCsExporter.GenerateCode(variable.Value, stream, indent + "\t\t\t", itemType, var);
                    }
                    else if (Plugin.IsCustomClassType(type))
                    {
                        StructCsExporter.GenerateCode(variable.Value, stream, indent + "\t\t\t", var, null, "");
                    }
                    else if (Plugin.IsStringType(type))
                    {
                        string nativeType = DataCsExporter.GetGeneratedNativeType(variable.NativeType);
                        string retStr     = DataCsExporter.GenerateCode(variable.Value, stream, indent + "\t\t\t", nativeType, string.Empty, string.Empty);
                        stream.WriteLine("{0}\t\t\t{1} = {2};", indent, var, retStr);
                    }
                }
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public static void GenerateClassConstructor(Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string var)
        {
            Debug.Check(!string.IsNullOrEmpty(var));

            if (method.Params.Count == 0)
            {
                return;
            }

            string paramsName = getParamsName(var, "");

            if (!method.IsPublic)
            {
                stream.WriteLine("{0}\t\t\t{1} = new object[{2}];", indent, paramsName, method.Params.Count);
            }

            for (int i = 0; i < method.Params.Count; ++i)
            {
                // const value
                if (!method.Params[i].IsProperty && !method.Params[i].IsPar)
                {
                    object obj = method.Params[i].Value;
                    if (obj != null)
                    {
                        string param     = getParamName(var, "", i);
                        string paramName = string.Format("{0}[{1}]", paramsName, i);

                        Type type = obj.GetType();
                        if (Plugin.IsArrayType(type))
                        {
                            string typename   = DataCsExporter.GetGeneratedNativeType(method.Params[i].NativeType);
                            int    startIndex = typename.IndexOf('<');
                            int    endIndex   = typename.LastIndexOf('>');
                            string itemType   = typename.Substring(startIndex + 1, endIndex - startIndex - 1);

                            ArrayCsExporter.GenerateCode(obj, stream, indent + "\t\t\t", itemType, param);
                            if (!method.IsPublic)
                            {
                                stream.WriteLine("{0}\t\t\t{1} = {2};", indent, paramName, param);
                            }
                        }
                        else if (Plugin.IsCustomClassType(type))
                        {
                            if (DesignerStruct.IsPureConstDatum(obj, method, method.Params[i].Name))
                            {
                                if (obj is Behaviac.Design.Agent)
                                {
                                    stream.WriteLine("{0}\t\t\t{1} = null;", indent, param);
                                }
                                StructCsExporter.GenerateCode(obj, stream, indent + "\t\t\t", param, null, "");
                                if (!method.IsPublic)
                                {
                                    stream.WriteLine("{0}\t\t\t{1} = {2};", indent, paramName, param);
                                }
                            }
                        }
                        else
                        {
                            string retStr = DataCsExporter.GenerateCode(obj, stream, string.Empty, method.Params[i].NativeType, string.Empty, string.Empty);
                            if (!method.IsPublic)
                            {
                                param = paramName;
                            }
                            stream.WriteLine("{0}\t\t\t{1} = {2};", indent, param, retStr);
                        }
                    }
                }
            }
        }
Ejemplo n.º 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);
                            }
                        }
                    }
                }
            }
        }