public void Export(string tadFilename, bool exportModified = true) { string tacPath = Path.GetDirectoryName(tadFilename) + "\\" + Path.GetFileName(tadFilename).ToLower().Replace(".tad", ".tac"); string inputFolder = Path.GetDirectoryName(Filename) + Header.RelativeOutputFolder; if (exportModified) { LoadingDialog loadingDialogHash = new LoadingDialog(); loadingDialogHash.SetData(this); Thread threadHash = new Thread(delegate() { CalculateHashes(); }); loadingDialogHash.ShowDialog(threadHash); } TACFile tacFile = new TACFile(); LoadingDialog loadingDialog = new LoadingDialog(); loadingDialog.SetData(tacFile); Thread thread = new Thread(delegate() { tacFile.Pack(tacPath, inputFolder, TADFile); }); loadingDialog.ShowDialog(thread); TADFile.Write(tadFilename); }
public void Unpack(string filename, string outputFolder) { string tadFilename = String.Format("{0}\\{1}{2}", Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename), TADFile.Extension); TADFile tadFile = new TADFile(tadFilename); Unpack(filename, outputFolder, tadFile); }
/// <summary> /// Loads the TAC inside an buffer for the filename crawler. /// </summary> /// <param name="filename">The TAC filename.</param> /// <param name="tadFile">The TAD file.</param> public void Load(string filename, TADFile tadFile) { TADFile = tadFile; if (!Helper.IsFileValid(filename)) { return; } using (FileStream stream = File.Open(filename, FileMode.Open)) { m_buffer = new byte[stream.Length]; stream.Read(m_buffer, 0, m_buffer.Length); } }
public void Write(string filename) { if (!Helper.IsFileValid(filename, false)) { return; } using (FileStream stream = File.Create(filename)) { using (BinaryWriter writer = new BinaryWriter(stream)) { Header.Write(writer); TADFile.Write(writer, true); } } }
public void Read(string filename) { Filename = filename; if (!Helper.IsFileValid(filename)) { return; } using (FileStream stream = File.Open(filename, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(stream)) { Header.Read(reader); TADFile = new TADFile(); TADFile.Read(reader, true); TADFile.Filename = Path.GetDirectoryName(Filename) + Header.RelativeTADPath; } } }
public CacheFile(TADFile tadFile) { TADFile = tadFile; }
public void ConvertLegacy(string filename) { DataCollection dataCollection; using (FileStream reader = new FileStream(filename, FileMode.Open)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); dataCollection = (DataCollection)binaryFormatter.Deserialize(reader); } if (dataCollection == null) { return; } Filename = filename.Replace(".shdcache", ".cache"); TADFile = new TADFile(); TADFile.Filename = filename.Replace(".shdcache", ".tad"); TADFile.Header.FileCount = BitConverter.ToUInt32(dataCollection.Header.FileCount1, 0); TADFile.Header.HeaderChecksum = BitConverter.ToUInt32(dataCollection.Header.HeaderChecksum, 0); TADFile.Header.TacSize = BitConverter.ToUInt32(dataCollection.Header.TacSize, 0); TADFile.Header.UnixTimestamp = new DateTime(1970, 1, 1).AddSeconds(BitConverter.ToInt32(dataCollection.Header.UnixTimestamp, 0)); string fileDir = Path.GetDirectoryName(Filename); string dir = fileDir; if (dataCollection.Files.Count > 0) { dir = Path.GetDirectoryName(dataCollection.Files[0].Meta.FilePath); } Header.RelativeOutputFolder = "\\" + Helper.GetRelativePath(dir, fileDir); Header.RelativeTADPath = "\\" + Helper.GetRelativePath(TADFile.Filename, fileDir); Header.RelativeTACPath = "\\" + Helper.GetRelativePath(TADFile.Filename.Replace(".tad", ".tac"), fileDir); foreach (FileStructure file in dataCollection.Files) { TADFileEntry entry = new TADFileEntry(); entry.FileOffset = BitConverter.ToUInt32(file.FileStart, 0); entry.FileSize = BitConverter.ToUInt32(file.FileSize, 0); entry.RelativePath = Helper.GetRelativePath(file.Meta.FilePath, dir); entry.Index = file.Meta.Index; byte[] firstHash = new byte[4]; Array.Copy(file.Hash1, firstHash, 4); entry.FirstHash = BitConverter.ToUInt32(firstHash, 0); byte[] secondHash = new byte[4]; Array.Copy(file.Hash1, 4, secondHash, 0, 4); entry.SecondHash = BitConverter.ToUInt32(secondHash, 0); byte[] unknown = new byte[4]; Array.Copy(file.Hash2, 0, unknown, 0, 4); entry.Unknown = BitConverter.ToUInt32(unknown, 0); entry.MD5Checksum = file.Meta.MD5Hash; TADFile.FileEntries.Add(entry); } FilenameDatabase filenameDB = new FilenameDatabase(); LoadingDialog loadingDialog = new LoadingDialog(); loadingDialog.SetData(filenameDB); Thread thread = new Thread(delegate() { filenameDB.MapFilenamesToTADInstance(this); }); loadingDialog.ShowDialog(thread); DescriptionDatabase descDB = new DescriptionDatabase(); loadingDialog = new LoadingDialog(); loadingDialog.SetData(descDB); thread = new Thread(delegate() { descDB.MapDescriptionToTADInstance(TADFile); }); loadingDialog.ShowDialog(thread); Write(Filename); if (File.Exists(filename)) { File.Delete(filename); } }
/// <summary> /// Packs the TAC file and changes the input TAD file accordingly. /// </summary> /// <param name="filename">The TAC filename.</param> /// <param name="inputFolder">The TAC extraction folder.</param> /// <param name="tadFile">The TAD file.</param> public void Pack(string filename, string inputFolder, TADFile tadFile, bool useExport = true) { DescriptionChanged(this, new DescriptionChangedArgs("Packing TAC...")); if (!Helper.IsFileValid(filename, false)) { Finished(this, new FinishedArgs(false)); return; } List <TADFileEntry> toRemove = new List <TADFileEntry>(); using (FileStream tacStream = File.Create(filename)) { uint fileCount = 0; int counter = 0; foreach (TADFileEntry entry in tadFile.FileEntries) { counter++; ProgressChanged(this, new ProgressChangedArgs(counter, tadFile.FileEntries.Count)); if (useExport) { if (!entry.Export) { toRemove.Add(entry); continue; //skip tad entries that are not flagged for exporting. } } fileCount++; if (String.IsNullOrEmpty(entry.RelativePath)) { Console.WriteLine("TAC was not unpacked before!"); break; } string sourceFilename = String.Format("{0}\\{1}", inputFolder, entry.RelativePath); byte[] buffer; if (!Helper.IsFileValid(sourceFilename)) { Finished(this, new FinishedArgs(false)); return; } using (FileStream stream = File.Open(sourceFilename, FileMode.Open)) { buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); entry.FileOffset = (uint)tacStream.Position; entry.FileSize = (uint)buffer.Length; entry.MD5Checksum = Helper.MD5Hash(buffer); } tacStream.Write(buffer, 0, buffer.Length); } tadFile.Header.FileCount = fileCount; tadFile.Header.TacSize = (uint)tacStream.Length; tadFile.Header.UnixTimestamp = DateTime.UtcNow + TimeSpan.FromDays(365 * 5); } foreach (TADFileEntry entry in toRemove) { tadFile.FileEntries.Remove(entry); } Finished(this, new FinishedArgs(true)); }
/// <summary> /// Unpacks the TAC file accordingly to the TAD file. /// </summary> /// <param name="filename">The TAC filename.</param> /// <param name="outputFolder">The extraction output folder.</param> /// <param name="tadFile">The TAD file.</param> /// <returns></returns> public bool Unpack(string tacFilename, string outputFolder, TADFile tadFile) { if (Path.GetExtension(tacFilename).ToLower() != Extension) { return(false); } if (!Directory.Exists(outputFolder)) { Directory.CreateDirectory(outputFolder); } DescriptionChanged(this, new DescriptionChangedArgs("Unpacking TAC...")); if (!Helper.IsFileValid(tacFilename)) { Finished(this, new FinishedArgs(false)); return(false); } using (FileStream tacStream = File.Open(tacFilename, FileMode.Open)) { int counter = 0; foreach (TADFileEntry entry in tadFile.FileEntries) { tacStream.Seek(entry.FileOffset, SeekOrigin.Begin); byte[] fileBuffer = new byte[entry.FileSize]; tacStream.Read(fileBuffer, 0, fileBuffer.Length); entry.MD5Checksum = Helper.MD5Hash(fileBuffer); string fileEntryPath = ""; if (String.IsNullOrEmpty(entry.Filename)) { string unknownDir = outputFolder + UnknownFilesPath; if (!Directory.Exists(unknownDir)) { Directory.CreateDirectory(unknownDir); } string Extension = Helper.ExtensionFinder(fileBuffer); string fileEntryName = String.Format("{0}{1}{2}", UnknownFilesPath, counter.ToString(), Extension); fileEntryPath = outputFolder + fileEntryName; } else { fileEntryPath = entry.Filename.Replace('/', '\\'); fileEntryPath = outputFolder + "\\" + fileEntryPath; fileEntryPath = Helper.SwitchExtension(fileEntryPath); string dir = Path.GetDirectoryName(fileEntryPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } if (!Helper.IsFileValid(fileEntryPath, false)) { Finished(this, new FinishedArgs(false)); return(false); } using (FileStream fileEntryStream = File.Create(fileEntryPath)) { entry.RelativePath = Helper.GetRelativePath(fileEntryStream.Name, outputFolder); fileEntryStream.Write(fileBuffer, 0, fileBuffer.Length); } counter++; ProgressChanged(this, new ProgressChangedArgs(counter, tadFile.FileEntries.Count)); } } Finished(this, new FinishedArgs(true)); return(true); }