コード例 #1
0
        /// <summary>
        /// Enables the detour of a method(WARNING: The method needs to have been detoured atleast once!)
        /// </summary>
        /// <param name="method">The original method that was detoured</param>
        /// <returns>If the detour was enabled successfully</returns>
        public static bool EnableDetour(MethodInfo method)
        {
            // Set the variables
            DetourWrapper wrapper = DM.Detours.First(a => a.Value.Original == method).Value;

            // Do the checks
            if (wrapper == null)
            {
                return(false);
            }

            return(wrapper.Detour());
        }
コード例 #2
0
        /// <summary>
        /// Calls the original method that was detoured
        /// </summary>
        /// <param name="method">The original method</param>
        /// <param name="instance">The instance for the method(null if static)</param>
        /// <param name="args">The arguments for the method</param>
        /// <returns>The value that the original function returns</returns>
        public static object CallOriginal(MethodInfo method, object instance = null, params object[] args)
        {
            // Set the variables
            DetourWrapper wrapper = DM.Detours.First(a => a.Value.Original == method).Value;

            // Do the checks
            if (wrapper == null)
            {
                throw new Exception("The detour specified was not found!");
            }

            return(wrapper.CallOriginal(args, instance));
        }
コード例 #3
0
        /// <summary>
        /// Calls the original method that was detoured
        /// </summary>
        /// <param name="instance">The instance for the method(null if static)</param>
        /// <param name="args">The arguments for the method</param>
        /// <returns>The value tahat the original function returns</returns>
        public static object CallOriginal(object instance = null, params object[] args)
        {
            StackTrace trace = new StackTrace(false);

            if (trace.FrameCount < 1)
            {
                throw new Exception("Invalid trace back to the original method! Please provide the methodinfo instead!");
            }

            MethodBase modded   = trace.GetFrame(1).GetMethod();
            MethodInfo original = null;

            if (!Attribute.IsDefined(modded, typeof(DetourAttribute)))
            {
                modded = trace.GetFrame(2).GetMethod();
            }
            DetourAttribute att = (DetourAttribute)Attribute.GetCustomAttribute(modded, typeof(DetourAttribute));

            if (att == null)
            {
                throw new Exception("This method can only be called from an overwritten method!");
            }
            if (!att.MethodFound)
            {
                throw new Exception("The original method was never found!");
            }
            original = att.Method;

            DetourWrapper wrapper = DM.Detours.First(a => a.Value.Original == original).Value;

            if (wrapper == null)
            {
                throw new Exception("The detour specified was not found!");
            }

            return(wrapper.CallOriginal(args, instance));
        }