Example #1
0
        /// <summary>
        /// Checks whether the buffer size for a file about to be loaded is sufficient and
        /// conditonally returns an address to a new buffer with adequate size.
        /// </summary>
        /// <returns>The address of a new buffer for the game to decompress a file to</returns>
        private static void *CheckBufferSize(int fileIndex, void *addressToDecompressTo, ONEFILE *thisPointer)
        {
            if (fileIndex >= 2)
            {
                // Get pointer and length.
                IntPtr onePointer = (IntPtr)thisPointer[0].InternalDataPointer - 0xC; // The start of file pointer is sometimes unused, so we use the InternalDataPointer instead and offset.
                int    fileLength = thisPointer[0].FileLength;

                // Now the ONE File
                MemoryONEArchive memoryOneFile = MemoryONEArchive.ParseONEFromMemory(onePointer, fileLength);

                // Now we estimate the size of it.
                int           actualFileIndex = fileIndex - 2;
                MemoryONEFile oneFile         = memoryOneFile.Files[actualFileIndex];
                byte[]        oneFileCopy     = GameProcess.ReadMemory((IntPtr)oneFile.CompressedDataPointer, oneFile.DataLength);
                int           oneFileLength   = Prs.Estimate(ref oneFileCopy);

                // Check if the size of allocation is sufficient.
                MemoryAddressDetails addressDetails = Allocator.GetAddressDetails((int)addressToDecompressTo);

                if (addressDetails.MemorySize < oneFileLength)
                {
                    // Allocate some new data for me please
                    addressToDecompressTo = (void *)Allocator.Allocate((int)addressToDecompressTo, oneFileLength);
                }
            }

            return(addressToDecompressTo);
        }