public static string GenerateCode(Behaviac.Design.VariableDef variable, StreamWriter stream, string indent, string typename, string var, string caller, string setValue = null) { string retStr = string.Empty; if (variable.ValueClass == Behaviac.Design.VariableDef.kConst || variable.ValueClass == Behaviac.Design.VariableDef.kPar) { bool shouldGenerate = true; if (variable.ValueClass == Behaviac.Design.VariableDef.kConst) { Type type = variable.Value.GetType(); if (Plugin.IsArrayType(type) || Plugin.IsCustomClassType(type) || Plugin.IsStringType(type)) { shouldGenerate = false; } } if (shouldGenerate) { retStr = DataCsExporter.GenerateCode(variable.Value, stream, indent, typename, var, caller, setValue); } } else if (variable.Property != null) { retStr = PropertyCsExporter.GenerateCode(variable.Property, stream, indent, typename, var, caller, setValue); } return(retStr); }
public static string GenerateCode(DefaultObject defaultObj, VariableDef variable, bool isRefParam, StreamWriter stream, string indent, string typename, string var, string caller, string setValue = null) { string retStr = string.Empty; if (variable.ValueClass == Behaviac.Design.VariableDef.kConst) { bool shouldGenerate = true; Type type = variable.Value.GetType(); if (Plugin.IsArrayType(type) || Plugin.IsCustomClassType(type) || (Plugin.IsStringType(type) && !variable.IsConst)) { shouldGenerate = false; } if (shouldGenerate) { retStr = DataCsExporter.GenerateCode(variable.Value, defaultObj, stream, indent, typename, var, caller, setValue); } } else if (variable.Property != null) { retStr = PropertyCsExporter.GenerateCode(defaultObj, variable.Property, variable.ArrayIndexElement, isRefParam, stream, indent, typename, var, caller, setValue); } return(retStr); }
public static void PostGenerateCode(Behaviac.Design.VariableDef variable, StreamWriter stream, string indent, string typename, string var, string caller, object parent = null, string paramName = "", string setValue = null) { if (variable.ValueClass == Behaviac.Design.VariableDef.kConst) { Type type = variable.Value.GetType(); if (Plugin.IsCustomClassType(type) && !DesignerStruct.IsPureConstDatum(variable.Value, parent, paramName)) { StructCsExporter.PostGenerateCode(variable.Value, stream, indent, var, parent, paramName); } } else if (variable.Property != null) { PropertyCsExporter.PostGenerateCode(variable.Property, variable.ArrayIndexElement, stream, indent, typename, var, caller, setValue); } }
/// <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); }
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); }