public void RepackFolder(string path, bool newFormat) { newUnpack = newFormat; string[] files = System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories); string folder = System.IO.Path.GetDirectoryName(path); string file = folder + "\\" + System.IO.Path.GetFileName(path) + ".dat"; NameOfFile = file.ToLower(); string tmpFile = folder + "\\" + System.IO.Path.GetFileName(path) + ".tmp"; System.IO.FileStream fsTmp = new System.IO.FileStream(tmpFile, System.IO.FileMode.Create); System.IO.FileStream fs2 = new System.IO.FileStream(file, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs2); int offset = 0; ArchiveHeader header = new ArchiveHeader(); foreach (string i in files) { byte[] buffer = PackFile(i, out int uncompressedSize, out int compressedSize); FileHeader f = new FileHeader() { Name = i.Replace(path + "\\", ""), Offset = offset, Size = uncompressedSize, EncryptedSize = buffer.Length, CompressedSize = compressedSize }; fsTmp.Write(buffer, 0, buffer.Length); header.Files.Add(f.Name, f); offset += buffer.Length; } System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.IO.BinaryWriter bw2 = new System.IO.BinaryWriter(ms); foreach (FileHeader i in header.Files.Values) { bw2.Write(i.Name.Length); bw2.Write(Encoding.Unicode.GetBytes(i.Name)); ms.Position++; bw2.Write((byte)1); bw2.Write((byte)1); ms.Position++; bw2.Write(i.Size); bw2.Write(i.CompressedSize); bw2.Write(i.EncryptedSize); bw2.Write(i.Offset); ms.Position += 60; } fsTmp.Flush(); byte[] table = ms.ToArray(); header.TableSize = table.Length; table = packAndEncrypt(table); header.TableCompressedSize = table.Length; bw.Write(0x45534f55); bw.Write(0x424c4144); bw.Write((byte)2); fs2.Position = 21; bw.Write(header.Files.Count); bw.Write((byte)1); bw.Write((byte)1); fs2.Position = 89; bw.Write(header.TableCompressedSize); bw.Write(header.TableSize); bw.Write(table); bw.Write((int)(fs2.Position + 4)); fsTmp.Position = 0; foreach (FileHeader i in header.Files.Values) { byte[] buf = new byte[i.EncryptedSize]; fsTmp.Read(buf, 0, i.EncryptedSize); bw.Write(buf); } fsTmp.Close(); bw.Flush(); fs2.Flush(); fs2.Close(); System.IO.File.Delete(tmpFile); }
public void ExtractFile(string path, string destination) { if (header.Files.ContainsKey(path)) { string folder = destination; folder = System.IO.Path.GetDirectoryName(folder + "\\" + path); string filename = System.IO.Path.GetFileName(path); if (!System.IO.Directory.Exists(folder)) { System.IO.Directory.CreateDirectory(folder); } System.IO.FileStream fs2 = new System.IO.FileStream(folder + "\\" + filename, System.IO.FileMode.Create); FileHeader file = header.Files[path]; fs.Position = file.Offset; byte[] src = br.ReadBytes(file.EncryptedSize); byte[] buf = new byte[file.EncryptedSize + 16]; src.CopyTo(buf, 0); byte[] buf2 = new byte[file.EncryptedSize + 16]; byte[] dst = new byte[file.Size]; aes.Mode = CipherMode.ECB; ICryptoTransform decrypt = aes.CreateDecryptor(Encoding.ASCII.GetBytes("bns_fgt_cb_2010!"), new byte[16]); decrypt.TransformBlock(buf, 0, file.EncryptedSize + 16, buf2, 0); int decompressedBytes = file.Size; fixed(byte *ptr2 = dst) { fixed(byte *ptr = buf2) { uncompress(ptr2, &decompressedBytes, ptr, file.CompressedSize); } } string fileName = System.IO.Path.GetFileName(path); if (fileName == "system.config.xml" || fileName == "client.config2.xml" || fileName == "system.config2.xml" || System.IO.Path.GetExtension(path) == ".x16") { fs2.Close(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(dst)) { System.Xml.XmlDocument xml = XML.XML.FromStream(ms); xml.Save(folder + "\\" + filename); } } else { if (newUnpack && (System.IO.Path.GetExtension(path) == ".x16" || System.IO.Path.GetExtension(path) == ".xml")) { byte[] temp = new byte[8]; Array.Copy(dst, 0, temp, 0, 8); if (Encoding.ASCII.GetString(temp).Equals("LMXBOSLB")) { temp = testUnpack(dst); fs2.Write(temp, 0, temp.Length); } else { fs2.Write(dst, 0, dst.Length); } } else { fs2.Write(dst, 0, dst.Length); } fs2.Flush(); fs2.Close(); } } }