Esempio n. 1
0
        public static ManifestFile FromFileSystemEntry(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var result = new ManifestFile
                    {
                        Header = ManifestHeader.Parse(reader)
                    };

                    switch (result.Header.Version)
                    {
                    case 5:
                    case 6:
                    case 7:
                        break;

                    default:
                        throw new InvalidDataException();
                    }

                    result.Assets = new Asset[result.Header.AssetCount];
                    for (var i = 0; i < result.Assets.Length; i++)
                    {
                        result.Assets[i] = new Asset
                        {
                            Header = AssetEntry.Parse(reader, result.Header.Version)
                        };
                    }

                    result.AssetReferences = new AssetReference[result.Header.AssetReferenceBufferSize / AssetReference.SizeInBytes];
                    for (var i = 0; i < result.AssetReferences.Length; i++)
                    {
                        result.AssetReferences[i] = AssetReference.Parse(reader);
                    }

                    foreach (var asset in result.Assets)
                    {
                        asset.AssetReferences = result.AssetReferences.AsSpan().Slice(
                            (int)asset.Header.AssetReferenceOffset / AssetReference.SizeInBytes,
                            (int)asset.Header.AssetReferenceCount);
                    }

                    {
                        var endPosition        = stream.Position + result.Header.ReferencedManifestNameBufferSize;
                        var manifestReferences = new List <ManifestReference>();

                        while (stream.Position < endPosition)
                        {
                            manifestReferences.Add(ManifestReference.Parse(reader));
                        }
                        result.ManifestReferences = manifestReferences;
                    }

                    {
                        var endPosition = stream.Position + result.Header.AssetNameBufferSize;
                        var assetNames  = ReadNameBuffer(reader, endPosition);

                        foreach (var asset in result.Assets)
                        {
                            asset.Name = assetNames[asset.Header.NameOffset];
                        }
                    }

                    {
                        var endPosition     = stream.Position + result.Header.SourceFileNameBufferSize;
                        var sourceFileNames = ReadNameBuffer(reader, endPosition);

                        foreach (var asset in result.Assets)
                        {
                            asset.SourceFileName = sourceFileNames[asset.Header.SourceFileNameOffset];
                        }
                    }

                    return(result);
                }
        }
Esempio n. 2
0
        public static ManifestFile FromFileSystemEntry(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var result = new ManifestFile
                    {
                        Header = ManifestHeader.Parse(reader)
                    };

                    if (result.Header.Version != 5)
                    {
                        throw new System.NotImplementedException();
                    }

                    result.Assets = new Asset[result.Header.AssetCount];
                    for (var i = 0; i < result.Assets.Length; i++)
                    {
                        result.Assets[i] = new Asset
                        {
                            Header = AssetEntry.Parse(reader)
                        };
                    }

                    result.AssetReferences = new AssetReference[result.Header.AssetReferenceBufferSize / AssetReference.SizeInBytes];
                    for (var i = 0; i < result.AssetReferences.Length; i++)
                    {
                        result.AssetReferences[i] = AssetReference.Parse(reader);
                    }

                    {
                        var endPosition        = stream.Position + result.Header.ReferencedManifestNameBufferSize;
                        var manifestReferences = new List <ManifestReference>();

                        while (stream.Position < endPosition)
                        {
                            manifestReferences.Add(ManifestReference.Parse(reader));
                        }
                        result.ManifestReferences = manifestReferences;
                    }

                    {
                        var endPosition = stream.Position + result.Header.AssetNameBufferSize;
                        var assetNames  = ReadNameBuffer(reader, endPosition);

                        foreach (var asset in result.Assets)
                        {
                            asset.Name = assetNames[asset.Header.NameOffset];
                        }
                    }

                    {
                        var endPosition     = stream.Position + result.Header.SourceFileNameBufferSize;
                        var sourceFileNames = ReadNameBuffer(reader, endPosition);

                        foreach (var asset in result.Assets)
                        {
                            asset.SourceFileName = sourceFileNames[asset.Header.SourceFileNameOffset];
                        }
                    }

                    var instanceDataOffset   = 4u;
                    var relocationDataOffset = 4u;
                    var importsDataOffset    = 4u;
                    foreach (var asset in result.Assets)
                    {
                        asset.InstanceDataOffset   = instanceDataOffset;
                        asset.RelocationDataOffset = relocationDataOffset;
                        asset.ImportsDataOffset    = importsDataOffset;

                        instanceDataOffset   += asset.Header.InstanceDataSize;
                        relocationDataOffset += asset.Header.RelocationDataSize;
                        importsDataOffset    += asset.Header.ImportsDataSize;
                    }

                    return(result);
                }
        }