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.IsRefType(type)) { string nativeType = DataCppExporter.GetBasicGeneratedNativeType(variable.NativeType); stream.WriteLine("{0}\t\t\t{1} = NULL;", indent, var); } else if (Plugin.IsArrayType(type) || Plugin.IsCustomClassType(type) || (Plugin.IsStringType(type) && !variable.IsConst)) { if (Plugin.IsArrayType(type)) { string nativeType = DataCppExporter.GetGeneratedNativeType(variable.NativeType); int startIndex = nativeType.IndexOf('<'); int endIndex = nativeType.LastIndexOf('>'); string itemType = nativeType.Substring(startIndex + 1, endIndex - startIndex - 1); ArrayCppExporter.GenerateCode(variable.Value, stream, indent + "\t\t\t", itemType, var); } else if (Plugin.IsCustomClassType(type)) { StructCppExporter.GenerateCode(variable.Value, stream, indent + "\t\t\t", var, null, ""); } else if ((Plugin.IsStringType(type) && !variable.IsConst)) { string nativeType = DataCppExporter.GetBasicGeneratedNativeType(variable.NativeType); string retStr = DataCppExporter.GenerateCode(variable.Value, stream, indent + "\t\t\t", nativeType, string.Empty, string.Empty); stream.WriteLine("{0}\t\t\t{1} = {2};", indent, var, retStr); } } } }
public static void GenerateClassConstructor(DefaultObject defaultObj, Behaviac.Design.MethodDef method, StringWriter stream, string indent, string var) { for (int i = 0; i < method.Params.Count; ++i) { // const value if (!method.Params[i].IsProperty && !method.Params[i].IsLocalVar) { object obj = method.Params[i].Value; if (obj != null) { string param = var + "_p" + i; Type type = obj.GetType(); if (Plugin.IsArrayType(type)) { string nativeType = DataCppExporter.GetGeneratedNativeType(method.Params[i].NativeType); int startIndex = nativeType.IndexOf('<'); int endIndex = nativeType.LastIndexOf('>'); string itemType = nativeType.Substring(startIndex + 1, endIndex - startIndex - 1); ArrayCppExporter.GenerateCode(obj, defaultObj, stream, indent + "\t\t\t", itemType, param); } else if (Plugin.IsCustomClassType(type)) { if (Plugin.IsRefType(type)) { stream.WriteLine("{0}\t\t\t{1} = NULL;", indent, param); } else if (DesignerStruct.IsPureConstDatum(obj, method, method.Params[i].Name)) { StructCppExporter.GenerateCode(obj, defaultObj, stream, indent + "\t\t\t", param, null, ""); } } else { string nativeType = DataCppExporter.GetBasicGeneratedNativeType(method.Params[i].NativeType); string retStr = DataCppExporter.GenerateCode(obj, defaultObj, stream, string.Empty, nativeType, string.Empty, string.Empty); stream.WriteLine("{0}\t\t\t{1} = {2};", indent, param, retStr); } } } } }
/// <summary> /// Generate the native 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 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 = MethodCppExporter.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 = ParameterCppExporter.GenerateCode(param, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.ParInfo) { Behaviac.Design.ParInfo par = obj as Behaviac.Design.ParInfo; retStr = ParInfoCppExporter.GenerateCode(par, false, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.PropertyDef) { Behaviac.Design.PropertyDef property = obj as Behaviac.Design.PropertyDef; retStr = PropertyCppExporter.GenerateCode(property, null, false, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.VariableDef) { Behaviac.Design.VariableDef variable = obj as Behaviac.Design.VariableDef; retStr = VariableCppExporter.GenerateCode(variable, false, stream, indent, typename, var, caller); } else if (obj is Behaviac.Design.RightValueDef) { Behaviac.Design.RightValueDef rightValue = obj as Behaviac.Design.RightValueDef; retStr = RightValueCppExporter.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, DataCppExporter.GetBasicGeneratedNativeType(typename), var); } else { typename = DataCppExporter.GetGeneratedNativeType(type); } int startIndex = typename.IndexOf('<'); int endIndex = typename.LastIndexOf('>'); string itemType = typename.Substring(startIndex + 1, endIndex - startIndex - 1); ArrayCppExporter.GenerateCode(obj, stream, indent, itemType, var); } // Struct type else if (Plugin.IsCustomClassType(type)) { retStr = var; if (!string.IsNullOrEmpty(typename)) { if (typename.EndsWith("*")) { stream.WriteLine("{0}{1} {2} = NULL;", indent, DataCppExporter.GetBasicGeneratedNativeType(typename), var); } else { stream.WriteLine("{0}{1} {2};", indent, DataCppExporter.GetBasicGeneratedNativeType(typename), var); } } StructCppExporter.GenerateCode(obj, stream, indent, var, null, ""); } // Other types else { retStr = obj.ToString(); if (Plugin.IsStringType(type)) // string { retStr = string.Format("\"{0}\"", retStr); if (typename.StartsWith("behaviac::wstring")) { retStr = string.Format("StringUtils::MBSToWCS({0})", retStr); } else { retStr = string.Format("(char*)({0})", retStr); } } else if (Plugin.IsBooleanType(type)) // bool { retStr = retStr.ToLowerInvariant(); } else if (Plugin.IsEnumType(type)) // enum { retStr = EnumCppExporter.GeneratedCode(obj); } else if (Plugin.IsFloatType(type)) // float { if (retStr.Contains(".") && !retStr.EndsWith("f") && !retStr.EndsWith("F")) { retStr = retStr + "f"; } } if (!string.IsNullOrEmpty(var)) { if (string.IsNullOrEmpty(typename)) { stream.WriteLine("{0}{1} = {2};", indent, var, retStr); } else { typename = DataCppExporter.GetGeneratedNativeType(typename); stream.WriteLine("{0}{1} {2} = {3};", indent, typename, var, retStr); } } } } return(retStr); }
public static string GenerateCode(Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string typename, string var, string caller) { string allParamTypes = string.Empty; string allParams = string.Empty; for (int i = 0; i < method.Params.Count; ++i) { string nativeType = DataCppExporter.GetGeneratedNativeType(method.Params[i].NativeType); string basicNativeType = DataCppExporter.GetBasicGeneratedNativeType(nativeType); string param = (string.IsNullOrEmpty(var) ? caller : var) + "_p" + i; allParamTypes += ", " + nativeType; 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) { ParameterCppExporter.GenerateCode(method.Params[i], stream, indent, basicNativeType, param, caller); } else { if (method.IsPublic) { param = ParameterCppExporter.GenerateCode(method.Params[i], stream, indent, basicNativeType, "", param); } else { ParameterCppExporter.GenerateCode(method.Params[i], stream, indent, basicNativeType, 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)) { StructCppExporter.GenerateCode(obj, stream, indent, param, method, method.Params[i].Name); } } } if (basicNativeType == "System::Object") { param = "*(System::Object*)&" + param; } if (i > 0) { allParams += ", "; } allParams += param; } string agentName = "pAgent"; if (method.Owner != Behaviac.Design.VariableDef.kSelf) { agentName = "pAgent_" + caller; stream.WriteLine("{0}Agent* {1} = Agent::GetInstance(pAgent, \"{2}\");", indent, agentName, method.Owner); stream.WriteLine("{0}BEHAVIAC_ASSERT({1});", indent, agentName); } string nativeReturnType = DataCppExporter.GetGeneratedNativeType(method.NativeReturnType); string retStr = ""; if (method.IsPublic) { if (method.IsStatic) { retStr = string.Format("{0}::{1}({2})", method.ClassName, method.BasicName, allParams); } else { retStr = string.Format("(({0}*){1})->{2}({3})", method.ClassName, agentName, method.BasicName, allParams); } } else { retStr = string.Format("(({0}*){1})->_Execute_Method_<{2}METHOD_TYPE_{3}, {4}{5} >({6})", method.ClassName, agentName, getNamespace(method.ClassName), method.Name.Replace("::", "_"), nativeReturnType, allParamTypes, allParams); } if (!string.IsNullOrEmpty(var)) { stream.WriteLine("{0}{1} {2} = {3};", indent, nativeReturnType, var, retStr); } return(retStr); }
public static string GenerateCode(DefaultObject defaultObj, Behaviac.Design.MethodDef method, StringWriter stream, string indent, string typename, string var, string caller) { string allParamTypes = string.Empty; string allParams = string.Empty; for (int i = 0; i < method.Params.Count; ++i) { string nativeType = DataCppExporter.GetGeneratedNativeType(method.Params[i].NativeType); string basicNativeType = DataCppExporter.GetBasicGeneratedNativeType(nativeType); string param = (string.IsNullOrEmpty(var) ? caller : var) + "_p" + i; allParamTypes += ", " + nativeType; if (method.Params[i].IsProperty || method.Params[i].IsLocalVar) // property { 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) { string property = PropertyCppExporter.GetProperty(defaultObj, prop, v.ArrayIndexElement, stream, indent, param, caller); string propName = prop.BasicName.Replace("[]", ""); ParameterCppExporter.GenerateCode(defaultObj, v.ArrayIndexElement, stream, indent, "int", param + "_index", param + caller); param = string.Format("({0})[{1}_index]", property, param); } } else { if ((method.Params[i].Property != null && method.Params[i].Property.IsCustomized) || method.Params[i].IsLocalVar) { ParameterCppExporter.GenerateCode(defaultObj, method.Params[i], stream, indent, basicNativeType, param, caller); } else { if (method.IsPublic) { param = ParameterCppExporter.GenerateCode(defaultObj, method.Params[i], stream, indent, basicNativeType, "", param); } else { ParameterCppExporter.GenerateCode(defaultObj, method.Params[i], stream, indent, basicNativeType, param, caller); } } } } else // const value { object obj = method.Params[i].Value; if (obj != null) { Type type = obj.GetType(); //if (Plugin.IsArrayType(type)) //{ // string nativeTypeStr = DataCppExporter.GetGeneratedNativeType(method.Params[i].NativeType); // int startIndex = nativeTypeStr.IndexOf('<'); // int endIndex = nativeTypeStr.LastIndexOf('>'); // string itemType = nativeTypeStr.Substring(startIndex + 1, endIndex - startIndex - 1); // ArrayCppExporter.GenerateCode(obj, stream, indent, itemType, param); //} //else if (Plugin.IsCustomClassType(type) && !DesignerStruct.IsPureConstDatum(obj, method, method.Params[i].Name)) { StructCppExporter.GenerateCode(obj, defaultObj, stream, indent, param, method, method.Params[i].Name); } } } if (basicNativeType == "System::Object") { param = "*(System::Object*)&" + param; } if (i > 0) { allParams += ", "; } allParams += param; } string agentName = "pAgent"; if (method.Owner != Behaviac.Design.VariableDef.kSelf) { agentName = "pAgent_" + caller; bool isGlobal = Plugin.IsInstanceName(method.Owner, null); PropertyDef ownerProperty = null; if (!isGlobal) { Debug.Check(defaultObj != null && defaultObj.Behavior != null && defaultObj.Behavior.AgentType != null); if (defaultObj != null && defaultObj.Behavior != null && defaultObj.Behavior.AgentType != null) { ownerProperty = defaultObj.Behavior.AgentType.GetPropertyByName(method.Owner); } } if (isGlobal || ownerProperty == null || ownerProperty.IsCustomized || ownerProperty.IsPar) // global or customized instance { stream.WriteLine("{0}Agent* {1} = Agent::GetInstance(pAgent, \"{2}\");", indent, agentName, method.Owner); } else // member instance { string propName = ownerProperty.Name.Replace("::", "_"); string nativeType = DataCppExporter.GetGeneratedNativeType(ownerProperty.NativeType); string prop = string.Format("(({0}*)pAgent)->_Get_Property_<{1}PROPERTY_TYPE_{2}, {3} >()", ownerProperty.ClassName, getNamespace(ownerProperty.ClassName), propName, nativeType); stream.WriteLine("{0}Agent* {1} = {2};", indent, agentName, prop); } stream.WriteLine("{0}BEHAVIAC_ASSERT({1});", indent, agentName); } string nativeReturnType = DataCppExporter.GetGeneratedNativeType(method.NativeReturnType); if (method.NativeReturnType.StartsWith("const ")) { nativeReturnType = "const " + nativeReturnType; } string retStr = ""; if (method.IsPublic) { if (method.IsStatic) { retStr = string.Format("{0}::{1}({2})", method.ClassName, method.BasicName, allParams); } else { retStr = string.Format("(({0}*){1})->{2}({3})", method.ClassName, agentName, method.BasicName, allParams); } } else { retStr = string.Format("(({0}*){1})->_Execute_Method_<{2}METHOD_TYPE_{3}, {4}{5} >({6})", method.ClassName, agentName, getNamespace(method.ClassName), method.Name.Replace("::", "_"), nativeReturnType, allParamTypes, allParams); } if (!string.IsNullOrEmpty(var)) { stream.WriteLine("{0}{1} {2} = {3};", indent, nativeReturnType, var, retStr); } return(retStr); }
public static string GenerateCode(Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string typename, string var, string caller) { string allParamTypes = string.Empty; string allParams = string.Empty; for (int i = 0; i < method.Params.Count; ++i) { string nativeType = DataCppExporter.GetGeneratedNativeType(method.Params[i].NativeType); bool isPointRefType = nativeType.EndsWith("*&"); string basicNativeType = DataCppExporter.GetBasicGeneratedNativeType(nativeType); string param = (string.IsNullOrEmpty(var) ? caller : var) + "_p" + i; string paramBasicType = basicNativeType; if (!isPointRefType && DataCppExporter.IsPtr(paramBasicType)) { paramBasicType = paramBasicType.Replace("*", ""); paramBasicType = paramBasicType.Trim(); } allParamTypes += ", " + paramBasicType; if (method.Params[i].IsProperty) // property { param = ParameterCppExporter.GenerateCode(method.Params[i], stream, indent, basicNativeType, string.Empty, param); if (!isPointRefType && DataCppExporter.IsPtr(basicNativeType)) { param = "*" + param; } } else { if (method.Params[i].IsPar) // par { ParameterCppExporter.GenerateCode(method.Params[i], stream, indent, basicNativeType, param, caller); if (!isPointRefType && DataCppExporter.IsPtr(basicNativeType)) { param = "*" + 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)) { StructCppExporter.GenerateCode(obj, stream, indent, param, method, method.Params[i].Name); } } if (!isPointRefType && DataCppExporter.IsAgentPtr(basicNativeType)) { param = "*" + param; } } } if (i > 0) { allParams += ", "; } allParams += param; } string agentName = "pAgent"; if (method.Owner != Behaviac.Design.VariableDef.kSelf) { agentName = "pAgent_" + caller; stream.WriteLine("{0}Agent* {1} = Agent::GetInstance(\"{2}\", pAgent->GetContextId());", indent, agentName, method.Owner); stream.WriteLine("{0}BEHAVIAC_ASSERT({1});", indent, agentName); } //string retStr = string.Format("(({0}*){1})->{2}({3})", method.ClassName, agentName, method.Name, allParams); string nativeReturnType = DataCppExporter.GetGeneratedNativeType(method.NativeReturnType); string retStr = string.Format("(({0}*){1})->_Execute_Method_<{2}METHOD_TYPE_{3}, {4}{5} >({6})", method.ClassName, agentName, getNamespace(method.ClassName), method.Name.Replace("::", "_"), nativeReturnType, allParamTypes, allParams); if (!string.IsNullOrEmpty(var)) { stream.WriteLine("{0}{1} {2} = {3};", indent, nativeReturnType, var, retStr); } return(retStr); }