Example #1
0
    public void LoadOverride(MethodInfo method)
    {
        OverrideAttribute attribute = (OverrideAttribute)Attribute.GetCustomAttribute(method, typeof(OverrideAttribute));
        bool flag = Overrides.Count((KeyValuePair <OverrideAttribute, OverrideWrapper> a) => a.Key.Method == attribute.Method) > 0;

        if (!flag)
        {
            OverrideWrapper overrideWrapper = new OverrideWrapper(attribute.Method, method, attribute, null);
            overrideWrapper.Override();
            Overrides.Add(attribute, overrideWrapper);
        }
    }
 public OverrideWrapper(MethodInfo original, MethodInfo modified, OverrideAttribute attribute, object instance = null)
 {
     Original  = original;
     Modified  = modified;
     Instance  = instance;
     Attribute = attribute;
     Local     = (Modified.DeclaringType.Assembly == Assembly.GetExecutingAssembly());
     RuntimeHelpers.PrepareMethod(original.MethodHandle);
     RuntimeHelpers.PrepareMethod(modified.MethodHandle);
     PtrOriginal  = Original.MethodHandle.GetFunctionPointer();
     PtrModified  = Modified.MethodHandle.GetFunctionPointer();
     OffsetBackup = new OverrideUtilities.OffsetBackup(PtrOriginal);
     Detoured     = false;
 }
Example #3
0
        // Token: 0x06000083 RID: 131 RVA: 0x000064B0 File Offset: 0x000046B0
        public static object CallOriginal(object instance = null, params object[] args)
        {
            StackTrace stackTrace = new StackTrace(false);
            bool       flag       = stackTrace.FrameCount < 1;
            bool       flag2      = flag;

            if (flag2)
            {
                throw new Exception("Invalid trace back to the original method! Please provide the methodinfo instead!");
            }
            MethodBase method   = stackTrace.GetFrame(1).GetMethod();
            MethodInfo original = null;
            bool       flag3    = !Attribute.IsDefined(method, typeof(OverrideAttribute));
            bool       flag4    = flag3;

            if (flag4)
            {
                method = stackTrace.GetFrame(2).GetMethod();
            }
            OverrideAttribute overrideAttribute = (OverrideAttribute)Attribute.GetCustomAttribute(method, typeof(OverrideAttribute));
            bool flag5 = overrideAttribute == null;
            bool flag6 = flag5;

            if (flag6)
            {
                throw new Exception("This method can only be called from an overwritten method!");
            }
            bool flag7 = !overrideAttribute.MethodFound;
            bool flag8 = flag7;

            if (flag8)
            {
                throw new Exception("The original method was never found!");
            }
            original = overrideAttribute.Method;
            bool flag9  = OverrideManager.Overrides.All((KeyValuePair <OverrideAttribute, OverrideWrapper> o) => o.Value.Original != original);
            bool flag10 = flag9;

            if (flag10)
            {
                throw new Exception("The Override specified was not found!");
            }
            OverrideWrapper value = OverrideManager.Overrides.First((KeyValuePair <OverrideAttribute, OverrideWrapper> a) => a.Value.Original == original).Value;

            return(value.CallOriginal(args, instance));
        }
Example #4
0
        /// <summary>
        /// Loads override information for method
        /// </summary>
        /// <param name="method">Method to override another</param>
        public static void LoadOverride(MethodInfo method)
        {
            // Get attribute related variables
            OverrideAttribute attribute =
                (OverrideAttribute)Attribute.GetCustomAttribute(method, typeof(OverrideAttribute));

            // Check if method has been overrided before
            if (Overrides.Count(a => a.Key.Method == attribute.Method) > 0)
            {
                return;
            }

            // Create wrapper for override
            OverrideWrapper wrapper = new OverrideWrapper(attribute.Method, method, attribute);

            // Override
            wrapper.Override();

            // Add override to the list
            Overrides.Add(attribute, wrapper);
        }
Example #5
0
        /// <summary>
        /// Calls the original method that was Overrideed
        /// </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(OverrideAttribute)))
            {
                modded = trace.GetFrame(2).GetMethod();
            }
            OverrideAttribute att = (OverrideAttribute)Attribute.GetCustomAttribute(modded, typeof(OverrideAttribute));

            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;

            if (OverrideManager.Overrides.All(o => o.Value.Original != original))
            {
                throw new Exception("The Override specified was not found!");
            }

            OverrideWrapper wrapper = OverrideManager.Overrides.First(a => a.Value.Original == original).Value;

            return(wrapper.CallOriginal(args, instance));
        }
Example #6
0
        /// <summary>
        /// Read a list of attributes
        /// </summary>
        private void ReadAttributes(ConstantPool cp, IModifiableAttributeProvider provider)
        {
            var count = stream.ReadU2();

            for (var i = 0; i < count; i++)
            {
                var nameIndex = stream.ReadU2();
                var name      = cp.GetEntry <ConstantPoolUtf8>(nameIndex).Value;
                var length    = stream.ReadU4();

                Attribute attr;
                int       tmp;
                switch (name)
                {
                case CodeAttribute.AttributeName:
                    attr = ReadCodeAttribute((MethodDefinition)provider, cp);
                    break;

                case ConstantValueAttribute.AttributeName:
                    tmp  = stream.ReadU2();
                    attr = new ConstantValueAttribute(((IConstantPoolValue)cp[tmp]).Value);
                    break;

                case ExceptionsAttribute.AttributeName:
                    attr = ReadExceptionsAttribute(cp);
                    break;

                case InnerClassesAttribute.AttributeName:
                    attr = ReadInnerClassesAttribute(cp);
                    break;

                case SyntheticAttribute.AttributeName:
                    attr = new SyntheticAttribute();
                    break;

                case SourceFileAttribute.AttributeName:
                    tmp  = stream.ReadU2();
                    attr = new SourceFileAttribute(cp.GetEntry <ConstantPoolUtf8>(tmp).Value);
                    break;

                case LineNumberTableAttribute.AttributeName:
                    attr = ReadLineNumberTableAttribute();
                    break;

                case LocalVariableTableAttribute.AttributeName:
                    attr = ReadLocalVariableTableAttribute(cp);
                    break;

                case DeprecatedAttribute.AttributeName:
                    attr = new DeprecatedAttribute();
                    break;

                case OverrideAttribute.AttributeName:
                    attr = new OverrideAttribute();
                    break;

                case SignatureAttribute.AttributeName:
                    tmp  = stream.ReadU2();
                    attr = new SignatureAttribute(cp.GetEntry <ConstantPoolUtf8>(tmp).Value);
                    break;

                case RuntimeVisibleAnnotationsAttribute.AttributeName:
                    attr = new RuntimeVisibleAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                    break;

                case RuntimeInvisibleAnnotationsAttribute.AttributeName:
                    attr = new RuntimeInvisibleAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                    break;

                case RuntimeVisibleParameterAnnotationsAttribute.AttributeName:
                    attr = new RuntimeVisibleParameterAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                    break;

                case RuntimeInvisibleParameterAnnotationsAttribute.AttributeName:
                    attr = new RuntimeInvisibleParameterAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                    break;

                case AnnotationDefaultAttribute.AttributeName:
                    attr = new AnnotationDefaultAttribute(ReadElementValue(cp));
                    break;

                default:
                    stream.Skip(length);
                    attr = new UnknownAttribute(name);
                    break;
                }
                provider.Add(attr);
            }
            provider.AttributesLoaded();
        }
Example #7
0
 public OverrideDetails(OverrideAttribute attribute, Type containingType, PropertyInfo property)
 {
     Attribute      = attribute;
     ContainingType = containingType;
     Property       = property;
 }