Exemple #1
0
        /// <summary>
        /// Opens a dialog that allows users to log in.
        /// </summary>
        /// <remarks>
        /// If the user fails to log in, this method will reopen the login dialog
        /// with an error message. The done delegate is not called until the user
        /// either logs in successfully or clicks cancel.
        /// </remarks>
        /// <param name="done">Delegate that is invoked when the user logs in
        /// successfully.</param>
        /// <param name="inst">The instance that is being launched.</param>
        /// <param name="message">An error message to be displayed on the login dialog.</param>
        /// <param name="canplayOffline">True if the user can play this instance offline.</param>
        private void DoLogin(LoginCompleteHandler done,
                             Instance inst,
                             string message      = "",
                             bool canplayOffline = false)
        {
            string username = "";
            string password = "";

            ReadUserInfo(out username, out password);

            ILoginDialog loginDlg = GUIManager.Main.LoginDialog(message);

            loginDlg.Parent          = (DirectLaunch ? null : MainWindow);
            loginDlg.DefaultPosition = DefWindowPosition.CenterParent;
            loginDlg.ShowInTaskbar   = DirectLaunch;

            if (!string.IsNullOrEmpty(username))
            {
                loginDlg.RememberUsername = true;
                loginDlg.Username         = username;
            }
            if (!string.IsNullOrEmpty(password))
            {
                loginDlg.RememberPassword = true;
                loginDlg.Password         = password;
            }
            loginDlg.Response += (o, args) =>
            {
                if (args.Response == DialogResponse.OK)
                {
                    string parameters = string.Format(
                        "user={0}&password={1}&version=1337",
                        Uri.EscapeDataString(loginDlg.Username),
                        Uri.EscapeDataString(loginDlg.Password), 13);

                    WriteUserInfo((loginDlg.RememberUsername ? loginDlg.Username : ""),
                                  (loginDlg.RememberPassword ? loginDlg.Password : ""));

                    // Start a new thread and post the login info to login.minecraft.net
                    SimpleTask loginTask = new SimpleTask(() =>
                    {
                        string reply    = "";
                        bool postFailed = false;
                        try
                        {
                            if (loggingIn)
                            {
                                return;
                            }
                            loggingIn = true;
                            reply     = AppUtils.ExecutePost("https://login.minecraft.net/",
                                                             parameters);
                        }
                        catch (System.Net.WebException e)
                        {
                            postFailed = true;
                            reply      = e.Message;
                        }
                        finally
                        {
                            loggingIn = false;
                        }

                        // If the login failed
                        if (!reply.Contains(":") || postFailed)
                        {
                            // Translate the error message to a more user friendly wording
                            string errorMessage = reply;
                            switch (reply.ToLower())
                            {
                            case "bad login":
                                errorMessage = "Invalid username or password.";
                                break;

                            case "old version":
                                errorMessage = "Invalid launcher version.";
                                break;

                            default:
                                errorMessage = "Login failed: " + reply;
                                break;
                            }

                            // Unable to resolve hostname.
                            if (reply.ToLower().Contains("name could not be resolved"))
                            {
                                errorMessage = string.Format(
                                    "Couldn't connect to login.minecraft.net, " +
                                    "please connect to the internet or use offline mode.");
                            }

                            // Error
                            MainWindow.Invoke((sender, e) =>
                                              DoLogin(done, inst, errorMessage));
                        }

                        // If the login succeeded
                        else
                        {
                            string[] responseValues = reply.Split(':');

                            // The response must have at least 4 values or it's invalid
                            if (responseValues.Length < 4)
                            {
                                // Error
                                MainWindow.Invoke((sender, e) =>
                                                  DoLogin(done, inst,
                                                          "Got an invalid response from server", canplayOffline));
                            }
                            // Now we can finally return our login info.
                            else
                            {
                                LoginInfo info = new LoginInfo(responseValues,
                                                               loginDlg.ForceUpdate);
                                done(info, inst);
                            }
                        }
                    }, "Logging in...");

                    if (!loggingIn)
                    {
                        MainWindow.Invoke((o2, args2) =>
                                          StartModalTask(loginTask));
                    }
                }
                else if (args.Response == DialogResponse.No)
                {
                    // Play offline
                    done(new LoginInfo(null, false, false), inst);
                }
                else
                {
                    // Login cancelled
                    done(new LoginInfo(), inst);
                }
            };
            loginDlg.Run();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // Initialize AppUtils
            AppUtils.Init();

            if (!args.Contains("-v"))
            {
                Console.WriteLine("Operating System: {0}", OSUtils.OS.ToString());
            }

            if (OSUtils.OS == OSEnum.Linux && !args.Contains("-v"))
            {
                if (Environment.CurrentDirectory.Equals(Environment.GetEnvironmentVariable("HOME")))
                {
                    string workingDir = Path.GetDirectoryName(AppUtils.ExecutableFileName);
                    Environment.CurrentDirectory = workingDir;
                    Console.WriteLine("Set working directory to {0}", workingDir);
                }
            }

            if (!File.Exists(AppSettings.Main.JavaPath) ||
                AppSettings.Main.JavaPath == "java")
            {
                Console.WriteLine("Detecting Java path.");
                try
                {
                    AppSettings.Main.AutoDetectJavaPath();
                    AppSettings.Main.Save();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            // -u or --update
            if (args.Length > 0 && (args[0] == "-u" || args[0] == "--update"))
            {
                Console.WriteLine("Updating MultiMC...");
                if (args.Length < 2)
                {
                    Console.WriteLine("Invalid number of arguments for -u.");
                    Environment.Exit(-1);
                }
                else
                {
                    InstallUpdate(args[1]);
                }
                Environment.Exit(0);
            }

            if (args.Contains("-v") || args.Contains("--version"))
            {
                Console.WriteLine(AppUtils.GetVersion());
                return;
            }

            // If we're not on Windows or not using Mono, use GTK#.
            if (OSUtils.OS != OSEnum.Windows ||
                OSUtils.Runtime == Runtime.Mono)
            {
                Toolkit = WindowToolkit.GtkSharp;
            }
            else
            {
                Toolkit = WindowToolkit.WinForms;
            }

            if (args.Contains("--gtk"))
            {
                Toolkit = WindowToolkit.GtkSharp;
            }
            else if (args.Contains("--winforms"))
            {
                Toolkit = WindowToolkit.WinForms;
            }

            Main main = new Main();

            if (args.Length > 0 && (args[0] == "-l" || args[0] == "--launch"))
            {
                if (args.Length < 2)
                {
                    Console.WriteLine("Usage: MultiMC --launch <instance name>");
                    return;
                }

                string instName = args[1];

                Console.WriteLine("Launching instance '{0}'", instName);
                main.Run(instName);
            }
            else
            {
                main.Run();
            }

            if (InstallUpdates)
            {
                string currentFile = AppUtils.ExecutableFileName;
                Console.WriteLine(string.Format("{0} -u \"{1}\"",
                                                Properties.Resources.NewVersionFileName,
                                                currentFile));
                if (OSUtils.OS == OSEnum.Linux)
                {
                    Process.Start("gksudo chmod",
                                  string.Format("+x \"{0}\"",
                                                Properties.Resources.NewVersionFileName));
                    ProcessStartInfo info =
                        new ProcessStartInfo(Properties.Resources.NewVersionFileName,
                                             string.Format("-u \"{0}\"", currentFile));
                    info.UseShellExecute = false;
                    Process.Start(info);
                }
                else
                {
                    Process.Start(Properties.Resources.NewVersionFileName,
                                  string.Format("-u \"{0}\"", currentFile));
                }
                Environment.Exit(0);
            }
        }
Exemple #3
0
        public Main()
        {
            GUIManager.Create();
            GUIManager.Main.Initialize();

            MainWindow = GUIManager.Main.MainWindow();

            MainWindow.Title = string.Format("MultiMC Beta {0} for {1}",
                                             AppUtils.GetVersion().ToString(2), OSUtils.OSName);

            MainWindow.DefaultPosition = DefWindowPosition.CenterScreen;

            // Main toolbar
            MainWindow.AddInstClicked           += AddInstClicked;
            MainWindow.RefreshClicked           += (o, args) => MainWindow.LoadInstances();
            MainWindow.ViewFolderClicked        += ViewFolderClicked;
            MainWindow.CentralModsFolderClicked += CentralModsClicked;

            MainWindow.SettingsClicked     += SettingsClicked;
            MainWindow.CheckUpdatesClicked += UpdateClicked;

            MainWindow.AboutClicked += AboutClicked;

            // Instance context menu
            MainWindow.InstanceLaunched += LaunchInstance;

            MainWindow.ChangeIconClicked += ChangeIconClicked;
            MainWindow.EditNotesClicked  += EditNotesClicked;

            MainWindow.ManageSavesClicked    += ManageSavesClicked;
            MainWindow.EditModsClicked       += EditModsClicked;
            MainWindow.RebuildJarClicked     += RebuildClicked;
            MainWindow.ViewInstFolderClicked += ViewInstFolderClicked;

            MainWindow.DeleteInstClicked += DeleteInstClicked;
            // on linux, provide the possiblity to nuke OpenAL libs
            if (OSUtils.OS == OSEnum.Linux)
            {
                MainWindow.RemoveOpenALClicked += RemoveOpenALClicked;
            }

            // Try to load the icon list.
            int  tries = 0;
            bool keepTryingToLoadIcons = true;

            customIconLoadError = false;
            while (tries < 2 && keepTryingToLoadIcons)
            {
                try
                {
                    InstIconList         = GUIManager.Main.LoadInstIcons(tries == 0);
                    MainWindow.ImageList = InstIconList;

                    // If none of the above return false, stop trying to load icons.
                    keepTryingToLoadIcons = false;
                }
                catch (ArgumentException)
                {
                    keepTryingToLoadIcons = true;
                    customIconLoadError   = true;
                }
                finally
                {
                    tries++;
                }
            }

            MainWindow.LoadInstances();

            MainWindow.Closed += new EventHandler(MainWindow_Closed);

            // Initialize problem detection
            try
            {
                Console.WriteLine("Initializing problem detection system...");
                ProblemDetection.Problems.InitProblems();
            }
            catch (System.Reflection.ReflectionTypeLoadException e)
            {
                Console.WriteLine("Problem detection failed to initialize:\n{0}", e);
            }
        }