protected override void Initialize() { base.Initialize(); var componentModel = (IComponentModel)GetService(typeof(SComponentModel)); var exportProvider = componentModel.DefaultExportProvider; var textBufferDisplayHostProvider = exportProvider.GetExportedValue<ITextBufferDisplayHostProvider>(); _textBufferDisplayHost = textBufferDisplayHostProvider.Create(); _textAdapter = exportProvider.GetExportedValue<ITextAdapter>(); _textAdapter.ActiveTextViewChanged += OnActiveTextViewChanged; Content = _textBufferDisplayHost.Visual; Update(_textAdapter.ActiveTextViewOpt); }
private ITextAdapter SelectTextAdapter(string filename, bool batchMode = false) { ITextAdapter result = null; // first look for adapters whose extension matches that of our filename List <ITextAdapter> matchingAdapters = _textAdapters.Where(adapter => adapter.Extension.Split(';').Any(s => filename.ToLower().EndsWith(s))).ToList(); result = matchingAdapters.FirstOrDefault(adapter => adapter.Identify(filename)); // if none of them match, then try all other adapters if (result == null) { result = _textAdapters.Except(matchingAdapters).FirstOrDefault(adapter => adapter.Identify(filename)); } if (result == null && !batchMode) { MessageBox.Show("None of the installed plugins are able to open the file.", "Unsupported Format", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(result); }
/// <summary> /// Exports an open file to a given adapter format. /// </summary> /// <param name="sourceAdapter">The adapter to be exported from.</param> /// <param name="exportAdapter">The adapter to be exported to.</param> /// <param name="outputFileName">The target filename to save to.</param> public static void ExportFile(ITextAdapter sourceAdapter, ITextAdapter exportAdapter, string outputFileName) { var newAdapter = (ITextAdapter)Activator.CreateInstance(exportAdapter.GetType()); if (newAdapter is ICreateFiles creator && newAdapter is IAddEntries add) { // Create creator.Create(); // Create the new entries foreach (var entry in sourceAdapter.Entries) { var newEntry = add.NewEntry(); newEntry.Name = entry.Name; newEntry.EditedText = entry.EditedText; newEntry.OriginalText = entry.OriginalText.Length == 0 ? entry.EditedText : entry.OriginalText; newEntry.MaxLength = entry.MaxLength; add.AddEntry(newEntry); } // Save the file (newAdapter as ISaveFiles)?.Save(new StreamInfo(File.Create(outputFileName), outputFileName)); } }
private void tsbBatchImportKUP_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Select the source directory containing text and kup file pairs..."; fbd.SelectedPath = Settings.Default.LastBatchDirectory == string.Empty ? Settings.Default.LastDirectory : Settings.Default.LastBatchDirectory; fbd.ShowNewFolderButton = false; if (fbd.ShowDialog() == DialogResult.OK) { string path = fbd.SelectedPath; int fileCount = 0; int importCount = 0; if (Directory.Exists(path)) { string[] types = _textAdapters.Select(x => x.Extension.ToLower()).ToArray(); List <string> files = new List <string>(); foreach (string type in types) { if (type != "*.kup") { string[] subTypes = type.Split(';'); foreach (string subType in subTypes) { files.AddRange(Directory.GetFiles(path, subType, SearchOption.AllDirectories)); } } } foreach (string file in files) { if (File.Exists(file)) { FileInfo fi = new FileInfo(file); ITextAdapter currentAdapter = SelectTextAdapter(file, true); if (currentAdapter != null && currentAdapter.CanSave && File.Exists(fi.FullName + ".kup")) { KUP kup = KUP.Load(fi.FullName + ".kup"); currentAdapter.Load(file, true); foreach (TextEntry entry in currentAdapter.Entries) { Entry kEntry = kup.Entries.Find(o => o.Name == entry.Name); if (kEntry != null) { entry.EditedText = kEntry.EditedText; } if (currentAdapter.EntriesHaveSubEntries && kEntry != null) { foreach (TextEntry sub in entry.SubEntries) { Entry kSub = (Entry)kEntry.SubEntries.Find(o => o.Name == sub.Name); if (kSub != null) { sub.EditedText = kSub.EditedText; } } } } currentAdapter.Save(); importCount++; } fileCount++; } } MessageBox.Show("Batch import completed successfully. " + importCount + " of " + fileCount + " files succesfully imported.", "Batch Import", MessageBoxButtons.OK, MessageBoxIcon.Information); } } Settings.Default.LastBatchDirectory = fbd.SelectedPath; Settings.Default.Save(); }
private void tsbBatchExportKUP_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description = "Select the source directory containing text files..."; fbd.SelectedPath = Settings.Default.LastBatchDirectory == string.Empty ? Settings.Default.LastDirectory : Settings.Default.LastBatchDirectory; fbd.ShowNewFolderButton = false; if (fbd.ShowDialog() == DialogResult.OK) { string path = fbd.SelectedPath; int fileCount = 0; if (Directory.Exists(path)) { string[] types = _textAdapters.Select(x => x.Extension.ToLower()).ToArray(); List <string> files = new List <string>(); foreach (string type in types) { if (type != "*.kup") { string[] subTypes = type.Split(';'); foreach (string subType in subTypes) { files.AddRange(Directory.GetFiles(path, subType, SearchOption.AllDirectories)); } } } // TODO: Ask how to handle overwrites and backups foreach (string file in files) { if (File.Exists(file)) { FileInfo fi = new FileInfo(file); ITextAdapter currentAdapter = SelectTextAdapter(file, true); if (currentAdapter != null) { KUP kup = new KUP(); currentAdapter.Load(file, true); foreach (TextEntry entry in currentAdapter.Entries) { Entry kEntry = new Entry(); kEntry.Name = entry.Name; kEntry.EditedText = entry.EditedText; kEntry.OriginalText = entry.OriginalText; kEntry.MaxLength = entry.MaxLength; kup.Entries.Add(kEntry); if (currentAdapter.EntriesHaveSubEntries) { foreach (TextEntry sub in entry.SubEntries) { Entry kSub = new Entry(); kSub.Name = sub.Name; kSub.EditedText = sub.EditedText; kSub.OriginalText = sub.OriginalText; kSub.MaxLength = sub.MaxLength; kSub.ParentEntry = entry; kEntry.SubEntries.Add(kSub); } } } kup.Save(fi.FullName + ".kup"); fileCount++; } } } MessageBox.Show("Batch export completed successfully. " + fileCount + " file(s) succesfully exported.", "Batch Export", MessageBoxButtons.OK, MessageBoxIcon.Information); } } Settings.Default.LastBatchDirectory = fbd.SelectedPath; Settings.Default.Save(); }
private void OpenFile(string filename = "") { var ofd = new OpenFileDialog { InitialDirectory = Settings.Default.LastDirectory, Filter = Tools.LoadFilters(_textAdapters) }; var dr = DialogResult.OK; if (filename == string.Empty) { dr = ofd.ShowDialog(); } if (dr != DialogResult.OK) { return; } if (filename == string.Empty) { filename = ofd.FileName; } var tempAdapter = SelectTextAdapter(filename); try { if (tempAdapter != null) { tempAdapter.Load(filename); _textAdapter = tempAdapter; _fileOpen = true; _hasChanges = false; // Select Game Handler foreach (ToolStripItem tsi in tsbGameSelect.DropDownItems) { if (tsi.Text == Settings.Default.SelectedGameHandler) { _gameHandler = (IGameHandler)tsi.Tag; tsbGameSelect.Text = tsi.Text; tsbGameSelect.Image = tsi.Image; break; } } if (_gameHandler == null) { _gameHandler = (IGameHandler)tsbGameSelect.DropDownItems[0].Tag; } LoadEntries(); UpdateTextView(); UpdatePreview(); UpdateForm(); } Settings.Default.LastDirectory = new FileInfo(filename).DirectoryName; Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show(this, ex.ToString(), tempAdapter != null ? $"{tempAdapter.Name} - {tempAdapter.Description} Manager" : "Supported Format Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private static int BatchExportKUP(string path, bool browseSubdirectories) { int fileCount = 0; Console.WriteLine(path); if (Directory.Exists(path)) { var types = _textAdapters.Select(x => x.Extension.ToLower()).Select(y => y.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)).SelectMany(z => z).Distinct().ToList(); List <string> files = new List <string>(); foreach (string type in types) { if (type != "*.kup") { files.AddRange(Directory.GetFiles(path, type, browseSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)); } } // TODO: Ask how to handle overwrites and backups //Console.WriteLine("{0}", files[0]); foreach (string file in files) { if (File.Exists(file)) { FileInfo fi = new FileInfo(file); ITextAdapter currentAdapter = SelectTextAdapter(_textAdapters, file, true); try { if (currentAdapter != null) { KUP kup = new KUP(); currentAdapter.Load(file, true); foreach (TextEntry entry in currentAdapter.Entries) { Entry kEntry = new Entry(); kEntry.Name = entry.Name; kEntry.EditedText = entry.EditedText; kEntry.OriginalText = entry.OriginalText; kEntry.MaxLength = entry.MaxLength; kup.Entries.Add(kEntry); if (currentAdapter.EntriesHaveSubEntries) { foreach (TextEntry sub in entry.SubEntries) { Entry kSub = new Entry(); kSub.Name = sub.Name; kSub.EditedText = sub.EditedText; kSub.OriginalText = sub.OriginalText; kSub.MaxLength = sub.MaxLength; kSub.ParentEntry = entry; kEntry.SubEntries.Add(kSub); } } } kup.Save(fi.FullName + ".kup"); fileCount++; } } catch (Exception) { } } } Console.WriteLine(string.Format("Batch export completed successfully. {0} file(s) succesfully exported.", fileCount)); } return(0); }
private static int BatchImportKUP(string path, bool browseSubdirectories) { int fileCount = 0; int importCount = 0; if (Directory.Exists(path)) { var types = _textAdapters.Select(x => x.Extension.ToLower()).Select(y => y.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)).SelectMany(z => z).Distinct().ToList(); List <string> files = new List <string>(); foreach (string type in types) { if (type != "*.kup") { files.AddRange(Directory.GetFiles(path, type, browseSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)); } } foreach (string file in files) { if (File.Exists(file)) { FileInfo fi = new FileInfo(file); ITextAdapter currentAdapter = SelectTextAdapter(_textAdapters, file, true); try { if (currentAdapter != null && currentAdapter.CanSave && File.Exists(fi.FullName + ".kup")) { KUP kup = KUP.Load(fi.FullName + ".kup"); currentAdapter.Load(file, true); foreach (TextEntry entry in currentAdapter.Entries) { Entry kEntry = kup.Entries.Find(o => o.Name == entry.Name); if (kEntry != null) { entry.EditedText = kEntry.EditedText; } if (currentAdapter.EntriesHaveSubEntries && kEntry != null) { foreach (TextEntry sub in entry.SubEntries) { Entry kSub = (Entry)kEntry.SubEntries.Find(o => o.Name == sub.Name); if (kSub != null) { sub.EditedText = kSub.EditedText; } } } } currentAdapter.Save(); importCount++; } fileCount++; } catch (Exception) { } } } Console.WriteLine(string.Format("Batch import completed successfully. {0} of {1} files succesfully imported.", importCount, fileCount)); } return(0); }