コード例 #1
0
ファイル: IntroUI.cs プロジェクト: nlgzrgn/Binary
        private void UserInteract()
        {
            using var dialog = new OpenFileDialog()
                  {
                      CheckFileExists = true,
                      Filter          = "Endscript Files|*.end",
                      Multiselect     = false,
                      Title           = "Select main Endscript (.end) file to install",
                  };

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            Launch.Deserialize(dialog.FileName, out Launch launch);

            if (launch.UsageID != eUsage.User)
            {
                throw new Exception($"Usage type of the endscript is stated to be {launch.Usage}, while should be User");
            }

            if (launch.GameID == GameINT.None)
            {
                throw new Exception($"Invalid stated game type named {launch.Game}");
            }

            using var browser = new FolderBrowserDialog()
                  {
                      Description            = $"Select Need for Speed: {launch.Game} directory to modify.",
                      RootFolder             = Environment.SpecialFolder.MyComputer,
                      UseDescriptionForTitle = true,
                      AutoUpgradeEnabled     = false,
                  };

            if (browser.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            launch.Directory = browser.SelectedPath;
            launch.ThisDir   = Path.GetDirectoryName(dialog.FileName);
            launch.CheckEndscript();
            launch.CheckFiles();
            launch.LoadLinks();

            var endscript = Path.Combine(launch.ThisDir, launch.Endscript);
            var parser    = new EndScriptParser(endscript);

            BaseCommand[] commands;

            try
            {
                commands = parser.Read();
            }
            catch (Exception ex)
            {
                var error = $"Error has occured -> File: {parser.CurrentFile}, Line: {parser.CurrentIndex}" +
                            Environment.NewLine + $"Command: [{parser.CurrentLine}]" + Environment.NewLine +
                            $"Error: {ex.GetLowestMessage()}";

                MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var profile = BaseProfile.NewProfile(launch.GameID, launch.Directory);

            profile.Load(launch);
            this.EnsureBackups(profile);
            var manager = new EndScriptManager(profile, commands, endscript);

            try
            {
                while (!manager.ProcessScript())
                {
                    var command = manager.CurrentCommand;

                    if (command is CheckboxCommand checkbox)
                    {
                        using var input = new Check(checkbox.Description, true);
                        input.ShowDialog();
                        checkbox.Choice = input.Value ? 1 : 0;

                                                #if DEBUG
                        Console.WriteLine($"Checkbox pending : option {input.Value} was chosen by user");
                                                #endif
                    }
                    else if (command is ComboboxCommand combobox)
                    {
                        var options = new string[combobox.Options.Length];
                        for (int i = 0; i < options.Length; ++i)
                        {
                            options[i] = combobox.Options[i].Name;
                        }
                        using var input = new Combo(options, combobox.Description, true);
                        input.ShowDialog();
                        combobox.Choice = input.Value < 0 ? 0 : input.Value;

                                                #if DEBUG
                        Console.WriteLine($"Checkbox pending : option {input.Value} was chosen by user");
                                                #endif
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetLowestMessage(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show("Execution has been interrupted", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var script = Path.GetFileName(dialog.FileName);

            if (manager.Errors.Any())
            {
                Utils.WriteErrorsToLog(manager.Errors, dialog.FileName);
                MessageBox.Show($"Script {script} has been applied, however, {manager.Errors.Count()} errors " +
                                $"has been detected. Check EndError.log for more information", "Information",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                MessageBox.Show($"Script {script} has been successfully applied",
                                "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            var save = MessageBox.Show("Would you like to save files?", "Prompt",
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (save == DialogResult.Yes)
            {
                profile.Save();
                this.AskForGameRun(profile);
            }
        }