Example #1
0
        private List <BFF.StaticData> getStaticData(List <string> files)
        {
            byte[]                buffer         = new byte[4];
            FileStream            fileStream     = new FileStream(this._files.OriginalArchive, FileMode.Open);
            List <BFF.StaticData> staticDataList = new List <BFF.StaticData>();

            for (int index = 0; index < files.Count; ++index)
            {
                FileInfo       fileInfo   = new FileInfo(string.Format("{0}\\{1}", (object)this._files.SourceFolder, (object)files[index]));
                BFF.StaticData staticData = new BFF.StaticData();
                staticData.fileSize = (int)fileInfo.Length;
                fileStream.Seek(304L + (long)(index * 42), SeekOrigin.Begin);
                fileStream.Read(buffer, 0, 4);
                staticData.dummy1 = BitConverter.ToUInt32(buffer, 0);
                fileStream.Read(buffer, 0, 4);
                staticData.dummy2 = BitConverter.ToUInt32(buffer, 0);
                fileStream.Read(buffer, 0, 4);
                fileStream.Read(buffer, 0, 4);
                staticData.everzero = BitConverter.ToInt32(buffer, 0);
                fileStream.Read(buffer, 0, 4);
                staticData.compressedSize = BitConverter.ToInt32(buffer, 0);
                fileStream.Read(buffer, 0, 4);
                staticData.fileSize = BitConverter.ToInt32(buffer, 0);
                fileStream.Read(buffer, 0, 4);
                staticData.everzero2 = BitConverter.ToInt32(buffer, 0);
                fileStream.Read(buffer, 0, 4);
                staticData.everzero3 = BitConverter.ToInt32(buffer, 0);
                staticDataList.Add(staticData);
            }
            fileStream.Close();
            return(staticDataList);
        }
Example #2
0
        public void Build()
        {
            this.StopProcessing.Reset();
            EventData     eventData = new EventData();
            List <string> fileList  = this.getFileList();

            if (this.RebuildStarted != null)
            {
                eventData.TotalFiles = fileList.Count;
                this.RebuildStarted((object)eventData, EventArgs.Empty);
            }
            Header header = new Header();

            header.sign = new byte[4]
            {
                (byte)32,
                (byte)75,
                (byte)65,
                (byte)80
            };
            header.sign2 = new byte[4]
            {
                (byte)3,
                (byte)0,
                (byte)64,
                (byte)16
            };
            header.sign3 = new byte[5]
            {
                (byte)32,
                (byte)84,
                (byte)88,
                (byte)69,
                (byte)84
            };
            header.filename = new byte[256];
            header.gecko    = new byte[256];
            header.filepath = new byte[256];
            header.pc       = new byte[256];
            int[] fromOriginalArchive = this.getStaticDataFromOriginalArchive();
            header.align        = fromOriginalArchive[0];
            header.field_offset = fromOriginalArchive[1];
            header.field_size   = fromOriginalArchive[2];
            header.offset       = header.align;
            List <BFF.StaticData> staticData1 = this.getStaticData(fileList);

            header.files = fileList.Count;
            string name = new FileInfo(this._files.OutputArchive).Name;

            Encoding.Default.GetBytes(name.Substring(0, name.Length - 4)).CopyTo((Array)header.filename, 0);
            Encoding.Default.GetBytes("Gecko.xml").CopyTo((Array)header.gecko, 0);
            Encoding.Default.GetBytes(this._files.SourceFolder).CopyTo((Array)header.filepath, 0);
            Encoding.Default.GetBytes("PC").CopyTo((Array)header.pc, 0);
            InfoTable[] infoTableArray = new InfoTable[fileList.Count];
            for (int index = 0; index < fileList.Count; ++index)
            {
                BFF.StaticData staticData2 = staticData1[index];
                infoTableArray[index].dummy1    = staticData2.dummy1;
                infoTableArray[index].dummy2    = staticData2.dummy2;
                infoTableArray[index].offset    = header.align;
                infoTableArray[index].type      = (short)2;
                infoTableArray[index].everzero  = staticData2.everzero;
                infoTableArray[index].everzero2 = staticData2.everzero2;
                infoTableArray[index].everzero3 = staticData2.everzero3;
                infoTableArray[index].size      = staticData2.fileSize;
                infoTableArray[index].zsize     = staticData2.fileSize;
            }
            header.infotable             = new byte[42 * fileList.Count];
            header.x118_sizeof_infotable = 42 * fileList.Count;
            FileStream fileStream1 = new FileStream(this._files.OriginalArchive, FileMode.Open);

            fileStream1.Read(header.infotable, 0, header.x118_sizeof_infotable);
            byte[] buffer1 = new byte[header.field_size];
            fileStream1.Seek((long)header.field_offset, SeekOrigin.Begin);
            fileStream1.Read(buffer1, 0, header.field_size);
            fileStream1.Close();
            for (int index = 0; index < fileList.Count; ++index)
            {
                string str = string.Format("{0}\\{1}", (object)this._files.SourceFolder, (object)fileList[index]);
                string s   = new FileInfo(str).Extension.Substring(1);
                if (s.Length > 3)
                {
                    s = s.Substring(0, 3);
                }
                byte[] bytes = Encoding.Default.GetBytes(s);
                infoTableArray[index].extension = new byte[4];
                bytes.CopyTo((Array)infoTableArray[index].extension, 0);
                FileStream fileStream2 = new FileStream(str, FileMode.Open);
                byte[]     buffer2     = new byte[(int)fileStream2.Length];
                fileStream2.Read(buffer2, 0, (int)fileStream2.Length);
                fileStream2.Close();
                infoTableArray[index].size  = buffer2.Length;
                infoTableArray[index].zsize = buffer2.Length;
            }
            IntPtr num1 = Marshal.AllocHGlobal(header.x118_sizeof_infotable);

            for (int index = 0; index < fileList.Count; ++index)
            {
                Marshal.StructureToPtr((object)infoTableArray[index], new IntPtr(num1.ToInt64() + (long)(42 * index)), false);
            }
            Marshal.Copy(num1, header.infotable, 0, header.infotable.Length);
            Marshal.FreeHGlobal(num1);
            header.x120_sizeof_nametable_plus_308h = 16 * fileList.Count;
            for (int index = 0; index < fileList.Count; ++index)
            {
                header.x120_sizeof_nametable_plus_308h += fileList[index].Length + 1;
            }
            byte[]     buffer3     = new byte[4];
            FileStream fileStream3 = new FileStream(this._files.OutputArchive, FileMode.Create);

            fileStream3.Seek(0L, SeekOrigin.Begin);
            fileStream3.Write(header.sign, 0, 4);
            fileStream3.Write(header.sign2, 0, 4);
            fileStream3.Write(BitConverter.GetBytes(header.files), 0, 4);
            fileStream3.Write(BitConverter.GetBytes(header.align), 0, 4);
            fileStream3.Seek(4L, SeekOrigin.Current);
            fileStream3.Write(BitConverter.GetBytes(2048), 0, 4);
            fileStream3.Write(header.filename, 0, header.filename.Length);
            fileStream3.Write(BitConverter.GetBytes(header.x118_sizeof_infotable), 0, 4);
            fileStream3.Seek(4L, SeekOrigin.Current);
            fileStream3.Write(BitConverter.GetBytes(header.x120_sizeof_nametable_plus_308h + 776), 0, 4);
            fileStream3.Write(BitConverter.GetBytes(header.field_offset), 0, 4);
            fileStream3.Write(BitConverter.GetBytes(header.field_size), 0, 4);
            fileStream3.Write(buffer3, 0, 1);
            fileStream3.Write(buffer3, 0, 1);
            fileStream3.Write(buffer3, 0, 1);
            fileStream3.Write(buffer3, 0, 1);
            int position1 = (int)fileStream3.Position;

            fileStream3.Write(header.infotable, 0, header.infotable.Length);
            fileStream3.Write(header.sign3, 0, 5);
            fileStream3.Write(buffer3, 0, 3);
            fileStream3.Write(header.gecko, 0, header.gecko.Length);
            fileStream3.Write(header.filepath, 0, header.filepath.Length);
            fileStream3.Write(header.pc, 0, header.pc.Length);
            long position2         = fileStream3.Position;
            int  count             = fileList.Count;
            int  nametablePlus308h = header.x120_sizeof_nametable_plus_308h;

            byte[]     buffer4     = new byte[nametablePlus308h];
            FileStream fileStream4 = new FileStream(this._files.OriginalArchive, FileMode.Open);

            fileStream4.Seek(fileStream3.Position, SeekOrigin.Begin);
            fileStream4.Read(buffer4, 0, nametablePlus308h);
            fileStream4.Close();
            fileStream3.Write(buffer4, 0, nametablePlus308h);
            fileStream3.Write(buffer1, 0, buffer1.Length);
            fileStream3.Seek((long)header.offset, SeekOrigin.Begin);
            IntPtr pContext = IntPtr.Zero;

            BFF.My_XMemCreateCompressionContext(1, IntPtr.Zero, (ushort)0, out pContext);
            for (int index = 0; index < fileList.Count && !this.StopProcessing.WaitOne(1); ++index)
            {
                if (this.FileProcessing != null)
                {
                    eventData.CurrentFile = index;
                    eventData.Filename    = fileList[index];
                    eventData.FileSize    = infoTableArray[index].size;
                    this.FileProcessing((object)eventData, EventArgs.Empty);
                }
                FileStream fileStream2 = new FileStream(string.Format("{0}\\{1}", (object)this._files.SourceFolder, (object)fileList[index]), FileMode.Open);
                int        length      = (int)fileStream2.Length;
                byte[]     numArray1   = new byte[length];
                fileStream2.Read(numArray1, 0, length);
                fileStream2.Close();
                int    position3 = (int)fileStream3.Position;
                int    pDestSize = length;
                byte[] numArray2 = new byte[pDestSize];
                bool   flag      = false;
                byte[] buffer2   = new byte[1];
                switch (BFF.My_XMemCompress(pContext, numArray2, out pDestSize, numArray1, length))
                {
                case -2116149247:
                    if (!flag)
                    {
                        fileStream3.Write(numArray1, 0, length);
                        pDestSize  = length;
                        buffer2[0] = (byte)0;
                    }
                    else
                    {
                        buffer2[0] = (byte)2;
                        fileStream3.Write(numArray2, 0, pDestSize);
                    }
                    int position4 = (int)fileStream3.Position;
                    fileStream3.Seek((long)(position1 + 16 + 42 * index), SeekOrigin.Begin);
                    fileStream3.Write(BitConverter.GetBytes(pDestSize), 0, 4);
                    uint num2 = !flag?this._getCRC(numArray1, length) : this._getCRC(numArray2, pDestSize);

                    fileStream3.Seek(14L, SeekOrigin.Current);
                    fileStream3.Write(BitConverter.GetBytes(num2), 0, 4);
                    fileStream3.Seek(-6L, SeekOrigin.Current);
                    byte[] buffer5 = new byte[1];
                    this.Assert(fileStream3.Read(buffer5, 0, 1), 1);
                    fileStream3.Seek(-1L, SeekOrigin.Current);
                    fileStream3.Write(buffer2, 0, 1);
                    if (index >= 0)
                    {
                        fileStream3.Seek((long)(position1 + 8 + 42 * index), SeekOrigin.Begin);
                        fileStream3.Write(BitConverter.GetBytes(position3), 0, 4);
                        fileStream3.Seek((long)position4, SeekOrigin.Begin);
                    }
                    int position5 = (int)fileStream3.Position;
                    int num3      = 2048 - position5 % 2048;
                    if (num3 == 0)
                    {
                        num3 = 2048;
                    }
                    int num4 = position5 + num3;
                    fileStream3.Seek((long)num4, SeekOrigin.Begin);
                    continue;

                case 0:
                    flag = true;
                    goto case -2116149247;

                default:
                    throw new Exception("Cannot compress file. Aborting");
                }
            }
            fileStream3.Close();
            this.Result = !this.StopProcessing.WaitOne(1) ? string.Format("{0} file(s) added to {1}", (object)fileList.Count, (object)this._files.OutputArchive) : "Aborted by user";
            if (this.RebuildEnded == null)
            {
                return;
            }
            this.RebuildEnded((object)null, EventArgs.Empty);
        }