private void ResourcePaste() { try { this.Enabled = false; if (Clipboard.ContainsData(MainForm.DataFormatSingleFile)) { IFormatter formatter = new BinaryFormatter(); Stream stream = Clipboard.GetData(MainForm.DataFormatSingleFile) as MemoryStream; MyDataFormat d = (MyDataFormat)formatter.Deserialize(stream); stream.Close(); this.ImportSingle(d); } else if (Clipboard.ContainsData(MainForm.DataFormatBatch)) { IFormatter formatter = new BinaryFormatter(); Stream stream = Clipboard.GetData(MainForm.DataFormatBatch) as MemoryStream; List <MyDataFormat> l = (List <MyDataFormat>)formatter.Deserialize(stream); stream.Close(); this.ImportBatch(l); } else if (Clipboard.ContainsFileDropList()) { StringCollection fileDrop = Clipboard.GetFileDropList(); if (fileDrop.Count == 0) { return; } if (fileDrop.Count == 1) { this.ImportSingle(fileDrop[0], "Resource->Paste"); } else { string[] batch = fileDrop.OfType <string>().ToArray(); this.ImportBatch(batch, "Resource->Paste"); } } } finally { this.Enabled = true; } }
private void ImportStream(MyDataFormat data, bool useName, bool rename, bool compress, DuplicateHandling dups, bool select) { if (useName && !string.IsNullOrEmpty(data.tgin.ResName)) { this.browserWidget1.ResourceName(data.tgin.ResInstance, data.tgin.ResName, true, rename); } IResourceIndexEntry rie = this.NewResource((TGIBlock)data.tgin, new MemoryStream(data.data), dups, compress); if (rie != null) { this.browserWidget1.Add(rie, select); } }
private void ImportSingle(MyDataFormat data) { ResourceDetails ir = new ResourceDetails(true, true) { Filename = data.tgin }; DialogResult dr = ir.ShowDialog(); if (dr != DialogResult.OK) { return; } data.tgin = ir; this.ImportStream(data, ir.UseName, ir.AllowRename, ir.Compress, ir.Replace ? DuplicateHandling.Replace : DuplicateHandling.Reject, true); }
private void ImportPackagesCommon(string[] packageList, string title, DuplicateHandling dups, bool compress, bool useNames = false, bool rename = false, List <uint> dupsList = null, AutoSaveState autoSaveState = AutoSaveState.Ask, IList <IResourceIndexEntry> selection = null ) { bool cpUseNames = this.controlPanel1.UseNames; DateTime now = DateTime.UtcNow; bool autoSave = false; if (autoSaveState == AutoSaveState.Ask) { switch (CopyableMessageBox.Show("Auto-save current package after each package imported?", title, CopyableMessageBoxButtons.YesNoCancel, CopyableMessageBoxIcon.Question)) { case 0: autoSave = true; break; case 2: return; } } else { autoSave = autoSaveState == AutoSaveState.Always; } try { this.browserWidget1.Visible = false; this.controlPanel1.UseNames = false; bool skipAll = false; foreach (string filename in packageList) { if (!string.IsNullOrEmpty(this.Filename) && Path.GetFullPath(this.Filename).Equals(Path.GetFullPath(filename))) { CopyableMessageBox.Show("Skipping current package.", this.importPackagesDialog.Title); continue; } this.lbProgress.Text = "Importing " + Path.GetFileNameWithoutExtension(filename) + "..."; Application.DoEvents(); IPackage imppkg; try { imppkg = Package.OpenPackage(0, filename); } catch (InvalidDataException ex) { if (skipAll) { continue; } int btn = CopyableMessageBox.Show( string.Format("Could not open package {0}.\n{1}", Path.GetFileName(filename), ex.Message), title, CopyableMessageBoxIcon.Error, new List <string>(new[] { "Skip this", "Skip all", "Abort" }), 0, 0); if (btn == 0) { continue; } if (btn == 1) { skipAll = true; continue; } break; } try { List <Tuple <MyDataFormat, DuplicateHandling> > limp = new List <Tuple <MyDataFormat, DuplicateHandling> >(); List <IResourceIndexEntry> lrie = selection == null ? imppkg.GetResourceList : imppkg.FindAll(rie => selection.Any(tgt => ((AResourceKey)tgt).Equals(rie))); this.progressBar1.Value = 0; this.progressBar1.Maximum = lrie.Count; foreach (IResourceIndexEntry rie in lrie) { try { if (rie.ResourceType == 0x0166038C) //NMAP { if (useNames) { this.browserWidget1.MergeNamemap( WrapperDealer.GetResource(0, imppkg, rie) as IDictionary <ulong, string>, true, rename); } } else { IResource res = WrapperDealer.GetResource(0, imppkg, rie, true); MyDataFormat impres = new MyDataFormat() { tgin = rie as AResourceIndexEntry, data = res.AsBytes }; // dups Replace | Reject | Allow // dupsList null | list of allowable dup types DuplicateHandling dupThis = dups == DuplicateHandling.Allow ? dupsList == null || dupsList.Contains(rie.ResourceType) ? DuplicateHandling.Allow : DuplicateHandling.Replace : dups; limp.Add(Tuple.Create(impres, dupThis)); this.progressBar1.Value++; if (now.AddMilliseconds(100) < DateTime.UtcNow) { Application.DoEvents(); now = DateTime.UtcNow; } } } catch (Exception ex) { string rk = ""; if (rie != null) { rk = "(RK: " + rie + ")\n"; } else { rk = "(RK is null)\n"; } CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n" + rk, title); throw new IgnoredException(ex); } } this.progressBar1.Value = 0; IEnumerable <IResourceIndexEntry> rieList = limp .Select( x => this.NewResource((AResourceKey)x.Item1.tgin, new MemoryStream(x.Item1.data), x.Item2, compress)) .Where(x => x != null); this.browserWidget1.AddRange(rieList); } catch (IgnoredException) { //just the thrown exception, stop looping break; } catch (Exception ex) { CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n", title); break; } finally { imppkg.Dispose(); } if (autoSave && !this.FileSave()) { break; } } } finally { this.lbProgress.Text = ""; this.progressBar1.Value = 0; this.progressBar1.Maximum = 0; this.controlPanel1.UseNames = cpUseNames; this.browserWidget1.Visible = true; ForceFocus.Focus(Application.OpenForms[0]); Application.DoEvents(); } }