ZeroLauncher/Manager/EngineManager.cs

314 lines
11 KiB
C#
Raw Normal View History

2024-03-07 21:04:59 +08:00
using System.Diagnostics;
2024-03-18 22:59:51 +08:00
using System.IO;
using System.Security.Cryptography;
2024-03-07 21:04:59 +08:00
using System.Windows;
using Zerolauncher.Defender;
namespace Zerolauncher.Manager
{
internal class EngineManager
{
private static Dictionary<string, SingleGame> mGame = new Dictionary<string, SingleGame>();
//运行时才能决定是否执行内联
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
2024-06-23 10:04:00 +08:00
private static string AccToKey(AccountNew account)
2024-03-07 21:04:59 +08:00
{
return string.Format("{0}{1}{2}", account.providerId, account.serverId, account.userName);
}
2024-06-23 10:04:00 +08:00
public static bool CreateGame(AccountNew account)
2024-03-07 21:04:59 +08:00
{
2024-03-11 10:57:12 +08:00
if (UpDateData.is_check == false)
{
2024-03-18 22:59:51 +08:00
MessageBox.Show("正在更新游戏数据请等待1-3秒。\n 请检查网络", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
2024-03-11 10:57:12 +08:00
return true;
}
2024-05-19 10:43:41 +08:00
//if (UpDateManager.state)
//{
// UpDateManager.DoUpdate();
// return true;
//}
2024-03-07 21:04:59 +08:00
var key = AccToKey(account);
if (mGame.ContainsKey(key)) { return false; }
2024-03-09 18:29:16 +08:00
if (CacheSha.errorCode != 0) {
switch (CacheSha.errorCode)
2024-03-07 21:04:59 +08:00
{
case 1:
2024-03-18 22:59:51 +08:00
MessageBox.Show("发生网络错误==EMS。\n 请检查网络", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
2024-03-07 21:04:59 +08:00
break;
case 2:
2024-03-18 22:59:51 +08:00
MessageBox.Show("发生游戏服务错误==EMS。\n 请联系管理员", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
2024-03-07 21:04:59 +08:00
break;
default:
2024-03-18 22:59:51 +08:00
MessageBox.Show("发生未知错误==EMS。\n 请联系管理员", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
2024-03-07 21:04:59 +08:00
break;
}
2024-03-11 10:57:12 +08:00
return true;
2024-03-07 21:04:59 +08:00
}
try
{
mGame[key] = new SingleGame(account);
}catch (Exception _ex)
{
2024-03-19 21:32:07 +08:00
Trace.WriteLine("lalalala3");
2024-03-18 22:59:51 +08:00
UpDateManager.DoUpdate();
2024-03-07 21:04:59 +08:00
}
return true;
}
2024-03-19 12:09:31 +08:00
public static bool CreateGame(int memberId)
2024-03-07 21:04:59 +08:00
{
2024-03-19 12:09:31 +08:00
return CreateGame(AccountManager.accountsList[memberId]);
2024-03-07 21:04:59 +08:00
}
2024-06-23 10:04:00 +08:00
public static int CheckGameState(AccountNew account)
2024-03-07 21:04:59 +08:00
{
var key = AccToKey(account);
if (mGame.ContainsKey(key)) { return mGame[key].mHandle; }
return -1;
}
2024-06-23 10:04:00 +08:00
public static short TurnGameSizeMini(AccountNew account)
2024-03-07 21:04:59 +08:00
{
var key = AccToKey(account);
if (mGame.ContainsKey(key))
{
var game = mGame[key];
if (game.mHandle != 0)
{
game.Send(StaticHandleS.MiniSize);
return 2;
}
return 1;
}
return 0;
}
2024-06-23 10:04:00 +08:00
public static short TurnGameSizeNormal(AccountNew account)
2024-03-07 21:04:59 +08:00
{
var key = AccToKey(account);
if (mGame.ContainsKey(key))
{
var game = mGame[key];
if (game.mHandle != 0)
{
game.Send(StaticHandleS.NormalSize);
return 2;
}
return 1;
}
return 0;
}
2024-06-23 10:04:00 +08:00
public static bool ExitGame(AccountNew account)
2024-03-07 21:04:59 +08:00
{
var key = AccToKey(account);
if (!mGame.ContainsKey(key)) { return false; }
mGame[key].Send(StaticHandleS.CloseGame);
return true;
}
2024-06-23 10:04:00 +08:00
public static void OnGameExit(AccountNew account)
2024-03-07 21:04:59 +08:00
{
mGame.Remove(AccToKey(account));
}
public static bool CheckEmpy()
{
return mGame.Count == 0;
}
}
class SingleGame
{
Process process;
string? restartUrl;
public int mHandle;
2024-06-23 10:04:00 +08:00
public AccountNew? account;
2024-03-09 18:29:16 +08:00
string gameMode;
2024-03-07 21:04:59 +08:00
2024-06-23 10:04:00 +08:00
public SingleGame(AccountNew? acc, string mod = StaticHandleA.GameMode)
2024-03-07 21:04:59 +08:00
{
restartUrl = null;
2024-03-09 18:29:16 +08:00
account = acc;
gameMode = mod;
2024-03-07 21:04:59 +08:00
mHandle = 0;
2024-03-09 18:29:16 +08:00
process = EngineShell.CheckEngineSafe(gameMode);
2024-03-18 22:59:51 +08:00
process.OutputDataReceived += Handle;
2024-05-19 10:43:41 +08:00
process.ErrorDataReceived += Handle;
2024-03-18 22:59:51 +08:00
process.Exited += Process_Exited;
2024-03-09 18:29:16 +08:00
2024-03-18 22:59:51 +08:00
process.Start();
process.BeginOutputReadLine(); // 开始异步读取
2024-03-07 21:04:59 +08:00
}
private void CreateProcess()
{
2024-03-09 18:29:16 +08:00
process = EngineShell.CheckEngineSafe(gameMode);
2024-03-07 21:04:59 +08:00
process.OutputDataReceived += Handle;
process.Exited += Process_Exited;
process.Start();
process.BeginOutputReadLine(); // 开始异步读取
}
private void Process_Exited(object? sender, EventArgs e)
{
2024-05-19 10:43:41 +08:00
Trace.WriteLine(
$"Exit time : {process.ExitTime}\n" +
$"Exit code : {process.ExitCode}\n" +
$"Elapsed time : {Math.Round((process.ExitTime - process.StartTime).TotalMilliseconds)}");
Trace.WriteLine($"进程已退出:{account.nickName}");
2024-03-09 18:29:16 +08:00
if (gameMode != StaticHandleA.GameMode) return;
if ( restartUrl == null)
2024-03-07 21:04:59 +08:00
{
EngineManager.OnGameExit(account);
return;
}
CreateProcess();
}
private void Handle(object sender, DataReceivedEventArgs e)
{
2024-03-18 22:59:51 +08:00
var lines = e.Data == null? [""] : e.Data.Split(" ", 2); // 切成两半
2024-03-07 21:04:59 +08:00
switch (lines[0])
{
2024-03-22 17:23:15 +08:00
case StaticHandleC.LoadDone:
2024-03-18 22:59:51 +08:00
if (gameMode == StaticHandleA.UpdateMode)
{
UpDateManager.DoUpdate1();
return;
}
2024-03-22 17:23:15 +08:00
Send($"{StaticHandleS.ShowWindow} {ServicesStaticInfo.ServicesName[account.providerId]}-{account.nickName}");
break;
case StaticHandleC.StartDone:
if (gameMode == StaticHandleA.UpdateMode)
{
UpDateManager.DoUpdate2();
return;
}
if (restartUrl == null)
2024-03-07 21:04:59 +08:00
{
2024-03-09 18:29:16 +08:00
Task.Run(async delegate
{
if (lines[1] == "True") await Task.Delay(5000);
2024-05-19 10:43:41 +08:00
LoginManager.DoLogin(this);
2024-03-09 18:29:16 +08:00
});
2024-03-07 21:04:59 +08:00
}
else
{
Send($"{StaticHandleS.GameSa} {restartUrl}");
restartUrl = null;
}
break;
case StaticHandleC.GameDone:
mHandle = int.Parse(lines[1]);
break;
case StaticHandleC.BrowserDone:
2024-05-19 10:43:41 +08:00
Trace.WriteLine($"尝试让游戏退出:{account.nickName}");
2024-03-07 21:04:59 +08:00
restartUrl = lines[1];
Send(StaticHandleS.CloseGame);
break;
2024-03-09 18:29:16 +08:00
case StaticHandleC.Version:
mHandle = int.Parse(lines[1]);
break;
2024-03-18 22:59:51 +08:00
case StaticHandleC.DownloadDone:
UpDateManager.OnDownLoadDone(lines[1]);
break;
2024-05-19 10:43:41 +08:00
case StaticHandleC.TakeVerify:
var args = lines[1].Split(' ');
_ = LoginManager.DoVerify(this, args[0], args[1]);
break;
default:
Trace.WriteLine($"from client:{e.Data}");
break;
2024-03-07 21:04:59 +08:00
}
}
public bool Send(string msg)
{
if(process.HasExited) { return false; }
process.StandardInput.WriteLine(msg);
return true;
}
}
public class FileReadException : Exception
{
public FileReadException(string message) : base(message)
{
}
}
class EngineShell
{
2024-03-18 22:59:51 +08:00
static bool needShowTis = true;
2024-03-07 21:04:59 +08:00
static bool is_check = false;
const string engine_file = @"ZeroEngine.exe";
2024-03-09 18:29:16 +08:00
public static Process CheckEngineSafe(string mod)
2024-03-07 21:04:59 +08:00
{
2024-03-09 18:29:16 +08:00
bool is_first_luancher = EngineManager.CheckEmpy();
2024-05-12 16:43:07 +08:00
#region
2024-05-19 10:43:41 +08:00
//if (mod == StaticHandleA.UpdateMode)
//{
// if (DataStream.dataStream.ecs.Length > 10)
// {
// string? now_bit;
// using (SHA256 sha256 = SHA256.Create())
// {
// using (FileStream fileStream = File.OpenRead(engine_file))
// {
// byte[] hashBytes = sha256.ComputeHash(fileStream);
// now_bit = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
// }
// }
// if (DataStream.dataStream.ecs != now_bit)
// {
// Trace.WriteLine("lalalala" + DataStream.dataStream.ecs);
// throw new FileReadException("error esu1!");
// }
// }
//}
//else if (!is_check && is_first_luancher)
//{
// string? now_bit;
// using (SHA256 sha256 = SHA256.Create())
// {
// using (FileStream fileStream = File.OpenRead(engine_file))
// {
// byte[] hashBytes = sha256.ComputeHash(fileStream);
// now_bit = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
// }
// }
// if (CacheSha.GetE() != now_bit)
// {
// Trace.WriteLine("lalalala1");
// throw new FileReadException("error esu0!");
// }
// if (DataStream.dataStream.ecs != now_bit) { DataStream.dataStream.ecs = now_bit; DataStream.write(); }
// is_check = true;
//}
2024-05-12 16:43:07 +08:00
#endregion
2024-03-09 18:29:16 +08:00
2024-03-07 21:04:59 +08:00
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = engine_file,
2024-03-18 22:59:51 +08:00
Arguments = $"{StaticHandleA.GameMode} {needShowTis}",
2024-03-07 21:04:59 +08:00
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
2024-05-19 10:43:41 +08:00
RedirectStandardOutput = true,
RedirectStandardError = true
},
2024-03-07 21:04:59 +08:00
EnableRaisingEvents = true
};
2024-03-18 22:59:51 +08:00
if (needShowTis) needShowTis = false;
2024-03-07 21:04:59 +08:00
return process;
}
}
}