Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
fix yggdrasil launch fail without twitch bound
Browse files Browse the repository at this point in the history
  • Loading branch information
kblackcn committed May 27, 2015
1 parent 9381a3f commit ef5c591
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 129 deletions.
136 changes: 67 additions & 69 deletions KMCCC.Shared/Launcher/LauncherCoreInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Tools;

Expand All @@ -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,
Expand Down Expand Up @@ -84,12 +82,74 @@ private LaunchResult GenerateArguments(LaunchOptions options, ref MinecraftLaunc
}
}

internal LaunchResult LaunchInternal(LaunchOptions options, params Action<MinecraftLaunchArguments>[] 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)
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -149,67 +209,5 @@ internal void Exit(LaunchHandle handle, int code)
}

#endregion

internal LaunchResult LaunchInternal(LaunchOptions options, params Action<MinecraftLaunchArguments>[] 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};
}
}
}
}
83 changes: 23 additions & 60 deletions KMCCC.Shared/Modules/Yggdrasil/YggdrasilClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ef5c591

Please sign in to comment.