void PushPropertyFunc(Type nVaueType, string szName, bool bCanGet, bool bCanSet, bool bStatic) { // bool alphaIsTransparency { get; set; } string szGetBody = string.Empty; string szSetBody = string.Empty; string szStatic = string.Empty; if (bCanGet) { szGetBody = " get; "; } if (bCanSet) { szSetBody = " set; "; } if (bStatic) { szStatic = "static "; } if (!bCanGet && bCanSet) { szSetBody = szSetBody.Replace(";", "{}"); } FCValueType value = FCValueType.TransType(nVaueType); string szValueType = value.GetTypeName(false); if (szValueType.IndexOf('`') != -1) { return; } m_szTempBuilder.AppendFormat(" public {0}{1} {2} {{{3}{4}}}\r\n", szStatic, szValueType, szName, szGetBody, szSetBody); }
void PushConstructor(ConstructorInfo cons) { ParameterInfo[] allParams = cons.GetParameters(); string szCallParam = string.Empty; Type nParamType; if (allParams != null) { for (int i = 0; i < allParams.Length; ++i) { ParameterInfo param = allParams[i]; nParamType = param.ParameterType; PushNameSpace(nParamType.Namespace); PushDelayType(nParamType); PushRefType(nParamType); if (i > 0) { szCallParam += ','; } FCValueType value = FCValueType.TransType(nParamType); szCallParam += value.GetTypeName(false); szCallParam = szCallParam + " " + param.Name; } } m_szTempBuilder.AppendFormat(" public {0}({1}){{}}\r\n", FCValueType.GetClassName(m_nClassType), szCallParam); }
void PushRefType(Type nType) { try { if (nType.IsByRef) { Type nRealType = nType.GetElementType(); if (nRealType != null) { nType = nRealType; } } FCValueType value = FCValueType.TransType(nType); if (value.IsArray || value.IsList) { m_AllRefType[value.m_value] = 1; } else if (value.IsMap) { m_AllRefType[value.m_key] = 1; m_AllRefType[value.m_value] = 1; } else { m_AllRefType[nType] = 1; } } catch (Exception e) { Debug.LogException(e); } }
void PushDelegateType(StringBuilder fileBuilder, string szLeft, Type nClassType) { //ConstructorInfo[] c1 = nClassType.GetConstructors(); MethodInfo method = nClassType.GetMethod("Invoke"); ParameterInfo[] allParams = method.GetParameters(); string szParamDesc = string.Empty; if (allParams != null) { for (int i = 0; i < allParams.Length; ++i) { PushRefType(allParams[i].ParameterType); FCValueType value_param = FCValueType.TransType(allParams[i].ParameterType); if (i > 0) { szParamDesc += ","; } szParamDesc += value_param.GetTypeName(false); szParamDesc += " arg" + i; } } FCValueType value_type = FCValueType.TransType(nClassType); FCValueType value_ret = FCValueType.TransType(method.ReturnType); string szClassName = value_type.GetValueName(false); fileBuilder.AppendFormat("{0}public delegate {1} {2}({3});\r\n", szLeft, value_ret.GetValueName(false), szClassName, szParamDesc); }
public void ExportDefaultClass(string szPath) { m_szFileBuilder.Length = 0; m_szFileBuilder.AppendLine(); foreach (Type nType in m_AllRefType.Keys) { if (m_AllExportType.ContainsKey(nType)) { continue; } if (nType.IsEnum) { PushInnerType(m_szFileBuilder, string.Empty, nType); continue; } if (nType.IsArray) { continue; } if (!nType.IsClass) { continue; } FCValueType value = FCValueType.TransType(nType); if (value.m_nTemplateType != fc_value_tempalte_type.template_none) { continue; } if (value.m_nValueType == fc_value_type.fc_value_delegate) { continue; } if (FCValueType.IsBaseType(value.m_nValueType)) { continue; } if (nType == typeof(Type)) { m_szFileBuilder.AppendLine("class Type{}"); } else if (nType == typeof(System.Object)) { m_szFileBuilder.AppendFormat("class {0}{{}}\r\n", nType.Name); } else if (nType == typeof(UnityEngine.Object)) { m_szFileBuilder.AppendLine("class UnityObject{}"); } else { m_szFileBuilder.AppendFormat("class {0} : {1}{{}}\r\n", nType.Name, nType.BaseType.Name); } m_szFileBuilder.AppendLine(); } string szPathName = szPath + "all_default_class.cs"; File.WriteAllText(szPathName, m_szFileBuilder.ToString()); }
string GetParentInitCall() { Type nBaseType = m_nClassType.BaseType; if (nBaseType == null) { return(string.Empty); } ConstructorInfo[] allConInfos = nBaseType.GetConstructors(); // 得到构造函数信息 if (allConInfos == null) { return(string.Empty); } // 先检测构造参数 int nMinParamCount = 10000; ConstructorInfo pCon = null; foreach (ConstructorInfo cons in allConInfos) { ParameterInfo[] allParams = cons.GetParameters(); int nCurParamCount = allParams != null ? allParams.Length : 0; if (nMinParamCount > nCurParamCount) { nMinParamCount = nCurParamCount; pCon = cons; } } if (nMinParamCount == 0 || pCon == null) { return(string.Empty); } ParameterInfo[] Params = pCon.GetParameters(); string szParamDesc = string.Empty; for (int i = 0; i < Params.Length; ++i) { if (i > 0) { szParamDesc += ','; } FCValueType value = FCValueType.TransType(Params[i].ParameterType); if (value.m_nValueType == fc_value_type.fc_value_string_a) { szParamDesc += "string.Empty"; } else if (value.m_nValueType == fc_value_type.fc_value_system_object) { szParamDesc += "null"; } else { szParamDesc += string.Format("default({0})", value.GetValueName(false)); } } return(string.Format("base({0})", szParamDesc)); }
void PushConstructor(ConstructorInfo cons, string szParentInitCall) { ParameterInfo[] allParams = cons.GetParameters(); // 如果是有参数的,就要考虑要不是导出 if (allParams != null && allParams.Length > 0) { if (m_bPartWrap) { if (!cons.IsDefined(typeof(PartWrapAttribute), false)) { return; } } // 如果该函数有不导出的标记 if (cons.IsDefined(typeof(DontWrapAttribute), false)) { return; } if (cons.IsDefined(typeof(ObsoleteAttribute), false)) { return; } } string szCallParam = string.Empty; Type nParamType; if (allParams != null) { for (int i = 0; i < allParams.Length; ++i) { ParameterInfo param = allParams[i]; nParamType = param.ParameterType; PushNameSpace(nParamType.Namespace); PushDelayType(nParamType); PushDelegateType(nParamType); PushRefType(nParamType); if (i > 0) { szCallParam += ','; } FCValueType value = FCValueType.TransType(nParamType); szCallParam += value.GetTypeName(false); szCallParam = szCallParam + " " + param.Name; } } if (string.IsNullOrEmpty(szParentInitCall)) { m_szTempBuilder.AppendFormat(" public {0}({1}){{}}\r\n", FCValueType.GetClassName(m_nClassType), szCallParam); } else { m_szTempBuilder.AppendFormat(" public {0}({1}):{2}{{}}\r\n", FCValueType.GetClassName(m_nClassType), szCallParam, szParentInitCall); } }
void MakeDeleteCallFunc(Type nClassType) { // 得到委托的参数 MethodInfo method = nClassType.GetMethod("Invoke"); ParameterInfo[] allParams = method.GetParameters(); // 函数参数 FCValueType ret_value = FCValueType.TransType(method.ReturnType); string szCallDesc = string.Empty; int nParamCount = allParams != null ? allParams.Length : 0; for (int i = 0; i < nParamCount; ++i) { FCValueType value_param = FCValueType.TransType(allParams[i].ParameterType); if (i > 0) { szCallDesc += ","; } szCallDesc += string.Format("{0} arg{1}", value_param.GetTypeName(true, true), i); } m_szTempBuilder.AppendFormat(" public {0} CallFunc({1})\r\n", ret_value.GetTypeName(true, true), szCallDesc); m_szTempBuilder.AppendLine(" {"); m_szTempBuilder.AppendLine(" try"); m_szTempBuilder.AppendLine(" {"); string szArg = string.Empty; for (int i = 0; i < nParamCount; ++i) { szArg = string.Format("arg{0}", i); FCValueType value_param = FCValueType.TransType(allParams[i].ParameterType); if (value_param.m_nTemplateType != fc_value_tempalte_type.template_none) { Debug.LogError(nClassType.FullName + "参数不支持模板"); continue; } if (FCValueType.IsRefType(value_param.m_nValueType)) { m_szTempBuilder.AppendFormat(" FCDll.PushCallParam(ref {0});\r\n", szArg); } else { m_szTempBuilder.AppendFormat(" FCDll.PushCallParam({0});\r\n", szArg); } } m_szTempBuilder.AppendLine(" FCLibHelper.fc_call(m_nThisPtr, m_szFuncName);"); m_szTempBuilder.AppendLine(" }"); m_szTempBuilder.AppendLine(" catch(Exception e)"); m_szTempBuilder.AppendLine(" {"); m_szTempBuilder.AppendLine(" Debug.LogException(e);"); m_szTempBuilder.AppendLine(" }"); m_szTempBuilder.AppendLine(" }"); }
public static bool IsDontExportMethod(MethodInfo method) { // 先检查函数的返回值 FCValueType ret_value = FCValueType.TransType(method.ReturnType); // 目前先暂不支持IEnumerable, 转换这个有点麻烦 if (ret_value.m_nTemplateType == fc_value_tempalte_type.template_ienumerable) { return(true); } return(false); }
// 定制UnityEvent<T>模板函数的导出 void PushUnityEventTemplateFunc(Type nClassType) { Type nBaseType = nClassType.BaseType; Type[] argTypes = nBaseType.GetGenericArguments();// GenericTypeArguments; if (argTypes == null || argTypes.Length == 0) { return; } Type nParamType = argTypes[0]; FCValueType nParamValue = FCValueType.TransType(nParamType); string szParamName = nParamValue.GetValueName(false); m_szTempBuilder.AppendFormat(" public void AddListener(UnityAction<{0}> call){{}}\r\n", szParamName); m_szTempBuilder.AppendFormat(" public void Invoke({0} arg0){{}}\r\n", szParamName); m_szTempBuilder.AppendFormat(" public void RemoveListener(UnityAction<{0}> call){{}}\r\n", szParamName); }
public static bool IsDontExportType(Type nType) { FCValueType ret_value = FCValueType.TransType(nType); // 目前先暂不支持IEnumerable, 转换这个有点麻烦 if (ret_value.m_nTemplateType == fc_value_tempalte_type.template_ienumerable) { return(true); } // 返回值是自定义模板的,也不支持,自动转换的代码太复杂,先不写了 if (ret_value.m_bCustomTemplate) { return(true); } return(false); }
void PushRefType(Type nType) { FCValueType value = FCValueType.TransType(nType); if (value.IsArray || value.IsList) { m_AllRefType[value.m_value] = 1; } else if (value.IsMap) { m_AllRefType[value.m_key] = 1; m_AllRefType[value.m_value] = 1; } else { m_AllRefType[nType] = 1; } }
static void TestExport() { Type t1 = typeof(IEnumerable <AssetBundle>); FCValueType v1 = FCValueType.TransType(t1); string s1 = v1.GetValueName(true); int iiii = 0; FCClassWrap pWrap = new FCClassWrap(); pWrap.BeginExport(""); pWrap.BeginModleWrap("AutoClass"); pWrap.WrapClass(typeof(UnityEngine.AssetBundle)); pWrap.EndModleWrap(); pWrap.EndExport(); MakeFCProj(); }
string GetDelegateWrapName(Type nType) { // 第一个,按 string szName = nType.Name; szName = szName.Replace("`", ""); switch (szName) { case "Action": case "UnityAction": case "Func": case "Comparer": break; default: szName = "Custom"; break; } MethodInfo method = nType.GetMethod("Invoke"); ParameterInfo[] allParams = method.GetParameters(); // 函数参数 int nParamCount = allParams != null ? allParams.Length : 0; for (int i = 0; i < nParamCount; ++i) { FCValueType value_param = FCValueType.TransType(allParams[i].ParameterType); szName += "_"; szName += value_param.GetTypeName(true, true); } FCValueType ret_value = FCValueType.TransType(method.ReturnType); if (ret_value.m_nValueType != fc_value_type.fc_value_void) { string szRetName = ret_value.GetValueName(true, true); szName = szName + "__" + szRetName; } return(szName); }
void PushRefType(Type nType) { try { FCValueType value = FCValueType.TransType(nType); if (value.IsArray || value.IsList) { m_AllRefType[value.m_value] = 1; } else if (value.IsMap) { m_AllRefType[value.m_key] = 1; m_AllRefType[value.m_value] = 1; } else { m_AllRefType[nType] = 1; } } catch (Exception e) { Debug.LogException(e); } }
// 功能:添加函数调用的方法 void PushMethodInfo(MethodInfo method) { if (0 != (MethodAttributes.SpecialName & method.Attributes)) { return; } if (m_bPartWrap) { if (!method.IsDefined(typeof(PartWrapAttribute), false)) { return; } } // 如果该函数有不导出的标记 if (method.IsDefined(typeof(DontWrapAttribute), false)) { return; } if (method.IsDefined(typeof(ObsoleteAttribute), false)) { return; } // 模板函数不导出了吧 bool bTemplateFunc = false; if (IsTemplateFunc(method)) { if (!m_CurSupportTemplateFunc.ContainsKey(method.Name)) { return; } bTemplateFunc = true; } bool bStatic = method.IsStatic; string szStatic = string.Empty; if (bStatic) { szStatic = "static "; } ParameterInfo[] allParams = method.GetParameters(); // 函数参数 string szCallParam = string.Empty; string szBody = string.Empty; if (allParams != null) { Type nParamType; for (int i = 0; i < allParams.Length; ++i) { ParameterInfo param = allParams[i]; nParamType = param.ParameterType; if (i > 0) { szCallParam += ','; } PushNameSpace(nParamType.Namespace); PushDelegateType(nParamType); PushRefType(nParamType); FCValueType value = FCValueType.TransType(nParamType); if (param.IsOut) { szCallParam += "out "; szBody += string.Format("{0}=default({1});", allParams[i].Name, value.GetTypeName(false)); } else if (nParamType.IsByRef) { szCallParam += "ref "; } szCallParam += value.GetTypeName(false); szCallParam += " "; szCallParam += allParams[i].Name; } } PushNameSpace(method.ReturnType.Namespace); PushDelegateType(method.ReturnType); PushRefType(method.ReturnType); FCValueType ret_value = FCValueType.TransType(method.ReturnType); if (ret_value.m_nTemplateType != fc_value_tempalte_type.template_none) { m_szTempBuilder.AppendFormat(" public {0}{1} {2}({3}){{ {4}return null; }}\r\n", szStatic, ret_value.GetTypeName(false), GetMeshName(method, bTemplateFunc), szCallParam, szBody); } else if (ret_value.m_nValueType == fc_value_type.fc_value_void) { m_szTempBuilder.AppendFormat(" public {0}{1} {2}({3}){{{4}}}\r\n", szStatic, ret_value.GetTypeName(false), GetMeshName(method, bTemplateFunc), szCallParam, szBody); } else { string szRetCShaprName = ret_value.GetTypeName(false); m_szTempBuilder.AppendFormat(" public {0}{1} {2}({3}){{ {4}return default({5}); }}\r\n", szStatic, ret_value.GetTypeName(false), GetMeshName(method, bTemplateFunc), szCallParam, szBody, szRetCShaprName); } }
string GetParentInitCall(ConstructorInfo consCall) { Type nBaseType = m_nClassType.BaseType; if (nBaseType == null) { return(string.Empty); } ConstructorInfo[] allConInfos = nBaseType.GetConstructors(); // 得到构造函数信息 if (allConInfos == null) { return(string.Empty); } ParameterInfo[] InParams = consCall.GetParameters(); if (InParams == null || InParams.Length == 0) { return(string.Empty); } // 先检测构造参数 ConstructorInfo pFindCon = null; foreach (ConstructorInfo cons in allConInfos) { ParameterInfo[] allParams = cons.GetParameters(); int nCurParamCount = allParams != null ? allParams.Length : 0; if (nCurParamCount == 0) { return(string.Empty); } if (nCurParamCount == InParams.Length) { // 比较一个参数 if (IsSameParam(InParams, allParams)) { pFindCon = cons; break; } } } if (pFindCon == null) { return(string.Empty); } ParameterInfo[] Params = pFindCon.GetParameters(); string szParamDesc = string.Empty; for (int i = 0; i < Params.Length; ++i) { if (i > 0) { szParamDesc += ','; } FCValueType value = FCValueType.TransType(Params[i].ParameterType); if (value.IsArray || value.IsList || value.IsMap || value.m_nValueType == fc_value_type.fc_value_system_object) { szParamDesc += "null"; } else if (value.m_nValueType == fc_value_type.fc_value_string_a) { szParamDesc += "string.Empty"; } else { szParamDesc += string.Format("default({0})", value.GetValueName(false)); } } return(string.Format("base({0})", szParamDesc)); }
// 功能:添加函数调用的方法 void PushMethodInfo(MethodInfo method) { if (0 != (MethodAttributes.SpecialName & method.Attributes)) { return; } if (m_bPartWrap) { if (!method.IsDefined(typeof(PartWrapAttribute), false)) { return; } } // 如果该函数有不导出的标记 if (method.IsDefined(typeof(DontWrapAttribute), false)) { return; } if (method.IsDefined(typeof(ObsoleteAttribute), false)) { return; } // 模板函数不导出了吧 if (IsTemplateFunc(method)) { return; } bool bStatic = method.IsStatic; string szStatic = string.Empty; if (bStatic) { szStatic = "static "; } ParameterInfo[] allParams = method.GetParameters(); // 函数参数 string szCallParam = string.Empty; if (allParams != null) { Type nParamType; string szParamType = string.Empty; for (int i = 0; i < allParams.Length; ++i) { nParamType = allParams[i].ParameterType; if (i > 0) { szCallParam += ','; } PushNameSpace(nParamType.Namespace); PushRefType(nParamType); FCValueType value = FCValueType.TransType(nParamType); szCallParam += value.GetTypeName(false); szCallParam += " "; szCallParam += allParams[i].Name; } } PushNameSpace(method.ReturnType.Namespace); PushRefType(method.ReturnType); FCValueType ret_value = FCValueType.TransType(method.ReturnType); if (ret_value.m_nTemplateType != fc_value_tempalte_type.template_none) { m_szTempBuilder.AppendFormat(" public {0}{1} {2}({3}){{ return null; }}\r\n", szStatic, ret_value.GetTypeName(false), method.Name, szCallParam); } else if (ret_value.m_nValueType == fc_value_type.fc_value_void) { m_szTempBuilder.AppendFormat(" public {0}{1} {2}({3}){{}}\r\n", szStatic, ret_value.GetTypeName(false), method.Name, szCallParam); } else { string szRetCShaprName = ret_value.GetTypeName(true); m_szTempBuilder.AppendFormat(" public {0}{1} {2}({3}){{ return default({4}); }}\r\n", szStatic, ret_value.GetTypeName(false), method.Name, szCallParam, szRetCShaprName); } }
public void ExportDefaultClass(string szPath) { m_szFileBuilder.Length = 0; m_szFileBuilder.AppendLine(); m_szFileBuilder.AppendLine("using System;"); foreach (Type nType in m_AllRefType.Keys) { if (m_AllExportType.ContainsKey(nType)) { continue; } if (nType.IsEnum) { PushInnerType(m_szFileBuilder, string.Empty, nType); continue; } if (nType.IsArray) { continue; } if (!nType.IsClass) { //if (!nType.IsValueType && !nType.IsInterface) // 如果不是结构体, 也是接口类 // continue; } FCValueType value = FCValueType.TransType(nType); if (value.m_nTemplateType != fc_value_tempalte_type.template_none) { continue; } if (value.m_nValueType == fc_value_type.fc_value_delegate) { continue; } if (FCValueType.IsBaseType(value.m_nValueType)) { continue; } // 如果是内部的类,不需要再导出了 if (FCExclude.IsDontExportClass(nType)) { continue; } //if (nType == typeof(IntPtr)) // continue; //if (nType == typeof(IEnumerator)) // continue; if (nType.Name == "T") { continue; } if (nType.Name.IndexOf("UnityEvent`") != -1) { continue; } if (nType.Name.IndexOf('&') != -1) { continue; } if (nType.Name.IndexOf('`') != -1) { continue; } if (nType == typeof(Type)) { m_szFileBuilder.AppendLine("class Type{}"); } else if (nType == typeof(System.Object)) { //m_szFileBuilder.AppendFormat("class {0}{{}}\r\n", nType.Name); continue; } else if (nType == typeof(UnityEngine.Object)) { m_szFileBuilder.AppendLine("class UnityObject{}"); } else { Type nParentType = nType.BaseType; if (nParentType != null && m_AllRefType.ContainsKey(nParentType)) { m_szFileBuilder.AppendFormat("class {0}:{1}{{}}\r\n", nType.Name, nParentType.Name); } else { m_szFileBuilder.AppendFormat("class {0}{{}}\r\n", nType.Name); } } m_szFileBuilder.AppendLine(); } string szPathName = szPath + "all_default_class.cs"; File.WriteAllText(szPathName, m_szFileBuilder.ToString()); }