public static void GenerateClassMember(Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string var) { Debug.Check(!string.IsNullOrEmpty(var)); if (method.Params.Count == 0) { return; } if (!method.IsPublic) { string paramsName = getParamsName(var, ""); stream.WriteLine("{0}\t\tobject[] {1};", indent, paramsName); } for (int i = 0; i < method.Params.Count; ++i) { // const value if (!method.Params[i].IsProperty && !method.Params[i].IsPar) { Type type = method.Params[i].Value.GetType(); if (method.IsPublic || Plugin.IsArrayType(type) || Plugin.IsCustomClassType(type)) { string param = getParamName(var, "", i); string nativeType = DataCsExporter.GetGeneratedNativeType(method.Params[i].NativeType); stream.WriteLine("{0}\t\t{1} {2};", indent, nativeType, param); } } } }
public static void PostGenerateCode(Behaviac.Design.PropertyDef property, StreamWriter stream, string indent, string typename, string var, string caller, string setValue = null) { string agentName = getGenerateAgentName(property, var, caller); string prop = setValue; if (property.Owner != Behaviac.Design.VariableDef.kSelf) { stream.WriteLine("{0}behaviac.Agent {1} = behaviac.Agent.GetInstance(\"{2}\", pAgent.GetContextId());", indent, agentName, property.Owner.Replace("::", ".")); stream.WriteLine("{0}Debug.Check({1} != null);", indent, agentName); } if (setValue != null) { stream.WriteLine("{0}AgentExtra_Generated.SetProperty({1}, \"{2}\", {3});", indent, agentName, property.BasicName, prop); } else { prop = getProperty(property, agentName); } uint id = Behaviac.Design.CRC32.CalcCRC(property.BasicName); stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, property.BasicName, id); if (string.IsNullOrEmpty(typename)) { typename = property.NativeType; } typename = DataCsExporter.GetGeneratedNativeType(typename); stream.WriteLine("{0}{1}.SetVariable<{2}>(\"{3}\", {4}, {5}u);", indent, agentName, typename, property.BasicName, prop, id); }
public static void PostGenerateCode(Behaviac.Design.MethodDef method, StreamWriter stream, string indent, string typename, string var, string caller) { string paramsName = getParamsName(var, caller); for (int i = 0; i < method.Params.Count; ++i) { //if (method.Params[i].NativeType.EndsWith("&")) if (method.Params[i].NativeType.IndexOf("&") >= 0) { object obj = method.Params[i].Value; if (obj != null) { string nativeType = DataCsExporter.GetGeneratedNativeType(method.Params[i].NativeType); string param = getParamName(var, caller, i); string paramName = string.Format("(({0}){1}[{2}])", nativeType, paramsName, i); if (!method.Params[i].IsProperty && !method.Params[i].IsPar) { Type type = obj.GetType(); if (!Plugin.IsArrayType(type) && !Plugin.IsCustomClassType(type)) { param = paramName; } } else { paramName = null; } ParameterCsExporter.PostGenerateCode(method.Params[i], stream, indent, nativeType, param, caller, method, paramName); } } } }
public static void PostGenerateCode(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) { PostGenerateCode(member, stream, indent, var + "." + property.Property.Name, parent, paramName); } else { if (method != null) { MethodDef.Param param = method.GetParam(paramName, property); if (param != null) { string nativeType = DataCsExporter.GetGeneratedNativeType(param.NativeType); ParameterCsExporter.PostGenerateCode(param, stream, indent, nativeType, var + "." + property.Property.Name, string.Empty, method, null); } } } } } }
public static string GenerateCode(Behaviac.Design.PropertyDef property, StreamWriter stream, string indent, string typename, string var, string caller, string setValue = null) { string agentName = getGenerateAgentName(property, var, caller); if (property.Owner != Behaviac.Design.VariableDef.kSelf) { stream.WriteLine("{0}behaviac.Agent {1} = behaviac.Agent.GetInstance(\"{2}\", pAgent.GetContextId());", indent, agentName, property.Owner.Replace("::", ".")); stream.WriteLine("{0}Debug.Check({1} != null);", indent, agentName); } string prop = getProperty(property, agentName); if (setValue == null) { if (!string.IsNullOrEmpty(var)) { if (string.IsNullOrEmpty(typename)) { stream.WriteLine("{0}{1} = {2};", indent, var, prop); } else { stream.WriteLine("{0}{1} {2} = {3};", indent, DataCsExporter.GetGeneratedNativeType(property.NativeType), var, prop); } } } else { stream.WriteLine("{0}AgentExtra_Generated.SetProperty({1}, \"{2}\", {3});", indent, agentName, property.BasicName, setValue); } return(prop); }
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); } } } }
public static void GenerateClassMember(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)) { string nativeType = DataCsExporter.GetGeneratedNativeType(variable.NativeType); bool setNull = Plugin.IsArrayType(type); if (!setNull && Plugin.IsCustomClassType(type)) { Attribute[] attributes = (Attribute[])type.GetCustomAttributes(typeof(Behaviac.Design.ClassDescAttribute), false); if (attributes.Length > 0) { Behaviac.Design.ClassDescAttribute classDesc = (Behaviac.Design.ClassDescAttribute)attributes[0]; if (!classDesc.IsStruct) { setNull = true; } } } if (setNull) { var += " = null"; } stream.WriteLine("{0}\t\t{1} {2};", indent, nativeType, var); } } }
public static void GenerateCode(object obj, StreamWriter stream, string indent, string itemTypename, string var) { if (obj != null) { Type type = obj.GetType(); if (Plugin.IsArrayType(type)) { System.Collections.IList list = (System.Collections.IList)obj; if (list.Count > 0) { stream.WriteLine("{0}{1} = new {2}();", indent, var, DataCsExporter.GetGeneratedNativeType(type)); stream.WriteLine("{0}{1}.Capacity = {2};", indent, var, list.Count); for (int i = 0; i < list.Count; ++i) { string itemName = string.Format("{0}_item{1}", var, i); DataCsExporter.GenerateCode(list[i], stream, indent, itemTypename, itemName, string.Empty); stream.WriteLine("{0}{1}.Add({2});", indent, var, itemName); } } } } }
public static string GenerateCode(Behaviac.Design.ParInfo par, StreamWriter stream, string indent, string typename, string var, string caller) { bool shouldDefineType = true; if (string.IsNullOrEmpty(typename)) { shouldDefineType = false; typename = par.NativeType; } typename = DataCsExporter.GetGeneratedNativeType(typename); uint id = Behaviac.Design.CRC32.CalcCRC(par.Name); stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, par.Name, id); string retStr = string.Format("pAgent.GetVariable<{0}>({1}u)", typename, id); if (!string.IsNullOrEmpty(var)) { if (shouldDefineType) { stream.WriteLine("{0}{1} {2} = {3};", indent, typename, var, retStr); } else { stream.WriteLine("{0}{1} = {2};", indent, var, retStr); } } return(retStr); }
public static string GetProperty(DefaultObject defaultObj, Behaviac.Design.PropertyDef property, MethodDef.Param arrayIndexElement, StringWriter stream, string indent, string var, string caller) { string agentName = GetGenerateAgentName(property, var, caller); if (property.Owner != Behaviac.Design.VariableDef.kSelf) { string instanceName = property.Owner.Replace("::", "."); bool isGlobal = Plugin.IsInstanceName(instanceName, null); PropertyDef ownerProperty = null; if (!isGlobal) { Debug.Check(defaultObj != null && defaultObj.Behavior != null && defaultObj.Behavior.AgentType != null); if (defaultObj != null && defaultObj.Behavior != null) { ownerProperty = defaultObj.Behavior.AgentType.GetPropertyByName(instanceName); } } if (isGlobal || ownerProperty == null || ownerProperty.IsCustomized || ownerProperty.IsPar) // 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(!System.Object.ReferenceEquals({1}, null) || Utils.IsStaticClass(\"{2}\"));", indent, agentName, instanceName); } return(getProperty(property, arrayIndexElement, agentName, stream, indent)); }
public static void PostGenerateCode(Behaviac.Design.ParInfo par, StreamWriter stream, string indent, string typename, string var, string caller) { if (string.IsNullOrEmpty(typename)) { typename = par.NativeType; } typename = DataCsExporter.GetGeneratedNativeType(typename); uint id = Behaviac.Design.CRC32.CalcCRC(par.Name); stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, par.Name, id); stream.WriteLine("{0}pAgent.SetVariable<{1}>(\"{2}\", {3}, {4}u);", indent, typename, par.Name, var, id); }
public static void PostGenerateCode(Behaviac.Design.PropertyDef property, MethodDef.Param arrayIndexElement, StreamWriter stream, string indent, string typename, string var, string caller) { if (string.IsNullOrEmpty(typename)) { typename = property.NativeType; } typename = DataCsExporter.GetGeneratedNativeType(typename); string propBasicName = property.BasicName.Replace("[]", ""); uint id = Behaviac.Design.CRC32.CalcCRC(propBasicName); stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, propBasicName, id); stream.WriteLine("{0}pAgent.SetVariable<{1}>(\"{2}\", {3}u, ({1}){4});", indent, typename, property.Name, id, var); }
public static string GetGeneratedNativeType(Type type) { if (type == null) { return(string.Empty); } if (Plugin.IsArrayType(type)) { Type itemType = type.GetGenericArguments()[0]; return(string.Format("List<{0}>", DataCsExporter.GetGeneratedNativeType(itemType))); } return(GetGeneratedNativeType(type.Name)); }
private static string getProperty(Behaviac.Design.PropertyDef property, string agentName) { if (property.IsPublic) { string className = property.ClassName.Replace("::", "."); if (property.IsStatic) { return(string.Format("{0}.{1}", className, property.BasicName)); } else { return(string.Format("(({0}){1}).{2}", className, agentName, property.BasicName)); } } string nativeType = DataCsExporter.GetGeneratedNativeType(property.NativeType); return(string.Format("({0})AgentExtra_Generated.GetProperty({1}, \"{2}\")", nativeType, agentName, property.BasicName)); }
public static void PostGenerateCode(Behaviac.Design.PropertyDef property, MethodDef.Param arrayIndexElement, StreamWriter stream, string indent, string typename, string var, string caller, string setValue = null) { if (property.IsPar || property.IsCustomized) { ParInfoCsExporter.PostGenerateCode(property, arrayIndexElement, stream, indent, typename, var, caller); return; } if (!property.IsReadonly) { string agentName = GetGenerateAgentName(property, var, caller); string prop = setValue; //if (property.Owner != Behaviac.Design.VariableDef.kSelf) //{ // stream.WriteLine("{0}behaviac.Agent {1} = behaviac.Agent.GetInstance(\"{2}\", pAgent.GetContextId());", indent, agentName, property.Owner.Replace("::", ".")); // stream.WriteLine("{0}Debug.Check({1} != null);", indent, agentName); //} string propBasicName = property.BasicName.Replace("[]", ""); if (setValue != null) { stream.WriteLine("{0}AgentExtra_Generated.SetProperty({1}, \"{2}\", {3});", indent, agentName, propBasicName, prop); } else { prop = getProperty(property, arrayIndexElement, agentName, stream, indent); } uint id = Behaviac.Design.CRC32.CalcCRC(propBasicName); stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, propBasicName, id); if (string.IsNullOrEmpty(typename)) { typename = property.NativeType; } typename = DataCsExporter.GetGeneratedNativeType(typename); stream.WriteLine("{0}{1}.SetVariable<{2}>(\"{3}\", {4}, {5}u);", indent, agentName, typename, property.BasicName, prop, id); } }
public static string GetProperty(Behaviac.Design.PropertyDef property, MethodDef.Param arrayIndexElement, StreamWriter stream, string indent) { string retStr = string.Empty; if (property != null) { string typename = DataCsExporter.GetGeneratedNativeType(property.NativeType); if (property.IsArrayElement && !typename.StartsWith("List<")) { typename = string.Format("List<{0}>", typename); } string propBasicName = property.BasicName.Replace("[]", ""); uint id = Behaviac.Design.CRC32.CalcCRC(propBasicName); stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, propBasicName, id); retStr = string.Format("pAgent.GetVariable<{0}>({1}u)", typename, id); } return(retStr); }
/// <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 GetPropertyNativeType(Behaviac.Design.PropertyDef property, MethodDef.Param arrayIndexElement) { string nativeType = DataCsExporter.GetGeneratedNativeType(property.NativeType); return(nativeType); }
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); } } } } } }
public static string GenerateCode(Behaviac.Design.PropertyDef property, bool isRefParam, StreamWriter stream, string indent, string typename, string var, string caller) { bool shouldDefineType = true; if (string.IsNullOrEmpty(typename)) { shouldDefineType = false; typename = property.NativeType; } typename = DataCsExporter.GetGeneratedNativeType(typename); if (property.IsArrayElement && !typename.StartsWith("List<")) { typename = string.Format("List<{0}>", typename); } string propBasicName = property.BasicName.Replace("[]", ""); uint id = Behaviac.Design.CRC32.CalcCRC(propBasicName); string retStr = string.Format("pAgent.GetVariable<{0}>({1}u)", typename, id); if (!string.IsNullOrEmpty(var)) { stream.WriteLine("{0}Debug.Check(behaviac.Utils.MakeVariableId(\"{1}\") == {2}u);", indent, propBasicName, id); //if (isRefParam) //{ // if (shouldDefineType) // stream.WriteLine("{0}{1} {2};", indent, typename, var); // Type type = property.Type; // if (type != null && (type.IsValueType || Plugin.IsEnumType(type) || Plugin.IsStringType(type))) // { // stream.WriteLine("{0}object var_{1} = pAgent.GetVariableObject({2}u);", indent, var, id); // stream.WriteLine("{0}if (var_{1} != null)", indent, var); // stream.WriteLine("{0}\t{1} = ({2})var_{1};", indent, var, typename); // stream.WriteLine("{0}else", indent, var); // if (!Plugin.IsArrayType(property.Type) && !Plugin.IsCustomClassType(property.Type)) // { // object defaultObj = Plugin.DefaultValue(property.Type); // Debug.Check(defaultObj != null); // string objStr = DesignerPropertyUtility.RetrieveExportValue(defaultObj); // if (defaultObj is char) // { // objStr = "(char)0"; // } // else if (Plugin.IsEnumType(defaultObj.GetType())) // { // objStr = string.Format("{0}.{1}", typename, objStr); // } // stream.WriteLine("{0}\t{1} = {2};", indent, var, objStr); // } // else // { // stream.WriteLine("{0}\t{1} = new {2}();", indent, var, DataCsExporter.GetGeneratedNativeType(property.Type)); // } // } // else // { // stream.WriteLine("{0}{1} = ({2})pAgent.GetVariableObject({3}u);", indent, var, typename, id); // } //} //else { if (shouldDefineType) { stream.WriteLine("{0}{1} {2} = {3};", indent, typename, var, retStr); } else { stream.WriteLine("{0}{1} = {2};", indent, var, retStr); } } } return(retStr); }
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); }
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); }
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); } } } } }