Exemple #1
0
 private static void InstallInternal(Assembly[] assemblies)
 {
     foreach (var destAndOri in destAndOris)
     {
         MethodInfo src              = null;
         var        dest             = destAndOri.Dest;
         var        monitorAttribute = dest.GetCustomAttribute(typeof(MonitorAttribute)) as MonitorAttribute;
         var        methodName       = dest.Name;
         var        paramTypes       = dest.GetParameters().Select(t => t.ParameterType).ToArray();
         if (monitorAttribute.Type != null)
         {
             src = monitorAttribute.Type.GetMethod(methodName, paramTypes);
         }
         else
         {
             var srcNamespaceAndClass = monitorAttribute.NamespaceName + "." + monitorAttribute.ClassName;
             foreach (var asm in assemblies)
             {
                 var type = asm.GetExportedTypes().FirstOrDefault(t => t.FullName == srcNamespaceAndClass);
                 if (type != null)
                 {
                     src = type.GetMethod(methodName, paramTypes);
                     break;
                 }
             }
         }
         if (src == null)
         {
             continue;
         }
         var ori    = destAndOri.Ori;
         var engine = DetourFactory.CreateDetourEngine();
         engine.Patch(src, dest, ori);
     }
 }
Exemple #2
0
 private static void InstallInternalEx(List <DestAndOri> destAndOris)
 {
     foreach (DestAndOri destAndOri in destAndOris)
     {
         MethodInfo src    = destAndOri.Src;
         MethodInfo dest   = destAndOri.Dest;
         MethodInfo ori    = destAndOri.Ori;
         IDetour    engine = DetourFactory.CreateDetourEngine();
         engine.Patch(src, dest, ori);
     }
 }
Exemple #3
0
        private static void InstallInternal(bool isInstall, Assembly[] assemblies)
        {
            foreach (var detour in destAndOris)
            {
                var hookMethod          = detour.HookMethod;
                var hookMethodAttribute = hookMethod.GetCustomAttribute <HookMethodAttribute>();

                //获取当前程序集中的基础类型
                var typeName = hookMethodAttribute.TargetTypeFullName;
                if (hookMethodAttribute.TargetType != null)
                {
                    typeName = hookMethodAttribute.TargetType.FullName;
                }
                var type = TypeResolver(typeName, assemblies);
                if (type != null && !assemblies.Contains(type.Assembly))
                {
                    type = null;
                }

                //获取方法
                var        methodName = hookMethodAttribute.GetTargetMethodName(hookMethod);
                MethodBase rawMethod  = null;
                if (type != null)
                {
                    MethodBase[] methods;

                    if (methodName == type.Name || methodName == ".ctor")
                    {//构造方法
                        methods    = type.GetConstructors(AllFlag);
                        methodName = ".ctor";
                    }
                    else
                    {
                        methods = type.GetMethods(AllFlag);
                    }

                    rawMethod = FindMethod(methods, methodName, hookMethod, assemblies);
                }
                if (rawMethod != null && rawMethod.IsGenericMethod)
                {
                    //泛型方法转成实际方法
                    rawMethod = ((MethodInfo)rawMethod).MakeGenericMethod(hookMethod.GetParameters().Select(o =>
                    {
                        var rt   = o.ParameterType;
                        var attr = o.GetCustomAttribute <RememberTypeAttribute>();
                        if (attr != null && attr.TypeFullNameOrNull != null)
                        {
                            rt = TypeResolver(attr.TypeFullNameOrNull, assemblies);
                        }
                        return(rt);
                    }).ToArray());
                }

                if (rawMethod == null)
                {
                    if (isInstall)
                    {
                        Debug.WriteLine("没有找到与试图Hook的方法\"{0}, {1}\"匹配的目标方法.", new object[] { hookMethod.ReflectedType.FullName, hookMethod });
                    }
                    continue;
                }
                if (detour.Obj is IMethodHookWithSet)
                {
                    ((IMethodHookWithSet)detour.Obj).HookMethod(rawMethod);
                }

                var originalMethod = detour.OriginalMethod;
                var engine         = DetourFactory.CreateDetourEngine();
                engine.Patch(rawMethod, hookMethod, originalMethod);

                Debug.WriteLine("已将目标方法 \"{0}, {1}\" 的调用指向 \"{2}, {3}\" Ori: \"{4}\".", rawMethod.ReflectedType.FullName, rawMethod
                                , hookMethod.ReflectedType.FullName, hookMethod
                                , originalMethod == null ? " (无)" : originalMethod.ToString());
            }
        }