예제 #1
0
        /// <summary>
        /// Get an object in memory from specified base address.
        /// </summary>
        /// <param name="t">The type of object.</param>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">t</exception>
        /// <exception cref="System.ArgumentException">
        /// Type can not be abstract!
        /// or
        /// Type must inherit from MemoryObject!
        /// </exception>
        public static IMemoryObject FromAddress(Type t, IntPtr address)
        {
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }
            if (!t.IsInterface)
            {
                throw new ArgumentException("Type must be interface!");
            }
            if (t != typeof(IMemoryObject) && !typeof(IMemoryObject).IsAssignableFrom(t))
            {
                throw new ArgumentException("Type must inherit from IMemoryObject!");
            }

            if (address == IntPtr.Zero)
            {
                return(null);
            }

            TypeDescriptor td = null;

            if (Main.Game != null)
            {
                if (!Main.Game.Types.TypesByInterface.TryGetValue(t, out td))
                {
                    td = null;

                    if (t.IsGenericType && !t.IsGenericTypeDefinition)
                    {
                        var t2 = t.GetGenericTypeDefinition();
                        if (!Main.Game.Types.TypesByInterface.TryGetValue(t2, out td))
                        {
                            td = null;
                        }
                    }
                }
            }

            if (td == null)
            {
                throw new ArgumentException("Type \"" + t.Name + "\" is not registered with game library!");
            }

            MemoryObject mo = null;

            if (td.IsGeneric)
            {
                var ci = td.GetGenericConstructor(t.GenericTypeArguments);
                mo = (MemoryObject)ci.Invoke(new object[0]);
            }
            else
            {
                mo = (MemoryObject)td.ConstructorNonGeneric.Invoke(new object[0]);
            }

            mo.Address = address;
            return(mo);
        }
예제 #2
0
        /// <summary>
        /// Get an object in memory from specified base address. This will try to use runtime dynamic cast to ensure object is valid type.
        /// It will return null if object could not be cast, type is not registered or memory could not be read properly. The base address
        /// of returned object may or may not equal specified address.
        /// </summary>
        /// <typeparam name="T">Type of object to get.</typeparam>
        /// <param name="address">The base address of object. If an object is returned the base address of object may be different!</param>
        /// <returns></returns>
        public static T FromAddressSafeCast <T>(IntPtr address) where T : IVirtualObject
        {
            if (address == IntPtr.Zero)
            {
                return(default(T));
            }

            TypeDescriptor t = null;

            if (Main.Game != null)
            {
                if (!Main.Game.Types.TypesByInterface.TryGetValue(typeof(T), out t))
                {
                    t = null;

                    var t3 = typeof(T);
                    if (t3.IsGenericType && !t3.IsGenericTypeDefinition)
                    {
                        var t2 = t3.GetGenericTypeDefinition();
                        if (!Main.Game.Types.TypesByInterface.TryGetValue(t2, out t))
                        {
                            t = null;
                        }
                    }
                }
            }

            if (t == null)
            {
                return(default(T));
            }

            T mo;

            try
            {
                mo = MemoryObject.FromAddress <T>(address);
            }
            catch (ArgumentException)
            {
                return(default(T));
            }

            try
            {
                var result = mo.As <T>();
                return(result);
            }
            catch (NotSupportedException)
            {
            }

            return(default(T));
        }
예제 #3
0
        /// <summary>
        /// Get an object in memory from specified base address.
        /// </summary>
        /// <typeparam name="T">Type of object to get.</typeparam>
        /// <param name="address">The base address of object.</param>
        /// <returns></returns>
        public static T FromAddress <T>(IntPtr address) where T : IMemoryObject
        {
            if (address == IntPtr.Zero)
            {
                return(default(T));
            }

            TypeDescriptor t = null;

            if (Main.Game != null)
            {
                if (!Main.Game.Types.TypesByInterface.TryGetValue(typeof(T), out t))
                {
                    t = null;

                    var t3 = typeof(T);
                    if (t3.IsGenericType && !t3.IsGenericTypeDefinition)
                    {
                        var t2 = t3.GetGenericTypeDefinition();
                        if (!Main.Game.Types.TypesByInterface.TryGetValue(t2, out t))
                        {
                            t = null;
                        }
                    }
                }
            }

            if (t == null)
            {
                throw new ArgumentException("Type \"" + typeof(T).Name + "\" is not registered with game library!");
            }

            MemoryObject mo = null;

            if (t.IsGeneric)
            {
                var ci = t.GetGenericConstructor(typeof(T).GenericTypeArguments);
                mo = (MemoryObject)ci.Invoke(new object[0]);
            }
            else
            {
                mo = (MemoryObject)t.ConstructorNonGeneric.Invoke(new object[0]);
            }

            mo.Address = address;
            object result = mo;

            return((T)result);
        }
예제 #4
0
 /// <summary>
 /// Get an object in memory from specified pointer.
 /// </summary>
 /// <param name="t">The type of object to get.</param>
 /// <param name="address">The pointer address.</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">t</exception>
 /// <exception cref="System.ArgumentException">
 /// Type can not be abstract!
 /// or
 /// Type must inherit from MemoryObject!
 /// </exception>
 public static IMemoryObject FromPointer(Type t, IntPtr address)
 {
     return(MemoryObject.FromAddress(t, Memory.ReadPointer(address)));
 }
예제 #5
0
 /// <summary>
 /// Get an object in memory from specified pointer.
 /// </summary>
 /// <typeparam name="T">Type of object to get.</typeparam>
 /// <param name="address">The pointer address of object.</param>
 /// <returns></returns>
 public static T FromPointer <T>(IntPtr address) where T : IMemoryObject
 {
     return(MemoryObject.FromAddress <T>(Memory.ReadPointer(address)));
 }