Exemplo n.º 1
0
            private Boolean TryHandleSpecialEntry(WpdEntry entry)
            {
                switch (entry.NameWithoutExtension)
                {
                case StringEntryTag:
                    HandleString(entry);
                    break;

                case StrTypeListEntryTag:
                    HandleStrTypeList(entry);
                    break;

                case TypeListEntryTag:
                    HandleTypeList(entry);
                    break;

                case VersionEntryTag:
                    HandleVersion(entry);
                    break;

                default:
                    return(false);
                }

                return(true);
            }
Exemplo n.º 2
0
        public unsafe void ReadFromStream(Stream input)
        {
            if (input.Length - input.Position < 16)
                return;

            byte[] buff = input.EnsureRead(16);
            fixed (byte* b = &buff[0])
            {
                Magic = Endian.ToBigInt32(b + 0);
                Count = Endian.ToBigInt32(b + 4);
            }

            Entries = new WpdEntry[Count];
            if (Count < 1)
                return;

            buff = input.EnsureRead(Count * 32);
            fixed (byte* b = &buff[0])
            {
                for (int i = 0; i < Count; i++)
                {
                    int offset = i * 32;

                    Entries[i] = new WpdEntry(
                        i,
                        new string((sbyte*)b + offset),
                        Endian.ToBigInt32(b + offset + 16),
                        Endian.ToBigInt32(b + offset + 20),
                        new string((sbyte*)b + offset + 24));
                }
            }
        }
Exemplo n.º 3
0
            public void Deserialize()
            {
                if (_header.Entries == null)
                {
                    return;
                }

                int entryCount = Math.Max(0, _header.Entries.Length - SpecialEntriesCount);

                WdbMovieEntry[] entries = new WdbMovieEntry[entryCount];

                for (int i = 0; i < entryCount; i++)
                {
                    WpdEntry entry = _header.Entries[i + SpecialEntriesCount];
                    _input.SetPosition(entry.Offset);

                    if (entry.Length != WdbMovieEntry.StructSize)
                    {
                        throw new InvalidDataException($"[WdbMovieHeader.Deserialize] Entry: {entry.Name}, Length: {entry.Length}, Expected length: {WdbMovieEntry.StructSize}");
                    }

                    WdbMovieEntry movieEntry = _input.ReadContent <WdbMovieEntry>();
                    movieEntry.Entry       = entry;
                    movieEntry.PackageName = _header.GetString(movieEntry.PackageNameOffset);
                    entries[i]             = movieEntry;
                }

                _header.Movies = entries;
            }
Exemplo n.º 4
0
 public UiWpdInjector(WpdArchiveListing listing, WpdEntry[] leafs, bool? conversion, IUiInjectionSource source)
 {
     _listing = listing;
     _leafs = leafs;
     _source = source;
     _conversion = conversion;
     _injectors = ProvideInjectors();
     _headers = new Lazy<Stream>(AcquireHeaders);
     _content = new Lazy<Stream>(AcquireContent);
 }
Exemplo n.º 5
0
 public UiWpdExtractor(WpdArchiveListing listing, WpdEntry[] leafs, bool? conversion, IUiExtractionTarget target)
 {
     _listing = listing;
     _leafs = leafs;
     _conversion = conversion;
     _target = target;
     _extractors = ProvideExtractors(conversion);
     _headers = new Lazy<Stream>(AcquireHeaders);
     _content = new Lazy<Stream>(AcquireContent);
 }
Exemplo n.º 6
0
        public static void InjectSingle(WpdArchiveListing listing, WpdEntry entry, MemoryStream output)
        {
            using (MemoryInjectionSource source = new MemoryInjectionSource())
            {
                source.RegisterStream(String.Empty, output);
                UiWpdInjector injector = new UiWpdInjector(listing, new[] {entry}, false, source);

                UiInjectionManager manager = new UiInjectionManager();
                injector.Inject(manager);
                manager.WriteListings();
            }
        }
Exemplo n.º 7
0
            private void HandleVersion(WpdEntry entry)
            {
                if (entry.Length != 4)
                {
                    throw new InvalidDataException($"[HandleVersion] Entry: {entry.Name}, Length: {entry.Length}, (entry.Length = {entry.Length}) != 4");
                }

                _input.SetPosition(entry.Offset);
                byte[] buff = _input.EnsureRead(4);

                _header._version = Endian.ToBigUInt32(buff, 0);
            }
        public void Inject(WpdEntry entry, Stream input, Lazy<Stream> headers, Lazy<Stream> content, Byte[] buff)
        {
            int sourceSize = (int)input.Length;
            headers.Value.Position = entry.Offset;

            SectionHeader sectionHeader = headers.Value.ReadContent<SectionHeader>();
            VtexHeader textureHeader = headers.Value.ReadContent<VtexHeader>();

            byte[] unknownData = new byte[textureHeader.GtexOffset - VtexHeader.Size];
            headers.Value.Read(unknownData, 0, unknownData.Length);

            GtexData data = headers.Value.ReadContent<GtexData>();

            if (data.MipMapData.Length != 1)
                throw new NotImplementedException();

            DdsHeader ddsHeader = DdsHeaderDecoder.FromFileStream(input);
            DdsHeaderEncoder.ToGtexHeader(ddsHeader, data.Header);

            GtexMipMapLocation mipMapLocation = data.MipMapData[0];
            int dataSize = sourceSize - 128;
            if (dataSize <= mipMapLocation.Length)
            {
                content.Value.Seek(mipMapLocation.Offset, SeekOrigin.Begin);
            }
            else
            {
                content.Value.Seek(0, SeekOrigin.End);
                mipMapLocation.Offset = (int)content.Value.Position;
            }

            input.CopyToStream(content.Value, dataSize, buff);
            mipMapLocation.Length = dataSize;

            using (MemoryStream ms = new MemoryStream(180))
            {
                sectionHeader.WriteToStream(ms);
                textureHeader.WriteToStream(ms);

                ms.Write(unknownData, 0, unknownData.Length);
                data.WriteToStream(ms);

                ms.SetPosition(0);

                DefaultWpdEntryInjector defaultInjector = new DefaultWpdEntryInjector();
                defaultInjector.Inject(entry, ms, headers, content, buff);
            }
        }
Exemplo n.º 9
0
        public void Inject(WpdEntry entry, Stream input, Lazy<Stream> headers, Lazy<Stream> content, Byte[] buff)
        {
            int sourceSize = (int)input.Length;
            if (sourceSize <= entry.Length)
            {
                headers.Value.Seek(entry.Offset, SeekOrigin.Begin);
            }
            else
            {
                headers.Value.Seek(0, SeekOrigin.End);
                entry.Offset = (int)headers.Value.Position;
            }

            input.CopyToStream(headers.Value, sourceSize, buff);
            entry.Length = sourceSize;
        }
Exemplo n.º 10
0
            private void HandleTypeList(WpdEntry entry)
            {
                if (entry.Length % 4 != 0)
                {
                    throw new InvalidDataException($"[HandleTypeList] Entry: {entry.Name}, Length: {entry.Length}, (entry.Length % 4 = {entry.Length % 4}) != 0");
                }

                _input.SetPosition(entry.Offset);
                int[] data = _input.DungerousReadStructs <Int32>(entry.Length / 4);
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = Endian.SwapInt32(data[i]);
                }

                _header._typeList = data;
            }
        public void Extract(WpdEntry entry, Stream output, Lazy<Stream> headers, Lazy<Stream> content, Byte[] buff)
        {
            headers.Value.Position = entry.Offset;

            SectionHeader sectionHeader = headers.Value.ReadContent<SectionHeader>();
            TextureHeader textureHeader = headers.Value.ReadContent<TextureHeader>();
            GtexData gtex = headers.Value.ReadContent<GtexData>();

            DdsHeader header = DdsHeaderDecoder.FromGtexHeader(gtex.Header);
            DdsHeaderEncoder.ToFileStream(header, output);

            foreach (GtexMipMapLocation mipMap in gtex.MipMapData)
            {
                content.Value.Position = mipMap.Offset;
                content.Value.CopyToStream(output, mipMap.Length, buff);
            }
        }
Exemplo n.º 12
0
            public void Deserialize()
            {
                if (_header.Entries == null)
                {
                    return;
                }

                for (int i = 0, s = SpecialEntriesCount; s > 0 && i < _header.Entries.Length; i++)
                {
                    WpdEntry entry = _header.Entries[i];
                    if (entry.NameWithoutExtension.StartsWith("!!"))
                    {
                        if (TryHandleSpecialEntry(entry))
                        {
                            s--;
                            continue;
                        }

                        Log.Warning("An unexpected special entry occurred: {0}", entry.Name);
                    }
                }
            }
Exemplo n.º 13
0
        public static GLTexture ReadFromWpd(WpdArchiveListing listing, WpdEntry entry)
        {
            using (Stream headers = listing.Accessor.ExtractHeaders())
            using (Stream content = listing.Accessor.ExtractContent())
            {
                headers.SetPosition(entry.Offset);

                GtexData gtex;
                SectionHeader sectionHeader = headers.ReadContent<SectionHeader>();
                switch (sectionHeader.Type)
                {
                    case SectionType.Txb:
                        gtex = ReadGtexFromTxb(headers);
                        break;
                    case SectionType.Vtex:
                        gtex = ReadGtexFromVtex(headers);
                        break;
                    default:
                        throw new NotImplementedException();
                }

                if (gtex.Header.LayerCount == 0)
                    return null;

                int offset = 0;
                byte[] rawData = new byte[gtex.MipMapData.Sum(d => d.Length)];
                foreach (GtexMipMapLocation mimMap in gtex.MipMapData)
                {
                    using (StreamSegment textureInput = new StreamSegment(content, mimMap.Offset, mimMap.Length, FileAccess.Read))
                    {
                        textureInput.EnsureRead(rawData, offset, mimMap.Length);
                        offset += mimMap.Length;
                    }
                }

                using (GLService.AcquireContext())
                    return ImageDDS.LoadFromStream(rawData, gtex);
            }
        }
Exemplo n.º 14
0
        public virtual void WriteToStream(Stream stream)
        {
            if (Entries == null)
            {
                return;
            }

            Count = Entries.Length;

            BinaryWriter bw = new BinaryWriter(stream);

            bw.Write(MagicNumber);
            bw.WriteBig(Count);
            bw.Write(0L);

            if (Count < 1)
            {
                return;
            }

            for (int i = 0; i < Count; i++)
            {
                WpdEntry entry = Entries[i];

                byte[] bytes = Encoding.ASCII.GetBytes(entry.NameWithoutExtension);
                Array.Resize(ref bytes, 16);
                bw.Write(bytes, 0, bytes.Length);

                bw.WriteBig(entry.Offset);
                bw.WriteBig(entry.Length);

                bytes = Encoding.ASCII.GetBytes(entry.Extension);
                Array.Resize(ref bytes, 8);
                bw.Write(bytes, 0, bytes.Length);
            }
        }
Exemplo n.º 15
0
        public virtual unsafe void ReadFromStream(Stream input)
        {
            if (input.Length - input.Position < 16)
            {
                return;
            }

            byte[] buff = input.EnsureRead(16);
            fixed(byte *b = &buff[0])
            {
                Magic = Endian.ToBigInt32(b + 0);
                Count = Endian.ToBigInt32(b + 4);
            }

            Entries = new WpdEntry[Count];
            if (Count < 1)
            {
                return;
            }

            buff = input.EnsureRead(Count * 32);
            fixed(byte *b = &buff[0])
            {
                for (int i = 0; i < Count; i++)
                {
                    int offset = i * 32;

                    Entries[i] = new WpdEntry(
                        i,
                        new string((sbyte *)b + offset),
                        Endian.ToBigInt32(b + offset + 16),
                        Endian.ToBigInt32(b + offset + 20),
                        new string((sbyte *)b + offset + 24));
                }
            }
        }
Exemplo n.º 16
0
        public static DxTexture ReadFromWpd(WpdArchiveListing listing, WpdEntry entry)
        {
            using (Stream headers = listing.Accessor.ExtractHeaders())
            using (Stream content = listing.Accessor.ExtractContent())
            {
                headers.SetPosition(entry.Offset);

                GtexData gtex;
                SectionHeader sectionHeader = headers.ReadContent<SectionHeader>();
                switch (sectionHeader.Type)
                {
                    case SectionType.Txb:
                        gtex = ReadGtexFromTxb(headers);
                        break;
                    case SectionType.Vtex:
                        gtex = ReadGtexFromVtex(headers);
                        break;
                    default:
                        throw new NotImplementedException();
                }

                return LoadFromStream(gtex, content);
            }
        }
Exemplo n.º 17
0
 public UiWpdTableLeaf(string name, WpdEntry entry, WpdArchiveListing listing)
     : base(name, UiNodeType.FileTableLeaf)
 {
     Entry = entry;
     Listing = listing;
 }
Exemplo n.º 18
0
 private void HandleString(WpdEntry entry)
 {
     _input.SetPosition(entry.Offset);
     _header._stringData = _input.EnsureRead(entry.Length);
 }
Exemplo n.º 19
0
        private void Inject(WpdEntry entry, String targetPath)
        {
            string targetExtension = entry.Extension.ToLowerInvariant();

            IWpdEntryInjector injector;
            if (_injectors.TryGetValue(targetExtension, out injector))
            {
                string targetFullPath = targetPath + '.' + injector.SourceExtension;
                using (Stream input = _source.TryOpen(targetFullPath))
                {
                    if (input != null)
                    {
                        injector.Inject(entry, input, _headers, _content, _buff);
                        _injected = true;
                        return;
                    }
                }
            }

            if (_conversion != true)
            {
                string targetFullPath = targetPath + '.' + targetExtension;
                using (Stream input = _source.TryOpen(targetFullPath))
                {
                    if (input != null)
                    {
                        DefaultInjector.Inject(entry, input, _headers, _content, _buff);
                        _injected = true;
                    }
                }
            }
        }
Exemplo n.º 20
0
 public void Show(WpdArchiveListing listing, WpdEntry entry)
 {
     Visibility = Visibility.Visible;
     Texture = GLTextureReader.ReadFromWpd(listing, entry);
 }
Exemplo n.º 21
0
            private void HandleVersion(WpdEntry entry)
            {
                if (entry.Length != 4)
                    throw new InvalidDataException($"[HandleVersion] Entry: {entry.Name}, Length: {entry.Length}, (entry.Length = {entry.Length}) != 4");

                _input.SetPosition(entry.Offset);
                byte[] buff = _input.EnsureRead(4);

                _header._version = Endian.ToBigUInt32(buff, 0);
            }
Exemplo n.º 22
0
 public void Extract(WpdEntry entry, Stream output, Lazy<Stream> headers, Lazy<Stream> content, Byte[] buff)
 {
     headers.Value.Position = entry.Offset;
     headers.Value.CopyToStream(output, entry.Length, buff);
 }
Exemplo n.º 23
0
        private IWpdEntryExtractor GetExtractor(WpdEntry entry, out String targetExtension)
        {
            targetExtension = entry.Extension.ToLowerInvariant();

            IWpdEntryExtractor result;
            if (_extractors.TryGetValue(targetExtension, out result))
                targetExtension = result.TargetExtension;
            else if (_conversion != true)
                result = DefaultExtractor;

            return result;
        }
Exemplo n.º 24
0
            private Boolean TryHandleSpecialEntry(WpdEntry entry)
            {
                switch (entry.NameWithoutExtension)
                {
                    case StringEntryTag:
                        HandleString(entry);
                        break;
                    case StrTypeListEntryTag:
                        HandleStrTypeList(entry);
                        break;
                    case TypeListEntryTag:
                        HandleTypeList(entry);
                        break;
                    case VersionEntryTag:
                        HandleVersion(entry);
                        break;
                    default:
                        return false;
                }

                return true;
            }
Exemplo n.º 25
0
            private void HandleStrTypeList(WpdEntry entry)
            {
                if (entry.Length % 4 != 0)
                    throw new InvalidDataException($"[HandleStrTypeList] Entry: {entry.Name}, Length: {entry.Length}, (entry.Length % 4 = {entry.Length % 4}) != 0");

                _input.SetPosition(entry.Offset);
                int[] data = _input.DungerousReadStructs<Int32>(entry.Length / 4);
                for (int i = 0; i < data.Length; i++)
                    data[i] = Endian.SwapInt32(data[i]);

                _header._strTypeList = data;
            }
Exemplo n.º 26
0
 private void HandleString(WpdEntry entry)
 {
     _input.SetPosition(entry.Offset);
     _header._stringData = _input.EnsureRead(entry.Length);
 }