コード例 #1
0
ファイル: Import.cs プロジェクト: yalaier/Sims4Tools
        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 = 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
            {
                browserWidget1.Visible = false;
                controlPanel1.UseNames = false;

                bool skipAll = false;
                foreach (string filename in packageList)
                {
                    if (Filename != null && Filename.Length > 0 && Path.GetFullPath(Filename).Equals(Path.GetFullPath(filename)))
                    {
                        CopyableMessageBox.Show("Skipping current package.", importPackagesDialog.Title);
                        continue;
                    }

                    lbProgress.Text = "Importing " + Path.GetFileNameWithoutExtension(filename) + "...";
                    Application.DoEvents();
                    IPackage imppkg = null;
                    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 string[] {
                            "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)));
                        progressBar1.Value   = 0;
                        progressBar1.Maximum = lrie.Count;
                        foreach (IResourceIndexEntry rie in lrie)
                        {
                            try
                            {
                                if (rie.ResourceType == 0x0166038C)//NMAP
                                {
                                    if (useNames)
                                    {
                                        browserWidget1.MergeNamemap(s4pi.WrapperDealer.WrapperDealer.GetResource(0, imppkg, rie) as IDictionary <ulong, string>, true, rename);
                                    }
                                }
                                else
                                {
                                    IResource res = s4pi.WrapperDealer.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));
                                    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);
                            }
                        }
                        progressBar1.Value = 0;

                        IEnumerable <IResourceIndexEntry> rieList = limp
                                                                    .Select(x => NewResource((AResourceKey)x.Item1.tgin, new MemoryStream(x.Item1.data), x.Item2, compress))
                                                                    .Where(x => x != null);
                        browserWidget1.AddRange(rieList);
                    }
                    catch (IgnoredException) { break; }//just the thrown exception, stop looping
                    catch (Exception ex)
                    {
                        CopyableMessageBox.IssueException(ex, "Could not import all resources - aborting.\n", title);
                        break;
                    }
                    finally { imppkg.Dispose(); }//Package.ClosePackage(0, imppkg); }
                    if (autoSave)
                    {
                        if (!fileSave())
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                lbProgress.Text        = "";
                progressBar1.Value     = 0;
                progressBar1.Maximum   = 0;
                controlPanel1.UseNames = CPuseNames;
                browserWidget1.Visible = true;
                ForceFocus.Focus(Application.OpenForms[0]);
                Application.DoEvents();
            }
        }
コード例 #2
0
        void UpdateList()
        {
            if (((MainForm)ParentForm).IsClosing)
            {
                return;
            }

            var cmp = listView1.ListViewItemSorter;
            IResourceIndexEntry sie = SelectedResource;

            bool     vis  = listView1.Visible;
            bool     uwc  = Application.UseWaitCursor;
            DateTime tick = DateTime.UtcNow.AddMilliseconds(tock);

            try
            {
                Application.UseWaitCursor = true;
                listView1.BeginUpdate();
                listView1.Visible = false;

                listView1.ListViewItemSorter = null;

                pbLabel.Text     = "";
                SelectedResource = null;

                pbLabel.Text = "Clear resource list...";
                Application.DoEvents();
                using (Splash splash = new Splash("Clear resource list..."))
                {
                    splash.Show();
                    Application.DoEvents();
                    listView1.Items.Clear();// this can be slow and steals the main thread!
                }
                #region Old clear listView1...

                /*if (!pbLabel.Visible || listView1.Items.Count > 10000)
                 * {
                 * }
                 * else
                 * {
                 *  // this is slower but gives some feedback
                 *  pb.Maximum = listView1.Items.Count;
                 *  pb.Value = listView1.Items.Count;
                 *  try
                 *  {
                 *      int i = listView1.Items.Count;
                 *      while (i > 0)
                 *      {
                 *          i--;
                 *          try
                 *          {
                 *              listView1.Items.RemoveAt(i);
                 *          }
                 *          finally
                 *          {
                 *              if (tick < DateTime.UtcNow)
                 *              {
                 *                  tick = DateTime.UtcNow.AddMilliseconds(tock);
                 *                  if (pb != null) { pb.Value = i; Application.DoEvents(); }
                 *              }
                 *          }
                 *      }
                 *  }
                 *  finally { if (pb != null) pb.Value = 0; }
                 * }/**/
                #endregion

                lookup = new Dictionary <IResourceIndexEntry, ListViewItem>();
                if (resourceList == null)
                {
                    return;
                }

                AddRange(resourceList);
            }
            finally
            {
                pbLabel.Text = "Restoring sorter...";
                Application.DoEvents();
                listView1.ListViewItemSorter = cmp;

                pbLabel.Text = "";
                Application.DoEvents();
                if (sie != null && lookup.ContainsKey(sie))
                {
                    SelectedResource = sie;
                }

                listView1.Visible = !this.Visible || !this.Created || vis;
                listView1.EndUpdate();
                Application.UseWaitCursor = uwc;
                Application.DoEvents();

                OnListUpdated(this, new EventArgs());
                if (this.Visible)
                {
                    ForceFocus.Focus(Application.OpenForms[0]);
                }
            }
        }