예제 #1
0
        private async Task <bool> InstallRemoteMod()
        {
            var mod          = this.SelectedRemoteRelease;
            var modDirectory = ModUtils.GetModDirectory(mod.Manifest);

            try
            {
                if (NotifyBackgroundTaskRunning(true) == false)
                {
                    throw new Exception("Mod fetching is currently in progress");
                }

                try
                {
                    this.ProgressText.Text = "Clearing mod directory";

                    var prevInstallationIndex = this.Config.KnownMods.FindIndex(x => x.Guid == this.SelectedRemoteRelease.Manifest.Guid);

                    ModInfo prevModInfo = null;
                    if (prevInstallationIndex >= 0)
                    {
                        prevModInfo = this.Config.KnownMods[prevInstallationIndex];
                        this.Config.KnownMods.RemoveAt(prevInstallationIndex);
                    }

                    await Task.Run(() =>
                    {
                        if (Directory.Exists(modDirectory))
                        {
                            Directory.Delete(modDirectory, true);
                        }

                        Directory.CreateDirectory(modDirectory);
                    });


                    this.ProgressText.Text = "Downloading package";
                    var package = this.SelectedRemoteRelease.Assets.First(x => x.ContentType == PackageTypeZip);
                    await DownloadModPackageAsync(package.BrowserDownloadUrl, modDirectory);

                    this.ProgressText.Text = "Verifying package integrity";
                    var modInfo = await Task.Run(() =>
                    {
                        var manifestPath = ModUtils.GetManifestPath(modDirectory);
                        var manifest     = XML.Deserialize <ModManifest>(manifestPath);

                        var expectedManifest = this.SelectedRemoteRelease.Manifest;
                        if (manifest.Guid != expectedManifest.Guid)
                        {
                            throw new Exception("Mod guid doesn't match");
                        }

                        return(ModUtils.BuildModInfoFromManifest(manifest, prevModInfo?.Active ?? false));
                    });

                    if (prevInstallationIndex >= 0)
                    {
                        this.Config.KnownMods.Insert(prevInstallationIndex, modInfo);
                    }
                    else
                    {
                        this.Config.KnownMods.Add(modInfo);
                    }
                }
                finally
                {
                    //Kill any running queries
                    this.RemoteUrlIndex++;
                    NotifyBackgroundTaskRunning(false);
                }
            }
            catch (Exception e)
            {
                this.ModDescriptionText.Text = e.Message;
                return(false);
            }

            return(true);
        }