示例#1
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));
        }
示例#2
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)));
 }
示例#3
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)));
 }