/// <summary> /// EVO_91: in topic mode, writes changes to BNK file back /// </summary> private void _SaveToCurrentDatabase() { EditHelper.Task currentTask = EditHelper.Instance.GetTask(_EditedFile); if (currentTask.isValid) { EditHelper.Instance.ApplyChanges(currentTask); } }
private void DBEditorForm_FormClosed(object sender, FormClosedEventArgs e) { _SearchBoxInstance = null; // Nettoyage log StatusBarLogManager.RemoveLog(this); // Task cleaning EditHelper.Task currentTask = EditHelper.Instance.GetTask(_EditedFile); EditHelper.Instance.RemoveTask(currentTask); }
/// <summary> /// Prépare le chargement d'une catégorie depuis la base de données courante /// </summary> /// <param name="category">Type de données recherché</param> /// <param name="culture">Code pays</param> private void _LoadFromCurrentDatabase(DB.Topic category, DB.Culture culture) { // Localisation du fichier BNK string bnkPath = Program.ApplicationSettings.TduMainFolder + LibraryConstants.FOLDER_DB + DB.GetBNKFileName(culture); BNK bnk = TduFile.GetFile(bnkPath) as BNK; if (bnk != null && bnk.Exists) { string fileName = DB.GetFileName(culture, category); string filePath = bnk.GetPackedFilesPaths(fileName)[0]; // EVO_91: using edit support from ModdingLibrary EditHelper.Task currentTask = EditHelper.Instance.AddTask(bnk, filePath, true); _EditedFile = currentTask.extractedFile; } }
/// <summary> /// Loads the whole specified topic for edit mode and returns corresponding TduFiles and EditTasks /// </summary> /// <param name="topic"></param> /// <param name="culture"></param> /// <param name="bnkFile"></param> /// <param name="rBnkFile"></param> /// <param name="returnedTasks"></param> /// <returns></returns> public static TduFile[] LoadTopicForEdit(DB.Topic topic, DB.Culture culture, BNK bnkFile, BNK rBnkFile, out EditHelper.Task[] returnedTasks) { TduFile[] returnedFiles = new TduFile[2]; returnedTasks = new EditHelper.Task[2]; if (bnkFile != null && bnkFile.Exists) { // Getting files string dbFilePath = bnkFile.GetPackedFilesPaths(DB.GetFileName(DB.Culture.Global, topic))[0]; returnedTasks[0] = EditHelper.Instance.AddTask(bnkFile, dbFilePath, true); if (culture != DB.Culture.Global && rBnkFile != null && rBnkFile.Exists) { string dbrFilePath = rBnkFile.GetPackedFilesPaths(DB.GetFileName(culture, topic))[0]; returnedTasks[1] = EditHelper.Instance.AddTask(rBnkFile, dbrFilePath, true); } // Loading these files DB main = TduFile.GetFile(returnedTasks[0].extractedFile) as DB; if (main == null || !main.Exists) { throw new Exception(topic + " main database loading failure."); } returnedFiles[0] = main; // Resource (optional) if (returnedTasks[1].isValid) { DBResource resource = TduFile.GetFile(returnedTasks[1].extractedFile) as DBResource; if (resource == null || !resource.Exists) { throw new Exception(string.Concat(topic, "-", culture, " resource database loading failure.")); } returnedFiles[1] = resource; } } return(returnedFiles); }
/// <summary> /// If database files are modified, asks the user if he's willing to continue without applying changes /// </summary> /// <returns>true if user has accepted to continue or nothing has changed, else false</returns> private bool _ManagePendingChanges() { bool result = true; EditHelper.Task currentTask = EditHelper.Instance.GetTask(_EditedFile); if (currentTask.isValid && currentTask.trackedFileHasChanged) { DialogResult dr = MessageBoxes.ShowQuestion(this, _QUESTION_CONTINUE, MessageBoxButtons.YesNo); if (dr != DialogResult.Yes) { result = false; } } return(result); }
private void discardButton_Click(object sender, EventArgs e) { // Click on "Discard" button // Supprime les tâches d'édition sélectionnées long checkedCount = ListView2.CheckedCount(editTasksListView); if (checkedCount == 0) { MessageBoxes.ShowWarning(this, _ERROR_DISCARD_CHECK); return; } string question = string.Format(_QUESTION_DISCARD_CHANGES, checkedCount); DialogResult dr = MessageBoxes.ShowQuestion(this, question, MessageBoxButtons.YesNo); if (dr != DialogResult.Yes) { return; } try { Cursor = Cursors.WaitCursor; // Parcours de la liste de tâches à l'écran foreach (ListViewItem anotherItem in editTasksListView.Items) { if (anotherItem.Checked) { EditHelper.Task currentTask = (EditHelper.Task)anotherItem.Tag; EditHelper.Instance.RemoveTask(currentTask); } } Cursor = Cursors.Default; } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } }
private void saveButton_Click(object sender, EventArgs e) { // Clic on 'Save changes' button EditHelper.Task currentTask = EditHelper.Instance.GetTask(_EditedFile); if (currentTask.trackedFileHasChanged) { try { Cursor = Cursors.WaitCursor; _SaveToCurrentDatabase(); // Signals StatusBarLogManager.ShowEvent(this, _STATUS_DB_UPDATE_SUCCESS); Cursor = Cursors.Default; } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } } }
private void refreshButton_Click(object sender, EventArgs e) { // Contrôle de la saisie ... if (openRadioButton.Checked && inputFilePath.Text.Equals("")) { return; } // Pending changes ? if (_ManagePendingChanges()) { // Récupération des infos try { string dbFileName; EditHelper.Task currentTask = EditHelper.Instance.GetTask(_EditedFile); Cursor = Cursors.WaitCursor; // Task update if (currentTask.isValid) { EditHelper.Instance.RemoveTask(currentTask); _EditedFile = null; } // Selon le type if (openRadioButton.Checked) { // Par nom de fichier dbFileName = inputFilePath.Text; } else { // Par catégorie string category = catComboBox.SelectedItem.ToString(); DB.Topic topic = (DB.Topic)Enum.Parse(typeof(DB.Topic), category); _LoadFromCurrentDatabase(topic, Program.ApplicationSettings.GetCurrentCulture()); dbFileName = _EditedFile; } _LeDB = TduFile.GetFile(dbFileName) as DBResource; if (_LeDB != null) { // Mise à jour des entrées _UpdateEntryList(false); } // Réinitialisation des champs d'édition lineLabel.Text = _LABEL_ENTRY_NUMBER; idTextBox.Text = valueTextBox.Text = ""; // Mise à jour statut toolStripStatusLabel.Text = ""; Cursor = Cursors.Default; } catch (Exception ex) { // EVO_91: Manage EditHelper's error codes switch (ex.Message) { case EditHelper.ERROR_CODE_TASK_EXISTS: ex = new Exception(_ERROR_EDITED_TWICE, ex); break; case EditHelper.ERROR_CODE_EXTRACT_FAILED: case EditHelper.ERROR_CODE_INVALID_PACKED_FILE: case EditHelper.ERROR_CODE_TEMP_FOLDER: ex = new Exception(_ERROR_EDITING, ex); break; } MessageBoxes.ShowError(this, ex); } } }
/// <summary> /// Loads vehicle data from database and reference /// </summary> /// <param name="slotRef">Reference of vehicle slot to load</param> private void _LoadVehicleData(string slotRef) { if (string.IsNullOrEmpty(slotRef)) { return; } Cursor = Cursors.WaitCursor; try { StatusBarLogManager.ShowEvent(this, _STATUS_LOADING_VEHICLE); // Cleaning tasks _ClearCurrentTasks(); _CurrentVehicle = slotRef; _CurrentSlotName = VehicleSlotsHelper.SlotReferenceReverse[slotRef]; // Loading database DB.Culture currentCulture = Program.ApplicationSettings.GetCurrentCulture(); EditHelper.Task[] currentTasks; string dbBnkFile = Program.ApplicationSettings.TduMainFolder + LibraryConstants.FOLDER_DB + DB.GetBNKFileName(DB.Culture.Global); BNK bnkFile = TduFile.GetFile(dbBnkFile) as BNK; string resourceBnkFile = Program.ApplicationSettings.TduMainFolder + LibraryConstants.FOLDER_DB + DB.GetBNKFileName(currentCulture); BNK rBnkFile = TduFile.GetFile(resourceBnkFile) as BNK; // 1.CarRims TduFile[] currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.CarRims, DB.Culture.Global, bnkFile, rBnkFile, out currentTasks); _CurrentCarRimsTask = currentTasks[0]; _CarRimsTable = currentFiles[0] as DB; // 2.Rims currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.Rims, currentCulture, bnkFile, rBnkFile, out currentTasks); _CurrentRimsTask = currentTasks[0]; _CurrentRimsResourceTask = currentTasks[1]; _RimsTable = currentFiles[0] as DB; _RimsResource = currentFiles[1] as DBResource; // 3.CarPhysicsData currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.CarPhysicsData, currentCulture, bnkFile, rBnkFile, out currentTasks); _CurrentPhysicsTask = currentTasks[0]; _CurrentPhysicsResourceTask = currentTasks[1]; _PhysicsTable = currentFiles[0] as DB; _PhysicsResource = currentFiles[1] as DBResource; // 4.Brands currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.Brands, currentCulture, bnkFile, rBnkFile, out currentTasks); _CurrentBrandsTask = currentTasks[0]; _CurrentBrandsResourceTask = currentTasks[1]; _BrandsTable = currentFiles[0] as DB; _BrandsResource = currentFiles[1] as DBResource; // 5.CarShops currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.CarShops, currentCulture, bnkFile, rBnkFile, out currentTasks); _CurrentCarShopsTask = currentTasks[0]; _CurrentCarShopsResourceTask = currentTasks[1]; _CarShopsTable = currentFiles[0] as DB; _CarShopsResource = currentFiles[1] as DBResource; // 6.CarPacks currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.CarPacks, DB.Culture.Global, bnkFile, rBnkFile, out currentTasks); _CurrentCarPacksTask = currentTasks[0]; _CarPacksTable = currentFiles[0] as DB; // 7.Colors currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.CarColors, currentCulture, bnkFile, rBnkFile, out currentTasks); _CurrentCarColorsTask = currentTasks[0]; _CurrentCarColorsResourceTask = currentTasks[1]; _CarColorsTable = currentFiles[0] as DB; _CarColorsResource = currentFiles[1] as DBResource; // 8.Interior currentFiles = DatabaseHelper.LoadTopicForEdit(DB.Topic.Interior, currentCulture, bnkFile, rBnkFile, out currentTasks); _CurrentInteriorTask = currentTasks[0]; _CurrentInteriorResourceTask = currentTasks[1]; _InteriorTable = currentFiles[0] as DB; _InteriorResource = currentFiles[1] as DBResource; // 9.Colors id reference ColorsHelper.InitIdReference(_CarColorsResource, _InteriorResource); // 10.Cameras string camBinFile = LibraryConstants.GetSpecialFile(LibraryConstants.TduSpecialFile.CamerasBin); _CameraData = TduFile.GetFile(camBinFile) as Cameras; /* GUI */ // Install Tab _InitializeInstallContents(); // Camera-IK Tab _RefreshCameraIKContents(); // Datasheet tab _InitializeDatasheetContents(); // Dealers tab _InitializeDealersContents(); // Tuner tab _InitializeTunerContents(); // Physics tab _InitializePhysicsContents(); // Colors tab _InitializeColorsContents(); // Modification flags _IsDatabaseModified = false; _IsCameraModified = false; // Clearing state vars _CurrentAvailabilitySpot = null; _CurrentTuningBrand = null; // Vehicle name display _UpdateSlotAndModName(); // Tabs are enabled vehicleEditTabControl.Enabled = true; StatusBarLogManager.ShowEvent(this, _STATUS_VEHICLE_READY); } catch (Exception ex) { // All tasks must be cleaned _ClearCurrentTasks(); // Processing special error messages if (EditHelper.ERROR_CODE_TASK_EXISTS.Equals(ex.Message)) { MessageBoxes.ShowError(this, new Exception(_ERROR_LOADING_VEHICLE_CONFLICT, ex)); } else { MessageBoxes.ShowError(this, new Exception(_ERROR_LOADING_VEHICLE, ex)); } StatusBarLogManager.ShowEvent(this, ""); } Cursor = Cursors.Default; }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Checking parameter string vehicleRef = _GetParameter(PatchInstructionParameter.ParameterName.vehicleDatabaseId); // Modifying corresponding topic EditHelper.Task dbTask = new EditHelper.Task(); string bnkFilePath = ""; try { string dbFileName = null; // 1. Creating edit task try { bnkFilePath = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(DB.Culture.Global)); BNK databaseBNK = TduFile.GetFile(bnkFilePath) as BNK; if (databaseBNK != null) { dbFileName = DB.GetFileName(DB.Culture.Global, DB.Topic.CarShops); dbTask = EditHelper.Instance.AddTask(databaseBNK, databaseBNK.GetPackedFilesPaths(dbFileName)[0], true); } } catch (Exception ex) { throw new Exception("Unable to create edit task on BNK file: " + bnkFilePath, ex); } // 2. Getting TduFile DB db; try { db = TduFile.GetFile(dbTask.extractedFile) as DB; if (db == null || !db.Exists) { throw new Exception("Extracted db file not found!"); } } catch (Exception ex) { throw new Exception("Unable to gain access to DB contents: " + dbFileName, ex); } // 3. Setting values in DB file DatabaseHelper.RemoveValueFromAnyColumn(db, vehicleRef, DatabaseConstants.COMPACT1_PHYSICS_DB_RESID); Log.Info("Entry updating completed: " + vehicleRef); // 4. Saving try { db.Save(); EditHelper.Instance.ApplyChanges(dbTask); } catch (Exception ex) { throw new Exception("Unable to save BNK file: " + bnkFilePath, ex); } } finally { // Cleaning up EditHelper.Instance.RemoveTask(dbTask); } }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Parameters string vehicleName = _GetParameter(PatchInstructionParameter.ParameterName.slotFullName); string cameraId = _GetParameter(PatchInstructionParameter.ParameterName.cameraIKIdentifier); // Loading reference VehicleSlotsHelper.InitReference(PatchHelper.CurrentPath); // Checking validity if (!VehicleSlotsHelper.SlotReference.ContainsKey(vehicleName)) { throw new Exception("Specified vehicle name is not supported: " + vehicleName); } if (!VehicleSlotsHelper.CamReference.ContainsKey(cameraId)) { throw new Exception("Specified camera identifier is not supported: " + cameraId); } // Edit task EditHelper.Task task = new EditHelper.Task(); try { try { string bnkFileName = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(DB.Culture.Global)); BNK dbBnkFile = TduFile.GetFile(bnkFileName) as BNK; if (dbBnkFile != null) { string dbFilePath = dbBnkFile.GetPackedFilesPaths(DB.GetFileName(DB.Culture.Global, DB.Topic.CarPhysicsData))[0]; task = EditHelper.Instance.AddTask(dbBnkFile, dbFilePath, true); } } catch (Exception ex) { throw new Exception("Unable to get TDU database contents in DB.BNK.", ex); } // Opens packed file DB physicsDB = TduFile.GetFile(task.extractedFile) as DB; if (physicsDB == null) { throw new Exception("Unable to get CarPhysicsData information."); } // Changes camera try { string vehicleRef = VehicleSlotsHelper.SlotReference[vehicleName]; VehicleSlotsHelper.ChangeCameraById(vehicleRef, cameraId, physicsDB); } catch (Exception ex) { throw new Exception("Unable to use new camera set: " + cameraId + " for " + vehicleName, ex); } // Saving try { physicsDB.Save(); EditHelper.Instance.ApplyChanges(task); EditHelper.Instance.RemoveTask(task); } catch (Exception ex) { throw new Exception("Unable to save and replace file in BNK.", ex); } } finally { EditHelper.Instance.RemoveTask(task); } }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Checking parameter string vehicleRef = _GetParameter(PatchInstructionParameter.ParameterName.vehicleDatabaseId); string spotAndSlots = _GetParameter(PatchInstructionParameter.ParameterName.resourceValues); // Modifying corresponding topic EditHelper.Task dbTask = new EditHelper.Task(); string bnkFilePath = ""; try { string dbFileName = null; // 1. Creating edit task try { bnkFilePath = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(DB.Culture.Global)); BNK databaseBNK = TduFile.GetFile(bnkFilePath) as BNK; if (databaseBNK != null) { dbFileName = DB.GetFileName(DB.Culture.Global, DB.Topic.CarShops); dbTask = EditHelper.Instance.AddTask(databaseBNK, databaseBNK.GetPackedFilesPaths(dbFileName)[0], true); } } catch (Exception ex) { throw new Exception("Unable to create edit task on BNK file: " + bnkFilePath, ex); } // 2. Getting TduFile DB db; try { db = TduFile.GetFile(dbTask.extractedFile) as DB; if (db == null || !db.Exists) { throw new Exception("Extracted db file not found!"); } } catch (Exception ex) { throw new Exception("Unable to gain access to DB contents: " + dbFileName, ex); } // 3. Setting values in DB file // One identifier (primary key: REF) List <Couple <string> > couples = Tools.ParseCouples(spotAndSlots); // Parsing couples foreach (Couple <string> couple in couples) { string spotRef = couple.FirstValue; string[] slots = couple.SecondValue.Split(new string[] { Tools.SYMBOL_VALUE_SEPARATOR3 }, StringSplitOptions.RemoveEmptyEntries); // Does id exist ? if (db.EntriesByPrimaryKey.ContainsKey(spotRef)) { // Already exists > modify it foreach (string anotherSlot in slots) { string slotColumnName = string.Format(DatabaseConstants.SLOT_CARSHOPS_PATTERN_DB_COLUMN, anotherSlot); DatabaseHelper.UpdateCellFromTopicWherePrimaryKey(db, slotColumnName, spotRef, vehicleRef); } Log.Info("Entry updating completed for spot: " + spotRef + " - " + couple.SecondValue); } else { Log.Error("Specified location does not exist! " + spotRef, null); } } Log.Info("Entry updating completed for vehicle: " + vehicleRef); // 4. Saving try { db.Save(); EditHelper.Instance.ApplyChanges(dbTask); } catch (Exception ex) { throw new Exception("Unable to save BNK file: " + bnkFilePath, ex); } } finally { // Cleaning up EditHelper.Instance.RemoveTask(dbTask); } }
private void applyButton_Click(object sender, EventArgs e) { // Click on "Apply" button // Applique les modifications sur les tâches sélectionnées long checkedCount = ListView2.CheckedCount(editTasksListView); if (checkedCount == 0) { MessageBoxes.ShowWarning(this, _ERROR_APPLY_CHECK); return; } try { Cursor = Cursors.WaitCursor; // Parcours de la liste de tâches à l'écran foreach (ListViewItem anotherItem in editTasksListView.Items) { if (anotherItem.Checked) { EditHelper.Task currentTask = (EditHelper.Task)anotherItem.Tag; // EVO_74 if (currentTask.trackedFileHasChanged) { // Application des modifs si nécessaire string newFileName = currentTask.extractedFile; FileHandler editedFile = FileHandler.GetHandler(newFileName); editedFile.Apply(); // EVO_71: externalization EditHelper.Instance.ApplyChanges(currentTask); // ANO_33: mise à jour de la liste if (FileBrowserForm.Instance.CurrentBnkFile != null) { if ( currentTask.parentBNK.FileName.Equals( FileBrowserForm.Instance.CurrentBnkFile.FileName)) { FileBrowserForm.Instance.SetBnkContentsChanged(); } } // EVO_47: On conserve la tâche pour cumuler les modifs sans avoir à la recréer. } else { checkedCount--; } } } // Mise à jour de la liste NotifyModelChanged(); Cursor = Cursors.Default; if (checkedCount > 0) { // EVO 32: Message de fin d'opération string message = string.Format(_INFO_APPLY_COMPLETE, checkedCount); StatusBarLogManager.ShowEvent(FileBrowserForm.Instance, message); } } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Checking parameters string fileName = _GetParameter(PatchInstructionParameter.ParameterName.resourceFileName); string values = _GetParameter(PatchInstructionParameter.ParameterName.resourceValues); // Modifying corresponding topic DB.Topic currentTopic = (DB.Topic)Enum.Parse(typeof(DB.Topic), fileName); EditHelper.Task dbTask = new EditHelper.Task(); string bnkFilePath = ""; try { string dbFileName = null; // 1. Creating edit task try { bnkFilePath = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(DB.Culture.Global)); BNK databaseBNK = TduFile.GetFile(bnkFilePath) as BNK; if (databaseBNK != null) { dbFileName = DB.GetFileName(DB.Culture.Global, currentTopic); dbTask = EditHelper.Instance.AddTask(databaseBNK, databaseBNK.GetPackedFilesPaths(dbFileName)[0], true); } } catch (Exception ex) { throw new Exception("Unable to create edit task on BNK file: " + bnkFilePath, ex); } // 2. Getting TduFile DB db; try { db = TduFile.GetFile(dbTask.extractedFile) as DB; if (db == null || !db.Exists) { throw new Exception("Extracted db file not found!"); } } catch (Exception ex) { throw new Exception("Unable to gain access to DB contents: " + dbFileName, ex); } // 3. Setting values in DB file // One identifier (primary key: REF) or 2 identifiers (2 first columns) List <Couple <string> > couples = Tools.ParseCouples(values); string[] idArray = new string[couples.Count]; int counter = 0; // Parsing couples foreach (Couple <string> couple in couples) { string id = couple.FirstValue; string value = couple.SecondValue; // Does id exist ? if (db.EntriesByPrimaryKey.ContainsKey(id)) { // Already exists > modify it DatabaseHelper.UpdateAllCellsFromTopicWherePrimaryKey(db, id, value); Log.Info("Entry updating completed: " + id + " - " + value); } else { // Does not exist > create it DatabaseHelper.InsertAllCellsIntoTopic(db, value); Log.Info("Entry adding completed: " + value); } idArray[counter++] = id; } // 4. Saving try { db.Save(); EditHelper.Instance.ApplyChanges(dbTask); } catch (Exception ex) { throw new Exception("Unable to save BNK file: " + bnkFilePath, ex); } } finally { // Cleaning up EditHelper.Instance.RemoveTask(dbTask); } }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Checking parameters string fileName = _GetParameter(PatchInstructionParameter.ParameterName.resourceFileName); string id = _GetParameter(PatchInstructionParameter.ParameterName.databaseId); // Modifying corresponding topic DB.Topic currentTopic = (DB.Topic)Enum.Parse(typeof(DB.Topic), fileName); EditHelper.Task dbTask = new EditHelper.Task(); string bnkFilePath = ""; try { string dbFileName = null; // 1. Creating edit task try { bnkFilePath = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(DB.Culture.Global)); BNK databaseBNK = TduFile.GetFile(bnkFilePath) as BNK; if (databaseBNK != null) { dbFileName = DB.GetFileName(DB.Culture.Global, currentTopic); dbTask = EditHelper.Instance.AddTask(databaseBNK, databaseBNK.GetPackedFilesPaths(dbFileName)[0], true); } } catch (Exception ex) { throw new Exception("Unable to create edit task on BNK file: " + bnkFilePath, ex); } // 2. Getting TduFile DB db; try { db = TduFile.GetFile(dbTask.extractedFile) as DB; if (db == null || !db.Exists) { throw new Exception("Extracted db file not found!"); } } catch (Exception ex) { throw new Exception("Unable to gain access to DB contents: " + dbFileName, ex); } // 3. Removing values in DB file DatabaseHelper.DeleteFromTopicWhereIdentifier(db, id); // 4. Saving try { db.Save(); EditHelper.Instance.ApplyChanges(dbTask); } catch (Exception ex) { throw new Exception("Unable to save BNK file: " + bnkFilePath, ex); } } finally { // Cleaning up EditHelper.Instance.RemoveTask(dbTask); } }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Checking parameters string fileName = _GetParameter(PatchInstructionParameter.ParameterName.resourceFileName); string values = _GetParameter(PatchInstructionParameter.ParameterName.resourceValues); // For each language file DB.Topic currentTopic = (DB.Topic)Enum.Parse(typeof(DB.Topic), fileName); for (int i = 0; i < 8; i++) { DB.Culture currentCulture = (DB.Culture)Enum.Parse(typeof(DB.Culture), i.ToString()); EditHelper.Task resourceTask = new EditHelper.Task(); string bnkFilePath = ""; string resourceFileName = null; // 1. Creating edit task try { bnkFilePath = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(currentCulture)); BNK databaseBNK = TduFile.GetFile(bnkFilePath) as BNK; if (databaseBNK != null) { resourceFileName = DB.GetFileName(currentCulture, currentTopic); resourceTask = EditHelper.Instance.AddTask(databaseBNK, databaseBNK.GetPackedFilesPaths(resourceFileName)[0], true); } } catch (Exception ex) { throw new Exception("Unable to create edit task on BNK file: " + bnkFilePath, ex); } // 2. Getting TduFile DBResource resource; try { resource = TduFile.GetFile(resourceTask.extractedFile) as DBResource; if (resource == null || !resource.Exists) { throw new Exception("Extracted resource db file not found!"); } } catch (Exception ex) { throw new Exception("Unable to gain access to DB contents: " + resourceFileName, ex); } // 3. Setting values in DB file List <Couple <string> > couples = Tools.ParseCouples(values); // Parsing couples foreach (Couple <string> couple in couples) { string id = couple.FirstValue; string value = couple.SecondValue; // Does id exist ? DBResource.Entry currentEntry = resource.GetEntryFromId(id); if (currentEntry.isValid) { // Already exists > modify it currentEntry.value = value; resource.UpdateEntry(currentEntry); Log.Info("Entry succesfully updated : " + id + " - " + value); } else { // Does not exist > create it currentEntry.isValid = true; currentEntry.id = new ResourceIdentifier(id, currentTopic); currentEntry.value = value; currentEntry.index = resource.EntryList.Count + 1; resource.InsertEntry(currentEntry); Log.Info("Entry succesfully added : " + id + " - " + value); } } // 4. Saving try { resource.Save(); EditHelper.Instance.ApplyChanges(resourceTask); } catch (Exception ex) { throw new Exception("Unable to save BNK file: " + bnkFilePath, ex); } // 5. Cleaning up EditHelper.Instance.RemoveTask(resourceTask); } }