diff --git a/App.xaml.cs b/App.xaml.cs index 9bd69b6..1d1ee41 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using System.IO; +using System.Runtime.InteropServices; using System.Windows; using Zerolauncher.Manager; @@ -11,6 +12,7 @@ namespace Zerolauncher public partial class App : Application { public static bool needUpdate = false; + static Mutex? mutex; static bool TryDeleteDirectory(string path, bool recursive) { @@ -25,8 +27,43 @@ namespace Zerolauncher } } + static bool CheckProcess() + { + Process[] processes = Process.GetProcesses(); + Process currentProcess = Process.GetCurrentProcess(); + bool processExist = false; + foreach (Process p in processes) + { + if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id) + { + processExist = true; + } + } + return processExist; + } + + [DllImport("User32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); protected override void OnStartup(StartupEventArgs e) { + if (CheckProcess()) + { + Trace.WriteLine("应用程序已经在运行,将其置于前台并退出"); + // 应用程序已经在运行,将其置于前台并退出 + var currentProcess = Process.GetCurrentProcess(); + foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName)) + { + if (process.Id != currentProcess.Id) + { + SetForegroundWindow(process.MainWindowHandle); + break; + } + } + + // 关闭当前的应用程序实例 + Shutdown(); + return; + } base.OnStartup(e); Task.Run(async () => diff --git a/Manager/EngineManager.cs b/Manager/EngineManager.cs index ff71ea3..9416362 100644 --- a/Manager/EngineManager.cs +++ b/Manager/EngineManager.cs @@ -26,7 +26,6 @@ namespace Zerolauncher.Manager } if (UpDateManager.state) { - Trace.WriteLine("lalalala2"); UpDateManager.DoUpdate(); return true; } @@ -174,24 +173,30 @@ namespace Zerolauncher.Manager var lines = e.Data == null? [""] : e.Data.Split(" ", 2); // 切成两半 switch (lines[0]) { - case StaticHandleC.StartDone: + case StaticHandleC.LoadDone: if (gameMode == StaticHandleA.UpdateMode) { UpDateManager.DoUpdate1(); return; } - if(restartUrl == null) + Send($"{StaticHandleS.ShowWindow} {ServicesStaticInfo.ServicesName[account.providerId]}-{account.nickName}"); + break; + case StaticHandleC.StartDone: + if (gameMode == StaticHandleA.UpdateMode) + { + UpDateManager.DoUpdate2(); + return; + } + if (restartUrl == null) { - Send($"{StaticHandleS.ShowWindow} {ServicesStaticInfo.ServicesName[account.providerId]}-{account.nickName}"); Task.Run(async delegate { if (lines[1] == "True") await Task.Delay(5000); - _ =LoginManager.DoLogin(this); + _ = LoginManager.DoLogin(this); }); } else { - Send($"{StaticHandleS.ShowWindow} {ServicesStaticInfo.ServicesName[account.providerId]}-{account.nickName}"); Send($"{StaticHandleS.GameSa} {restartUrl}"); restartUrl = null; } diff --git a/Manager/LoginManager.cs b/Manager/LoginManager.cs index 9c19aa2..11e263a 100644 --- a/Manager/LoginManager.cs +++ b/Manager/LoginManager.cs @@ -1,6 +1,9 @@ -using System.Diagnostics; +using Newtonsoft.Json.Linq; +using System.Diagnostics; using System.Net; using System.Net.Http; +using System.Runtime.ConstrainedExecution; +using System.Text.RegularExpressions; namespace Zerolauncher.Manager { @@ -61,81 +64,117 @@ namespace Zerolauncher.Manager switch (game.account.providerId) { case 0: + game.Send($"{StaticHandleS.HintText} 尝试登玩家{game.account.nickName}到{ServicesStaticInfo.ServicesName[game.account.providerId]}的1服"); { - var values = new Dictionary - { - { "loginFrom", "uframe" }, - { "postLoginHandler", "default" }, - { "layoutSelfAdapting", "true" }, - { "externalLogin", "qq" }, - { "displayMode", "popup" }, - { "layout", "vertical" }, - { "appId", "www_home" }, - { "mainDivId", "popup_login_div" }, - { "includeFcmInfo", "false" }, - { "userNameLabel", "4399用户名" }, - { "userNameTip", "请输入4399用户名" }, - { "welcomeTip", "欢迎回到4399" }, - { "username", game.account.userName }, - { "password", game.account.userPWD } - }; - - var content = new FormUrlEncodedContent(values); - HttpResponseMessage? response = null; + HttpResponseMessage response; try { - response = await client.PostAsync("http://ptlogin.4399.com/ptlogin/login.do?v=1", content); + response = await client.GetAsync("http://180.97.189.249:2023"); } catch (Exception ex) { game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name},消息:{ex.Message}"); - need_web = $"http://web.4399.com/stat/togame.php?target=ddt&server_id=S{game.account.serverId}"; + need_web = $"http://180.97.189.249:2023"; break; } var responseString = await response.Content.ReadAsStringAsync(); - // eventHandles.__errorCallback('密码错误'); - if (responseString.Contains("eventHandles.__errorCallback")) + int count = 0; + while (responseString.Contains("adfwkey")) { - game.Send($"{StaticHandleS.HintText} 登录异常:{responseString.Split("eventHandles.__errorCallback('")[1].Split("'")[0]}"); - need_web = $"http://web.4399.com/stat/togame.php?target=ddt&server_id=S{game.account.serverId}"; + if (count++ <= 10) + { + game.Send($"{StaticHandleS.HintText} 网络被服务器拦截多次,请重试或联系管理员"); + need_web = $"http://180.97.189.249:2023"; + break; + } + var key = responseString.Split("adfwkey=")[1].Split("\"", 2)[0]; + game.Send($"{StaticHandleS.HintText} 被服务验证拦截-验证码key:{key}"); + try + { + response = await client.GetAsync("http://180.97.189.249:2023"); + } + catch (Exception ex) + { + game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name},消息:{ex.Message}"); + need_web = $"http://180.97.189.249:2023"; + break; + } + responseString = await response.Content.ReadAsStringAsync(); + } + } + { + var values = new Dictionary + { + { "lUsername", game.account.userName }, + { "lPassword", game.account.userPWD } + }; + var content = new FormUrlEncodedContent(values); + HttpResponseMessage response; + try + { + response = await client.PostAsync("http://180.97.189.249:2023/form/login.php", content); + } + catch (Exception ex) + { + game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name},消息:{ex.Message}"); + need_web = $"http://180.97.189.249:2023"; + break; + } + var responseString = await response.Content.ReadAsStringAsync(); + JObject jsonObj = JObject.Parse(responseString); + game.Send($"{StaticHandleS.HintText} {jsonObj["title"]}"); + game.Send($"{StaticHandleS.HintText} {jsonObj["content"]}"); + if ((int)jsonObj["type"] != 0) + { + need_web = $"http://180.97.189.249:2023"; break; } if(!game.Send($"{StaticHandleS.HintText} 登录成功,开始解析iframe")) return; } { - HttpResponseMessage? response = null; + HttpResponseMessage response; try { - response = await client.PostAsync("http://web.4399.com/stat/togame.php?target=ddt&server_id=S" + game.account.serverId, null); + response = await client.GetAsync("http://180.97.189.249:2023/Play/Play.php?id=1"); } catch (Exception ex) { game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name},消息:{ex.Message}"); - need_web = $"http://web.4399.com/stat/togame.php?target=ddt&server_id=S{game.account.serverId}"; + need_web = $"http://180.97.189.249:2023"; break; } var responseString = await response.Content.ReadAsStringAsync(); - if (!responseString.Contains("name=\"game_box\"")) + if (!responseString.Contains("var swfPath")) { - game.Send($"{StaticHandleS.HintText} 解析异常:服务器返回数据不正确"); - need_web = $"http://web.4399.com/stat/togame.php?target=ddt&server_id=S{game.account.serverId}"; + game.Send($"{StaticHandleS.HintText} 解析异常:服务器返回数据不正确1"); + need_web = $"http://180.97.189.249:2023"; break; } - responseString = responseString.Split("name=\"game_box\"")[1].Split("src=\"")[1].Split("\"")[0]; - try + //responseString = "" + responseString.Split("var swfPath")[1].Split("};")[0] + "};"; + string pattern = @"var swfPath = ""(.*?)"";(.*?){(.*?)};"; + Match match = Regex.Match(responseString, pattern, RegexOptions.Singleline); + + if (!match.Success) { - response = await client.PostAsync(responseString, null); - } - catch (Exception ex) - { - game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name.Replace(" ", "_")},消息:{ex.Message.Replace(" ", "_")}"); - need_web = $"http://web.4399.com/stat/togame.php?target=ddt&server_id=S{game.account.serverId}"; + game.Send($"{StaticHandleS.HintText} 解析异常:服务器返回数据不正确2"); + //Trace.WriteLine($"---------{responseString}|"); + need_web = $"http://180.97.189.249:2023"; break; } - responseString = await response.Content.ReadAsStringAsync(); - responseString = "http://" + response.RequestMessage.RequestUri.ToString().Split("/")[2] + "/" + responseString.Split("movie\" value='")[1].Split("'")[0]; - Trace.WriteLine(responseString); - game.Send($"{StaticHandleS.GameSa} {responseString}"); + string url = match.Groups[1].Value; + string parameters = match.Groups[3].Value; + + string patternParams = @"(\w+): ""(.*?)"""; + MatchCollection matches = Regex.Matches(parameters, patternParams); + + url += "?"; + foreach (Match m in matches) + { + url += $"{m.Groups[1].Value}={WebUtility.UrlEncode(m.Groups[2].Value)}&"; + } + url = url.TrimEnd('&'); + Trace.WriteLine(url); + game.Send($"{StaticHandleS.GameSa} {url}"); } break; default: diff --git a/Manager/UpDateManager.cs b/Manager/UpDateManager.cs index b44386c..d4a4439 100644 --- a/Manager/UpDateManager.cs +++ b/Manager/UpDateManager.cs @@ -21,17 +21,17 @@ namespace Zerolauncher.Manager bool needMian= false; if(checkMain) { - //string filePath = Process.GetCurrentProcess().MainModule.FileName; + string filePath = Process.GetCurrentProcess().MainModule.FileName; string? now_bit; - //using (SHA256 sha256 = SHA256.Create()) - //{ - // using (FileStream fileStream = File.OpenRead(filePath)) - // { - // byte[] hashBytes = sha256.ComputeHash(fileStream); - // now_bit = BitConverter.ToString(hashBytes).Replace("-", string.Empty); - // } - //} - now_bit = "67FD2BCB9F6756A82621840896DCE80BB4ACEF22A8D1FF98FC835BE291F5CC84"; + using (SHA256 sha256 = SHA256.Create()) + { + using (FileStream fileStream = File.OpenRead(filePath)) + { + byte[] hashBytes = sha256.ComputeHash(fileStream); + now_bit = BitConverter.ToString(hashBytes).Replace("-", string.Empty); + } + } + //now_bit = "170CD25DCCF3FFCCFAA423B85E1715E7CD28959333C4C5F86FB1E73666359998"; needMian = now_bit == CacheSha.GetM() && now_bit != null; } if(!needEngine) @@ -86,13 +86,13 @@ namespace Zerolauncher.Manager public static void DoUpdate1() { - Task.Run(async () => - { - updateProcess.Send($"{StaticHandleS.ShowWindow} 自助更新"); - await Task.Delay(1000); - updateProcess.Send($"{StaticHandleS.UpdateInfo} {UpDateData.tis}"); - updateProcess.Send($"{StaticHandleS.UseBrowser} {UpDateData.lanzou + UpDateData.auto_packet_url}"); - }); + updateProcess.Send($"{StaticHandleS.ShowWindow} 自助更新"); + } + + public static void DoUpdate2() + { + updateProcess.Send($"{StaticHandleS.UpdateInfo} {UpDateData.tis}"); + updateProcess.Send($"{StaticHandleS.UseBrowser} {UpDateData.lanzou + UpDateData.auto_packet_url}"); } public static void OnDownLoadDone(string path) diff --git a/StaticHandleProtol.cs b/StaticHandleProtol.cs index f7eeb36..ac2d7a3 100644 --- a/StaticHandleProtol.cs +++ b/StaticHandleProtol.cs @@ -14,12 +14,13 @@ class StaticHandleC { - public const string StartDone = "0a"; - public const string BrowserDone = "0b"; - public const string StartGame = "0c"; - public const string GameDone = "0d"; - public const string Version = "0e"; - public const string DownloadDone = "0f"; + public const string LoadDone = "0a"; + public const string StartDone = "0b"; + public const string BrowserDone = "0c"; + public const string StartGame = "0d"; + public const string GameDone = "0e"; + public const string Version = "0f"; + public const string DownloadDone = "0g"; } class StaticHandleA