private void deleteToolStripButton_Click(object sender, EventArgs e) { // Click on 'Delete' button if (_LeDB == null || entryList.SelectedItems.Count != 1 || entryList.SelectedIndices[0] == entryList.Items.Count - 1) { return; } try { Cursor = Cursors.WaitCursor; // Getting current identifier and corresponding entry string id = entryList.SelectedItems[0].SubItems[1].Text; DBResource.Entry currentEntry = _LeDB.GetEntryFromId(id); // Removing item _LeDB.DeleteEntry(currentEntry); _LeDB.Save(); // List update _UpdateEntryList(true); StatusBarLogManager.ShowEvent(this, _STATUS_DEL_SUCCESS); Cursor = Cursors.Default; } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } }
/// <summary> /// Met à jour l'entrée à la ligne indiquée /// </summary> /// <param name="entry">Entrée à mettre à jour</param> private void _UpdateEntryFromLine(DBResource.Entry entry) { if (!entry.isValid) { return; } try { // Recherche de l'item foreach (ListViewItem anotherItem in entryList.Items) { int currentLine = int.Parse(anotherItem.Text); if (currentLine == entry.index) { if (!_EditedEntry.isComment) { anotherItem.SubItems[1].Text = entry.id.Id; } anotherItem.SubItems[2].Text = entry.value; break; } } } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } }
private void okButton_Click(object sender, EventArgs e) { // Click on OK button > Apply then close try { // According to selected update mode if (selectResourceRadioButton.Checked) { // Another resource id string columnName = _DbTopic.Structure[_ColumnId].name; DatabaseHelper.UpdateCellFromTopicWithEntryId(_DbTopic, columnName, _ResourceId, _EntryId); } else { // Changed resource value DBResource.Entry editedEntry = _DbResource.GetEntryFromId(_ResourceId); editedEntry.value = resourceValueTextBox.Text; _DbResource.UpdateEntry(editedEntry); } DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } finally { Cursor = Cursors.Default; } }
/// <summary> /// Returns final resource value from specified cell. /// Does not support cross reference still. /// </summary> /// <param name="cell">Cell to get value</param> /// <param name="topicResource">Resource file to get values (when valueType = ValueInResource)</param> /// <returns></returns> public static string GetResourceValueFromCell(DB.Cell cell, DBResource topicResource) { string result; // According to value type... switch (cell.valueType) { case DB.ValueType.ReferenceL: case DB.ValueType.ValueInResource: case DB.ValueType.ValueInResourceH: // Corresponding value must be found into resource file DBResource.Entry currentEntry = topicResource.GetEntryFromId(cell.value); result = currentEntry.value; break; default: result = cell.value; break; } // Failsafe operation if (result == null) { result = DatabaseConstants.DEFAULT_RESOURCE_VALUE; Log.Info("WARNING ! Resource with code " + cell.value + " was not found in " + topicResource.CurrentTopic + "-" + topicResource.CurrentCulture + ". Please fix it!"); } return(result); }
/// <summary> /// Returns track name from specified resource identifier /// </summary> /// <param name="trackNameDbResource"></param> /// <returns></returns> private string _GetNameFromResource(string trackNameDbResource) { string returnedName = TrackPackForm._DEFAULT_TRACK_NAME; if (_HousesResource != null && trackNameDbResource != null) { DBResource.Entry currentEntry = _HousesResource.GetEntryFromId(trackNameDbResource); returnedName = currentEntry.value; } return(returnedName); }
private void insertToolStripButton_Click(object sender, EventArgs e) { // Click on 'Insert' button if (_LeDB == null || entryList.SelectedItems.Count != 1) { return; } try { // EVO_48 : status bar update StatusBarLogManager.ShowEvent(this, _STATUS_INSERTING); // Getting current entry index int selectedLine; if (entryList.SelectedItems[0].Text.Equals(_ENTRY_END)) { selectedLine = entryList.Items.Count; } else { selectedLine = int.Parse(entryList.SelectedItems[0].Text); } DBResource.Entry newData = new DBResource.Entry { isValid = true, index = selectedLine }; _EditedEntry = newData; // Updating fields lineLabel.Text = selectedLine.ToString(); valueTextBox.Clear(); idTextBox.Clear(); idTextBox.Focus(); } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } }
private void modifyButton_Click(object sender, EventArgs e) { // Clic sur le bouton 'Modify' if (_LeDB == null || entryList.SelectedItems.Count != 1 || entryList.SelectedIndices[0] == entryList.Items.Count - 1) { return; } // EVO 48 : mise à jour de la barre de statut StatusBarLogManager.ShowEvent(this, _STATUS_EDITING); // Récupération de la donnée à éditer int selectedLine = int.Parse(entryList.SelectedItems[0].Text); DBResource.Entry data = _LeDB.GetEntryFromLine(selectedLine); if (data.isValid) { _EditedEntry = data; // Mise à jour des champs d'édition lineLabel.Text = data.index.ToString(); valueTextBox.Text = data.value; if (data.isComment) { idTextBox.Text = ""; valueTextBox.Focus(); } else { idTextBox.Text = data.id.Id; idTextBox.Focus(); } } }
/// <summary> /// What the instruction should do /// </summary> protected override void _Process() { // Checking parameters string databaseIdentifier = _GetParameter(PatchInstructionParameter.ParameterName.databaseId); string rimName = _GetParameter(PatchInstructionParameter.ParameterName.rimName); string tempFolder = ""; try { // 1. Extracting brands BNK databaseBNK; string rimsDBFileName; string rimsDBFilePath; try { string bnkFilePath = string.Concat(LibraryConstants.GetSpecialFolder(LibraryConstants.TduSpecialFolder.Database), DB.GetBNKFileName(PatchHelper.CurrentCulture)); rimsDBFileName = DB.GetFileName(PatchHelper.CurrentCulture, DB.Topic.Rims); databaseBNK = TduFile.GetFile(bnkFilePath) as BNK; if (databaseBNK == null) { throw new Exception("Database BNK file is invalid."); } // Files are extracted to a temporary location rimsDBFilePath = databaseBNK.GetPackedFilesPaths(rimsDBFileName)[0]; tempFolder = File2.SetTemporaryFolder(null, LibraryConstants.FOLDER_TEMP, true); databaseBNK.ExtractPackedFile(rimsDBFilePath, tempFolder, true); } catch (Exception ex) { throw new Exception("Unable to extract DB file.", ex); } // 2. Getting TduFile DBResource rimsDBResource; try { rimsDBResource = TduFile.GetFile(tempFolder + @"\" + rimsDBFileName) as DBResource; if (rimsDBResource == null || !rimsDBResource.Exists) { throw new Exception("Extracted rim db file not found!"); } } catch (Exception ex) { throw new Exception("Unable to gain access to DB contents.", ex); } // 3. Setting value in DB file DBResource.Entry rimsEntry = rimsDBResource.GetEntryFromId(databaseIdentifier); try { if (rimsEntry.isValid) { rimsEntry.value = rimName; rimsDBResource.UpdateEntry(rimsEntry); } else { throw new Exception("Unable to retrieve a DB entry for identifier: " + databaseIdentifier); } } catch (Exception ex) { throw new Exception("Unable to locate DB entry to update.", ex); } // 4. Saving try { rimsDBResource.Save(); } catch (Exception ex) { throw new Exception("Unable to save DB files.", ex); } // 5. File replacing try { // Rims databaseBNK.ReplacePackedFile(rimsDBFilePath, tempFolder + @"\" + rimsDBFileName); } catch (Exception ex) { throw new Exception("Unable to replace database files in BNK.", ex); } } finally { // Cleaning up try { File2.RemoveTemporaryFolder(null, tempFolder); } catch (Exception ex) { // Silent Exception2.PrintStackTrace(ex); } } }
private void editOKButton_Click(object sender, EventArgs e) { // Clic sur le bouton 'OK' if (!_EditedEntry.isValid) { return; } // EVO_xx : line adding feature bool isAddingEntry = (_EditedEntry.id == null); // Prépare les données pour la mise à jour string newId = idTextBox.Text; string value = valueTextBox.Text; try { Cursor = Cursors.WaitCursor; // According to mode if (isAddingEntry) { // Add // Is this identifier a UID ? if (_LeDB.GetEntryFromId(newId).isValid) { idTextBox.Focus(); throw new Exception(_ERROR_ID_EXISTS); } // Identifier update if (!_EditedEntry.isComment) { _EditedEntry.id = new ResourceIdentifier(newId, _LeDB.CurrentTopic); } // Value update _EditedEntry.value = value; // Update file data _LeDB.InsertEntry(_EditedEntry); _LeDB.Save(); // List update _UpdateEntryList(true); // Signals if (_EditedFile == null) { StatusBarLogManager.ShowEvent(this, _STATUS_INS_SUCCESS_1); } else { StatusBarLogManager.ShowEvent(this, _STATUS_INS_SUCCESS_2); } } else { // Modify // Is update necessary ? if (!newId.Equals(_EditedEntry.id.Id) || !value.Equals(_EditedEntry.value)) { // Identifier update if (!_EditedEntry.isComment) { _EditedEntry.id = new ResourceIdentifier(newId, _LeDB.CurrentTopic); } // Value update _EditedEntry.value = value; // Met à jour les données du fichier DBResource.Entry entryAfterUpdate = _LeDB.UpdateEntry(_EditedEntry); _LeDB.Save(); // Met à jour la ligne dans le tableau _UpdateEntryFromLine(entryAfterUpdate); // Signals if (_EditedFile == null) { StatusBarLogManager.ShowEvent(this, _STATUS_MOD_SUCCESS_1); } else { StatusBarLogManager.ShowEvent(this, _STATUS_MOD_SUCCESS_2); } } else { StatusBarLogManager.ShowEvent(this, ""); } } // Clearing fields lineLabel.Text = ""; idTextBox.Clear(); valueTextBox.Clear(); Cursor = Cursors.Default; } catch (Exception ex) { MessageBoxes.ShowError(this, ex); } }
/// <summary> /// Fixes specified corruption /// </summary> /// <param name="corruptionEntry"></param> public static void Fix(Corruption corruptionEntry) { // Has topic been loaded yet ? if (_Data.ContainsKey(corruptionEntry.topic)) { // Data access TduFile[] files = _Data[corruptionEntry.topic]; DB db = files[0] as DB; DBResource res = files[1] as DBResource; TduFile[] refFiles = _Data[corruptionEntry.referencedTopic]; DB refDb = refFiles[0] as DB; DBResource refRes = refFiles[1] as DBResource; TduFile[] backupFiles = _BackupData[corruptionEntry.topic]; DB backupDb = backupFiles[0] as DB; DBResource backupRes = backupFiles[1] as DBResource; TduFile[] backupRefFiles = _BackupData[corruptionEntry.referencedTopic]; DB backupRefDb = backupRefFiles[0] as DB; DBResource backupRefRes = backupRefFiles[1] as DBResource; // Integrity checks if (db == null || res == null || refDb == null || refRes == null) { throw new Exception("Database loading error."); } if (backupDb == null || backupRes == null || backupRefDb == null || backupRefRes == null) { throw new Exception("Database backup loading error."); } string colName = corruptionEntry.corruptedCell.name; // According to corruption kind switch (corruptionEntry.kind) { case CorruptionKind.UnknownReferencedTopic: throw new Exception("UnknownReferencedTopic issue can't be solved yet."); case CorruptionKind.StructureMissingColumns: throw new Exception("StructureMissingColumns issue can't be solved yet."); case CorruptionKind.MissingReference: // If referenced entry does not exist in backup, restore referenced id from backup // else restore entry string referencedEntryPk = corruptionEntry.corruptedValue; List <DB.Entry> entries = DatabaseHelper.SelectAllCellsFromTopicWherePrimaryKey(backupRefDb, new[] { referencedEntryPk }); if (entries.Count == 0) { DB.Entry currentEntry = DatabaseHelper.SelectAllCellsFromTopicWhereId(db, corruptionEntry.entryId)[0]; string entryPk = currentEntry.primaryKey; List <DB.Cell> backupCells = DatabaseHelper.SelectCellsFromTopicWherePrimaryKey(colName, backupDb, new[] { entryPk }); if (backupCells.Count == 0) { // Worst case : referenced item not found in backup > must restore original reference DB.Cell cleanCell = DatabaseHelper.SelectCellsFromTopicWithEntryId(colName, backupDb, new[] { corruptionEntry.entryId })[0]; DatabaseHelper.UpdateCellFromTopicWithEntryId(db, colName, cleanCell.value, corruptionEntry.entryId); Log.Info("Fixed issue : " + corruptionEntry.kind + " by setting new reference: " + cleanCell.value + " in " + colName + ", line " + corruptionEntry.entryId); } else { DatabaseHelper.UpdateCellFromTopicWherePrimaryKey(db, colName, entryPk, backupCells[0].value); Log.Info("Fixed issue : " + corruptionEntry.kind + " by setting new reference: " + backupCells[0].value + " in " + colName + ", line " + corruptionEntry.entryId); } db.Save(); } else { DatabaseHelper.InsertAllCellsIntoTopic(refDb, entries[0]); refDb.Save(); Log.Info("Fixed issue : " + corruptionEntry.kind + " by creating new line."); } break; case CorruptionKind.MissingResource: // If resource exists in backup, restore resource id // else restore referenced resource id from backup DBResource backupResource; DBResource resource; if (corruptionEntry.topic == corruptionEntry.referencedTopic) { // Resource is in the same topic backupResource = backupRes; resource = res; } else { // Resource is in another topic backupResource = backupRefRes; resource = refRes; } DBResource.Entry backupResEntry = backupResource.GetEntryFromId(corruptionEntry.corruptedValue); if (backupResEntry.isValid) { resource.InsertEntry(backupResEntry); resource.Save(); Log.Info("Fixed issue : " + corruptionEntry.kind + " by creating new resource: " + backupResEntry.value + " in " + resource.CurrentTopic + ", id=" + backupResEntry.id); } else { DB.Cell backupCell = DatabaseHelper.SelectCellsFromTopicWithEntryId(colName, backupDb, corruptionEntry.entryId)[0]; // Trying to get resource from backup backupResEntry = backupResource.GetEntryFromId(backupCell.value); if (backupResEntry.isValid) { DatabaseHelper.UpdateCellFromTopicWithEntryId(db, colName, backupCell.value, corruptionEntry.entryId); db.Save(); Log.Info("Fixed issue : " + corruptionEntry.kind + " by setting new reference to resource: " + backupCell.value + " in " + colName + ", line " + corruptionEntry.entryId); } else { // Applying default resource value backupResEntry.id = new ResourceIdentifier(backupCell.value, backupResource.CurrentTopic); backupResEntry.isValid = true; backupResEntry.value = _DEFAULT_RESOURCE_VALUE; backupResEntry.index = resource.EntryList.Count; resource.InsertEntry(backupResEntry); resource.Save(); Log.Info("Fixed issue : " + corruptionEntry.kind + " by creating new resource: " + backupResEntry.value + " in " + resource.CurrentTopic + ", id=" + backupResEntry.id); } } break; } } else { throw new Exception("Please load corresponding data and resource first!"); } }
/// <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); } }