diff --git a/FunGame.Core/Api/Utility/General.cs b/FunGame.Core/Api/Utility/General.cs index ffafdda..8017c3c 100644 --- a/FunGame.Core/Api/Utility/General.cs +++ b/FunGame.Core/Api/Utility/General.cs @@ -5,6 +5,7 @@ using System.Net.NetworkInformation; using System.Reflection; using System.Runtime.InteropServices; using System.Text; +using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; using Milimoe.FunGame.Core.Library.Constant; @@ -104,6 +105,35 @@ namespace Milimoe.FunGame.Core.Api.Utility } return -1; } + + /// + /// 将JsonElement转换为泛型 + /// + /// 泛型 + /// 为JsonElement的对象 + /// + public static T? ConvertJsonObject(object obj) + { + return ((JsonElement)obj).ToObject(); + } + } + + /// + /// Json工具类 + /// + public static class JsonUtility + { + /// + /// 将JsonElement转换为泛型 + /// + /// 泛型 + /// JsonElement + /// + public static T? ToObject(this JsonElement element) + { + var json = element.GetRawText(); + return JsonSerializer.Deserialize(json); + } } #endregion diff --git a/FunGame.Core/Library/Common/Network/JsonObject.cs b/FunGame.Core/Library/Common/Network/JsonObject.cs index 0a3b43a..bc8d7be 100644 --- a/FunGame.Core/Library/Common/Network/JsonObject.cs +++ b/FunGame.Core/Library/Common/Network/JsonObject.cs @@ -3,20 +3,22 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Library.Common.Network { [Serializable] - internal class JsonObject + public class JsonObject { - internal SocketMessageType MessageType { get; } = SocketMessageType.Unknown; - internal string Token { get; } - internal object[] Parameters { get; } - internal string JsonString { get; } + public SocketMessageType MessageType { get; } = SocketMessageType.Unknown; + public string Token { get; } + public object[] Parameters { get; } + public string JsonString { get; } - internal JsonObject(SocketMessageType MessageType, string Token, object[] Parameters) + [JsonConstructor] + public JsonObject(SocketMessageType MessageType, string Token, object[] Parameters) { this.MessageType = MessageType; this.Token = Token; @@ -24,12 +26,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network this.JsonString = JsonSerializer.Serialize(this); } - internal static string GetString(SocketMessageType MessageType, string Token, object[] Parameters) + public static string GetString(SocketMessageType MessageType, string Token, object[] Parameters) { return new JsonObject(MessageType, Token, Parameters).JsonString; } - internal static JsonObject? GetObject(string JsonString) + public static JsonObject? GetObject(string JsonString) { return JsonSerializer.Deserialize(JsonString); } diff --git a/FunGame.Core/Service/SocketManager.cs b/FunGame.Core/Service/SocketManager.cs index 37f5262..5fd791c 100644 --- a/FunGame.Core/Service/SocketManager.cs +++ b/FunGame.Core/Service/SocketManager.cs @@ -169,8 +169,7 @@ namespace Milimoe.FunGame.Core.Service Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg); if (json != null) { - result[0] = json.MessageType; - result[1] = json.Parameters; + result = new object[] { json.MessageType, json.Parameters }; } return result; } @@ -197,8 +196,7 @@ namespace Milimoe.FunGame.Core.Service Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg); if (json != null) { - result[0] = json.MessageType; - result[1] = json.Parameters; + result = new object[] { json.MessageType, json.Parameters }; } return result; } diff --git a/FunGame.Desktop/Controller/MainController.cs b/FunGame.Desktop/Controller/MainController.cs index a0c49ef..001aa8e 100644 --- a/FunGame.Desktop/Controller/MainController.cs +++ b/FunGame.Desktop/Controller/MainController.cs @@ -16,7 +16,7 @@ namespace Milimoe.FunGame.Desktop.Controller { public class MainController { - private MainModel MainModel; + private MainModel MainModel { get; } public MainController(Main Main) { diff --git a/FunGame.Desktop/Model/MainModel.cs b/FunGame.Desktop/Model/MainModel.cs index 4420c4f..30ced86 100644 --- a/FunGame.Desktop/Model/MainModel.cs +++ b/FunGame.Desktop/Model/MainModel.cs @@ -1,4 +1,9 @@ -using Milimoe.FunGame.Core.Api.Utility; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Constant; @@ -6,11 +11,6 @@ using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Others; using Milimoe.FunGame.Desktop.UI; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Milimoe.FunGame.Desktop.Model { @@ -19,8 +19,6 @@ namespace Milimoe.FunGame.Desktop.Model public Core.Library.Common.Network.Socket? Socket { get; private set; } public Main Main { get; } - private int CurrentRetryTimes = -1; - private int MaxRetryTimes { get; } = SocketSet.MaxRetryTimes; private Task? ReceivingTask; public MainModel(Main main) @@ -117,11 +115,11 @@ namespace Milimoe.FunGame.Desktop.Model } if (!Others.Config.FunGame_isConnected) { - CurrentRetryTimes++; - if (CurrentRetryTimes == 0) Main?.GetMessage("开始连接服务器...", true, TimeType.General); - else Main?.GetMessage("第" + CurrentRetryTimes + "次重试连接服务器..."); + Main!.CurrentRetryTimes++; + if (Main!.CurrentRetryTimes == 0) Main!.GetMessage("开始连接服务器...", true, TimeType.General); + else Main!.GetMessage("第" + Main!.CurrentRetryTimes + "次重试连接服务器..."); // 超过重连次数上限 - if (CurrentRetryTimes + 1 > MaxRetryTimes) + if (Main!.CurrentRetryTimes + 1 > Main!.MaxRetryTimes) { throw new Exception("无法连接至服务器,请检查网络并重启游戏再试。"); } @@ -141,12 +139,9 @@ namespace Milimoe.FunGame.Desktop.Model Main?.GetMessage("连接服务器成功,请登录账号以体验FunGame。"); Main?.UpdateUI(MainControllerSet.Connected); StartReceiving(); - return ConnectResult.Success; } - return ConnectResult.ConnectFailed; }); - t.Wait(5000); - Main?.GetMessage("ERROR: 连接超时,远程服务器没有回应。", false); + return ConnectResult.Success; } Socket?.Close(); Config.FunGame_isRetrying = false; @@ -273,13 +268,13 @@ namespace Milimoe.FunGame.Desktop.Model private void SocketHandle_Connect(object[] objs) { string msg = ""; - if (objs.Length > 0) msg = (string)objs[0]; + if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject(objs[0])!; string[] strings = msg.Split(';'); string ServerName = strings[0]; string ServerNotice = strings[1]; Config.FunGame_ServerName = ServerName; Config.FunGame_Notice = ServerNotice; - if (objs.Length > 1) msg = (string)objs[1]; + if (objs.Length > 1) msg = NetworkUtility.ConvertJsonObject(objs[1])!; Socket!.Token = msg; Main?.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n"); // 设置等待登录的黄灯 @@ -288,14 +283,14 @@ namespace Milimoe.FunGame.Desktop.Model private void SocketHandle_GetNotice(object[] objs) { - if (objs.Length > 0) Config.FunGame_Notice = (string)objs[0]; + if (objs.Length > 0) Config.FunGame_Notice = NetworkUtility.ConvertJsonObject(objs[0])!; } private void SocketHandle_CheckLogin(object[] objs) { string msg = ""; // 返回的objs是该Login的User对象的各个属性 - if (objs.Length > 0) msg = (string)objs[0]; + if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject(objs[0])!; Main?.GetMessage(msg); Main?.UpdateUI(MainControllerSet.SetUser, new object[] { Factory.New(msg) }); } @@ -303,9 +298,9 @@ namespace Milimoe.FunGame.Desktop.Model private void SocketHandle_Disconnect(object[] objs) { string msg = ""; - if (objs.Length > 0) msg = (string)objs[0]; + if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject(objs[0])!; Main?.GetMessage(msg); - Main?.UpdateUI(MainControllerSet.Disconnected); + Main?.UpdateUI(MainControllerSet.Disconnect); Close(); } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index f96ccc7..9b81e03 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -25,9 +25,10 @@ namespace Milimoe.FunGame.Desktop.UI /** * 定义全局变量 */ - private int LOCATION_X, LOCATION_Y; // 窗口当前坐标 - private int MAX_CONNECTEDRETRY = 20; // 最大重试连接次数 - private int NOW_CONNECTEDRETRY = -1; // 当前重试连接次数 + public int MaxRetryTimes { get; } = SocketSet.MaxRetryTimes; // 最大重试连接次数 + public int CurrentRetryTimes { get; set; } = -1; // 当前重试连接次数 + + private int loc_x, loc_y; // 窗口当前坐标 /** * 定义全局对象 @@ -105,7 +106,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Green); SetButtonEnableIfLogon(true, ClientState.Online); Others.Config.FunGame_isConnected = true; - NOW_CONNECTEDRETRY = 0; + CurrentRetryTimes = 0; break; case Others.MainControllerSet.SetGreenAndPing: @@ -113,7 +114,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Green, ping: NetworkUtility.GetServerPing(Others.Constant.SERVER_IPADRESS)); SetButtonEnableIfLogon(true, ClientState.Online); Others.Config.FunGame_isConnected = true; - NOW_CONNECTEDRETRY = 0; + CurrentRetryTimes = 0; break; case Others.MainControllerSet.SetYellow: @@ -121,7 +122,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); Others.Config.FunGame_isConnected = true; - NOW_CONNECTEDRETRY = 0; + CurrentRetryTimes = 0; break; case Others.MainControllerSet.WaitConnectAndSetYellow: @@ -129,7 +130,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); Others.Config.FunGame_isConnected = true; - NOW_CONNECTEDRETRY = 0; + CurrentRetryTimes = 0; if (MainController != null && Others.Config.FunGame_isAutoConnect) { // 自动连接服务器 @@ -142,7 +143,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Yellow, true); SetButtonEnableIfLogon(false, ClientState.WaitLogin); Others.Config.FunGame_isConnected = true; - NOW_CONNECTEDRETRY = 0; + CurrentRetryTimes = 0; break; case Others.MainControllerSet.SetRed: @@ -157,7 +158,7 @@ namespace Milimoe.FunGame.Desktop.UI SetServerStatusLight((int)LightType.Red); SetButtonEnableIfLogon(false, ClientState.WaitConnect); LogoutAccount(); - if (Others.Config.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY) + if (Others.Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes) { Task.Run(() => { @@ -190,7 +191,7 @@ namespace Milimoe.FunGame.Desktop.UI LogoutAccount(); if (Others.Config.FunGame_isAutoConnect) { - NOW_CONNECTEDRETRY = -1; + CurrentRetryTimes = -1; Task.Run(() => { Thread.Sleep(1000); @@ -824,8 +825,8 @@ namespace Milimoe.FunGame.Desktop.UI if (e.Button == MouseButtons.Left) { //获取鼠标左键按下时的位置 - LOCATION_X = e.Location.X; - LOCATION_Y = e.Location.Y; + loc_x = e.Location.X; + loc_y = e.Location.Y; } } @@ -839,8 +840,8 @@ namespace Milimoe.FunGame.Desktop.UI if (e.Button == MouseButtons.Left) { //计算鼠标移动距离 - Left += e.Location.X - LOCATION_X; - Top += e.Location.Y - LOCATION_Y; + Left += e.Location.X - loc_x; + Top += e.Location.Y - loc_y; } } @@ -1255,7 +1256,7 @@ namespace Milimoe.FunGame.Desktop.UI case Others.Constant.FunGame_Retry: if (!Others.Config.FunGame_isRetrying) { - NOW_CONNECTEDRETRY = -1; + CurrentRetryTimes = -1; MainController?.Do(MainControllerSet.Connect); } else @@ -1264,7 +1265,7 @@ namespace Milimoe.FunGame.Desktop.UI case Others.Constant.FunGame_Connect: if (!Others.Config.FunGame_isConnected) { - NOW_CONNECTEDRETRY = -1; + CurrentRetryTimes = -1; MainController?.Do(MainControllerSet.GetServerConnection); } break; @@ -1277,9 +1278,7 @@ namespace Milimoe.FunGame.Desktop.UI case Others.Constant.FunGame_DisconnectWhenNotLogin: if (Others.Config.FunGame_isConnected && MainController != null) { - MainController?.Do(MainControllerSet.Close); - UpdateUI(MainControllerSet.Disconnect); - WritelnGameInfo(DateTimeUtility.GetNowShortTime() + " >> 你已成功断开与服务器的连接。 "); + MainController?.Do(MainControllerSet.Disconnect); } break; case Others.Constant.FunGame_ConnectTo: @@ -1308,7 +1307,7 @@ namespace Milimoe.FunGame.Desktop.UI { Others.Constant.SERVER_IPADRESS = ip; Others.Constant.SERVER_PORT = port; - NOW_CONNECTEDRETRY = -1; + CurrentRetryTimes = -1; MainController?.Do(MainControllerSet.Connect); } else if (ErrorType == Core.Library.Constant.ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址!");