Пример #1
0
        public static void Save(LaunchContext context)
        {
            using (var sw = new StreamWriter(File.Open(FileManager.SettingsFilePath, FileMode.Create)))
            {
                var copy = context.Copy();
                copy.Password = Tools.EncodePassword(copy.Password);

                var s = new XmlSerializer(typeof (LaunchContext));
                s.Serialize(sw, copy);
            }
        }
Пример #2
0
        public CabinetForm(LaunchContext context)
            : this()
        {
            this.context = context;

            Load += (s, e) =>
            {
                Text = String.Format(Strings.CabinetTitle, context.Login);
                ShowSkin();
            };
        }
Пример #3
0
        public SettingsForm(LaunchContext context)
        {
            InitializeComponent();
            Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);

            Context = context;

            Load += (sender, args) =>
            {
                javaHomePath.Text = context.JavaHomePath;

                if (context.InitialJavaHeapSize < 256)
                    context.InitialJavaHeapSize = 256;

                if (context.InitialJavaHeapSize > 4096)
                    context.InitialJavaHeapSize = 4096;

                if (context.MaximumJavaHeapSize < 256)
                    context.MaximumJavaHeapSize = 256;

                if (context.MaximumJavaHeapSize > 4096)
                    context.MaximumJavaHeapSize = 4096;

                initialSize.Value = context.InitialJavaHeapSize;
                maximumSize.Value = context.MaximumJavaHeapSize;

                labelInitialSize.Text = initialSize.Value + Strings.MegaBytes;
                labelMaximumSize.Text = maximumSize.Value + Strings.MegaBytes;
            };

            initialSize.ValueChanged += (sender, args) =>
            {
                context.InitialJavaHeapSize = (initialSize.Value / 32) * 32;
                initialSize.Value = context.InitialJavaHeapSize;

                if (context.InitialJavaHeapSize > context.MaximumJavaHeapSize)
                    maximumSize.Value = context.InitialJavaHeapSize;

                labelInitialSize.Text = initialSize.Value + Strings.MegaBytes;
            };

            maximumSize.ValueChanged += (sender, args) =>
            {
                context.MaximumJavaHeapSize = (maximumSize.Value / 32) * 32;
                maximumSize.Value = context.MaximumJavaHeapSize;

                if (context.MaximumJavaHeapSize < context.InitialJavaHeapSize)
                    initialSize.Value = context.MaximumJavaHeapSize;

                labelMaximumSize.Text = maximumSize.Value + Strings.MegaBytes;
            };
        }
Пример #4
0
        /// <summary>
        /// Определяет местоположение Java на компьютере, используя контекст запуска.
        /// </summary>
        /// <param name="context">Контекст запуска</param>
        public static void LocateJavaFromSettings(LaunchContext context)
        {
            var javawFilePath = Path.Combine(context.JavaHomePath, "bin", JavaFileName);

            if (!File.Exists(javawFilePath))
                throw new InvalidOperationException(String.Format(Errors.UnableToFindJavaExe, JavaFileName));

            JavaFilePath = javawFilePath;
        }
Пример #5
0
        /// <summary>
        /// Определяет местоположение Java на комьютере, если оно не задано в контексте.
        /// </summary>
        /// <param name="context">Контекст запуска</param>
        public static void LocateJava(LaunchContext context)
        {
            if (!String.IsNullOrEmpty(context.JavaHomePath))
                return;

            string javaHome;

            if (LocateJavaHome(out javaHome))
            {
                context.JavaHomePath = javaHome;
            }
            else
                throw new Exception(Errors.UnableToFindJava);

            JavaHomePath = javaHome;

            var javawFilePath = Path.Combine(JavaHomePath, "bin", JavaFileName);

            if (!File.Exists(javawFilePath))
                throw new InvalidOperationException(String.Format(Errors.UnableToFindJavaExe, JavaFileName));

            JavaFilePath = javawFilePath;
        }
Пример #6
0
        /// <summary>
        /// Запускает клиент Minecraft, дожидается полного запуска и закрывает ланчер.
        /// </summary>
        /// <param name="context">Контекст запуска</param>
        public static void Run(LaunchContext context)
        {
            try
            {
                FileManager.LocateJavaFromSettings(context);
                FileManager.LocateMinecraft();

                var pi = new ProcessStartInfo
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardError = true,
                    FileName = FileManager.JavaFilePath,
                    Arguments = String.Format(
                        "-cp \"{0}\";\"{1}\";\"{2}\";\"{3}\"; -Djava.library.path=\"{4}\" -Xms{5}M -Xmx{6}M {7}{8}{9}{10} net.minecraft.client.Minecraft {11} {12} {13} {14}",
                        Path.Combine(FileManager.MinecraftBinDirectory, "minecraft.jar"),
                        Path.Combine(FileManager.MinecraftBinDirectory, "lwjgl.jar"),
                        Path.Combine(FileManager.MinecraftBinDirectory, "lwjgl_util.jar"),
                        Path.Combine(FileManager.MinecraftBinDirectory, "jinput.jar"),
                        Path.Combine(FileManager.MinecraftBinDirectory, "natives"),
                        context.InitialJavaHeapSize,
                        context.MaximumJavaHeapSize,
                        "-Dsun.java2d.noddraw=true",
                        "-Dsun.java2d.d3d=false",
                        "-Dsun.java2d.opengl=false",
                        "-Dsun.java2d.pmoffscreen=false",
                        context.Login,
                        context.SessionID,
                        Links.Server,
                        Links.ServerPort)
                };

                var directoryInfo = new DirectoryInfo(FileManager.MinecraftDirectory).Parent;

                if (directoryInfo != null)
                    pi.EnvironmentVariables["appdata"] = directoryInfo.FullName;

                var game = new Process
                {
                    StartInfo = pi
                };

                if (!game.Start())
                    return;

                game.ErrorDataReceived += (s, e) => File.AppendAllText(Path.Combine(FileManager.StartupDirectory, "log.txt"), e.Data + Environment.NewLine);
                game.BeginErrorReadLine();

                var clientStarted = false;

                var waitForClientTask = new Task(() =>
                {
                    for (var count = 0; count < 10; count++)
                    {
                        if (game.HasExited)
                            break;

                        if (game.MainWindowHandle != IntPtr.Zero && game.MainWindowTitle == "Minecraft")
                        {
                            Tools.SetForegroundWindow(game.MainWindowHandle);
                            Tools.SetFocus(game.MainWindowHandle);
                            clientStarted = true;
                            break;
                        }

                        Thread.Sleep(1000);
                    }
                });

                // ReSharper disable ImplicitlyCapturedClosure
                waitForClientTask.ContinueWith(x =>
                {
                    if (clientStarted)
                    {
                        Application.Exit();
                    }

                    if (game.HasExited)
                    {
                        Tools.InfoBoxShow("Запустить клиент не удалось!");
                    }
                },
                TaskScheduler.FromCurrentSynchronizationContext());
                // ReSharper restore ImplicitlyCapturedClosure
                waitForClientTask.Start();
            }
            catch (Exception ex)
            {
                Tools.InfoBoxShow(ex.Message);
            }
        }
Пример #7
0
        private void FormLoad(object sender, EventArgs e)
        {
            try
            {
                context = SettingsManager.Load();
            }
            catch (Exception ex)
            {
                Tools.InfoBoxShow(Errors.SettingsLoadError + ex.Message);
            }

            if (context == null || String.IsNullOrEmpty(context.JavaHomePath))
            {
                context = new LaunchContext
                {
                    InitialJavaHeapSize = 512,
                    MaximumJavaHeapSize = 1024
                };

                try
                {
                    FileManager.LocateJava(context);
                }
                catch (Exception ex)
                {
                    Tools.InfoBoxShow(ex.Message);
                }
            }

            if (checkForUpdatesOnStart)
                updaterControl.ForceCheckForUpdate(true);

            LoginTextBox.Text = context.Login;
            PasswordTextBox.Text = context.Password;
        }
Пример #8
0
        private void SettingsButtonClick(object sender, EventArgs e)
        {
            using (var sf = new SettingsForm(context.Copy()))
            {
                if (sf.ShowDialog() != DialogResult.OK)
                    return;

                context = sf.Context;
                SettingsManager.Save(context);
            }
        }