예제 #1
0
        public FileIndex(string idxFile, string mulFile, string uopFile, int length, int file, string uopEntryExtension, int idxLength, bool hasExtra)
        {
            Index = new Entry3D[length];

            string idxPath = null;
            MulPath = null;
            string uopPath = null;

            if (Files.MulPath == null)
                Files.LoadMulPath();

            if (Files.MulPath.Count > 0)
            {
                idxPath = Files.MulPath[idxFile.ToLower()];
                MulPath = Files.MulPath[mulFile.ToLower()];

                if (!String.IsNullOrEmpty(uopFile) && Files.MulPath.ContainsKey(uopFile.ToLower()))
                    uopPath = Files.MulPath[uopFile.ToLower()];

                if (String.IsNullOrEmpty(idxPath))
                {
                    idxPath = null;
                }
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(idxPath)))
                        idxPath = Path.Combine(Files.RootDir, idxPath);

                    if (!File.Exists(idxPath))
                        idxPath = null;
                }

                if (String.IsNullOrEmpty(MulPath))
                {
                    MulPath = null;
                }
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(MulPath)))
                        MulPath = Path.Combine(Files.RootDir, MulPath);

                    if (!File.Exists(MulPath))
                        MulPath = null;
                }

                if (String.IsNullOrEmpty(uopPath))
                {
                    uopPath = null;
                }
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(uopPath)))
                        uopPath = Path.Combine(Files.RootDir, uopPath);

                    if (!File.Exists(uopPath))
                        uopPath = null;
                    else
                        MulPath = uopPath;
                }
            }

            /* UOP files support code, written by Wyatt (c) www.ruosi.org
             * idxLength variable was added for compatibility with legacy code for art (see art.cs)
             * At the moment the only UOP file having entries with extra field is gumpartlegacy.uop,
             * and it's two dwords in the beginning of the entry.
             * It's possible that UOP can include some entries with unknown hash: not really unknown for me, but
             * not useful for reading legacy entries. That's why i removed unknown hash exception throwing from this code
             */
            if (MulPath != null && MulPath.EndsWith(".uop"))
            {
                using (FileStream index = new FileStream(MulPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    FileInfo fi = new FileInfo(MulPath);
                    string uopPattern = fi.Name.Replace(fi.Extension, "").ToLowerInvariant();

                    using (BinaryReader br = new BinaryReader(Stream))
                    {
                        br.BaseStream.Seek(0, SeekOrigin.Begin);

                        if (br.ReadInt32() != 0x50594D)
                            throw new ArgumentException("Bad UOP file.");

                        br.ReadInt64(); // version + signature
                        long nextBlock = br.ReadInt64();
                        br.ReadInt32(); // block capacity
                        int count = br.ReadInt32();

                        if (idxLength > 0)
                            IdxLength = idxLength * 12;

                        Dictionary<ulong, int> hashes = new Dictionary<ulong, int>();

                        for (int i = 0; i < length; i++)
                        {
                            string entryName = string.Format("build/{0}/{1:D8}{2}", uopPattern, i, uopEntryExtension);
                            ulong hash = HashFileName(entryName);

                            if (!hashes.ContainsKey(hash))
                                hashes.Add(hash, i);
                        }

                        br.BaseStream.Seek(nextBlock, SeekOrigin.Begin);

                        do
                        {
                            int filesCount = br.ReadInt32();
                            nextBlock = br.ReadInt64();

                            for (int i = 0; i < filesCount; i++)
                            {
                                long offset = br.ReadInt64();
                                int headerLength = br.ReadInt32();
                                int compressedLength = br.ReadInt32();
                                int decompressedLength = br.ReadInt32();
                                ulong hash = br.ReadUInt64();
                                br.ReadUInt32(); // Adler32
                                short flag = br.ReadInt16();

                                int entryLength = flag == 1 ? compressedLength : decompressedLength;

                                if (offset == 0)
                                    continue;

                                int idx;
                                if (hashes.TryGetValue(hash, out idx))
                                {
                                    if (idx < 0 || idx > Index.Length)
                                        throw new IndexOutOfRangeException("hashes dictionary and files collection have different count of entries!");

                                    Index[idx].lookup = (int)(offset + headerLength);
                                    Index[idx].length = entryLength;

                                    if (hasExtra)
                                    {
                                        long curPos = br.BaseStream.Position;

                                        br.BaseStream.Seek(offset + headerLength, SeekOrigin.Begin);

                                        byte[] extra = br.ReadBytes(8);

                                        short extra1 = (short)((extra[3] << 24) | (extra[2] << 16) | (extra[1] << 8) | extra[0]);
                                        short extra2 = (short)((extra[7] << 24) | (extra[6] << 16) | (extra[5] << 8) | extra[4]);

                                        Index[idx].lookup += 8;
                                        Index[idx].extra = extra1 << 16 | extra2;

                                        br.BaseStream.Seek(curPos, SeekOrigin.Begin);
                                    }
                                }
                            }
                        }
                        while (br.BaseStream.Seek(nextBlock, SeekOrigin.Begin) != 0);
                    }
                }
            }
            else if ((idxPath != null) && (MulPath != null))
            {
                using (FileStream index = new FileStream(idxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    int count = (int)(index.Length / 12);
                    IdxLength = index.Length;
                    GCHandle gc = GCHandle.Alloc(Index, GCHandleType.Pinned);
                    byte[] buffer = new byte[index.Length];
                    index.Read(buffer, 0, (int)index.Length);
                    Marshal.Copy(buffer, 0, gc.AddrOfPinnedObject(), (int)Math.Min(IdxLength, length * 12));
                    gc.Free();
                    for (int i = count; i < length; ++i)
                    {
                        Index[i].lookup = -1;
                        Index[i].length = -1;
                        Index[i].extra = -1;
                    }
                }
            }
            else
            {
                Stream = null;
                return;
            }

            Entry5D[] patches = Verdata.Patches;

            if (file > -1)
            {
                for (int i = 0; i < patches.Length; ++i)
                {
                    Entry5D patch = patches[i];

                    if (patch.file == file && patch.index >= 0 && patch.index < length)
                    {
                        Index[patch.index].lookup = patch.lookup;
                        Index[patch.index].length = patch.length | (1 << 31);
                        Index[patch.index].extra = patch.extra;
                    }
                }
            }
        }
예제 #2
0
        public static void LoadFromVD(int filetype, int body, BinaryReader bin)
        {
            AnimIdx[] cache = GetCache(filetype);
            FileIndex fileIndex;
            int index;
            GetFileIndex(body, filetype, 0, 0, out fileIndex, out index);
            int animlength = Animations.GetAnimLength(body, filetype) * 5;
            Entry3D[] entries = new Entry3D[animlength];

            for (int i = 0; i < animlength; ++i)
            {
                entries[i].lookup = bin.ReadInt32();
                entries[i].length = bin.ReadInt32();
                entries[i].extra = bin.ReadInt32();
            }
            foreach (Entry3D entry in entries)
            {
                if ((entry.lookup > 0) && (entry.lookup < bin.BaseStream.Length) && (entry.length > 0))
                {
                    bin.BaseStream.Seek(entry.lookup, SeekOrigin.Begin);
                    cache[index] = new AnimIdx(bin, entry.extra);
                }
                ++index;
            }
        }
예제 #3
0
        public FileIndex(string idxFile, string mulFile, int file)
        {
            string idxPath = null;
            MulPath = null;
            if (Files.MulPath == null)
                Files.LoadMulPath();
            if (Files.MulPath.Count > 0)
            {
                idxPath = Files.MulPath[idxFile.ToLower()];
                MulPath = Files.MulPath[mulFile.ToLower()];
                if (String.IsNullOrEmpty(idxPath))
                    idxPath = null;
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(idxPath)))
                        idxPath = Path.Combine(Files.RootDir, idxPath);
                    if (!File.Exists(idxPath))
                        idxPath = null;
                }
                if (String.IsNullOrEmpty(MulPath))
                    MulPath = null;
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(MulPath)))
                        MulPath = Path.Combine(Files.RootDir, MulPath);
                    if (!File.Exists(MulPath))
                        MulPath = null;
                }
            }

            if ((idxPath != null) && (MulPath != null))
            {
                using (FileStream index = new FileStream(idxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    int count = (int)(index.Length / 12);
                    IdxLength = index.Length;
                    Index = new Entry3D[count];
                    GCHandle gc = GCHandle.Alloc(Index, GCHandleType.Pinned);
                    byte[] buffer = new byte[index.Length];
                    index.Read(buffer, 0, (int)index.Length);
                    Marshal.Copy(buffer, 0, gc.AddrOfPinnedObject(), (int)index.Length);
                    gc.Free();
                }
            }
            else
            {
                Stream = null;
                Index = new Entry3D[1];
                return;
            }
            Entry5D[] patches = Verdata.Patches;

            if (file > -1)
            {
                for (int i = 0; i < patches.Length; ++i)
                {
                    Entry5D patch = patches[i];

                    if (patch.file == file && patch.index >= 0 && patch.index < Index.Length)
                    {
                        Index[patch.index].lookup = patch.lookup;
                        Index[patch.index].length = patch.length | (1 << 31);
                        Index[patch.index].extra = patch.extra;
                    }
                }
            }
        }
예제 #4
0
        public Stream Seek(int index, out int length, out int extra, out bool patched)
        {
            if (index < 0 || index >= Index.Length)
            {
                length  = extra = 0;
                patched = false;
                return(null);
            }

            Entry3D e = Index[index];

            if (e.lookup < 0)
            {
                length  = extra = 0;
                patched = false;
                return(null);
            }

            length = e.length & 0x7FFFFFFF;
            extra  = e.extra;

            if ((e.length & (1 << 31)) != 0)
            {
                patched = true;
                _Verdata.Seek(e.lookup);
                return(_Verdata.Stream);
            }

            if (e.length < 0)
            {
                length  = extra = 0;
                patched = false;
                return(null);
            }

            if ((Stream == null) || (!Stream.CanRead) || (!Stream.CanSeek))
            {
                if (MulPath == null)
                {
                    Stream = null;
                }
                else
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                }
            }

            if (Stream == null)
            {
                length  = extra = 0;
                patched = false;
                return(null);
            }
            else if (Stream.Length < e.lookup)
            {
                length  = extra = 0;
                patched = false;
                return(null);
            }

            patched = false;

            Stream.Seek(e.lookup, SeekOrigin.Begin);
            return(Stream);
        }
예제 #5
0
        public FileIndex(
            string idxFile,
            string mulFile,
            string uopFile,
            int length,
            int file,
            string uopEntryExtension,
            int idxLength,
            bool hasExtra, Verdata verdata, Files files)
        {
            _files        = files;
            this._Verdata = verdata;
            Index         = new Entry3D[length];

            string idxPath = null;

            MulPath = null;
            string uopPath = null;

            if (_files.MulPath == null)
            {
                _files.LoadMulPath();
            }

            if (_files.MulPath.Count > 0)
            {
                idxPath = _files.MulPath[idxFile.ToLower()];
                MulPath = _files.MulPath[mulFile.ToLower()];

                if (!String.IsNullOrEmpty(uopFile) && _files.MulPath.ContainsKey(uopFile.ToLower()))
                {
                    uopPath = _files.MulPath[uopFile.ToLower()];
                }

                if (String.IsNullOrEmpty(idxPath))
                {
                    idxPath = null;
                }
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(idxPath)))
                    {
                        idxPath = Path.Combine(_files.RootDir, idxPath);
                    }

                    if (!File.Exists(idxPath))
                    {
                        idxPath = null;
                    }
                }

                if (String.IsNullOrEmpty(MulPath))
                {
                    MulPath = null;
                }
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(MulPath)))
                    {
                        MulPath = Path.Combine(_files.RootDir, MulPath);
                    }

                    if (!File.Exists(MulPath))
                    {
                        MulPath = null;
                    }
                }

                if (String.IsNullOrEmpty(uopPath))
                {
                    uopPath = null;
                }
                else
                {
                    if (String.IsNullOrEmpty(Path.GetDirectoryName(uopPath)))
                    {
                        uopPath = Path.Combine(_files.RootDir, uopPath);
                    }

                    if (!File.Exists(uopPath))
                    {
                        uopPath = null;
                    }
                    else
                    {
                        MulPath = uopPath;
                    }
                }
            }

            /* UOP files support code, written by Wyatt (c) www.ruosi.org
             * idxLength variable was added for compatibility with legacy code for art (see art.cs)
             * At the moment the only UOP file having entries with extra field is gumpartlegacy.uop,
             * and it's two dwords in the beginning of the entry.
             * It's possible that UOP can include some entries with unknown hash: not really unknown for me, but
             * not useful for reading legacy entries. That's why i removed unknown hash exception throwing from this code
             */
            if (MulPath != null && MulPath.EndsWith(".uop"))
            {
                using (var index = new FileStream(MulPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                    var    fi         = new FileInfo(MulPath);
                    string uopPattern = fi.Name.Replace(fi.Extension, "").ToLowerInvariant();

                    using (var br = new BinaryReader(Stream))
                    {
                        br.BaseStream.Seek(0, SeekOrigin.Begin);

                        if (br.ReadInt32() != 0x50594D)
                        {
                            throw new ArgumentException("Bad UOP file.");
                        }

                        br.ReadInt64();                         // version + signature
                        long nextBlock = br.ReadInt64();
                        br.ReadInt32();                         // block capacity
                        int count = br.ReadInt32();

                        if (idxLength > 0)
                        {
                            IdxLength = idxLength * 12;
                        }

                        var hashes = new Dictionary <ulong, int>();

                        for (int i = 0; i < length; i++)
                        {
                            string entryName = string.Format("build/{0}/{1:D8}{2}", uopPattern, i, uopEntryExtension);
                            ulong  hash      = HashFileName(entryName);

                            if (!hashes.ContainsKey(hash))
                            {
                                hashes.Add(hash, i);
                            }
                        }

                        br.BaseStream.Seek(nextBlock, SeekOrigin.Begin);

                        do
                        {
                            int filesCount = br.ReadInt32();
                            nextBlock = br.ReadInt64();

                            for (int i = 0; i < filesCount; i++)
                            {
                                long  offset             = br.ReadInt64();
                                int   headerLength       = br.ReadInt32();
                                int   compressedLength   = br.ReadInt32();
                                int   decompressedLength = br.ReadInt32();
                                ulong hash = br.ReadUInt64();
                                br.ReadUInt32();                                 // Adler32
                                short flag = br.ReadInt16();

                                int entryLength = flag == 1 ? compressedLength : decompressedLength;

                                if (offset == 0)
                                {
                                    continue;
                                }

                                int idx;
                                if (hashes.TryGetValue(hash, out idx))
                                {
                                    if (idx < 0 || idx > Index.Length)
                                    {
                                        throw new IndexOutOfRangeException("hashes dictionary and files collection have different count of entries!");
                                    }

                                    Index[idx].lookup = (int)(offset + headerLength);
                                    Index[idx].length = entryLength;

                                    if (hasExtra)
                                    {
                                        long curPos = br.BaseStream.Position;

                                        br.BaseStream.Seek(offset + headerLength, SeekOrigin.Begin);

                                        byte[] extra = br.ReadBytes(8);

                                        var extra1 = (ushort)((extra[3] << 24) | (extra[2] << 16) | (extra[1] << 8) | extra[0]);
                                        var extra2 = (ushort)((extra[7] << 24) | (extra[6] << 16) | (extra[5] << 8) | extra[4]);

                                        Index[idx].lookup += 8;
                                        Index[idx].extra   = extra1 << 16 | extra2;

                                        br.BaseStream.Seek(curPos, SeekOrigin.Begin);
                                    }
                                }
                            }
                        }while (br.BaseStream.Seek(nextBlock, SeekOrigin.Begin) != 0);
                    }
                }
            }
            else if ((idxPath != null) && (MulPath != null))
            {
                using (var index = new FileStream(idxPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                    var count = (int)(index.Length / 12);
                    IdxLength = index.Length;
                    GCHandle gc     = GCHandle.Alloc(Index, GCHandleType.Pinned);
                    var      buffer = new byte[index.Length];
                    index.Read(buffer, 0, (int)index.Length);
                    Marshal.Copy(buffer, 0, gc.AddrOfPinnedObject(), (int)Math.Min(IdxLength, length * 12));
                    gc.Free();
                    for (int i = count; i < length; ++i)
                    {
                        Index[i].lookup = -1;
                        Index[i].length = -1;
                        Index[i].extra  = -1;
                    }
                }
            }
            else
            {
                Stream = null;
                return;
            }

            Entry5D[] patches = _Verdata.Patches;

            if (file > -1)
            {
                for (int i = 0; i < patches.Length; ++i)
                {
                    Entry5D patch = patches[i];

                    if (patch.file == file && patch.index >= 0 && patch.index < length)
                    {
                        Index[patch.index].lookup = patch.lookup;
                        Index[patch.index].length = patch.length | (1 << 31);
                        Index[patch.index].extra  = patch.extra;
                    }
                }
            }
        }
예제 #6
0
        public FileIndex(string idxFile, string mulFile, int file)
        {
            string idxPath = null;

            MulPath = null;
            if (Files.MulPath == null)
            {
                Files.LoadMulPath();
            }
            if (Files.MulPath.Count > 0)
            {
                idxPath = Files.MulPath[idxFile.ToLower()];
                MulPath = Files.MulPath[mulFile.ToLower()];
                if (string.IsNullOrEmpty(idxPath))
                {
                    idxPath = null;
                }
                else
                {
                    if (string.IsNullOrEmpty(Path.GetDirectoryName(idxPath)))
                    {
                        idxPath = Path.Combine(Files.RootDir, idxPath);
                    }
                    if (!File.Exists(idxPath))
                    {
                        idxPath = null;
                    }
                }
                if (string.IsNullOrEmpty(MulPath))
                {
                    MulPath = null;
                }
                else
                {
                    if (string.IsNullOrEmpty(Path.GetDirectoryName(MulPath)))
                    {
                        MulPath = Path.Combine(Files.RootDir, MulPath);
                    }
                    if (!File.Exists(MulPath))
                    {
                        MulPath = null;
                    }
                }
            }

            if ((idxPath != null) && (MulPath != null))
            {
                using (var index = new FileStream(idxPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    Stream = new FileStream(MulPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                    var count = (int)(index.Length / 12);
                    IdxLength = index.Length;
                    Index     = new Entry3D[count];
                    GCHandle gc     = GCHandle.Alloc(Index, GCHandleType.Pinned);
                    var      buffer = new byte[index.Length];
                    index.Read(buffer, 0, (int)index.Length);
                    Marshal.Copy(buffer, 0, gc.AddrOfPinnedObject(), (int)index.Length);
                    gc.Free();
                }
            }
            else
            {
                Stream = null;
                Index  = new Entry3D[1];
                return;
            }
            Entry5D[] patches = Verdata.Patches;

            if (file > -1)
            {
                for (int i = 0; i < patches.Length; ++i)
                {
                    Entry5D patch = patches[i];

                    if (patch.file == file && patch.index >= 0 && patch.index < Index.Length)
                    {
                        Index[patch.index].lookup = patch.lookup;
                        Index[patch.index].length = patch.length | (1 << 31);
                        Index[patch.index].extra  = patch.extra;
                    }
                }
            }
        }
예제 #7
0
        public FileIndex(string idxFile, string mulFile, int file)
        {
            string idxPath = null;

            _mulPath = null;

            if (Files.MulPath == null)
            {
                Files.LoadMulPath();
            }

            if (Files.MulPath.Count > 0)
            {
                idxPath  = Files.MulPath[idxFile.ToLower()];
                _mulPath = Files.MulPath[mulFile.ToLower()];
                if (string.IsNullOrEmpty(idxPath))
                {
                    idxPath = null;
                }
                else
                {
                    if (string.IsNullOrEmpty(Path.GetDirectoryName(idxPath)))
                    {
                        idxPath = Path.Combine(Files.RootDir, idxPath);
                    }

                    if (!File.Exists(idxPath))
                    {
                        idxPath = null;
                    }
                }

                if (string.IsNullOrEmpty(_mulPath))
                {
                    _mulPath = null;
                }
                else
                {
                    if (string.IsNullOrEmpty(Path.GetDirectoryName(_mulPath)))
                    {
                        _mulPath = Path.Combine(Files.RootDir, _mulPath);
                    }

                    if (!File.Exists(_mulPath))
                    {
                        _mulPath = null;
                    }
                }
            }

            if ((idxPath != null) && (_mulPath != null))
            {
                using (var index = new FileStream(idxPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    Stream = new FileStream(_mulPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    var count = (int)(index.Length / 12);
                    IdxLength = index.Length;
                    Index     = new Entry3D[count];
                    GCHandle gc     = GCHandle.Alloc(Index, GCHandleType.Pinned);
                    var      buffer = new byte[index.Length];
                    index.Read(buffer, 0, (int)index.Length);
                    Marshal.Copy(buffer, 0, gc.AddrOfPinnedObject(), (int)index.Length);
                    gc.Free();
                }
            }
            else
            {
                Stream = null;
                Index  = new Entry3D[1];
                return;
            }

            if (file <= -1)
            {
                return;
            }

            Entry5D[] verdataPatches = Verdata.Patches;
            foreach (var patch in verdataPatches)
            {
                if (patch.File != file || patch.Index < 0 || patch.Index >= Index.Length)
                {
                    continue;
                }

                Index[patch.Index].Lookup = patch.Lookup;
                Index[patch.Index].Length = patch.Length | (1 << 31);
                Index[patch.Index].Extra  = patch.Extra;
            }
        }