public bool Unpack(string DirName, string PackageName) { FileInfo pakFile = new FileInfo(PackageName); FileStream stmori = pakFile.Open(FileMode.Open, FileAccess.Read); BinaryReader brori = new BinaryReader(stmori); ScwHeader scwHdr = (ScwHeader)StructConverter.BytesToStruct(brori.ReadBytes( Marshal.SizeOf(typeof(ScwHeader))), typeof(ScwHeader)); if (scwHdr.Magic != "DataPack5" || scwHdr.MajorVer != 5) { throw new FormatException("File"); } stmori.Seek(scwHdr.IndexOffset, SeekOrigin.Begin); byte[] comprIdx = brori.ReadBytes((int)scwHdr.IndexLen); byte[] uncomprIdx = new byte[scwHdr.IndexEntries * Marshal.SizeOf(typeof(ScwIndexEntry))]; DecryptBlock(comprIdx); LzssUncompress(uncomprIdx, (int)scwHdr.IndexEntries * Marshal.SizeOf(typeof(ScwIndexEntry)), comprIdx, (int)scwHdr.IndexLen); MemoryStream stmidx = new MemoryStream(uncomprIdx); BinaryReader bridx = new BinaryReader(stmidx); List <ScwIndexEntry> entries = new List <ScwIndexEntry>((int)scwHdr.IndexEntries); //ArrayList entries2 = new ArrayList((int)scwHdr.IndexEntries); for (int i = 0; i < scwHdr.IndexEntries; i++) { entries.Add((ScwIndexEntry)StructConverter.BytesToStruct( bridx.ReadBytes(Marshal.SizeOf(typeof(ScwIndexEntry))), typeof(ScwIndexEntry))); } Directory.SetCurrentDirectory(DirName); if (Path.GetFileName(PackageName).StartsWith("Graphic", StringComparison.CurrentCultureIgnoreCase)) { for (int i = 0; i < scwHdr.IndexEntries; i++) { ScwIndexEntry entry = entries[i]; FileInfo newf = new FileInfo(entry.Name + ".png"); Stream stmnew = newf.Open(FileMode.Create, FileAccess.Write); stmori.Seek(entry.Offset + scwHdr.DataOffset, SeekOrigin.Begin); byte[] newbuff = brori.ReadBytes((int)entry.Length); DecryptBlock2(newbuff, CalcKey(entry.Name)); stmnew.Write(newbuff, 0, (int)entry.Length); stmnew.Close(); } } else if (Path.GetFileName(PackageName).StartsWith("Scr", StringComparison.CurrentCultureIgnoreCase)) { for (int i = 0; i < scwHdr.IndexEntries; i++) { ScwIndexEntry entry = entries[i]; FileInfo newf = new FileInfo(entry.Name); Stream stmnew = newf.Open(FileMode.Create, FileAccess.Write); stmori.Seek(entry.Offset + scwHdr.DataOffset, SeekOrigin.Begin); stmnew.Write(brori.ReadBytes((int)entry.Length), 0, (int)entry.Length); stmnew.Close(); } } else { throw new FormatException("Not supported prefix!"); } stmori.Close(); return(true); }
public bool Pack(string DirName, string PackageName, string NewPakName = null) { //if (NewPakName == null) // NewPakName = PackageName.Replace(".pak", "2.pak"); FileInfo pakFile = new FileInfo(PackageName); FileStream stmori = pakFile.Open(FileMode.Open, FileAccess.ReadWrite); BinaryReader brori = new BinaryReader(stmori); BinaryWriter bwori = new BinaryWriter(stmori); ScwHeader scwHdr = (ScwHeader)StructConverter.BytesToStruct(brori.ReadBytes( Marshal.SizeOf(typeof(ScwHeader))), typeof(ScwHeader)); if (scwHdr.Magic != "DataPack5" || scwHdr.MajorVer != 5) { throw new FormatException("File"); } stmori.Seek(scwHdr.IndexOffset, SeekOrigin.Begin); byte[] comprIdx = brori.ReadBytes((int)scwHdr.IndexLen); byte[] uncomprIdx = new byte[scwHdr.IndexEntries * Marshal.SizeOf(typeof(ScwIndexEntry))]; DecryptBlock(comprIdx); LzssUncompress(uncomprIdx, (int)scwHdr.IndexEntries * Marshal.SizeOf(typeof(ScwIndexEntry)), comprIdx, (int)scwHdr.IndexLen); MemoryStream stmidx = new MemoryStream(uncomprIdx); BinaryReader bridx = new BinaryReader(stmidx); List <ScwIndexEntry> entries = new List <ScwIndexEntry>((int)scwHdr.IndexEntries); //ArrayList entries2 = new ArrayList((int)scwHdr.IndexEntries); for (int i = 0; i < scwHdr.IndexEntries; i++) { entries.Add((ScwIndexEntry)StructConverter.BytesToStruct( bridx.ReadBytes(Marshal.SizeOf(typeof(ScwIndexEntry))), typeof(ScwIndexEntry))); } Directory.SetCurrentDirectory(DirName); long dataend = scwHdr.IndexOffset - scwHdr.DataOffset; for (int i = 0; i < scwHdr.IndexEntries; i++) { ScwIndexEntry entry = entries[i]; if (Path.GetFileName(PackageName).StartsWith("Graphic", StringComparison.CurrentCultureIgnoreCase)) { if (File.Exists(entry.Name + ".png")) { FileInfo newf = new FileInfo(entry.Name + ".png"); byte[] stmnew = File.ReadAllBytes(entry.Name + ".png"); DecryptBlock2(stmnew, CalcKey(entry.Name)); if (newf.Length <= entry.Length) { stmori.Seek(entry.Offset + scwHdr.DataOffset, SeekOrigin.Begin); bwori.Write(stmnew); entry.Length = (uint)newf.Length; } else { stmori.Seek(dataend + scwHdr.DataOffset, SeekOrigin.Begin); bwori.Write(stmnew); entry.Offset = (uint)dataend; dataend += newf.Length; dataend = dataend % 4 == 0 ? dataend : dataend + (4 - dataend % 4); entry.Length = (uint)newf.Length; } entries[i] = entry; } } else if (Path.GetFileName(PackageName).StartsWith("Scr", StringComparison.CurrentCultureIgnoreCase)) { if (File.Exists(entry.Name)) { FileInfo newf = new FileInfo(entry.Name); if (newf.Length <= entry.Length) { stmori.Seek(entry.Offset + scwHdr.DataOffset, SeekOrigin.Begin); bwori.Write(File.ReadAllBytes(entry.Name)); entry.Length = (uint)newf.Length; } else { stmori.Seek(dataend + scwHdr.DataOffset, SeekOrigin.Begin); bwori.Write(File.ReadAllBytes(entry.Name)); entry.Offset = (uint)dataend; dataend += newf.Length; dataend = dataend % 4 == 0 ? dataend : dataend + (4 - dataend % 4); entry.Length = (uint)newf.Length; } entries[i] = entry; } } } stmidx.Seek(0, SeekOrigin.Begin); for (int i = 0; i < scwHdr.IndexEntries; i++) { byte[] bentry = StructConverter.StructToBytes(entries[i]); stmidx.Write(bentry, 0, Marshal.SizeOf(typeof(ScwIndexEntry))); } uint newlen = 0; comprIdx = LzssPack(uncomprIdx, (uint)uncomprIdx.Length, ref newlen); DecryptBlock(comprIdx, (int)newlen); stmori.Seek(dataend + scwHdr.DataOffset, SeekOrigin.Begin); bwori.Write(comprIdx, 0, (int)newlen); long endpos = stmori.Position; scwHdr.IndexOffset = (uint)dataend + scwHdr.DataOffset; scwHdr.IndexLen = (uint)newlen; stmori.Seek(0, SeekOrigin.Begin); bwori.Write(StructConverter.StructToBytes(scwHdr)); stmori.SetLength(endpos); stmori.Close(); return(true); }