public void LoadDetour(MethodInfo method) { // Setup variables DetourAttribute attribute = (DetourAttribute)Attribute.GetCustomAttribute(method, typeof(DetourAttribute)); // Do checks if (attribute == null) { return; } if (!attribute.MethodFound) { return; } if (Detours.Count(a => a.Key.Method == attribute.Method) > 0) { return; } try { DetourWrapper wrapper = new DetourWrapper(attribute.Method, method, attribute); wrapper.Detour(); Detours.Add(attribute, wrapper); } catch (Exception ex) { PointBlankLogging.LogError("Error detouring: " + method.Name, ex); } }
public DetourWrapper(MethodInfo original, MethodInfo modified, DetourAttribute attribute, object instance = null) { // Set the variables Original = original; Modified = modified; Instance = instance; Attribute = attribute; Local = (Modified.DeclaringType.Assembly == Assembly.GetExecutingAssembly()); ptrOriginal = Original.MethodHandle.GetFunctionPointer(); ptrModified = Modified.MethodHandle.GetFunctionPointer(); OffsetBackup = new RedirectionHelper.OffsetBackup(ptrOriginal); Detoured = false; }
public override void Unload() { if (!Initialized) { return; } // Main code while (Detours.Count > 0) { DetourAttribute att = Detours.Keys.ElementAt(0); Detours[att].Revert(); Detours.Remove(att); } // Set the variables Initialized = false; }
public override bool Inject() { Type[] types = Cthulhu_SpecialInjector.Assembly.GetTypes(); bool result; for (int i = 0; i < types.Length; i++) { Type type = types[i]; BindingFlags[] array = Cthulhu_SpecialInjector.bindingFlagCombos; for (int j = 0; j < array.Length; j++) { BindingFlags bindingFlags = array[j]; MethodInfo[] methods = type.GetMethods(bindingFlags); for (int k = 0; k < methods.Length; k++) { MethodInfo methodInfo = methods[k]; object[] customAttributes = methodInfo.GetCustomAttributes(typeof(DetourAttribute), true); for (int l = 0; l < customAttributes.Length; l++) { DetourAttribute detourAttribute = (DetourAttribute)customAttributes[l]; BindingFlags bindingFlags2 = (detourAttribute.bindingFlags != BindingFlags.Default) ? detourAttribute.bindingFlags : bindingFlags; MethodInfo method = detourAttribute.source.GetMethod(methodInfo.Name, bindingFlags2); bool flag = method == null; if (flag) { Log.Error(string.Format("Cthulhu :: Detours :: Can't find source method '{0} with bindingflags {1}", methodInfo.Name, bindingFlags2)); result = false; return(result); } bool flag2 = !Detours.TryDetourFromTo(method, methodInfo); if (flag2) { result = false; return(result); } } } } } result = true; return(result); }
private void Start() { foreach (Type _class in Assembly.GetExecutingAssembly().GetTypes()) { if (!_class.IsClass) { continue; } // Service and menu checks if (MenuManager.IsMenu(_class)) { MenuManager.LoadClass(_class); } else if (ServiceManager.IsService(_class)) { IService service = ServiceManager.LoadClass(_class); ServiceManager.Start(service); } // Detour checks foreach (MethodInfo method in _class.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { DetourAttribute detour = (DetourAttribute)Attribute.GetCustomAttribute(method, typeof(DetourAttribute)); if (detour != null) { if (detour.Modified == null) { detour.Modified = method; } DetourManager.Detour(detour.Original, detour.Modified); } } } _ticker = new Thread(new ThreadStart(Tick)); _ticker.Start(); }
public override void Unload() { if (!Initialized) { return; } // Unload the events PointBlankPluginEvents.OnPluginLoaded -= OnPluginLoaded; PointBlankPluginEvents.OnPluginUnloaded -= OnPluginUnloaded; // Main code while (Detours.Count > 0) { DetourAttribute att = Detours.Keys.ElementAt(0); Detours[att].Revert(); Detours.Remove(att); } // Set the variables Initialized = false; }