Пример #1
0
        private bool DepthFirstImageSearch(out long offset)
        {
            bool success = false;

            offset = -1;
            int count = _r.ReadWZInt();

            for (int i = 0; i < count; i++)
            {
                byte type = _r.ReadByte();
                Logging(type.ToString());
                switch (type)
                {
                case 1:
                    _r.BaseStream.Seek(10, SeekOrigin.Current);
                    continue;

                case 2:
                    int x = _r.ReadInt32();
                    type = _r.PeekFor(() => {
                        _r.BaseStream.Seek(x + _r.ContentsStart, SeekOrigin.Begin);
                        return(_r.ReadByte());
                    });
                    break;

                case 3:
                case 4:
                    _r.ReadWZString();
                    break;

                default:
                    throw new Exception("Unknown object type in WzDirectory.");
                }

                _r.ReadWZInt();
                _r.ReadWZInt();
                offset = _r.BaseStream.Position;
                if (type == 4)
                {
                    success = true;
                    break;
                }

                if (type == 3)
                {
                    try {
                        offset = _r.PeekFor(() => {
                            _r.BaseStream.Seek(_r.ReadWZOffset(), SeekOrigin.Begin);
                            long o;
                            success = DepthFirstImageSearch(out o);
                            return(o);
                        });
                        break;
                    } catch {}
                }
                _r.BaseStream.Seek(4, SeekOrigin.Current);
            }
            return(success);
        }
Пример #2
0
        public static IEnumerable <WZProperty> DirectoryChildren(WZProperty self)
        {
            using (WZReader reader = self.FileContainer.GetContentReader(null, self))
            {
                reader.BaseStream.Seek(self.Offset, SeekOrigin.Begin);
                int          count    = reader.ReadWZInt();
                WZProperty[] children = new WZProperty[count];
                string       name     = null;
                for (int i = 0; i < count; ++i)
                {
                    byte type = reader.ReadByte();
                    switch (type)
                    {
                    case 1:
                        reader.ReadBytes(10);
                        continue;

                    case 2:
                        int dedupedAt = (int)(reader.ReadInt32() + reader.ContentsStart);
                        reader.PeekFor(() => {
                            reader.BaseStream.Position = dedupedAt;
                            type = reader.ReadByte();
                            name = reader.ReadWZString(false, self.Encrypted | self.Container.Encrypted);
                        });
                        break;

                    case 3:
                    case 4:
                        name = reader.ReadWZString(false, self.Encrypted | self.Container.Encrypted);
                        break;

                    default:
                        throw new Exception("Unknown child type");
                    }
                    if (name == null)
                    {
                        throw new InvalidOperationException("Found a property without a name, this shouldn't be possible.");
                    }

                    uint       size          = (uint)reader.ReadWZInt();
                    int        checksum      = reader.ReadWZInt();
                    uint       offset        = reader.ReadWZOffset();
                    WZProperty childProperty = new WZProperty(
                        name,
                        self != null ? Path.Combine(self.Path, name) : name,
                        reader.Package,
                        type == 3 ? PropertyType.Directory : type == 4 ? PropertyType.Image : throw new InvalidOperationException("Not sure what this is, but I don't handle it"),
                        self,
                        size,
                        checksum,
                        offset
                        );
                    // These can be lazy loaded
                    yield return(Resolve(reader.Package, childProperty));
                }
            }
        }
Пример #3
0
        public static IEnumerable <WZProperty> ImageChildren(WZProperty self)
        {
            using (WZReader reader = self.FileContainer.GetContentReader(null, self))
            {
                if (self.Offset >= reader.BaseStream.Length)
                {
                    yield break;
                }
                reader.BaseStream.Seek(self.Offset, SeekOrigin.Begin);

                if (reader.ReadByte() != 0x73)
                {
                    throw new InvalidOperationException("Image should have header of 0x73");
                }
                long oldPosition = reader.BaseStream.Position;
                if (!reader.ReadWZString(false, self.Encrypted).Equals("Property"))
                {
                    reader.BaseStream.Position = oldPosition;
                    if (!reader.ReadWZStringExpecting(out self.Encrypted, "Property", false))
                    {
                        throw new InvalidOperationException("Unknown encryption method");
                    }
                }
                if (reader.ReadInt16() != 0)
                {
                    throw new InvalidOperationException("Image needs to have 0 as part of header");
                }

                foreach (WZProperty prop in PropertyList(reader, self).ToArray())
                {
                    yield return(prop);
                }
            }
        }
Пример #4
0
        public static WZProperty Image(WZReader reader, WZProperty self)
        {
            if (self.Offset >= reader.BaseStream.Length)
            {
                return(self);
            }
            self.ContainerStartLocation = self.Offset;
            reader.Container            = self;
            byte imgType = reader.ReadByte();

            if (imgType == 1)
            {
                return(new WZPropertyWeak <string>(() =>
                {
                    using (reader = self.FileContainer.GetContentReader(null, self))
                    {
                        reader.BaseStream.Seek(self.Offset, SeekOrigin.Begin);
                        reader.ReadByte();
                        return reader.ReadLuaScript();
                    }
                }, self.NameWithoutExtension, self.Path, self.FileContainer, PropertyType.Lua, self.Parent, self.Size, self.Checksum, self.Offset));
            }
            if (imgType != 0x73)
            {
                throw new InvalidOperationException("Unknown image type, not supported!");
            }
            long oldPosition = reader.BaseStream.Position;

            if (!reader.ReadWZString(false, self.Encrypted).Equals("Property"))
            {
                reader.BaseStream.Position = oldPosition;
                if (!reader.ReadWZStringExpecting(out self.Encrypted, "Property", false))
                {
                    throw new InvalidOperationException("Unknown encryption method");
                }
            }
            if (reader.ReadInt16() != 0)
            {
                throw new InvalidOperationException("Image needs to have 0 as part of header");
            }

            return(self);
        }