/// <summary> /// Crée une copie de sauvegarde du fichier spécifié. /// Les dates de création/modification restent inchangées. /// </summary> /// <param name="fileName">Nom du fichier à sauvegarder</param> /// <param name="copyFileName">Nom de la copie</param> public static void BackupFile(string fileName, string copyFileName) { try { // Vérifications if (fileName == null || copyFileName == null || fileName.Equals(copyFileName)) { throw new Exception(_ERROR_BACKUP_PARAMS); } // Copie avec préservation de la date de modification FileInfo sourceInfo = new FileInfo(fileName); DateTime modDate = sourceInfo.LastWriteTime; File.Copy(fileName, copyFileName, true); File2.RemoveAttribute(copyFileName, FileAttributes.ReadOnly); new FileInfo(copyFileName) { LastWriteTime = modDate }; File2.AddAttribute(copyFileName, FileAttributes.ReadOnly); } catch (Exception ex) { string errorMessage = string.Format(_ERROR_BACKUP, fileName, copyFileName); throw new Exception(errorMessage, ex); } }
/// <summary> /// Saves the current file to disk. /// </summary> public override void Save() { try { byte[] xmlData; // Saving XML then re-building config file _Doc.Save(_WorkingFileName); using ( BinaryReader reader = new BinaryReader(new FileStream(_WorkingFileName, FileMode.Open, FileAccess.Read))) { xmlData = reader.ReadBytes((int)reader.BaseStream.Length); } using ( BinaryWriter writer = new BinaryWriter(new FileStream(_WorkingFileName, FileMode.Create, FileAccess.Write))) { writer.Write(xmlData); writer.Write(_DirtyData); } // File encryption File2.RemoveAttribute(_FileName, FileAttributes.ReadOnly); XTEAEncryption.Encrypt(_WorkingFileName, _FileName); } catch (Exception ex) { throw new Exception("Unable to save file: " + _FileName, ex); } }
/// <summary> /// Get file from patch contents and put it into mod repository /// </summary> /// <param name="fileName"></param> /// <param name="fileType"></param> /// <param name="sourceDirectory"></param> /// <param name="modFolder"></param> private static void _GetContents(string fileName, string fileType, string sourceDirectory, string modFolder) { string subFolder = ""; if (FileTypeVP.TYPE_GAUGES_HIGH.Equals(fileType)) { subFolder = InstallHelper.FOLDER_HI_GAUGE_PART; } else if (FileTypeVP.TYPE_GAUGES_LOW.Equals(fileType)) { subFolder = InstallHelper.FOLDER_LO_GAUGE_PART; } string sourceFileName = PatchHelper.CurrentPath + @"\" + sourceDirectory + @"\" + fileName; string destinationFileName = modFolder + subFolder + fileName; // Removing read-only flag on destination file if (File.Exists(destinationFileName)) { File2.RemoveAttribute(destinationFileName, FileAttributes.ReadOnly); } // If file with same name already exists in repo, it is overwritten File.Copy(sourceFileName, destinationFileName, true); }
/// <summary> /// Backups target file then replaces it by file to install /// </summary> /// <param name="targetFileName">File to replace</param> /// <param name="fileToInstall">New file</param> private static void _Replace(string targetFileName, string fileToInstall) { if (string.IsNullOrEmpty(fileToInstall) || string.IsNullOrEmpty(targetFileName)) { return; } try { // Does backup already exist ? string backupFileName = targetFileName + "." + EXTENSION_BACKUP; if (!File.Exists(backupFileName)) { throw new Exception(_ERROR_NOTHING_TO_UPDATE + ": " + targetFileName); } // Removing read-only attribute File2.RemoveAttribute(targetFileName, FileAttributes.ReadOnly); // Copy File.Copy(fileToInstall, targetFileName, true); } catch (Exception ex) { MessageBoxes.ShowWarning(_Instance, ex.Message); } }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="type"></param> /// <param name="backupDirectory"></param> /// <param name="tduFolder"></param> private static void _UninstallFile(string name, string type, string backupDirectory, string tduFolder) { string sourceSubFolder = ""; if (FileTypeVP.TYPE_GAUGES_HIGH.Equals(type)) { sourceSubFolder = InstallHelper.FOLDER_HI_GAUGE_PART; } else if (FileTypeVP.TYPE_GAUGES_LOW.Equals(type)) { sourceSubFolder = InstallHelper.FOLDER_LO_GAUGE_PART; } string sourceFileName = string.Concat(backupDirectory, sourceSubFolder, name); string destinationFileName = string.Concat(tduFolder, name); // Removing read-only flag on destination file if (File.Exists(destinationFileName)) { File2.RemoveAttribute(destinationFileName, FileAttributes.ReadOnly); } // Backup file is kept File.Copy(sourceFileName, destinationFileName, true); }
/// <summary> /// Get file from patch contents and put it into mod folder /// </summary> /// <param name="bnkFilePath"></param> /// <param name="sourceDirectory"></param> /// <param name="packedFileName"></param> /// <param name="modFolder"></param> private static void _GetContents(string bnkFilePath, string sourceDirectory, string packedFileName, string modFolder) { if (sourceDirectory == null) { sourceDirectory = ""; } FileInfo fi = new FileInfo(bnkFilePath); string sourceFileName = string.Concat(PatchHelper.CurrentPath, @"\", sourceDirectory, @"\", packedFileName); // BUG_84: suffix added to folder string modSubFolder = string.Concat(fi.Name, _SUFFIX_PACKED); string destinationFileName = string.Concat(modFolder, @"\", modSubFolder, @"\", packedFileName); // If file with same name already exists in repo, it is overwritten fi = new FileInfo(destinationFileName); if (fi.Directory == null) { throw new Exception("Invalid file name: " + destinationFileName); } if (!Directory.Exists(fi.Directory.FullName)) { Directory.CreateDirectory(fi.Directory.FullName); } // Removing read-only flag on destination file if (fi.Exists) { File2.RemoveAttribute(destinationFileName, FileAttributes.ReadOnly); } File.Copy(sourceFileName, destinationFileName, true); }
/// <summary> /// Writes camera file back /// </summary> public override void Save() { // Removing read-only attribute if (File.Exists(_FileName)) { File2.RemoveAttribute(_FileName, FileAttributes.ReadOnly); } using (BinaryWriter writer = new BinaryWriter(new FileStream(_FileName, FileMode.Create, FileAccess.Write))) { // Header _WriteHeader(writer); // Camera index _WriteIndex(writer); // Browsing camera sets foreach (CamEntry anotherEntry in _entries) { if (anotherEntry.isValid) { // Views foreach (View anotherView in anotherEntry.views) { // Zero1 (4 bytes) writer.Write(0); // Unknown1 (180 bytes) writer.Write(anotherView.unknown1); // Tag (?) writer.Write(anotherView.tag); // Camera id writer.Write(anotherView.cameraId); // Zero2 (2 bytes) writer.Write((short)0); // View type writer.Write((short)anotherView.type); // Zero3 (2 bytes) writer.Write((short)0); // View name + custom properties writer.Write(_SetViewName(anotherView)); // Unknown2 (420 bytes) // Positions : 195>source, 211>target anotherView.unknown2[195] = (byte)anotherView.source; anotherView.unknown2[211] = (byte)anotherView.target; writer.Write(anotherView.unknown2); } } } } }
/// <summary> /// Met à jour les tailles des entrées spécifiées. /// Ecrit directement dans le fichier /// </summary> /// <param name="idList">liste d'identifiants internes de fichiers</param> /// <param name="size1List">liste des tailles 1 à appliquer</param> /// <param name="size2List">liste des tailles 2 à appliquer</param> public void UpdateEntrySizes(Collection <uint> idList, Collection <uint> size1List, Collection <uint> size2List) { if (idList == null || size1List == null || size2List == null) { return; } // Parcours des entrées for (int i = 0; i < idList.Count; i++) { uint fileId = idList[i]; uint size1 = size1List[i]; uint size2 = size2List[i]; // Récupération de l'entrée Entry e = EntryList[fileId]; // Mise à jour de la structure e.firstSize = size1; e.secondSize = size2; // Enregistrement dans le fichier BinaryWriter mapWriter = null; try { // ANO_22 : on enlève l'attribut 'lecture seule' sur le fichier File2.RemoveAttribute(FileName, FileAttributes.ReadOnly); mapWriter = new BinaryWriter(new FileStream(FileName, FileMode.Open)); mapWriter.BaseStream.Seek(e.address, SeekOrigin.Begin); // Taille 1 mapWriter.BaseStream.Seek(0x4, SeekOrigin.Current); mapWriter.Write(BinaryTools.ToLittleEndian(size1)); // Taille 2 mapWriter.BaseStream.Seek(0x4, SeekOrigin.Current); mapWriter.Write(BinaryTools.ToLittleEndian(size2)); // Mise à jour de la liste d'entrées EntryList.Remove(fileId); EntryList.Add(fileId, e); } finally { if (mapWriter != null) { mapWriter.Close(); } } } }
/// <summary> /// Writes current BNK file to disk. /// This method is not to be called directly in BNK context. /// </summary> public override void Save() { try { // Removing read-only flag File2.RemoveAttribute(_FileName, FileAttributes.ReadOnly); // Getting all data byte[] fileSizeData = _WriteSection(SectionType.FileSize); byte[] typeMappingData = new byte[0]; byte[] fileNameData = _WriteSection(SectionType.FileName); byte[] fileOrderData = _WriteSection(SectionType.FileOrder); byte[] fileData = _WriteSection(SectionType.FileData); if (_GetSection(SectionType.TypeMapping).isPresent) { typeMappingData = _WriteSection(SectionType.TypeMapping); } // Computing new section addresses _ComputeSectionAddresses(fileSizeData, typeMappingData, fileNameData, fileOrderData); // Warning ! Files address may have changed. fileSize section data may need to be updated (written again) fileSizeData = _WriteSection(SectionType.FileSize); // Header is written, finally byte[] headerData = _WriteSection(SectionType.Header); // Writing into file using (BinaryWriter newBnkWriter = new BinaryWriter(new FileStream(_FileName, FileMode.Create, FileAccess.Write))) { newBnkWriter.Write(headerData); newBnkWriter.Write(fileSizeData); newBnkWriter.Write(typeMappingData); newBnkWriter.Write(fileNameData); newBnkWriter.Write(fileOrderData); newBnkWriter.Write(fileData); } } catch (Exception ex) { throw new Exception("Error when saving BNK file: " + _FileName, ex); } }
protected override void _Process() { // Parameters string fileName = _GetParameter(PatchInstructionParameter.ParameterName.fileName); string address = _GetParameter(PatchInstructionParameter.ParameterName.valueAddress); string value = _GetParameter(PatchInstructionParameter.ParameterName.value); string length = _GetParameter(PatchInstructionParameter.ParameterName.valueLength); int intAddress = int.Parse(address); long longValue; long longLength; if (string.IsNullOrEmpty(value)) { longValue = 0; } else { longValue = long.Parse(value); } if (string.IsNullOrEmpty(length)) { longLength = 1; } else { longLength = long.Parse(length); } BinaryWriter writer = null; try { // Removes the read-only attribute File2.RemoveAttribute(fileName, FileAttributes.ReadOnly); // File opening writer = new BinaryWriter(new FileStream(fileName, FileMode.Open, FileAccess.Write)); // Data writing writer.Seek(intAddress, SeekOrigin.Begin); switch (longLength) { case 1: writer.Write((byte)longValue); break; case 2: writer.Write((short)longValue); break; case 4: writer.Write((int)longValue); break; case 8: default: writer.Write(longValue); break; } } finally { if (writer != null) { writer.Close(); } } }