From ef5c59186a31400afadb59ef734aae7f4dfc1f07 Mon Sep 17 00:00:00 2001 From: ZhouYiran Date: Wed, 27 May 2015 23:49:03 +0800 Subject: [PATCH] fix yggdrasil launch fail without twitch bound --- KMCCC.Shared/Launcher/LauncherCoreInternal.cs | 136 +++++++++--------- .../Modules/Yggdrasil/YggdrasilClient.cs | 83 +++-------- 2 files changed, 90 insertions(+), 129 deletions(-) diff --git a/KMCCC.Shared/Launcher/LauncherCoreInternal.cs b/KMCCC.Shared/Launcher/LauncherCoreInternal.cs index 1c23eda..2bc6491 100644 --- a/KMCCC.Shared/Launcher/LauncherCoreInternal.cs +++ b/KMCCC.Shared/Launcher/LauncherCoreInternal.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.IO; using System.Linq; - using System.Runtime.InteropServices; using System.Threading.Tasks; using Tools; @@ -17,13 +16,12 @@ partial class LauncherCore { internal object Locker = new object(); - private LaunchResult GenerateArguments(LaunchOptions options, ref MinecraftLaunchArguments args) { try { var authentication = options.Authenticator.Do(); - if (!String.IsNullOrWhiteSpace(authentication.Error)) + if (!string.IsNullOrWhiteSpace(authentication.Error)) return new LaunchResult { Success = false, @@ -84,12 +82,74 @@ private LaunchResult GenerateArguments(LaunchOptions options, ref MinecraftLaunc } } + internal LaunchResult LaunchInternal(LaunchOptions options, params Action[] argumentsOperators) + { + lock (Locker) + { + if (!File.Exists(JavaPath)) + { + return new LaunchResult {Success = false, ErrorType = ErrorType.NoJAVA, ErrorMessage = "指定的JAVA位置不存在"}; + } + CurrentCode = Random.Next(); + var args = new MinecraftLaunchArguments(); + var result = GenerateArguments(options, ref args); + if (result != null) + { + return result; + } + if (argumentsOperators == null) return LaunchGame(args); + foreach (var opt in argumentsOperators) + { + try + { + if (opt != null) + { + opt(args); + } + } + catch (Exception exp) + { + return new LaunchResult {Success = false, ErrorType = ErrorType.OperatorException, ErrorMessage = "指定的操作器引发了异常", Exception = exp}; + } + } + return LaunchGame(args); + } + } + + private LaunchResult LaunchGame(MinecraftLaunchArguments args) + { + try + { + var handle = new LaunchHandle(args.Authentication) + { + Code = CurrentCode, + Core = this, + Arguments = args, + Process = Process.Start(new ProcessStartInfo(JavaPath) + { + Arguments = args.ToArguments(), + UseShellExecute = false, + WorkingDirectory = GameRootPath, + RedirectStandardError = true, + RedirectStandardOutput = true + }) + }; + handle.Work(); + Task.Factory.StartNew(handle.Process.WaitForExit).ContinueWith(t => Exit(handle, handle.Process.ExitCode)); + return new LaunchResult {Success = true, Handle = handle}; + } + catch (Exception exp) + { + return new LaunchResult {Success = false, ErrorType = ErrorType.Unknown, ErrorMessage = "启动时出现了异常", Exception = exp}; + } + } + #region 复制文件夹 public void CopyVersionDirectory(string directoryName, string versionId) { - CopyDirectory(String.Format(@"{0}\versions\{2}\{1}", GameRootPath, directoryName, versionId), - String.Format(@"{0}\{1}", GameRootPath, directoryName)); + CopyDirectory(string.Format(@"{0}\versions\{2}\{1}", GameRootPath, directoryName, versionId), + string.Format(@"{0}\{1}", GameRootPath, directoryName)); } public void CopyDirectory(string source, string target) @@ -117,14 +177,14 @@ public void CopyDirectory(string source, string target) public void CopyVersionDirectories(string ver) { - var root = String.Format(@"{0}\versions\{1}\moddir", GameRootPath, ver); + var root = string.Format(@"{0}\versions\{1}\moddir", GameRootPath, ver); if (!Directory.Exists(root)) { return; } foreach (var dir in new DirectoryInfo(root).EnumerateDirectories()) { - CopyDirectory(dir.FullName, String.Format(@"{0}\{1}", GameRootPath, dir.Name)); + CopyDirectory(dir.FullName, string.Format(@"{0}\{1}", GameRootPath, dir.Name)); } } @@ -149,67 +209,5 @@ internal void Exit(LaunchHandle handle, int code) } #endregion - - internal LaunchResult LaunchInternal(LaunchOptions options, params Action[] argumentsOperators) - { - lock (Locker) - { - if (!File.Exists(JavaPath)) - { - return new LaunchResult { Success = false, ErrorType = ErrorType.NoJAVA, ErrorMessage = "指定的JAVA位置不存在" }; - } - CurrentCode = Random.Next(); - var args = new MinecraftLaunchArguments(); - var result = GenerateArguments(options, ref args); - if (result != null) - { - return result; - } - if (argumentsOperators == null) return LaunchGame(args); - foreach (var opt in argumentsOperators) - { - try - { - if (opt != null) - { - opt(args); - } - } - catch (Exception exp) - { - return new LaunchResult { Success = false, ErrorType = ErrorType.OperatorException, ErrorMessage = "指定的操作器引发了异常", Exception = exp }; - } - } - return LaunchGame(args); - } - } - - private LaunchResult LaunchGame(MinecraftLaunchArguments args) - { - try - { - var handle = new LaunchHandle(args.Authentication) - { - Code = CurrentCode, - Core = this, - Arguments = args, - Process = Process.Start(new ProcessStartInfo(JavaPath) - { - Arguments = args.ToArguments(), - UseShellExecute = false, - WorkingDirectory = GameRootPath, - RedirectStandardError = true, - RedirectStandardOutput = true - }) - }; - handle.Work(); - Task.Factory.StartNew(handle.Process.WaitForExit).ContinueWith(t => Exit(handle, handle.Process.ExitCode)); - return new LaunchResult {Success = true, Handle = handle}; - } - catch (Exception exp) - { - return new LaunchResult {Success = false, ErrorType = ErrorType.Unknown, ErrorMessage = "启动时出现了异常", Exception = exp}; - } - } } } \ No newline at end of file diff --git a/KMCCC.Shared/Modules/Yggdrasil/YggdrasilClient.cs b/KMCCC.Shared/Modules/Yggdrasil/YggdrasilClient.cs index a131fce..a4e6b8a 100644 --- a/KMCCC.Shared/Modules/Yggdrasil/YggdrasilClient.cs +++ b/KMCCC.Shared/Modules/Yggdrasil/YggdrasilClient.cs @@ -50,6 +50,25 @@ public void Clear() } } + private void UpdateFomrResponse(AuthenticationResponse response) + { + AccessToken = Guid.Parse(response.AccessToken); + if (response.User != null) + { + AccountType = response.User.Legacy ? "Legacy" : "Mojang"; + Properties = response.User.Properties != null + ? response.User.Properties.ToJson() + : "{}"; + } + else + { + AccountType = "Mojang"; + Properties = "{}"; + } + DisplayName = response.SelectedProfile.Name; + UUID = Guid.Parse(response.SelectedProfile.Id); + } + #region Refresh public bool Refresh(bool twitchEnabled = true) @@ -76,21 +95,7 @@ public bool Refresh(bool twitchEnabled = true) { return false; } - AccessToken = Guid.Parse(response.AccessToken); - if (response.User != null) - { - AccountType = response.User.Legacy ? "Legacy" : "Mojang"; - if (response.User.Properties != null) - { - Properties = response.User.Properties.ToJson(); - } - } - else - { - AccountType = "Mojang"; - } - DisplayName = response.SelectedProfile.Name; - UUID = Guid.Parse(response.SelectedProfile.Id); + UpdateFomrResponse(response); return true; } catch (Exception) @@ -125,21 +130,7 @@ public bool Refresh(Guid accessToken, bool twitchEnabled = true) { return false; } - AccessToken = Guid.Parse(response.AccessToken); - if (response.User != null) - { - AccountType = response.User.Legacy ? "Legacy" : "Mojang"; - if (response.User.Properties != null) - { - Properties = response.User.Properties.ToJson(); - } - } - else - { - AccountType = "Mojang"; - } - DisplayName = response.SelectedProfile.Name; - UUID = Guid.Parse(response.SelectedProfile.Id); + UpdateFomrResponse(response); return true; } catch (Exception) @@ -179,21 +170,7 @@ public bool Authenticate(string email, string password, bool twitchEnabled = tru { return false; } - AccessToken = Guid.Parse(response.AccessToken); - if (response.User != null) - { - AccountType = response.User.Legacy ? "Legacy" : "Mojang"; - if (response.User.Properties != null) - { - Properties = response.User.Properties.ToJson(); - } - } - else - { - AccountType = "Mojang"; - } - DisplayName = response.SelectedProfile.Name; - UUID = Guid.Parse(response.SelectedProfile.Id); + UpdateFomrResponse(response); return true; } catch (Exception) @@ -233,21 +210,7 @@ public bool Authenticate(string email, string password, bool twitchEnabled = tru task.SetResult(false); return; } - AccessToken = Guid.Parse(response.AccessToken); - if (response.User != null) - { - AccountType = response.User.Legacy ? "Legacy" : "Mojang"; - if (response.User.Properties != null) - { - Properties = response.User.Properties.ToJson(); - } - } - else - { - AccountType = "Mojang"; - } - DisplayName = response.SelectedProfile.Name; - UUID = Guid.Parse(response.SelectedProfile.Id); + UpdateFomrResponse(response); task.SetResult(true); } catch (Exception exception)