void MakeMethod() { MethodInfo[] allMethods = FCValueType.GetMethods(m_nClassType, m_bOnlyThisAPI);// m_nClassType.GetMethods(); // 函数+get/set方法 if (allMethods == null) { return; } m_CurMethods.Clear(); m_CurValidMethods.Clear(); string szDeclareName = string.Empty; bool bNeedExport = false; foreach (MethodInfo method in allMethods) { if (!IsNeedExportMember(method.Name)) { continue; } if (m_CurDontWrapName.ContainsKey(method.Name)) { continue; } if (FCExclude.IsDontExportMethod(method)) { continue; } bNeedExport = true; // 去掉参数都一样的,因为FC脚本中 []与List是一个数据类型 szDeclareName = FCValueType.GetMethodDeclare(method, ref bNeedExport); if (!bNeedExport) { continue; } if (m_CurValidMethods.ContainsKey(szDeclareName)) { // 必要的话,这里做个替换 FCValueType.ReplaceMethod(m_CurValidMethods, m_CurMethods, szDeclareName, method); continue; } m_CurValidMethods[szDeclareName] = method; m_CurMethods.Add(method); } foreach (MethodInfo method in m_CurMethods) { PushMethodInfo(method); } // 特殊导出UnityEvent<T>模板类 Type nBaseType = m_nClassType.BaseType; if (nBaseType != null && nBaseType.Name == "UnityEvent`1") { PushUnityEventTemplateFunc(m_nClassType); } }
static void ExportAll() { FCClassWrap pWrap = new FCClassWrap(); pWrap.BeginExport(""); Dictionary <string, List <Type> > allExportTypes = FCExclude.GetAllExportType(); foreach (var v in allExportTypes) { WrapAllClass(pWrap, v.Key, v.Value); } WrapCustomAttribClass(pWrap); // 导出打有[ClassAutoWrap]标签的类 pWrap.EndExport(); MakeFCProj(); }
// 功能:添加公有变量 void PushFieldInfo(FieldInfo value) { if (m_bPartWrap) { if (!value.IsDefined(typeof(PartWrapAttribute), false)) { return; } } // 如果该变量有不导出的标记 if (value.IsDefined(typeof(DontWrapAttribute), false)) { return; } if (value.IsDefined(typeof(ObsoleteAttribute), false)) { return; } if (m_CurDontWrapName.ContainsKey(value.Name)) { return; } if (FCExclude.IsDontExportFieldInfo(value)) { return; } PushNameSpace(value.FieldType.Namespace); PushDelegateType(value.FieldType); PushRefType(value.FieldType); bool bCanWrite = !(value.IsInitOnly || value.IsLiteral); // 生成get_value, set_value方法 FCValueType ret_value = FCTemplateWrap.Instance.PushGetTypeWrap(value.FieldType); if (ret_value.m_nTemplateType == fc_value_tempalte_type.template_none && ret_value.m_nValueType == fc_value_type.fc_value_delegate) { PushPropertyFunc(value.FieldType, value.Name, false, bCanWrite, value.IsStatic); } else { PushPropertyFunc(value.FieldType, value.Name, true, bCanWrite, value.IsStatic); } }
static void PrepareWrap(FCClassWrap pWrap, Type nClassType) { // 添加黑名单函数 List <string> rList = FCExclude.GetClassBlackList(nClassType); if (rList != null) { foreach (string funcName in rList) { pWrap.PushCurrentDontWrapName(funcName); } } // 目前只有两个类型支持模板函数,其他的需要用户自己扩展 if (nClassType == typeof(UnityEngine.Component) || nClassType == typeof(UnityEngine.GameObject)) { List <Type> aSupportType = FCExclude.SupportTemplateTypes; pWrap.PushTemplateFuncWrapSupport("AddComponent", aSupportType); pWrap.PushTemplateFuncWrapSupport("GetComponent", aSupportType); } }
// 功能:添加get-set方法 void PushPropertyInfo(PropertyInfo property) { if (m_bPartWrap) { if (!property.IsDefined(typeof(PartWrapAttribute), false)) { return; } } // 如果该变量有不导出的标记 if (property.IsDefined(typeof(DontWrapAttribute), false)) { return; } if (property.IsDefined(typeof(ObsoleteAttribute), false)) { return; } if (m_CurDontWrapName.ContainsKey(property.Name)) { return; } if (FCExclude.IsDontExportPropertyInfo(property)) { return; } //if (property.IsDefined(typeof(DefaultMemberAttribute), false)) //{ // return; //} PushNameSpace(property.PropertyType.Namespace); PushDelegateType(property.PropertyType); PushRefType(property.PropertyType); Type nVaueType = property.PropertyType; MethodInfo metGet = property.GetGetMethod(); MethodInfo metSet = property.GetSetMethod(); bool bStatic = false; bool bCanRead = false; bool bCanWrite = false; try { if (property.CanRead) { bCanRead = metGet != null; if (metGet != null) { bStatic = metGet.IsStatic; } } if (property.CanWrite) { bCanWrite = metSet != null; if (metSet != null) { bStatic = metSet.IsStatic; } if (bCanWrite) { if (FCExclude.IsDissablePropertySetMethod(m_nClassType, property.Name)) { bCanWrite = false; } } } } catch (Exception e) { Debug.LogException(e); } PushPropertyFunc(nVaueType, property.Name, bCanRead, bCanWrite, bStatic); }
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()); }
// 功能:得到所有要导出的类 public static Dictionary <string, List <Type> > GetAllExportType() { Dictionary <string, List <Type> > allExportType = new Dictionary <string, List <Type> >(); Assembly[] all_assemb = AppDomain.CurrentDomain.GetAssemblies(); string szFullName = string.Empty; foreach (Assembly ab in all_assemb) { if (ab.ManifestModule is System.Reflection.Emit.ModuleBuilder) { continue; } szFullName = ab.FullName; foreach (Type t in ab.GetExportedTypes()) { if (t.IsEnum) { continue; } if (t.IsArray) { continue; } if (t.IsInterface) { continue; } if (t.IsValueType) { // 如果是必须要导出的类 if (IsMustExportStruct(t)) { PushExportType(allExportType, t); } continue; } if (!t.IsClass) { // 如果是必须要导出的类 if (IsMustExportStruct(t)) { PushExportType(allExportType, t); } continue; } if (t.IsNested) // 如果是内嵌类 { continue; } if (t.IsDefined(typeof(ObsoleteAttribute), false)) { continue; } if (!FCExclude.IsNeedExportNamespace(t.Namespace)) { continue; } // 如果是排除的类型 if (FCExclude.IsExclude(t)) { continue; } if (t.BaseType == typeof(MulticastDelegate)) { continue; } if (string.IsNullOrEmpty(t.Namespace)) { continue; } PushExportType(allExportType, t); } } PushExportType(allExportType, typeof(UnityEngine.Events.UnityEvent)); return(allExportType); }