public TempFile CreateTempFile(FileEntry entry, PackageFileEntry be = null, FormatConverter exporter = null) { if (this.TempFiles.ContainsKey(entry)) { this.DeleteTempFile(entry); } TempFile temp = new TempFile(entry, be, exporter); return(temp); }
public void RegisterExporter(string key, FormatConverter obj) { try { this.cmbOptions.Items.Add(obj.Title, key); this.Exporters.Add(key, obj); } catch (Exception exc) { Console.WriteLine(exc.Message); } }
public object FileData(PackageFileEntry be = null, FormatConverter exporter = null) { if (exporter == null) { return(this.FileStream(be)); } else { MemoryStream stream = this.FileStream(be); return(stream == null ? null : exporter.Export(this.FileStream(be))); } }
public static void RegisterConverter(dynamic pis) { FormatConverter format = new FormatConverter() { Key = pis.key, Extension = pis.extension, Type = pis.type, Title = pis.title }; format.ExportEvent += pis.export; AddConverter(format); }
public SaveOptionsDialog(string type) { XamlReader.Load(this); this.cmbOptions.Items.Add("Default"); this.cmbOptions.SelectedIndex = 0; this.cmbOptions.SelectedIndexChanged += (object sender, EventArgs e) => { this.SelectedExporter = this.cmbOptions.SelectedIndex == 0 ? null : this.Exporters[this.cmbOptions.SelectedKey]; }; if (ScriptActions.Converters.ContainsKey(type)) { this.RegisterExporters(ScriptActions.Converters[type]); } }
public TempFile GetTempFile(FileEntry file, PackageFileEntry entry = null, FormatConverter exporter = null) { TempFile path; if (!this.TempFiles.ContainsKey(file) || this.TempFiles[file].Disposed || !File.Exists(this.TempFiles[file].Path) || this.TempFiles[file].ExporterKey != exporter.Key || this.TempFiles[file].Entry != entry) { if (this.TempFiles.ContainsKey(file)) { this.DeleteTempFile(file); } path = this.CreateTempFile(file, entry, exporter); } else { path = this.TempFiles[file]; } return(path); }
public static void AddConverter(FormatConverter format) { if (format.Key == null) { Console.WriteLine("[ERROR] Converter must have a key variable!"); return; } if (!Converters.ContainsKey(format.Type)) { Converters.Add(format.Type, new Dictionary <string, FormatConverter>()); } if (Converters[format.Type].ContainsKey(format.Key)) { Console.WriteLine("[ERROR] Conveter is already registered with key {0}", format.Key); return; } Converters[format.Type].Add(format.Key, format); }
public TempFile(FileEntry entry, PackageFileEntry be = null, FormatConverter exporter = null) { this.Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Definitions.TempDir, $"{entry._path.HashedString}.{entry._extension.ToString()}"); if (exporter != null && exporter.Extension != null) { this.Path += "." + exporter.Extension; } object file_data = entry.FileData(be, exporter); if (file_data is byte[]) { File.WriteAllBytes(this.Path, (byte[])file_data); } else if (file_data is Stream) { using (FileStream file_stream = File.Create(this.Path)) ((Stream)file_data).CopyTo(file_stream); ((Stream)file_data).Close(); } else if (file_data is string) { File.WriteAllText(this.Path, (string)file_data); } else if (file_data is string[]) { File.WriteAllLines(this.Path, (string[])file_data); } this.Entry = be; if (exporter != null) { this.ExporterKey = exporter.Key; } }
public void LoadConverters() { FormatConverter cXML = new FormatConverter() { Key = "script_cxml", Title = "Custom XML", Extension = "xml", Type = "scriptdata" }; cXML.ExportEvent += (MemoryStream ms, bool escape) => { return(new CustomXMLNode("table", (Dictionary <string, object>) new ScriptData(new BinaryReader(ms)).Root, "").ToString(0, escape)); }; ScriptActions.AddConverter(cXML); FormatConverter Strings = new FormatConverter() { Key = "diesel_strings", Title = "Diesel", Extension = "strings", Type = "strings" }; Strings.ImportEvent += (path) => new StringsFile(path); ScriptActions.AddConverter(Strings); /*ScriptActions.AddConverter(new FormatConverter() * { * Key = "texture_dds", * Title = "DDS", * Extension = "dds", * Type = "texture" * });*/ FormatConverter stringsCSV = new FormatConverter() { Key = "strings_csv", Title = "CSV", Extension = "csv", Type = "strings" }; //Excel doesn't seem to like it? stringsCSV.ExportEvent += (MemoryStream ms, bool arg0) => { StringsFile str = new StringsFile(ms); StringBuilder builder = new StringBuilder(); builder.Append("ID,String\n"); foreach (var entry in str.LocalizationStrings) { builder.Append("\"" + entry.ID.ToString() + "\",\"" + entry.Text + "\"\n"); } Console.WriteLine(builder.ToString()); return(builder.ToString()); }; ScriptActions.AddConverter(stringsCSV); FormatConverter scriptJSON = new FormatConverter() { Key = "script_json", Title = "JSON", Extension = "json", Type = "scriptdata" }; scriptJSON.ExportEvent += (MemoryStream ms, bool arg0) => { ScriptData sdata = new ScriptData(new BinaryReader(ms)); return((new JSONNode("table", sdata.Root, "")).ToString()); }; ScriptActions.AddConverter(scriptJSON); FormatConverter stringsJSON = new FormatConverter() { Key = "strings_json", Title = "JSON", Extension = "json", Type = "strings" }; //Excel doesn't seem to like it? stringsJSON.ExportEvent += (MemoryStream ms, bool arg0) => { StringsFile str = new StringsFile(ms); StringBuilder builder = new StringBuilder(); builder.Append("{\n"); for (int i = 0; i < str.LocalizationStrings.Count; i++) { StringEntry entry = str.LocalizationStrings[i]; builder.Append("\t"); builder.Append("\"" + entry.ID + "\" : \"" + entry.Text + "\""); if (i < str.LocalizationStrings.Count - 1) { builder.Append(","); } builder.Append("\n"); } builder.Append("}"); Console.WriteLine(builder.ToString()); return(builder.ToString()); }; ScriptActions.AddConverter(stringsJSON); FormatConverter stringsView = new FormatConverter() { Key = "strings_view", Title = "Strings view", Extension = "strings", Type = "strings" }; //Excel doesn't seem to like it? stringsView.ExportEvent += (MemoryStream ms, bool arg0) => { new StringViewer(ms).Show(); return(ms); }; ScriptActions.AddConverter(stringsView); }
public void ViewFile(FileEntry entry, PackageFileEntry be = null) { try { Console.WriteLine(entry.BundleEntries.Count); if (entry.BundleEntries.Count == 0) { return; } string typ = Definitions.TypeFromExtension(entry._extension.ToString()); FormatConverter exporter = null; if (ScriptActions.Converters.ContainsKey(typ)) { // if(ScriptActions.Converters[typ].Count > 1) // { SaveOptionsDialog dlg = new SaveOptionsDialog(typ); DialogResult dlgres = dlg.ShowModal(); if (dlgres == DialogResult.Cancel) { return; } exporter = dlg.SelectedExporter; // } } //Thread thread = new Thread(() => //{ TempFile temp = this.GetTempFile(entry, be, exporter); //{ GC.Collect(); ProcessStartInfo pi = new ProcessStartInfo(temp.Path); pi.UseShellExecute = true; if (General.IsLinux) { pi.Arguments = temp.Path; pi.FileName = "xdg-open"; } else { pi.FileName = temp.Path; } Process proc = Process.Start(pi); //temp.RunProcess = proc; if (!this.TempFiles.ContainsKey(entry)) { this.TempFiles.Add(entry, temp); } /*if (proc == null)//seconds -> milliseconds * Thread.Sleep(20 * 1000); * proc?.WaitForExit(); * while((!(proc?.HasExited ?? true))) * { } * * if (General.IsLinux && (proc?.ExitCode == 3 || proc?.ExitCode == 4)) * Console.WriteLine("No default file association for filetype {0}", Path.GetExtension(temp.Path)); * * while (!this.IsFileAvailable(temp.Path)) * { * Console.WriteLine("Waiting on file"); * } * } * this.TempFiles.Remove(entry);*/ //}); //thread.IsBackground = true; //thread.Start(); } catch (Exception exc) { Console.WriteLine(exc.Message); Console.WriteLine(exc.StackTrace); } }