private static void Install(Assembly[] assemblies, IEnumerable <IMethodHook> monitors) { foreach (var monitor in monitors) { var all = monitor.GetType().GetMethods(AllFlag); var hookMethods = all.Where(t => t.GetCustomAttributesData().Any(a => typeof(HookMethodAttribute).IsAssignableFrom(a.Constructor.DeclaringType))); var originalMethods = all.Where(t => t.GetCustomAttributesData().Any(a => typeof(OriginalMethodAttribute).IsAssignableFrom(a.Constructor.DeclaringType))).ToArray(); var destCount = hookMethods.Count(); foreach (var hookMethod in hookMethods) { DestAndOri destAndOri = new DestAndOri(); destAndOri.Obj = monitor; destAndOri.HookMethod = hookMethod; if (destCount == 1) { destAndOri.OriginalMethod = originalMethods.FirstOrDefault(); } else { var originalMethodName = hookMethod.GetCustomAttribute <HookMethodAttribute>().GetOriginalMethodName(hookMethod); destAndOri.OriginalMethod = FindMethod(originalMethods, originalMethodName, hookMethod, assemblies); } destAndOris.Add(destAndOri); } } InstallInternal(true, assemblies); AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad; }
/// <summary> /// 安装监视器 /// </summary> public static void Install(string dir = null) { if (installed) { return; } installed = true; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); IEnumerable <IMethodHook> monitors; if (string.IsNullOrEmpty(dir)) { monitors = assemblies.SelectMany(t => t.GetImplementedObjectsByInterface <IMethodHook>()); } else { assemblies = assemblies.Concat(Directory .GetFiles(dir, "*.dll") .Select(d => { try { return(Assembly.LoadFrom(d)); } catch { return(null); } }) .Where(x => x != null)) .Distinct() .ToArray(); monitors = assemblies .SelectMany(d => d.GetImplementedObjectsByInterface <IMethodHook>()); } foreach (var monitor in monitors) { var all = monitor.GetType().GetMethods(AllFlag); var hookMethods = all.Where(t => t.CustomAttributes.Any(a => typeof(HookMethodAttribute).IsAssignableFrom(a.AttributeType))); var originalMethods = all.Where(t => t.CustomAttributes.Any(a => typeof(OriginalMethodAttribute).IsAssignableFrom(a.AttributeType))).ToArray(); var destCount = hookMethods.Count(); foreach (var hookMethod in hookMethods) { DestAndOri destAndOri = new DestAndOri(); destAndOri.Obj = monitor; destAndOri.HookMethod = hookMethod; if (destCount == 1) { destAndOri.OriginalMethod = originalMethods.FirstOrDefault(); } else { var originalMethodName = hookMethod.GetCustomAttribute <HookMethodAttribute>().GetOriginalMethodName(hookMethod); destAndOri.OriginalMethod = FindMethod(originalMethods, originalMethodName, hookMethod, assemblies); } destAndOris.Add(destAndOri); } } InstallInternal(true, assemblies); AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad; }
/// <summary> /// 安装监视器 /// </summary> public static void Install(string dir = null) { if (installed) { return; } installed = true; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); IEnumerable <IMethodMonitor> monitors; if (string.IsNullOrEmpty(dir)) { monitors = assemblies.SelectMany(t => t.GetImplementedObjectsByInterface <IMethodMonitor>()); } else { monitors = Directory .GetFiles(dir) .SelectMany(d => Assembly.LoadFrom(d).GetImplementedObjectsByInterface <IMethodMonitor>()); } foreach (var monitor in monitors) { DestAndOri destAndOri = new DestAndOri(); destAndOri.Obj = monitor; destAndOri.Dest = monitor .GetType() .GetMethods(AllFlag) .FirstOrDefault(t => t.CustomAttributes.Any(a => a.AttributeType == typeof(MonitorAttribute))); destAndOri.Ori = monitor .GetType() .GetMethods(AllFlag) .FirstOrDefault(t => t.CustomAttributes.Any(a => a.AttributeType == typeof(OriginalAttribute))); if (destAndOri.Dest != null) { destAndOris.Add(destAndOri); } } InstallInternal(assemblies); AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad; }
public static void InstallEx(params Assembly[] assemblies) { lock (Monitor.Mutex) { List <DestAndOri> destAndOris = new List <DestAndOri>(); List <IMethodMonitor> monitors = new List <IMethodMonitor>(); foreach (Assembly a in assemblies) { monitors.AddRange(a.GetImplementedObjectsByInterface <IMethodMonitor>()); } foreach (IMethodMonitor monitor in monitors) { DestAndOri destAndOri = new DestAndOri(); destAndOri.Dest = monitor.GetType().GetMethods().FirstOrDefault((MethodInfo t) => t.GetCustomAttributes(typeof(MonitorAttribute), false).Length > 0); destAndOri.Ori = monitor.GetType().GetMethods().FirstOrDefault((MethodInfo t) => t.GetCustomAttributes(typeof(OriginalAttribute), false).Length > 0); if (!(destAndOri.Dest == null) && !(destAndOri.Ori == null)) { MethodInfo src = null; MethodInfo dest = destAndOri.Dest; MonitorAttribute monitorAttribute = dest.GetCustomAttributes(typeof(MonitorAttribute), false).FirstOrDefault <object>() as MonitorAttribute; string methodName = dest.Name; Type[] paramTypes = (from t in dest.GetParameters() select t.ParameterType).ToArray <Type>(); if (monitorAttribute.Type != null) { src = monitorAttribute.Type.GetMethod(methodName, paramTypes); } else { string srcNamespaceAndClass = monitorAttribute.NamespaceName + "." + monitorAttribute.ClassName; foreach (Assembly asm in assemblies) { Type type = asm.GetType(srcNamespaceAndClass); if (type == null) { type = asm.GetExportedTypes().FirstOrDefault((Type t) => t.FullName == srcNamespaceAndClass); } if (type != null) { src = type.GetMethod(methodName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, paramTypes, null); break; } } } if (!(src == null)) { destAndOri.Src = src; if (destAndOri.Dest != null) { List <DestAndOri> list; if (!Monitor.destAndOrisDict.TryGetValue(destAndOri.Src, out list)) { list = new List <DestAndOri>(); Monitor.destAndOrisDict[destAndOri.Src] = list; } if (!list.Contains(destAndOri)) { if (list.Count > 0) { destAndOri.Src = list.LastOrDefault <DestAndOri>().Dest; } list.Add(destAndOri); destAndOris.Add(destAndOri); } } } } } Monitor.InstallInternalEx(destAndOris); } }