示例#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);
        }
示例#2
0
        private void RemoveMod(object sender, RoutedEventArgs e)
        {
            var modInfos = this.SelectedMods.Select(guid => this.Config.KnownMods.Find(x => x.Guid == guid)).ToArray();

            if (modInfos.Length == 0)
            {
                return;
            }

            var nl      = Environment.NewLine;
            var message = "This operation will delete following mods:" + nl;

            message += string.Join(nl, modInfos.Select(x => $" - {x.DisplayName}"));
            message += nl + nl + "Are you sure?";
            var result = MessageBox.Show(message, "Delete mods", MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                foreach (var mod in modInfos)
                {
                    var modDirectory = ModUtils.GetModDirectory(mod);
                    if (Directory.Exists(modDirectory))
                    {
                        try
                        {
                            Directory.Delete(modDirectory, true);
                        }
                        catch
                        {
                            var caption = "Error while deleting mod";
                            var error   = "PatchZone could not delete all data of the following mod:" + nl;
                            error += $" - {ModUtils.GetReadableIdentifier(mod)}" + nl + nl;
                            error += "This usually happens when the game is still running or mod assets are open in dev tools." + nl;
                            error += "Please close the game and all dev tools before continuing.";
                            result = MessageBox.Show(error, caption, MessageBoxButton.OKCancel);

                            if (result == MessageBoxResult.Cancel)
                            {
                                return;
                            }

                            try
                            {
                                Directory.Delete(modDirectory, true);
                            }
                            catch (Exception ex)
                            {
                                error  = "Repeated attempt to delete data of the following mod failed:" + nl;
                                error += $" - {ModUtils.GetReadableIdentifier(mod)}" + nl + nl;
                                error += "Please delete following directory to finish the cleanup process:" + nl;
                                error += modDirectory + nl + nl;

                                result = MessageBox.Show(error, caption, MessageBoxButton.OKCancel);

                                if (result == MessageBoxResult.Cancel)
                                {
                                    return;
                                }
                            }
                        }
                    }

                    this.Config.KnownMods.Remove(mod);
                }

                NotifyConfigurationChanged();
            }
        }
        private void CreateMod(object sender, RoutedEventArgs e)
        {
            this.ModNameBox.BorderBrush = this.DefaultBorderBrush;
            this.ModGUIDBox.BorderBrush = this.DefaultBorderBrush;

            var modName = this.ModNameBox.Text;

            if (string.IsNullOrEmpty(modName))
            {
                this.ModNameBox.BorderBrush = Brushes.Red;
                return;
            }

            Guid guid;
            var  guidString = this.ModGUIDBox.Text;

            if (string.IsNullOrEmpty(guidString))
            {
                guid = Guid.NewGuid();
            }
            else
            {
                if (Guid.TryParse(guidString, out guid) == false)
                {
                    this.ModGUIDBox.BorderBrush = Brushes.Red;
                    return;
                }
            }

            var modInfo = new ModInfo
            {
                Guid        = guid,
                Active      = true,
                DisplayName = modName
            };

            var modPath         = ModUtils.GetModDirectory(modInfo);
            var modTemplatePath = Path.Combine(PatchZoneCore.PatchZoneInstallationPath, ModTemplatePath);

            CopyDirectory(modTemplatePath, modPath);

            bool makeNextUpper  = false;
            var  escapedModName = new StringBuilder();

            foreach (char x in modName)
            {
                var toUpper = makeNextUpper;
                makeNextUpper = false;

                if (char.IsNumber(x))
                {
                    escapedModName.Append(x);
                }
                else if ('a' <= x && x <= 'z')
                {
                    escapedModName.Append(toUpper ? char.ToUpper(x) : x);
                }
                else if ('A' <= x && x <= 'Z')
                {
                    escapedModName.Append(x);
                }
                else if (char.IsWhiteSpace(x) || toUpper)
                {
                    makeNextUpper = true;
                }
            }

            if (escapedModName.Length == 0)
            {
                escapedModName.Append("PatchZoneMod");
            }

            var gameRoot = PatchZoneCore.GameInstallationPath;

            if (gameRoot[gameRoot.Length - 1] != Path.DirectorySeparatorChar)
            {
                gameRoot += Path.DirectorySeparatorChar;
            }

            FixupNewModInfo(modPath, new Dictionary <string, string>
            {
                { "___GAME_ROOT___", gameRoot },
                { "___MOD_NAME_FULL___", modName },
                { "___MOD_GUID___", guid.ToString() },
                { "___MOD_NAME___", escapedModName.ToString() },
            });

            this.Config.KnownMods.Add(modInfo);

            if (this.StartVSBox.IsChecked ?? false)
            {
                try
                {
                    var solutionFiles = Directory.EnumerateFiles(modPath, "*.sln", SearchOption.TopDirectoryOnly);
                    Process.Start(solutionFiles.First());
                }
                catch
                { }
            }

            this.Close();
        }