private void batchExportXMSBTToolStripMenuItem_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Select a directory containing MSBT files:"; fbd.ShowNewFolderButton = false; fbd.SelectedPath = Settings.Default.XMSBTDirectory; if (fbd.ShowDialog() == DialogResult.OK) { Settings.Default.XMSBTDirectory = fbd.SelectedPath; if (Directory.Exists(fbd.SelectedPath)) { DirectoryInfo dir = new DirectoryInfo(fbd.SelectedPath); FileInfo[] files = dir.GetFiles("*.msbt"); bool overwrite = true; string result = ""; if (dir.GetFiles("*.xmsbt").Length > 0) { overwrite = MessageBox.Show("Is it OK to overwrite XMSBT files in the directory?", "Overwrite?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; } if (files.Length > 0) { foreach (FileInfo file in files) { try { MSBT msbt = new MSBT(file.FullName); msbt.ExportXMSBT(file.FullName.Substring(0, file.FullName.Length - 4) + "xmsbt", overwrite); } catch (Exception ex) { result = ex.Message; } } if (result.Length == 0) { result = "Successfully batch exported files to XMSBT."; } } else { result = "There are no MSBT files to export in the selected directory."; } MessageBox.Show(result, "XMSBT Batch Export Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } } Settings.Default.Save(); Settings.Default.Reload(); }
private void OpenFile(string filename = "") { ofdOpenFile.InitialDirectory = Settings.Default.InitialDirectory; DialogResult dr = DialogResult.OK; if (filename != string.Empty) { _msbt = new MSBT(filename); _fileOpen = true; _hasChanges = false; LoadFile(); UpdateForm(); Settings.Default.InitialDirectory = new FileInfo(filename).DirectoryName; Settings.Default.Save(); Settings.Default.Reload(); } else { dr = ofdOpenFile.ShowDialog(); if (dr == DialogResult.OK) { try { _msbt = new MSBT(ofdOpenFile.FileName); _fileOpen = true; _hasChanges = false; LoadFile(); Settings.Default.InitialDirectory = new FileInfo(ofdOpenFile.FileName).DirectoryName; Settings.Default.Save(); Settings.Default.Reload(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), ex.Message, MessageBoxButtons.OK); _fileOpen = false; _hasChanges = false; } UpdateForm(); } } }
public string ExportXMSBTMod(string outFilename, string sourceFilename, bool overwrite = true) { string result = "Successfully exported to XMSBT."; try { if (!System.IO.File.Exists(outFilename) || (System.IO.File.Exists(outFilename) && overwrite)) { if (HasLabels) { XmlDocument xmlDocument = new XmlDocument(); XmlWriterSettings xmlSettings = new XmlWriterSettings(); xmlSettings.Encoding = FileEncoding; xmlSettings.Indent = true; xmlSettings.IndentChars = "\t"; xmlSettings.CheckCharacters = false; XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", FileEncoding.WebName, null); xmlDocument.AppendChild(xmlDeclaration); XmlElement xmlRoot = xmlDocument.CreateElement("xmsbt"); xmlDocument.AppendChild(xmlRoot); MSBT source = new MSBT(sourceFilename); foreach (Label lbl in LBL1.Labels) { bool export = true; foreach (Label lblSource in source.LBL1.Labels) { if (lbl.Name == lblSource.Name && lbl.String.Value.SequenceEqual(lblSource.String.Value)) { export = false; break; } } if (export) { XmlElement xmlEntry = xmlDocument.CreateElement("entry"); xmlRoot.AppendChild(xmlEntry); XmlAttribute xmlLabelAttribute = xmlDocument.CreateAttribute("label"); xmlLabelAttribute.Value = lbl.Name; xmlEntry.Attributes.Append(xmlLabelAttribute); XmlElement xmlString = xmlDocument.CreateElement("text"); xmlString.InnerText = FileEncoding.GetString(lbl.String.Value).Replace("\n", "\r\n").TrimEnd('\0').Replace("\0", @"\0"); xmlEntry.AppendChild(xmlString); } } System.IO.StreamWriter stream = new StreamWriter(outFilename, false, FileEncoding); xmlDocument.Save(XmlWriter.Create(stream, xmlSettings)); stream.Close(); } else { result = "XMSBT does not currently support files without an LBL1 section."; } } else { result = outFilename + " already exists and overwrite was set to false."; } } catch (Exception ex) { result = "Unknown Error - " + ex.Message; } return(result); }
private void btnFindText_Click(object sender, EventArgs e) { lstResults.Items.Clear(); if (txtSearchDirectory.Text.Trim() != string.Empty && Directory.Exists(txtSearchDirectory.Text.Trim())) { if (txtFindText.Text.Trim() != string.Empty) { string[] files = Directory.GetFiles(txtSearchDirectory.Text.Trim(), "*.msbt", (chkSearchSubfolders.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)); ListBox lstTemp = new ListBox(); foreach (string file in files) { MSBT msbt = null; try { msbt = new MSBT(file); } catch (InvalidMSBTException imex) { continue; } if (msbt.HasLabels) { lstTemp.Sorted = true; } else { lstTemp.Sorted = false; } lstTemp.Items.Clear(); for (int i = 0; i < msbt.TXT2.NumberOfStrings; i++) { if (msbt.HasLabels) { lstTemp.Items.Add(msbt.LBL1.Labels[i]); } else { lstTemp.Items.Add(msbt.TXT2.Strings[i]); } } // Find the strings for (int i = 0; i < msbt.TXT2.NumberOfStrings; i++) { IEntry ent = null; if (msbt.HasLabels) { ent = msbt.LBL1.Labels[i]; } else { ent = msbt.TXT2.Strings[i]; } if (lstTemp.Items.Contains(ent)) { lstTemp.SelectedItem = ent; } if (chkMatchCase.Checked) { if (msbt.FileEncoding.GetString(ent.Value).Contains(txtFindText.Text)) { SearchResult result = new SearchResult(); result.Filename = file; result.Entry = ent; result.Index = lstTemp.SelectedIndex; lstResults.Items.Add(result); } } else { if (msbt.FileEncoding.GetString(ent.Value).ToLower().Contains(txtFindText.Text.ToLower())) { SearchResult result = new SearchResult(); result.Filename = file; result.Entry = ent; result.Index = lstTemp.SelectedIndex; lstResults.Items.Add(result); } } } } } } if (lstResults.Items.Count == 0) { MessageBox.Show("Could not find \"" + txtFindText.Text + "\".", "Find", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void batchImportXMSBTToolStripMenuItem_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Select a directory containing MSBT and XMSBT files of the same name:"; fbd.ShowNewFolderButton = false; fbd.SelectedPath = Settings.Default.XMSBTDirectory; if (fbd.ShowDialog() == DialogResult.OK) { Settings.Default.XMSBTDirectory = fbd.SelectedPath; if (Directory.Exists(fbd.SelectedPath)) { DirectoryInfo dir = new DirectoryInfo(fbd.SelectedPath); FileInfo[] msbtFiles = dir.GetFiles("*.msbt"); FileInfo[] xmsbtFiles = dir.GetFiles("*.xmsbt"); string result = ""; bool addLabels = MessageBox.Show("Add labels in the XMSBT files that don't exist in the MSBT files?", "Add Labels?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; Dictionary <string, FileInfo> matches = new Dictionary <string, FileInfo>(); foreach (FileInfo file in msbtFiles) { string name = file.FullName.Substring(0, file.FullName.Length - 4); foreach (FileInfo xFile in xmsbtFiles) { if (name == xFile.FullName.Substring(0, xFile.FullName.Length - 5)) { matches.Add(name, file); break; } } } if (matches.Count > 0) { foreach (string file in matches.Keys) { try { MSBT msbt = new MSBT(matches[file].FullName); msbt.ImportXMSBT(matches[file].FullName.Substring(0, matches[file].FullName.Length - 4) + "xmsbt", addLabels); msbt.Save(); } catch (Exception ex) { result = ex.Message; } } if (result.Length == 0) { result = "Successfully batch imported from XMSBT."; } } else { result = "There are no MSBT files that match an XMSBT file in the selected directory."; } MessageBox.Show(result, "XMSBT Batch Import Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } } Settings.Default.Save(); Settings.Default.Reload(); }