//Get a PackedFile from a system file path static public PackedFile PackFile(string windowsPath) { if (!File.Exists(windowsPath)) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] File doesn't exist:" + windowsPath); return(null); } //Get properties // Content string content = File.ReadAllText(windowsPath, System.Text.Encoding.UTF8); // File relative path string tcpRoot = TCP2_Utils.FindReadmePath(); if (tcpRoot == null) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] Can't find TCP2 Readme file!\nCan't determine root folder to pack/unpack files."); return(null); } tcpRoot = UnityToSystemPath(tcpRoot); string relativePath = windowsPath.Replace(tcpRoot, ""); PackedFile pf = new PackedFile(relativePath, content); return(pf); }
public File(string source) { if (!System.IO.File.Exists(source)) { return; } bigHeader = new byte[0x10]; refPackHeader = new byte[0x08]; preBuffer = new byte[0x800000]; stream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); stream.Read(bigHeader, 0, 0x10); stream.Position = 0; uint headerSize = HeaderSize; stream.Read((bigHeader = new byte[headerSize]), 0, (int)headerSize); Files = new PackedFile[FileCount]; int position = 0x10; for (int idx = 0; idx < FileCount; ++idx) { Files[idx] = new PackedFile(); Files[idx].Offset = FileHelper.Invert(FileHelper.GetUInt(position, bigHeader)); position += 0x04; Files[idx].Size = FileHelper.Invert(FileHelper.GetUInt(position, bigHeader)); position += 0x04; Files[idx].Name = FileHelper.GetString(position, bigHeader); position += Files[idx].Name.Length + 1; } }
public static void ExportBinary(PackedFile CurrentPackedFile, string _exportDirectory) { string extractTo = null; // TODO: Add support for ModManager //extractTo = ModManager.Instance.CurrentModSet ? ModManager.Instance.CurrentModDirectory : null; if (extractTo == null) { DirectoryDialog dialog = new DirectoryDialog { Description = "Please point to folder to extract to", SelectedPath = String.IsNullOrEmpty(_exportDirectory) ? System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) : _exportDirectory }; extractTo = dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK ? dialog.SelectedPath : null; _exportDirectory = dialog.SelectedPath; } if (!string.IsNullOrEmpty(extractTo)) { List <PackedFile> files = new List <PackedFile>(); files.Add(CurrentPackedFile); FileExtractor extractor = new FileExtractor(extractTo); extractor.ExtractFiles(files); MessageBox.Show(string.Format("File exported as binary.")); } }
/// <summary> /// Deletes specified packed file /// </summary> /// <param name="packedFilePath"></param> public void DeletePackedFile(string packedFilePath) { try { if (!string.IsNullOrEmpty(packedFilePath)) { // Getting packed file... PackedFile packedFile = _GetPackedFile(packedFilePath); if (packedFile.exists) { _DeletePackedFile(packedFile); } else { throw new Exception(string.Format(_FORMAT_ERROR_PACKED_FILE_NOT_EXISTS, packedFilePath)); } } else { throw new Exception("Forbidden operation."); } } catch (Exception ex) { throw new Exception("Unable to delete packed file: " + packedFilePath + "\r\n" + ex.Message, ex); } }
void TestCompression() { // Test creating ZLib compressed blobs GetFileList(m_strTestDirectories); var results = new List <String>(); Console.WriteLine("Testing individual zlib file compression"); foreach (string file in fileList) { if (file.Contains(".pak")) { continue; } PackedFile packed = pSarc.CompressFile(file, File.ReadAllBytes(file)); string msg = string.Format("Packed file: {0}, Blocks: {1}, Original Size: {2}, Compressed Size: {3}", packed.TocEntry.FileName, packed.TocEntry.BlockListStart, packed.TocEntry.OriginalSize, packed.CompressedFile.LongLength); Console.WriteLine(msg); results.Add(msg); } File.WriteAllLines(m_strTestDirectories + @"\commpressionResults.txt", results); Console.Write("Press any key to continue ..."); Console.ReadKey(); }
/** * <summary>Removes all entries identical between the <paramref name="unoptimizedFile"/> and the <paramref name="referenceFiles"/> from the <paramref name="unoptimizedFile"/>.</summary> * <remarks>This function was intended to be passed a <see cref="PackedFile"/> that contains DB tables. If it is passed a PackedFile without DB tables it will not work properly.</remarks> * * <param name="unoptimizedFile">The <see cref="PackedFile"/> to be optimized. It must contain a DB table for the method to work.</param> * <param name="referenceFiles">A <see cref="List{DBFile}"/> of <see cref="DBFile">DBFiles</see> that should be checked for identical table rows in the <paramref name="unoptimizedFile"/>.</param> * * <returns>A new <see cref="PackedFile"/> that contains the optimized data from the <paramref name="unoptimizedFile"/> or null if the resulting <see cref="PackedFile"/> would be empty.</returns> */ public PackedFile OptimizePackedDBFile(PackedFile unoptimizedFile, List <DBFile> referenceFiles) { PackedFile result = unoptimizedFile; DBFile modDBFile = FromPacked(unoptimizedFile); if (modDBFile != null) { foreach (DBFile file in referenceFiles) { if (TypesCompatible(modDBFile, file)) { modDBFile.Entries.RemoveAll(file.ContainsRow); } } if (modDBFile.Entries.Count != 0) { result.Data = PackedFileDbCodec.GetCodec(unoptimizedFile).Encode(modDBFile); } else { result = null; } } return(result); }
/* * Create an optimized packed file from the given one. */ PackedFile CreateOptimizedFile(PackedFile toOptimize) { PackedFile result = toOptimize; // special handling for db files; leave all others as they are. if (toOptimize.FullPath.StartsWith("db")) { try { DBFile modDbFile = FromPacked(toOptimize); if (modDbFile != null) { DBFile gameDbFile = FindInGamePacks(toOptimize); if (TypesCompatible(modDbFile, gameDbFile)) { DBFileHeader header = new DBFileHeader(modDbFile.Header); DBFile optimizedFile = new DBFile(header, modDbFile.CurrentType); optimizedFile.Entries.AddRange(GetDifferingRows(modDbFile, gameDbFile)); if (optimizedFile.Entries.Count != 0) { result.Data = PackedFileDbCodec.GetCodec(toOptimize).Encode(optimizedFile); } else { result = null; } } } } catch (Exception e) { Console.Error.WriteLine(e); } } return(result); }
/* * Query if given packed file can be deccoded. * Is not entirely reliable because it only reads the header and checks if a * type definition is available for the given GUID and/or type name and version. * The actual decode tries out all available type infos for that type name * but that is less efficient because it has to read the whole file at least once * if successful. */ public static bool CanDecode(PackedFile packedFile, out string display) { bool result = true; string key = DBFile.Typename(packedFile.FullPath); if (DBTypeMap.Instance.IsSupported(key)) { try { DBFileHeader header = PackedFileDbCodec.readHeader(packedFile); int maxVersion = DBTypeMap.Instance.MaxVersion(key); if (maxVersion != 0 && header.Version > maxVersion) { display = string.Format("{0}: needs {1}, has {2}", key, header.Version, DBTypeMap.Instance.MaxVersion(key)); result = false; } else { display = string.Format("Version: {0}", header.Version); } } catch (Exception x) { display = string.Format("{0}: {1}", key, x.Message); } } else { display = string.Format("{0}: no definition available", key); result = false; } return(result); }
public void Save() { if (PackedFile.Exists) { PackedFile.Delete(); System.Threading.Thread.Sleep(100); } using (FileStream fs = new FileStream(PackedFile.FullName, FileMode.CreateNew, FileAccess.Write)) { //Writing Header //4 bytes fs.Write(BitConverter.GetBytes(FileHeaderDefinition.TotalEntries), 0, 4); //8 bytes foreach size foreach (Int64 size in FileHeaderDefinition.EntriesSize) { fs.Write(BitConverter.GetBytes(size), 0, 8); } foreach (Item it in Data) { fs.Write(it.SerializedData, 0, it.SerializedData.Length); } fs.Close(); } }
/* * Insert the previously given values into the db table. * A warning will be printed and no data added if the given data doesn't * fit the db file's structure. */ public override void Execute() { // insert always into packed files at the save to file foreach (PackedFile packed in PackedFiles) { // we'll read from packed, but that is in the source pack; // get or create the db file in the target pack DBFile targetFile = GetTargetFile(packed); foreach (RowValues insertValues in Source.Values) { if (targetFile.CurrentType.Fields.Count == insertValues.Count) { DBRow newRow = targetFile.GetNewEntry(); for (int i = 0; i < newRow.Count; i++) { newRow[i].Value = insertValues[i]; } targetFile.Entries.Add(newRow); } else { Console.WriteLine("Cannot insert: was given {0} values, expecting {1} in {2}", insertValues.Count, targetFile.CurrentType.Fields.Count, packed.FullPath); Console.WriteLine("Values: {0}", string.Join(",", insertValues)); } } // encode and store in target pack PackedFile newPacked = new PackedFile(packed.FullPath, false); newPacked.Data = PackedFileDbCodec.GetCodec(newPacked).Encode(targetFile); SaveTo.Add(newPacked, true); } }
// write files that failed to filesystem individually for later inspection void ExtractFiles(string dir, PackFile pack, ICollection <Tuple <string, int> > toExtract) { if (toExtract.Count != 0) { string path = Path.Combine(dir, "failed"); Directory.CreateDirectory(path); foreach (Tuple <string, int> failed in toExtract) { string failType = failed.Item1; string failPath = string.Format("db\\{0}_tables\\{0}", failType); PackedFile found = null; foreach (PackedFile packed in pack.Files) { if (packed.FullPath.Equals(failPath)) { found = packed; break; } } if (found != null) { string filePath = Path.Combine(path, string.Format("{0}_{1}", failType, failed.Item2)); File.WriteAllBytes(Path.Combine(dir, filePath), found.Data); } else { Console.WriteLine("cant extract {0}", failPath); } } } }
void SetCurrentPackFile(PackedFile packedFile) { if (_packedFile != null && _dataChanged) { Commit(); } _packedFile = packedFile; if (packedFile != null) { byte[] data = packedFile.Data; using (MemoryStream stream = new MemoryStream(data, 0, data.Length)) { var codec = new TextCodec(); var decodedData = codec.Decode(stream); textEditor.Text = decodedData; var extention = Path.GetExtension(_packedFile.Name); textEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(extention); HighlightingComboBox_SelectionChanged(null, null); } } }
/// <summary> /// Writes content info section into a byte array /// </summary> private byte[] _WriteContentInfoSection() { // TODO dynamical limit ? int dataLength = 16384; byte[] tempData = new byte[dataLength]; using (BinaryWriter dataWriter = new BinaryWriter(new MemoryStream(tempData))) { // Pour chaque fichier de la liste ... for (int i = 0; i < _FileList.Count; i++) { PackedFile aFile = _FileList[i]; // Adresse de début du fichier dataWriter.Write(aFile.startAddress); // Taille du fichier dataWriter.Write(aFile.fileSize); // TODO 2x4 octets.... dataWriter.Write(aFile.unknown1); dataWriter.Write(aFile.unknown2); } dataLength = (int)dataWriter.BaseStream.Position; } byte[] returnedData = new byte[dataLength]; Array.Copy(tempData, returnedData, dataLength); return(returnedData); }
/// <summary> /// Returns array of bytes for file data section /// </summary> /// <param name="files"></param> /// <returns></returns> private byte[] _MakeFileData(IEnumerable <byte[]> files) { byte[] returnedData = new byte[0]; if (files != null) { // Computing theorical size: current size of each file + up to 16 padding bytes per file int maxSize = 0; foreach (byte[] anotherFileData in files) { maxSize += (anotherFileData.Length + 16); } byte[] tempData = new byte[maxSize]; long realDataSize; // Writing all files Section fileDataSection = _GetSection(SectionType.FileData); using (BinaryWriter writer = new BinaryWriter(new MemoryStream(tempData))) { int index = 0; foreach (byte[] anotherFileData in files) { // Updating packed file information PackedFile correspondingFile = _FileList[index++]; correspondingFile.fileSize = (uint)anotherFileData.Length; correspondingFile.startAddress = (uint)writer.BaseStream.Position + fileDataSection.address; // Data writer.Write(anotherFileData); // Padding... if (index < _FileList.Count) { uint paddingLength = _GetPaddingLength((uint)anotherFileData.Length, _SecondaryBlockSize); byte[] padding = new byte[paddingLength]; byte[] paddingString = String2.ToByteArray(_PADDING_SEQUENCE); Array.Copy(paddingString, padding, paddingLength); writer.Write(padding); } } realDataSize = writer.BaseStream.Position; } // Putting real data into result returnedData = new byte[realDataSize]; Array.Copy(tempData, 0, returnedData, 0, realDataSize); // Updating usableSize fileDataSection.usableSize = (uint)realDataSize; } return(returnedData); }
//Create an archive of PackedFile public static void CreateArchive(PackedFile[] packedFiles, string outputFile) { if(packedFiles == null || packedFiles.Length == 0) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] No file to pack!"); return; } System.Text.StringBuilder sbIndex = new System.Text.StringBuilder(); System.Text.StringBuilder sbContent = new System.Text.StringBuilder(); sbIndex.AppendLine("# TCP2 PACKED SHADERS"); int cursor = 0; foreach(PackedFile pf in packedFiles) { sbContent.Append(pf.content); sbIndex.AppendLine( pf.path + ";" + cursor.ToString() + ";" + pf.content.Length ); // PATH ; START ; LENGTH cursor += pf.content.Length; } string archiveContent = sbIndex.ToString() + "###\n" + sbContent.ToString(); string fullPath = Application.dataPath + "/" + outputFile; string directory = Path.GetDirectoryName(fullPath); if(!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllText(fullPath, archiveContent); AssetDatabase.Refresh(); Debug.Log("[TCP2 CreateArchive] Created archive:\n" + fullPath); }
public override void TestFile(PackedFile file) { allTestedFiles.Add(file.FullPath); byte[] original = file.Data; UnitVariantFile uvFile = null; using (MemoryStream stream = new MemoryStream(original, 0, original.Length)) { uvFile = codec.Decode(stream); } byte[] bytes = UnitVariantCodec.Encode(uvFile); if (file.Size != bytes.Length) { wrongSize.Add(file.FullPath); } else { // verify data byte[] origData = file.Data; for (int i = 0; i < origData.Length; i++) { if (origData [i] != bytes [i]) { wrongData.Add(file.FullPath); return; } } supported.Add(file.FullPath); } }
//Get a PackedFile from a system file path public static PackedFile PackFile(string windowsPath) { if (!File.Exists(windowsPath)) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] File doesn't exist:" + windowsPath); return(null); } //Get properties // Content var content = File.ReadAllText(windowsPath, Encoding.UTF8); content = content.Replace("\r\n", "\n"); // File relative path var tcpRoot = FindReadmePath(); if (tcpRoot == null) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] Can't find TCP2 Readme file!\nCan't determine root folder to pack/unpack files."); return(null); } tcpRoot = ToSystemSlashPath(tcpRoot); var relativePath = windowsPath.Replace(tcpRoot, ""); var pf = new PackedFile(relativePath, content); return(pf); }
public override bool CanTest(PackedFile packed) { bool result = packed.FullPath.StartsWith("db"); result &= !DBFile.Typename(packed.FullPath).Equals("models_building_tables"); result &= !DBFile.Typename(packed.FullPath).Equals("models_naval_tables"); return(result); }
public PackedFileNode(PackedFile file) : base(file) { if (file.FullPath.StartsWith("db")) { file.RenameEvent += Renamed; } }
public static bool HeaderVersionObsolete(PackedFile packedFile) { DBFileHeader header = PackedFileDbCodec.readHeader(packedFile); string type = DBFile.Typename(packedFile.FullPath); int maxVersion = GameManager.Instance.GetMaxDbVersion(type); return(DBTypeMap.Instance.IsSupported(type) && maxVersion != 0 && (header.Version < maxVersion)); }
/// <summary> /// Opens current entry in file list /// </summary> public void OpenSelectedEntry() { if (fileListView.SelectedItems.Count == 1) { Cursor = Cursors.WaitCursor; ListViewItem currentItem = fileListView.SelectedItems[0]; Object tag = currentItem.Tag; try { if (tag is FileSystemInfo) { // File or directory string path = ((tag as FileSystemInfo).FullName); try { _Refresh(path); CurrentFolder = path; } catch (NotImplementedException ex) { // Not applicable -> entry will be opened with default assos __SystemRun(path); } } else if (tag is PackedFile) { // Packed file -> it must be extracted first // Reloads BNK to get correct contents Bnk parentBnk = new Bnk { Name = CurrentFolder }; parentBnk.Read(); PackedFile updatedPackedFile = parentBnk.GetPackedFile((tag as PackedFile).Id); // TODO handle temporary folder string path = @"T:\COMMUN\" + updatedPackedFile.Name; Bnk.Extract(updatedPackedFile, path); // Opens with default file associations for now __SystemRun(path); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); } finally { Cursor = Cursors.Default; } } }
/******************************************************************************************** * This function constructs the System.Data.DataTable we use to not only store our data, * * but to bind as our visuals data source. * ********************************************************************************************/ private DataTable CreateTable(PackedFile currentPackedFile, DBFile table) { DataTable constructionTable = new DataTable(currentPackedFile.Name); DataColumn constructionColumn; List <DataColumn> keyList = new List <DataColumn>(); constructionTable.BeginLoadData(); foreach (FieldInfo columnInfo in table.CurrentType.Fields) { // Create the new column, using object as the data type for all columns, this way we avoid the WPF DataGrid's built in // data validation abilities in favor of our own implementation. constructionColumn = new DataColumn(columnInfo.Name, typeof(string)); if (columnInfo.TypeCode == TypeCode.Int16 || columnInfo.TypeCode == TypeCode.Int32 || columnInfo.TypeCode == TypeCode.Single) { constructionColumn = new DataColumn(columnInfo.Name, typeof(double)); } constructionColumn.AllowDBNull = true; constructionColumn.Unique = false; constructionColumn.ReadOnly = true; // Save the FKey if it exists if (!String.IsNullOrEmpty(columnInfo.ForeignReference)) { constructionColumn.ExtendedProperties.Add("FKey", columnInfo.ForeignReference); } // If the column is a primary key, save it for later adding if (columnInfo.PrimaryKey) { keyList.Add(constructionColumn); } constructionTable.Columns.Add(constructionColumn); } // If the table has primary keys, set them. if (keyList.Count > 0) { constructionTable.PrimaryKey = keyList.ToArray(); } // Now that the DataTable schema is constructed, add in all the data. foreach (List <FieldInstance> rowentry in table.Entries) { constructionTable.Rows.Add(rowentry.Select(n => n.Value).ToArray <object>()); } constructionTable.EndLoadData(); constructionTable.AcceptChanges(); return(constructionTable); }
public byte[] Process(PackedFile file) { byte[] result = file.Data; using (MemoryStream stream = new MemoryStream()) { DBFile dbFile = PackedFileDbCodec.Decode(file); TextDbCodec.Instance.Encode(stream, dbFile); result = stream.ToArray(); } return(result); }
public void PackedFileLoaded(PackedFile packedFile) { currentCount++; if (currentCount % 10 <= 0) { label.Text = string.Format("Opening {0} ({1} of {2} files loaded)", file, currentCount, count); progress.PerformStep(); Application.DoEvents(); } }
public PackedFile[] ReadLengthPrefixedFileArray() { var fileCount = Read7BitEncodedInt(); var files = new PackedFile[fileCount]; for (int i = 0; i < fileCount; ++i) { files[i] = new PackedFile(ReadLengthPrefixedBlob(), ReadString(), ReadDateTime()); } return(files); }
/// <summary> /// Returns information about packed file from its path. /// </summary> /// <param name="packedFilePath">Le nom du fichier a récupérer.</param> /// <returns>Les infos sur le fichier</returns> private PackedFile _GetPackedFile(string packedFilePath) { PackedFile returnedFile = new PackedFile(); if (_FileByPathList.ContainsKey(packedFilePath)) { returnedFile = _FileByPathList[packedFilePath]; } return(returnedFile); }
public byte[] Process(PackedFile file) { byte[] result; MemoryStream stream = new MemoryStream(); using (var writer = new StreamWriter(stream)) { LocFile locFile = LocCodec.Instance.Decode(file.Data); locFile.Export(writer); result = stream.ToArray(); } return(result); }
public void PackedFileLoaded(PackedFile packedFile) { currentCount++; if (currentCount % 10 <= 0) { progressReporter.ReportProgressAsync(() => { label.Text = string.Format("Opening {0} ({1} of {2} files loaded)", file, currentCount, codecFileCount); progress.PerformStep(); }); } }
void CheckPack(PackFile pack) { PackedFile referenced = null; Dictionary <PackedFile, List <string> > referencing = new Dictionary <PackedFile, List <string> >(); foreach (PackedFile packed in pack) { if (packed.FullPath.StartsWith("db")) { if (DBFile.Typename(packed.FullPath).Equals(ReferencedTable)) { referenced = packed; } foreach (string referenceFrom in referencesFrom) { if (referenceFrom.Split('.')[0].Equals(DBFile.Typename(packed.FullPath))) { List <string> referencingList; if (!referencing.TryGetValue(packed, out referencingList)) { referencingList = new List <string>(); referencing.Add(packed, referencingList); } referencingList.Add(referenceFrom); } } } } if (referenced != null) { foreach (PackedFile referencingFile in referencing.Keys) { foreach (string fieldReference in referencing[referencingFile]) { CheckResult result = new CheckResult { ReferencingTable = referencingFile, ReferencingFieldName = fieldReference.Split('.')[1], ReferencedTable = referenced, ReferencedFieldName = this.ReferencedFieldName }; if (result.UnfulfilledReferences.Count > 0) { FailedResults.Add(pack, result); break; } } if (FailedResults.ContainsKey(pack)) { break; } } } }
public static bool CanDecode(PackedFile dbFile) { bool valid = false; try { DBFileHeader header = PackedFileDbCodec.readHeader(dbFile); DBFile decoded = PackedFileDbCodec.Decode(dbFile); valid = (decoded.Entries.Count == header.EntryCount); return(valid); } catch (Exception) { } return(valid); }
/* * Fills the given string collection with data from the field in the given packed file. */ public static void FillFromPacked(SortedSet <string> result, PackedFile packed, string fieldName) { DBFile dbFile = PackedFileDbCodec.Decode(packed); foreach (DBRow entry in dbFile.Entries) { string toAdd = entry[fieldName].Value; if (toAdd != null) { result.Add(toAdd); } } }
//Get a PackedFile from a system file path public static PackedFile PackFile(string windowsPath) { if(!File.Exists(windowsPath)) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] File doesn't exist:" + windowsPath); return null; } //Get properties // Content string content = File.ReadAllText(windowsPath, System.Text.Encoding.UTF8); // File relative path string tcpRoot = TCP2_Utils.FindReadmePath(); if(tcpRoot == null) { EditorApplication.Beep(); Debug.LogError("[TCP2 PackFile] Can't find TCP2 Readme file!\nCan't determine root folder to pack/unpack files."); return null; } tcpRoot = UnityToSystemPath(tcpRoot); string relativePath = windowsPath.Replace(tcpRoot, ""); PackedFile pf = new PackedFile(relativePath, content); return pf; }