Exemplo n.º 1
0
        private VManifest(FArchive Ar)
        {
            using (Ar)
            {
                Header = new VHeader(Ar);
                var compressedBuffer   = Ar.ReadBytes((int)Header.CompressedSize);
                var uncompressedBuffer = ZlibStream.UncompressBuffer(compressedBuffer);
                if (uncompressedBuffer.Length != Header.UncompressedSize)
                {
                    throw new ParserException(Ar, $"Decompression failed, {uncompressedBuffer.Length} != {Header.UncompressedSize}");
                }

                using var manifest = new FByteArchive("UncompressedValorantManifest", uncompressedBuffer);
                Chunks             = manifest.ReadArray <VChunk>((int)Header.ChunkCount);
                Paks = manifest.ReadArray((int)Header.PakCount, () => new VPak(manifest));

                if (manifest.Position != manifest.Length)
                {
                    throw new ParserException(manifest, $"Parsing failed, {manifest.Position} != {manifest.Length}");
                }
            }

            _client = new HttpClient(new HttpClientHandler
            {
                UseProxy                       = false,
                UseCookies                     = false,
                AutomaticDecompression         = DecompressionMethods.All,
                CheckCertificateRevocationList = false,
                PreAuthenticate                = false,
                MaxConnectionsPerServer        = 1337,
                UseDefaultCredentials          = false,
                AllowAutoRedirect              = false
            });
        }
Exemplo n.º 2
0
 public void InitViewsFromBuffer(byte[] bulkData)
 {
     using var tempAr = new FByteArchive("SerializedByteStream", bulkData);
     tempAr.ReadArray(CompressedTrackOffsets);
     tempAr.ReadArray(CompressedScaleOffsets.OffsetData);
     tempAr.ReadArray(CompressedByteStream);
 }
Exemplo n.º 3
0
        // UE4.23-4.24 has changed compressed data layout for streaming, so it's worth making a separate
        // serializer function for it.
        private void SerializeCompressedData2(FAssetArchive Ar)
        {
            var compressedRawDataSize = Ar.Read <int>();

            CompressedTrackToSkeletonMapTable = Ar.ReadArray <FTrackToSkeletonMap>();
            var compressedCurveNames = Ar.ReadArray(() => new FSmartName(Ar));

            // Since 4.23, this is FUECompressedAnimData::SerializeCompressedData
            KeyEncodingFormat            = Ar.Read <AnimationKeyFormat>();
            TranslationCompressionFormat = Ar.Read <AnimationCompressionFormat>();
            RotationCompressionFormat    = Ar.Read <AnimationCompressionFormat>();
            ScaleCompressionFormat       = Ar.Read <AnimationCompressionFormat>();

            var compressedNumFrames = Ar.Read <int>();

            // SerializeView() just serializes array size
            var compressedTrackOffsetsNum = Ar.Read <int>();
            var compressedScaleOffsetsNum = Ar.Read <int>();

            CompressedScaleOffsets = new FCompressedOffsetData(Ar.Read <int>());
            var compressedByteStreamNum = Ar.Read <int>();
            // ... end of FUECompressedAnimData::SerializeCompressedData

            var numBytes            = Ar.Read <int>();
            var bUseBulkDataForLoad = Ar.ReadBoolean();

            // In UE4.23 CompressedByteStream field exists in FUECompressedAnimData (as TArrayView) and in
            // FCompressedAnimSequence (as byte array). Serialization is done in FCompressedAnimSequence,
            // either as TArray or as bulk, and then array is separated onto multiple "views" for
            // FUECompressedAnimData. We'll use a different name for "joined" serialized array here to
            // avoid confuse.
            byte[] serializedByteStream;

            if (bUseBulkDataForLoad)
            {
                throw new NotImplementedException("Anim: bUseBulkDataForLoad not implemented");
                //todo: read from bulk to serializedByteStream
            }
            else
            {
                serializedByteStream = Ar.ReadBytes(numBytes);
            }

            // Setup all array views from single array. In UE4 this is done in FUECompressedAnimData::InitViewsFromBuffer.
            // We'll simply copy array data away from SerializedByteStream, and then SerializedByteStream
            // will be released from memory as it is a local variable here.
            // Note: copying is not byte-order wise, so if there will be any problems in the future,
            // should use byte swap functions.
            using (var tempAr = new FByteArchive("SerializedByteStream", serializedByteStream, Ar.Versions))
            {
                CompressedTrackOffsets            = tempAr.ReadArray <int>(compressedTrackOffsetsNum);
                CompressedScaleOffsets.OffsetData = tempAr.ReadArray <int>(compressedScaleOffsetsNum);
                CompressedByteStream = tempAr.ReadBytes(compressedByteStreamNum);
            }

            var curveCodecPath            = Ar.ReadFString();
            var compressedCurveByteStream = Ar.ReadArray <byte>();
        }
Exemplo n.º 4
0
        public IoGlobalData(IoStoreReader globalReader)
        {
            FByteArchive metaAr;

            if (globalReader.Game >= EGame.GAME_UE5_0)
            {
                metaAr        = new FByteArchive("ScriptObjects", globalReader.Read(new FIoChunkId(0, 0, EIoChunkType5.ScriptObjects)));
                GlobalNameMap = FNameEntrySerialized.LoadNameBatch(metaAr);
            }
            else // UE4.26+
            {
                var nameHashesChunk = globalReader.ChunkIndex(new FIoChunkId(0, 0, EIoChunkType.LoaderGlobalNameHashes));
                var nameCount       = (int)(globalReader.TocResource.ChunkOffsetLengths[nameHashesChunk].Length / sizeof(ulong) - 1);

                var nameAr = new FByteArchive("LoaderGlobalNames", globalReader.Read(new FIoChunkId(0, 0, EIoChunkType.LoaderGlobalNames)));
                GlobalNameMap = FNameEntrySerialized.LoadNameBatch(nameAr, nameCount);

                metaAr = new FByteArchive("LoaderInitialLoadMeta", globalReader.Read(new FIoChunkId(0, 0, EIoChunkType.LoaderInitialLoadMeta)));
            }

            var numObjects    = metaAr.Read <int>();
            var scriptObjects = metaAr.ReadArray <FScriptObjectEntry>(numObjects);

            ObjectHashStore = new ObjectIndexHashEntry[numObjects];
            ObjectHashHeads = new ObjectIndexHashEntry[4096];
            for (int i = 0; i < ObjectHashHeads.Length; i++)
            {
                ObjectHashHeads[i] = new ObjectIndexHashEntry();
            }

            for (int i = 0; i < numObjects; i++)
            {
                ref var e = ref scriptObjects[i];

                var scriptName = GlobalNameMap[(int)e.ObjectName.NameIndex];

                var entry = new ObjectIndexHashEntry
                {
                    Name        = scriptName.Name,
                    ObjectIndex = e.GlobalIndex
                };
                ObjectHashStore[i] = entry;

                var hash = ObjectIndexToHash(e.GlobalIndex);
                entry.Next            = ObjectHashHeads[hash];
                ObjectHashHeads[hash] = entry;
            }
Exemplo n.º 5
0
        private IReadOnlyDictionary <string, GameFile> ReadIndexLegacy(bool caseInsensitive)
        {
            Ar.Position = Info.IndexOffset;
            var index = new FByteArchive($"{Name} - Index", ReadAndDecrypt((int)Info.IndexSize));

            string mountPoint;

            try
            {
                mountPoint = index.ReadFString();
            }
            catch (Exception e)
            {
                throw new InvalidAesKeyException($"Given aes key '{AesKey?.KeyString}'is not working with '{Name}'", e);
            }

            ValidateMountPoint(ref mountPoint);
            MountPoint = mountPoint;
            var fileCount = index.Read <int>();
            var files     = new Dictionary <string, GameFile>(fileCount);

            for (var i = 0; i < fileCount; i++)
            {
                var path  = string.Concat(mountPoint, index.ReadFString());
                var entry = new FPakEntry(this, path, index);
                if (entry.IsDeleted && entry.Size == 0)
                {
                    continue;
                }
                if (entry.IsEncrypted)
                {
                    EncryptedFileCount++;
                }
                if (caseInsensitive)
                {
                    files[path.ToLowerInvariant()] = entry;
                }
                else
                {
                    files[path] = entry;
                }
            }

            return(Files = files);
        }
Exemplo n.º 6
0
        public static byte[]? Decompress(bool shouldDecompress, ref string audioFormat, byte[]?input)
        {
            if (input == null)
            {
                return(null);
            }
            if (!shouldDecompress)
            {
                return(input);
            }
            if (audioFormat.Equals("ADPCM", StringComparison.OrdinalIgnoreCase))
            {
                using var archive = new FByteArchive("WhoDoesntLoveCats", input);
                switch (ADPCMDecoder.GetAudioFormat(archive))
                {
                case EAudioFormat.WAVE_FORMAT_PCM:
                    audioFormat = "WAV";
                    return(input);

                case EAudioFormat.WAVE_FORMAT_ADPCM:
                    return(input);
                }
            }
            else if (audioFormat.Equals("OPUS", StringComparison.OrdinalIgnoreCase))
            {
                return(input);
            }
            else if (audioFormat.Equals("WEM", StringComparison.OrdinalIgnoreCase))
            {
                return(input);
            }
            else if (audioFormat.IndexOf("OGG", StringComparison.OrdinalIgnoreCase) > -1)
            {
                audioFormat = "OGG";
                return(input);
            }

            return(null);
        }
Exemplo n.º 7
0
        public FRawStaticIndexBuffer(FArchive Ar) : this()
        {
            if (Ar.Ver < EUnrealEngineObjectUE4Version.SUPPORT_32BIT_STATIC_MESH_INDICES)
            {
                Indices16 = Ar.ReadBulkArray <ushort>();
            }
            else
            {
                var is32bit = Ar.ReadBoolean();
                var data    = Ar.ReadBulkArray <byte>();
                var tempAr  = new FByteArchive("IndicesReader", data, Ar.Versions);

                if (Ar.Versions["RawIndexBuffer.HasShouldExpandTo32Bit"])
                {
                    var bShouldExpandTo32Bit = Ar.ReadBoolean();
                }

                if (tempAr.Length == 0)
                {
                    tempAr.Dispose();
                    return;
                }

                if (is32bit)
                {
                    var count = (int)tempAr.Length / 4;
                    Indices32 = tempAr.ReadArray <uint>(count);
                }
                else
                {
                    var count = (int)tempAr.Length / 2;
                    Indices16 = tempAr.ReadArray <ushort>(count);
                }
                tempAr.Dispose();
            }
        }
        public FRawStaticIndexBuffer(FArchive Ar) : this()
        {
            if (Ar.Ver < UE4Version.VER_UE4_SUPPORT_32BIT_STATIC_MESH_INDICES)
            {
                Indices16 = Ar.ReadBulkArray <ushort>();
            }
            else
            {
                var is32bit = Ar.ReadBoolean();
                var data    = Ar.ReadBulkArray <byte>();
                var tempAr  = new FByteArchive("IndicesReader", data, Ar.Versions);

                if (Ar.Game >= EGame.GAME_UE4_25)
                {
                    Ar.Position += 4;
                }

                if (tempAr.Length == 0)
                {
                    tempAr.Dispose();
                    return;
                }

                if (is32bit)
                {
                    var count = (int)tempAr.Length / 4;
                    Indices32 = tempAr.ReadArray <uint>(count);
                }
                else
                {
                    var count = (int)tempAr.Length / 2;
                    Indices16 = tempAr.ReadArray <ushort>(count);
                }
                tempAr.Dispose();
            }
        }
Exemplo n.º 9
0
        public FSkinWeightVertexBuffer(FArchive Ar, bool numSkelCondition)
        {
            var bNewWeightFormat = FAnimObjectVersion.Get(Ar) >= FAnimObjectVersion.Type.UnlimitedBoneInfluences;

            #region FSkinWeightDataVertexBuffer
            var dataStripFlags = Ar.Read <FStripDataFlags>();

            #region FSkinWeightDataVertexBuffer::SerializeMetaData
            bool bVariableBonesPerVertex;
            bool bExtraBoneInfluences;
            uint maxBoneInfluences;
            bool bUse16BitBoneIndex;
            uint numVertices;
            uint numBones;

            if (Ar.Game < EGame.GAME_UE4_24)
            {
                bExtraBoneInfluences = Ar.ReadBoolean();
                numVertices          = Ar.Read <uint>();
                maxBoneInfluences    = bExtraBoneInfluences ? 8u : 4u;
            }
            else if (!bNewWeightFormat)
            {
                bExtraBoneInfluences = Ar.ReadBoolean();
                if (FSkeletalMeshCustomVersion.Get(Ar) >= FSkeletalMeshCustomVersion.Type.SplitModelAndRenderData)
                {
                    Ar.Position += 4; // var stride = Ar.Read<uint>();
                }
                numVertices             = Ar.Read <uint>();
                maxBoneInfluences       = bExtraBoneInfluences ? 8u : 4u;
                numBones                = maxBoneInfluences * numVertices;
                bVariableBonesPerVertex = false;
            }
            else
            {
                bVariableBonesPerVertex = Ar.ReadBoolean();
                maxBoneInfluences       = Ar.Read <uint>();
                numBones             = Ar.Read <uint>();
                numVertices          = Ar.Read <uint>();
                bExtraBoneInfluences = maxBoneInfluences > _NUM_INFLUENCES_UE4;
                // bUse16BitBoneIndex doesn't exist before version IncreaseBoneIndexLimitPerChunk
                if (FAnimObjectVersion.Get(Ar) >= FAnimObjectVersion.Type.IncreaseBoneIndexLimitPerChunk)
                {
                    bUse16BitBoneIndex = Ar.ReadBoolean();
                }
            }
            #endregion

            byte[] newData = Array.Empty <byte>();
            if (!dataStripFlags.IsDataStrippedForServer())
            {
                if (!bNewWeightFormat)
                {
                    Weights = Ar.ReadBulkArray(() => new FSkinWeightInfo(Ar, bExtraBoneInfluences));
                }
                else
                {
                    newData = Ar.ReadBulkArray <byte>();
                }
            }
            else
            {
                bExtraBoneInfluences = numSkelCondition;
            }
            #endregion

            if (bNewWeightFormat)
            {
                #region FSkinWeightLookupVertexBuffer
                var lookupStripFlags = Ar.Read <FStripDataFlags>();

                #region FSkinWeightLookupVertexBuffer::SerializeMetaData
                //if (bNewWeightFormat)
                //{
                var numLookupVertices = Ar.Read <int>();
                //}
                #endregion

                if (!lookupStripFlags.IsDataStrippedForServer())
                {
                    Ar.ReadBulkArray <uint>(); // LookupData
                }
                #endregion

                // Convert influence data
                if (newData.Length > 0)
                {
                    using var tempAr = new FByteArchive("WeightsReader", newData, Ar.Versions);
                    Weights          = new FSkinWeightInfo[numVertices];
                    for (var i = 0; i < Weights.Length; i++)
                    {
                        Weights[i] = new FSkinWeightInfo(tempAr, bExtraBoneInfluences);
                    }
                }
            }
        }
Exemplo n.º 10
0
        public void SerializeRenderItem(FAssetArchive Ar, bool bHasVertexColors, byte numVertexColorChannels)
        {
            if (Ar.Game < EGame.GAME_UE4_24)
            {
                SerializeRenderItem_Legacy(Ar, bHasVertexColors, numVertexColorChannels);
                return;
            }

            var stripDataFlags  = Ar.Read <FStripDataFlags>();
            var bIsLODCookedOut = Ar.ReadBoolean();
            var bInlined        = Ar.ReadBoolean();

            RequiredBones = Ar.ReadArray <short>();
            if (!stripDataFlags.IsDataStrippedForServer() && !bIsLODCookedOut)
            {
                Sections = new FSkelMeshSection[Ar.Read <int>()];
                for (var i = 0; i < Sections.Length; i++)
                {
                    Sections[i] = new FSkelMeshSection();
                    Sections[i].SerializeRenderItem(Ar);
                }

                ActiveBoneIndices = Ar.ReadArray <short>();
                Ar.Position      += 4; //var buffersSize = Ar.Read<uint>();

                if (bInlined)
                {
                    SerializeStreamedData(Ar, bHasVertexColors);
                    if (Ar.Game == EGame.GAME_ROGUECOMPANY)
                    {
                        Ar.Position += 10; // FStripDataFlags, ElementSize, ElementCount
                        Ar.SkipBulkArrayData();
                        Ar.Position += 10;
                    }
                }
                else
                {
                    var bulk = new FByteBulkData(Ar);
                    if (bulk.Header.ElementCount > 0)
                    {
                        using (var tempAr = new FByteArchive("LodReader", bulk.Data, Ar.Versions))
                        {
                            SerializeStreamedData(tempAr, bHasVertexColors);
                        }

                        var skipBytes = 5;
                        if (FUE5ReleaseStreamObjectVersion.Get(Ar) < FUE5ReleaseStreamObjectVersion.Type.RemovingTessellation && !stripDataFlags.IsClassDataStripped((byte)EClassDataStripFlag.CDSF_AdjacencyData))
                        {
                            skipBytes += 5;
                        }
                        skipBytes   += 4 * 4 + 2 * 4 + 2 * 4;
                        skipBytes   += FSkinWeightVertexBuffer.MetadataSize(Ar);
                        Ar.Position += skipBytes;

                        if (HasClothData())
                        {
                            var clothIndexMapping = Ar.ReadArray <long>();
                            Ar.Position += 2 * 4;
                        }

                        var profileNames = Ar.ReadArray(Ar.ReadFName);
                    }
                }
            }
        }
        public static TypeMappings Parse(FArchive Ar)
        {
            var magic = Ar.Read <ushort>();

            if (magic != FileMagic)
            {
                throw new ParserException(".usmap file has an invalid magic constant");
            }

            var version = Ar.Read <Version>();

            if (version < 0 || version > Version.LATEST)
            {
                throw new ParserException($".usmap has an invalid version {(byte) version}");
            }

            var compression = Ar.Read <ECompressionMethod>();

            var compSize   = Ar.Read <uint>();
            var decompSize = Ar.Read <uint>();

            var data = new byte[decompSize];

            switch (compression)
            {
            case ECompressionMethod.None:
                if (compSize != decompSize)
                {
                    throw new ParserException("No compression: Compression size must be equal to decompression size");
                }
                Ar.Read(data, 0, (int)compSize);
                break;

            case ECompressionMethod.Oodle:
                Oodle.Decompress(Ar.ReadBytes((int)compSize), 0, (int)compSize, data, 0, (int)decompSize);
                break;

            case ECompressionMethod.Brotli:
                throw new NotImplementedException();

            default:
                throw new ParserException($"Invalid compression method {compression}");
            }

            Ar = new FByteArchive(Ar.Name, data);
            var nameSize = Ar.Read <uint>();
            var nameLut  = new List <String>((int)nameSize);

            for (int i = 0; i < nameSize; i++)
            {
                var nameLength = Ar.Read <byte>();
                nameLut.Add(ReadStringUnsafe(Ar, nameLength));
            }

            var enumCount = Ar.Read <uint>();
            var enums     = new Dictionary <string, Dictionary <int, string> >((int)enumCount);

            for (int i = 0; i < enumCount; i++)
            {
                var enumName = Ar.ReadName(nameLut) !;

                var enumNamesSize = Ar.Read <byte>();
                var enumNames     = new Dictionary <int, string>(enumNamesSize);
                for (int j = 0; j < enumNamesSize; j++)
                {
                    var value = Ar.ReadName(nameLut) !;
                    enumNames[j] = value;
                }

                enums.Add(enumName, enumNames);
            }

            var structCount = Ar.Read <uint>();
            var structs     = new Dictionary <string, Struct>();

            var mappings = new TypeMappings(structs, enums);

            for (int i = 0; i < structCount; i++)
            {
                var s = ParseStruct(mappings, Ar, nameLut);
                structs[s.Name] = s;
            }

            return(mappings);
        }
Exemplo n.º 12
0
        private IReadOnlyDictionary <string, GameFile> ReadIndexUpdated(bool caseInsensitive)
        {
            // Prepare primary index and decrypt if necessary
            Ar.Position = Info.IndexOffset;
            FArchive primaryIndex = new FByteArchive($"{Name} - Primary Index", ReadAndDecrypt((int)Info.IndexSize));

            string mountPoint;

            try
            {
                mountPoint = primaryIndex.ReadFString();
            }
            catch (Exception e)
            {
                throw new InvalidAesKeyException($"Given aes key '{AesKey?.KeyString}'is not working with '{Name}'", e);
            }

            ValidateMountPoint(ref mountPoint);
            MountPoint = mountPoint;

            var fileCount = primaryIndex.Read <int>();

            EncryptedFileCount = 0;

            primaryIndex.Position += 8; // PathHashSeed

            if (!primaryIndex.ReadBoolean())
            {
                throw new ParserException(primaryIndex, "No path hash index");
            }

            primaryIndex.Position += 36; // PathHashIndexOffset (long) + PathHashIndexSize (long) + PathHashIndexHash (20 bytes)

            if (!primaryIndex.ReadBoolean())
            {
                throw new ParserException(primaryIndex, "No directory index");
            }

            var directoryIndexOffset = primaryIndex.Read <long>();
            var directoryIndexSize   = primaryIndex.Read <long>();

            primaryIndex.Position += 20; // Directory Index hash
            var encodedPakEntriesSize = primaryIndex.Read <int>();
            var encodedPakEntries     = primaryIndex.ReadBytes(encodedPakEntriesSize);

            if (primaryIndex.Read <int>() < 0)
            {
                throw new ParserException("Corrupt pak PrimaryIndex detected");
            }

            // Read FDirectoryIndex
            Ar.Position = directoryIndexOffset;
            var directoryIndex = new FByteArchive($"{Name} - Directory Index", ReadAndDecrypt((int)directoryIndexSize));

            unsafe
            {
                fixed(byte *ptr = encodedPakEntries)
                {
                    var directoryIndexLength = directoryIndex.Read <int>();

                    var files = new Dictionary <string, GameFile>(fileCount);

                    for (var i = 0; i < directoryIndexLength; i++)
                    {
                        var dir           = directoryIndex.ReadFString();
                        var dirDictLength = directoryIndex.Read <int>();

                        for (var j = 0; j < dirDictLength; j++)
                        {
                            var name  = directoryIndex.ReadFString();
                            var path  = string.Concat(mountPoint, dir, name);
                            var entry = new FPakEntry(this, path, ptr + directoryIndex.Read <int>());
                            if (entry.IsEncrypted)
                            {
                                EncryptedFileCount++;
                            }
                            if (caseInsensitive)
                            {
                                files[path.ToLowerInvariant()] = entry;
                            }
                            else
                            {
                                files[path] = entry;
                            }
                        }
                    }

                    Files = files;

                    return(files);
                }
            }
        }
Exemplo n.º 13
0
        public FIoStoreTocResource(FArchive Ar, EIoStoreTocReadOptions readOptions = EIoStoreTocReadOptions.Default)
        {
            var streamBuffer = new byte[Ar.Length];

            Ar.Read(streamBuffer, 0, streamBuffer.Length);
            using var archive = new FByteArchive(Ar.Name, streamBuffer);

            // Header
            Header = new FIoStoreTocHeader(archive);

            if (Header.Version < EIoStoreTocVersion.PartitionSize)
            {
                Header.PartitionCount = 1;
                Header.PartitionSize  = uint.MaxValue;
            }

            // Chunk IDs
            ChunkIds = archive.ReadArray <FIoChunkId>((int)Header.TocEntryCount);

            // Chunk offsets
            ChunkOffsetLengths = new FIoOffsetAndLength[Header.TocEntryCount];
            for (int i = 0; i < Header.TocEntryCount; i++)
            {
                ChunkOffsetLengths[i] = new FIoOffsetAndLength(archive);
            }

            // Chunk perfect hash map
            uint perfectHashSeedsCount         = 0;
            uint chunksWithoutPerfectHashCount = 0;

            if (Header.Version >= EIoStoreTocVersion.PerfectHashWithOverflow)
            {
                perfectHashSeedsCount         = Header.TocChunkPerfectHashSeedsCount;
                chunksWithoutPerfectHashCount = Header.TocChunksWithoutPerfectHashCount;
            }
            else if (Header.Version >= EIoStoreTocVersion.PerfectHash)
            {
                perfectHashSeedsCount = Header.TocChunkPerfectHashSeedsCount;
            }
            if (perfectHashSeedsCount > 0)
            {
                ChunkPerfectHashSeeds = archive.ReadArray <int>((int)perfectHashSeedsCount);
            }
            if (chunksWithoutPerfectHashCount > 0)
            {
                ChunkIndicesWithoutPerfectHash = archive.ReadArray <int>((int)chunksWithoutPerfectHashCount);
            }

            // Compression blocks
            CompressionBlocks = new FIoStoreTocCompressedBlockEntry[Header.TocCompressedBlockEntryCount];
            for (int i = 0; i < Header.TocCompressedBlockEntryCount; i++)
            {
                CompressionBlocks[i] = new FIoStoreTocCompressedBlockEntry(archive);
            }

            // Compression methods
            unsafe
            {
                var bufferSize = (int)(Header.CompressionMethodNameLength * Header.CompressionMethodNameCount);
                var buffer     = stackalloc byte[bufferSize];
                archive.Serialize(buffer, bufferSize);
                CompressionMethods    = new CompressionMethod[Header.CompressionMethodNameCount + 1];
                CompressionMethods[0] = CompressionMethod.None;
                for (var i = 0; i < Header.CompressionMethodNameCount; i++)
                {
                    var name = new string((sbyte *)buffer + i * Header.CompressionMethodNameLength, 0, (int)Header.CompressionMethodNameLength).TrimEnd('\0');
                    if (string.IsNullOrEmpty(name))
                    {
                        continue;
                    }
                    if (!Enum.TryParse(name, true, out CompressionMethod method))
                    {
                        Log.Warning($"Unknown compression method '{name}' in {Ar.Name}");
                        method = CompressionMethod.Unknown;
                    }

                    CompressionMethods[i + 1] = method;
                }
            }

            // Chunk block signatures
            if (Header.ContainerFlags.HasFlag(EIoContainerFlags.Signed))
            {
                var hashSize = archive.Read <int>();
                // tocSignature and blockSignature both byte[hashSize]
                // and ChunkBlockSignature of FSHAHash[Header.TocCompressedBlockEntryCount]
                archive.Position += hashSize + hashSize + FSHAHash.SIZE * Header.TocCompressedBlockEntryCount;

                // You could verify hashes here but nah
            }

            // Directory index
            if (Header.Version >= EIoStoreTocVersion.DirectoryIndex &&
                readOptions.HasFlag(EIoStoreTocReadOptions.ReadDirectoryIndex) &&
                Header.ContainerFlags.HasFlag(EIoContainerFlags.Indexed) &&
                Header.DirectoryIndexSize > 0)
            {
                DirectoryIndexBuffer = archive.ReadBytes((int)Header.DirectoryIndexSize);
            }

            // Meta
            if (readOptions.HasFlag(EIoStoreTocReadOptions.ReadTocMeta))
            {
                ChunkMetas = new FIoStoreTocEntryMeta[Header.TocEntryCount];
                for (int i = 0; i < Header.TocEntryCount; i++)
                {
                    ChunkMetas[i] = new FIoStoreTocEntryMeta(archive);
                }
            }
        }
        public FStaticMeshLODResources(FAssetArchive Ar)
        {
            var stripDataFlags = Ar.Read <FStripDataFlags>();

            Sections     = Ar.ReadArray(() => new FStaticMeshSection(Ar));
            MaxDeviation = Ar.Read <float>();

            if (Ar.Game < EGame.GAME_UE4_23)
            {
                if (!stripDataFlags.IsDataStrippedForServer() && !stripDataFlags.IsClassDataStripped((byte)EClassDataStripFlag.CDSF_MinLodData))
                {
                    SerializeBuffersLegacy(Ar, stripDataFlags);
                }

                return;
            }

            var bIsLODCookedOut = Ar.ReadBoolean();
            var bInlined        = Ar.ReadBoolean();

            if (Ar.Game == EGame.GAME_ROGUECOMPANY)
            {
                bInlined = true;
            }

            if (!stripDataFlags.IsDataStrippedForServer() && !bIsLODCookedOut)
            {
                if (bInlined)
                {
                    SerializeBuffers(Ar);
                    if (Ar.Game == EGame.GAME_ROGUECOMPANY)
                    {
                        Ar.Position += 10;
                    }
                }
                else
                {
                    var bulkData = new FByteBulkData(Ar);
                    if (bulkData.Header.ElementCount > 0)
                    {
                        var tempAr = new FByteArchive("StaticMeshBufferReader", bulkData.Data, Ar.Versions);
                        SerializeBuffers(tempAr);
                        tempAr.Dispose();
                    }

                    // https://github.com/EpicGames/UnrealEngine/blob/4.27/Engine/Source/Runtime/Engine/Private/StaticMesh.cpp#L560
                    Ar.Position += 8; // DepthOnlyNumTriangles + Packed
                    Ar.Position += 4 * 4 + 2 * 4 + 2 * 4 + 5 * 2 * 4;
                    // StaticMeshVertexBuffer = 2x int32, 2x bool
                    // PositionVertexBuffer = 2x int32
                    // ColorVertexBuffer = 2x int32
                    // IndexBuffer = int32 + bool
                    // ReversedIndexBuffer
                    // DepthOnlyIndexBuffer
                    // ReversedDepthOnlyIndexBuffer
                    // WireframeIndexBuffer
                    if (FUE5ReleaseStreamObjectVersion.Get(Ar) < FUE5ReleaseStreamObjectVersion.Type.RemovingTessellation)
                    {
                        Ar.Position += 2 * 4; // AdjacencyIndexBuffer
                    }
                }
            }

            // FStaticMeshBuffersSize
            // uint32 SerializedBuffersSize = 0;
            // uint32 DepthOnlyIBSize       = 0;
            // uint32 ReversedIBsSize       = 0;
            Ar.Position += 12;
        }
Exemplo n.º 15
0
        // UE ref https://github.com/EpicGames/UnrealEngine/blob/26450a5a59ef65d212cf9ce525615c8bd673f42a/Engine/Source/Runtime/Engine/Private/SkeletalMeshLODRenderData.cpp#L710
        public void SerializeRenderItem(FAssetArchive Ar, bool bHasVertexColors, byte numVertexColorChannels)
        {
            var stripDataFlags  = Ar.Read <FStripDataFlags>();
            var bIsLODCookedOut = false;

            if (Ar.Game != EGame.GAME_Splitgate)
            {
                bIsLODCookedOut = Ar.ReadBoolean();
            }
            var bInlined = Ar.ReadBoolean();

            RequiredBones = Ar.ReadArray <short>();
            if (!stripDataFlags.IsDataStrippedForServer() && !bIsLODCookedOut)
            {
                Sections = new FSkelMeshSection[Ar.Read <int>()];
                for (var i = 0; i < Sections.Length; i++)
                {
                    Sections[i] = new FSkelMeshSection();
                    Sections[i].SerializeRenderItem(Ar);
                }

                ActiveBoneIndices = Ar.ReadArray <short>();

                if (Ar.Game == EGame.GAME_KenaBridgeofSpirits)
                {
                    Ar.ReadArray <byte>(); // EAssetType_array1
                }
                Ar.Position += 4;          //var buffersSize = Ar.Read<uint>();

                if (bInlined)
                {
                    SerializeStreamedData(Ar, bHasVertexColors);
                    if (Ar.Game == EGame.GAME_RogueCompany)
                    {
                        Ar.Position += 12; // 1 (Long) + 2^16 (Int)
                        var elementSize  = Ar.Read <int>();
                        var elementCount = Ar.Read <int>();
                        if (elementSize > 0 && elementCount > 0)
                        {
                            Ar.SkipBulkArrayData();
                        }
                    }
                }
                else
                {
                    var bulk = new FByteBulkData(Ar);
                    if (bulk.Header.ElementCount > 0)
                    {
                        using (var tempAr = new FByteArchive("LodReader", bulk.Data, Ar.Versions))
                        {
                            SerializeStreamedData(tempAr, bHasVertexColors);
                        }

                        var skipBytes = 5;
                        if (FUE5ReleaseStreamObjectVersion.Get(Ar) < FUE5ReleaseStreamObjectVersion.Type.RemovingTessellation && !stripDataFlags.IsClassDataStripped((byte)EClassDataStripFlag.CDSF_AdjacencyData))
                        {
                            skipBytes += 5;
                        }
                        skipBytes   += 4 * 4 + 2 * 4 + 2 * 4;
                        skipBytes   += FSkinWeightVertexBuffer.MetadataSize(Ar);
                        Ar.Position += skipBytes;

                        if (HasClothData())
                        {
                            var clothIndexMapping = Ar.ReadArray <long>();
                            Ar.Position += 2 * 4;
                        }

                        var profileNames = Ar.ReadArray(Ar.ReadFName);
                    }
                }
            }

            if (Ar.Game == EGame.GAME_ReadyOrNot)
            {
                Ar.Position += 4;
            }
        }