Ejemplo n.º 1
0
        // TODO: merge with ^
        private void UpdateChecker_DoWorkForced(object sender, DoWorkEventArgs e)
        {
            if (!(sender is BackgroundWorker worker))
            {
                throw new Exception("what");
            }

            if (!(e.Argument is List <Tuple <string, ModInfo, List <ModManifestDiff> > > updatableMods) || updatableMods.Count == 0)
            {
                return;
            }

            var updates = new List <ModDownload>();
            var errors  = new List <string>();

            using (var client = new UpdaterWebClient())
            {
                foreach (Tuple <string, ModInfo, List <ModManifestDiff> > info in updatableMods)
                {
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }

                    ModInfo mod = info.Item2;
                    if (!string.IsNullOrEmpty(mod.GitHubRepo))
                    {
                        if (string.IsNullOrEmpty(mod.GitHubAsset))
                        {
                            errors.Add($"[{ mod.Name }] GitHubRepo specified, but GitHubAsset is missing.");
                            continue;
                        }

                        ModDownload d = modUpdater.GetGitHubReleases(mod, info.Item1, client, errors);
                        if (d != null)
                        {
                            updates.Add(d);
                        }
                    }
                    else if (!string.IsNullOrEmpty(mod.GameBananaItemType) && mod.GameBananaItemId.HasValue)
                    {
                        ModDownload d = modUpdater.GetGameBananaReleases(mod, info.Item1, errors);
                        if (d != null)
                        {
                            updates.Add(d);
                        }
                    }
                    else if (!string.IsNullOrEmpty(mod.UpdateUrl))
                    {
                        List <ModManifestEntry> localManifest = info.Item3
                                                                .Where(x => x.State == ModManifestState.Unchanged)
                                                                .Select(x => x.Current).ToList();

                        ModDownload d = modUpdater.CheckModularVersion(mod, info.Item1, localManifest, client, errors);
                        if (d != null)
                        {
                            updates.Add(d);
                        }
                    }
                }
            }

            e.Result = new Tuple <List <ModDownload>, List <string> >(updates, errors);
        }
Ejemplo n.º 2
0
        private void HandleUri(string uri)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }

            Activate();

            var fields = uri.Substring("sa2mm:".Length).Split(',');

            // TODO: lib-ify
            string itemType = fields.FirstOrDefault(x => x.StartsWith("gb_itemtype", StringComparison.InvariantCultureIgnoreCase));

            itemType = itemType.Substring(itemType.IndexOf(":") + 1);

            string itemId = fields.FirstOrDefault(x => x.StartsWith("gb_itemid", StringComparison.InvariantCultureIgnoreCase));

            itemId = itemId.Substring(itemId.IndexOf(":") + 1);

            GameBananaItem gbi = GameBananaItem.Load(itemType, long.Parse(itemId));

            var dummyInfo = new ModInfo()
            {
                Name = gbi.Name, Author = gbi.OwnerName
            };

            DialogResult result = MessageBox.Show(this, $"Do you want to install mod \"{dummyInfo.Name}\" by {dummyInfo.Author} from {new Uri(fields[0]).DnsSafeHost}?", "Mod Download", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result != DialogResult.Yes)
            {
                return;
            }

            #region create update folder
            do
            {
                try
                {
                    result = DialogResult.Cancel;
                    if (!Directory.Exists(updatePath))
                    {
                        Directory.CreateDirectory(updatePath);
                    }
                }
                catch (Exception ex)
                {
                    result = MessageBox.Show(this, "Failed to create temporary update directory:\n" + ex.Message
                                             + "\n\nWould you like to retry?", "Directory Creation Failed", MessageBoxButtons.RetryCancel);
                }
            } while (result == DialogResult.Retry);
            #endregion

            string dummyPath = dummyInfo.Name;

            foreach (char c in Path.GetInvalidFileNameChars())
            {
                dummyPath = dummyPath.Replace(c, '_');
            }

            dummyPath = Path.Combine("mods", dummyPath);

            var updates = new List <ModDownload>
            {
                new ModDownload(dummyInfo, dummyPath, fields[0], null, 0)
            };

            using (var progress = new ModDownloadDialog(updates, updatePath))
            {
                progress.ShowDialog(this);
            }

            do
            {
                try
                {
                    result = DialogResult.Cancel;
                    Directory.Delete(updatePath, true);
                }
                catch (Exception ex)
                {
                    result = MessageBox.Show(this, "Failed to remove temporary update directory:\n" + ex.Message
                                             + "\n\nWould you like to retry? You can remove the directory manually later.",
                                             "Directory Deletion Failed", MessageBoxButtons.RetryCancel);
                }
            } while (result == DialogResult.Retry);

            LoadModList();
        }