Beispiel #1
0
        /// <summary>
        /// Does stuff, depending on the current state of <see cref="apkButtonState"/>.
        /// </summary>
        private async void ApkButtonClickEvent(object sender, EventArgs e)
        {
            // If we're currently creating something, exit
            if (apkButtonState == ApkButtonState.Creating)
            {
                return;
            }

            // Check for java, exit safely with a warning if not found!
            if (!CrossPlatformOperations.IsJavaInstalled())
            {
                MessageBox.Show(this, Text.JavaNotFound, Text.WarningWindowTitle, MessageBoxButtons.OK);
                SetApkButtonState(ApkButtonState.Create);
                UpdateStateMachine();
                log.Error("Java not found! Aborting Android APK creation.");
                return;
            }

            // Check if xdelta is installed on unix, exit with a warning if not.
            if (OS.IsUnix && !CrossPlatformOperations.CheckIfXdeltaIsInstalled())
            {
                MessageBox.Show(this, Text.XdeltaNotFound, Text.WarningWindowTitle, MessageBoxButtons.OK);
                SetApkButtonState(ApkButtonState.Create);
                UpdateStateMachine();
                log.Error("Xdelta not found. Aborting Android APK creation...");
                return;
            }

            UpdateStateMachine();

            if (apkButtonState == ApkButtonState.Create)
            {
                SetApkButtonState(ApkButtonState.Creating);
                UpdateStateMachine();

                progressBar.Visible = true;
                bool useHqMusic = hqMusicAndroidCheck.Checked.Value;

                var progressIndicator = new Progress <int>(UpdateProgressBar);
                await Task.Run(() => Profile.CreateAPK(profileList[profileIndex.Value], useHqMusic, progressIndicator));

                SetApkButtonState(ApkButtonState.Create);
                progressBar.Visible = false;
                UpdateStateMachine();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Does a bunch of stuff, depending on the current state of <see cref="updateState"/>.
        /// </summary>
        private async void PlayButtonClickEvent(object sender, EventArgs e)
        {
            // State Check
            UpdateStateMachine();

            // Check if 1.1 is installed by forcing invalidation
            Profile.Is11Installed(true);

            switch (updateState)
            {
                #region Download
            case PlayButtonState.Download:

                log.Info("Attempting to clone repository " + currentMirror + "...");
                bool successful = true;

                // Update playButton states
                SetPlayButtonState(PlayButtonState.Downloading);

                // Enable progressBar
                progressBar.Visible   = true;
                progressLabel.Visible = true;
                progressBar.Value     = 0;

                // Set up progressBar update method
                var c = new CloneOptions
                {
                    OnTransferProgress = TransferProgressHandlerMethod
                };

                // Everything after this is on a different thread, so the rest of the launcher isn't locked up.
                try
                {
                    if (Directory.Exists(CrossPlatformOperations.CURRENTPATH + "/PatchData"))
                    {
                        log.Info("PatchData directory already exists, cleaning up...");
                        HelperMethods.DeleteDirectory(CrossPlatformOperations.CURRENTPATH + "/PatchData");
                    }

                    await Task.Run(() => Repository.Clone(currentMirror, CrossPlatformOperations.CURRENTPATH + "/PatchData", c));
                }
                catch (UserCancelledException)
                {
                    // We deliberately cancelled this!
                    successful = false;
                }
                catch (LibGit2SharpException ex)        // This is for any exceptions from libgit
                {
                    // Libgit2sharp error messages are always in english!
                    if (ex.Message.ToLower().Contains("failed to send request") || ex.Message.ToLower().Contains("connection with the server was terminated") ||
                        ex.Message.ToLower().Contains("failed to resolve address"))
                    {
                        log.Error("Internet connection dropped while attempting to clone repository" + currentMirror + "!");
                        MessageBox.Show(this, Text.InternetConnectionDrop, Text.WarningWindowTitle, MessageBoxType.Warning);
                    }
                    else
                    {
                        log.Error("LibGit2SharpException: " + ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace);
                        MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error);
                        if (Directory.Exists(CrossPlatformOperations.CURRENTPATH + "/PatchData"))
                        {
                            HelperMethods.DeleteDirectory(CrossPlatformOperations.CURRENTPATH + "/PatchData");
                        }
                    }
                    successful = false;
                }
                catch (Exception ex)                 // This is if somehow any other exception might get thrown as well.
                {
                    log.Error(ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace);
                    MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error);

                    if (Directory.Exists(CrossPlatformOperations.CURRENTPATH + " / PatchData"))
                    {
                        HelperMethods.DeleteDirectory(CrossPlatformOperations.CURRENTPATH + "/PatchData");
                    }
                    successful = false;
                }

                log.Info("Repository clone attempt finished " + (successful ? "successfully." : "unsuccessfully."));

                currentGitObject = 0;

                // Reset progressBar after clone is finished
                progressLabel.Visible = false;
                progressLabel.Text    = "";
                progressBar.Visible   = false;
                progressBar.Value     = 0;

                // Just need to switch this to anything that isn't an "active" state so SetUpdateState() actually does something
                SetPlayButtonState(PlayButtonState.Install);

                // This needs to be run BEFORE the state check so that the Mod Settings tab doesn't weird out
                LoadProfilesAndAdjustLists();

                // Do a state check
                UpdateStateMachine();

                break;
                #endregion

                #region Downloading

            case PlayButtonState.Downloading:
                var result = MessageBox.Show(this, Text.CloseOnCloningText, Text.WarningWindowTitle, MessageBoxButtons.YesNo, MessageBoxType.Warning, MessageBoxDefaultButton.No);
                if (result != DialogResult.Yes)
                {
                    return;
                }

                log.Info("User cancelled download!");
                isGitProcessGettingCancelled = true;

                // We don't need to delete any folders here, the cancelled gitClone will do that automatically for us :)
                // But we should probably wait a bit before proceeding, since cleanup can take a while
                Thread.Sleep(1000);
                isGitProcessGettingCancelled = false;
                break;

                #endregion

                #region Select11
            case PlayButtonState.Select11:

                log.Info("Requesting user input for AM2R_11.zip...");

                OpenFileDialog fileFinder = GetSingleZipDialog(Text.Select11FileDialog);

                if (fileFinder.ShowDialog(this) != DialogResult.Ok)
                {
                    log.Info("User cancelled the selection.");
                    return;
                }

                // Default filename is whitespace
                if (String.IsNullOrWhiteSpace(fileFinder.FileName))
                {
                    log.Error("User did not supply valid input. Cancelling import.");
                    return;
                }

                // If either a directory was selected or the file somehow went missing, cancel
                if (!File.Exists(fileFinder.FileName))
                {
                    log.Error("Selected AM2R_11.zip file not found! Cancelling import.");
                    break;
                }

                IsZipAM2R11ReturnCodes errorCode = Profile.CheckIfZipIsAM2R11(fileFinder.FileName);
                if (errorCode != IsZipAM2R11ReturnCodes.Successful)
                {
                    log.Error("User tried to input invalid AM2R_11.zip file (" + errorCode + "). Cancelling import.");
                    MessageBox.Show(this, Text.ZipIsNotAM2R11 + "\n\nError Code: " + errorCode, Text.ErrorWindowTitle, MessageBoxType.Error);
                    return;
                }

                // We check if it exists first, because someone coughDRUIDcough might've copied it into here while on the showDialog
                if (fileFinder.FileName != CrossPlatformOperations.CURRENTPATH + "/AM2R_11.zip")
                {
                    File.Copy(fileFinder.FileName, CrossPlatformOperations.CURRENTPATH + "/AM2R_11.zip");
                }

                log.Info("AM2R_11.zip successfully imported.");

                UpdateStateMachine();
                break;
                #endregion

                #region Install
            case PlayButtonState.Install:
                progressBar.Visible = true;
                progressBar.Value   = 0;
                SetPlayButtonState(PlayButtonState.Installing);

                // Make sure the main interface state machines properly
                UpdateApkState();
                UpdateProfileState();

                // If the file cannot be launched due to anti-virus shenanigans or any other reason, we try catch here
                try
                {
                    // Check if xdelta is installed on unix and exit if not
                    if (OS.IsUnix && !CrossPlatformOperations.CheckIfXdeltaIsInstalled())
                    {
                        MessageBox.Show(this, Text.XdeltaNotFound, Text.WarningWindowTitle, MessageBoxButtons.OK);

                        SetPlayButtonState(PlayButtonState.Install);
                        UpdateStateMachine();
                        log.Error("Xdelta not found. Aborting installing a profile...");
                        return;
                    }
                    var  progressIndicator = new Progress <int>(UpdateProgressBar);
                    bool useHqMusic        = hqMusicPCCheck.Checked.Value;
                    await Task.Run(() => Profile.InstallProfile(profileList[profileIndex.Value], useHqMusic, progressIndicator));

                    // This is just for visuals because the average windows end user will ask why it doesn't go to the end otherwise.
                    if (OS.IsWindows)
                    {
                        Thread.Sleep(500);
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace);
                    MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error);
                }
                progressBar.Visible = false;
                progressBar.Value   = 0;

                // Just need to switch this to anything that isn't an "active" state so SetUpdateState() actually does something
                SetPlayButtonState(PlayButtonState.Play);
                UpdateStateMachine();
                break;
                #endregion

                #region Play
            case PlayButtonState.Play:

                if (!IsProfileIndexValid())
                {
                    return;
                }

                ProfileXML profile = profileList[profileIndex.Value];
                Visible = false;
                SetPlayButtonState(PlayButtonState.Playing);

                // Make sure the main interface state machines properly
                UpdateApkState();
                UpdateProfileState();

                this.ShowInTaskbar    = false;
                trayIndicator.Visible = true;
                WindowState windowStateBeforeLaunching = this.WindowState;
                Minimize();

                string envVarText      = customEnvVarTextBox?.Text;
                bool   createDebugLogs = profileDebugLogCheck.Checked.Value;

                await Task.Run(() => Profile.RunGame(profile, createDebugLogs, envVarText));

                this.ShowInTaskbar    = true;
                trayIndicator.Visible = false;
                Show();
                BringToFront();
                Visible     = true;
                WindowState = windowStateBeforeLaunching;

                SetPlayButtonState(PlayButtonState.Play);
                UpdateStateMachine();
                break;

                #endregion

            default: throw new NotImplementedException("Encountered invalid update state: " + updateState + "!");
            }
        }