Ejemplo n.º 1
0
        /// <summary>
        /// Get an object in memory from specified base address.
        /// </summary>
        /// <param name="vid">The unique ID of type.</param>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Type can not be abstract!
        /// or
        /// Type must inherit from MemoryObject!</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Type with id  + vid +  was not found!</exception>
        /// <exception cref="System.InvalidCastException"></exception>
        public static IMemoryObject FromAddress(ulong vid, IntPtr address)
        {
            var game = Main.Game;

            if (game == null)
            {
                throw new ArgumentException("Game library is not loaded! Unable to use types.");
            }

            if (address != IntPtr.Zero)
            {
                var impl = game.GetImplementationById(vid);
                if (impl == null)
                {
                    throw new ArgumentOutOfRangeException("Type with id " + vid + " was not found!");
                }

                List <TypeDescriptor> ls = null;
                if (!game.Types.TypesByImplementation.TryGetValue(impl, out ls) || ls == null || ls.Count == 0)
                {
                    throw new ArgumentException("Type " + impl.Name + " does not have any registered descriptors!");
                }

                TypeDescriptor td = null;
                foreach (var x in ls)
                {
                    if (x.OffsetInFullType == 0)
                    {
                        td = x;
                        break;
                    }

                    if (td == null || x.OffsetInFullType < td.OffsetInFullType)
                    {
                        td = x;
                    }
                }

                // Special case, we still want to detect if the cast is valid.
                if (td.VTable.HasValue)
                {
                    return(VirtualObject.FromAddress(address));
                }

                var mo = td.Creator();
                mo.Address = address - td.OffsetInFullType;
                return(mo);
            }

            return(null);
        }
Ejemplo n.º 2
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 VirtualObject!
        /// </exception>
        public static IVirtualObject FromAddressSafeCast(Type t, IntPtr address)
        {
            if (t == null)
            {
                throw new ArgumentNullException("t");
            }
            if (!t.IsInterface)
            {
                throw new ArgumentException("Type must be interface!");
            }
            if (t != typeof(IVirtualObject) && !typeof(IVirtualObject).IsAssignableFrom(t))
            {
                throw new ArgumentException("Type must inherit from IVirtualObject!");
            }

            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)
            {
                return(null);
            }

            VirtualObject mo = null;

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

            mo.Address = address;

            try
            {
                return(mo.As(t));
            }
            catch (NotSupportedException)
            {
            }

            return(null);
        }