From 52a33c0ddb0589f94f5252f65a770fb6ae49970b Mon Sep 17 00:00:00 2001 From: Mili Date: Sun, 18 Sep 2022 17:20:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core.Api/Interface/Interface.cs | 3 +- FunGame.Core.Api/Model/Enum/CommonEnums.cs | 4 +- FunGame.Core.Api/Util/AssemblyHelper.cs | 89 --------- FunGame.Core.Api/Util/INIHelper.cs | 112 ----------- FunGame.Core.Api/Util/Utility.cs | 177 ++++++++++++++++++ FunGame.Desktop/Models/Config/Config.cs | 32 ++-- FunGame.Desktop/UI/Main/Main.cs | 158 ++++++++-------- .../Utils/{WebHelper.cs => SocketHelper.cs} | 64 +++---- 8 files changed, 306 insertions(+), 333 deletions(-) delete mode 100644 FunGame.Core.Api/Util/AssemblyHelper.cs delete mode 100644 FunGame.Core.Api/Util/INIHelper.cs rename FunGame.Desktop/Utils/{WebHelper.cs => SocketHelper.cs} (86%) diff --git a/FunGame.Core.Api/Interface/Interface.cs b/FunGame.Core.Api/Interface/Interface.cs index 75d68c1..573eec7 100644 --- a/FunGame.Core.Api/Interface/Interface.cs +++ b/FunGame.Core.Api/Interface/Interface.cs @@ -34,7 +34,6 @@ namespace FunGame.Core.Api.Interface public interface ServerInterface { - public string DBConnection(); - public Hashtable GetServerSettings(); + } } diff --git a/FunGame.Core.Api/Model/Enum/CommonEnums.cs b/FunGame.Core.Api/Model/Enum/CommonEnums.cs index b5b3e19..3216373 100644 --- a/FunGame.Core.Api/Model/Enum/CommonEnums.cs +++ b/FunGame.Core.Api/Model/Enum/CommonEnums.cs @@ -123,11 +123,11 @@ namespace FunGame.Core.Api.Model.Enum #region Method - public enum WebHelperMethod + public enum SocketHelperMethod { CreateSocket, CloseSocket, - StartWebHelper, + StartSocketHelper, Login, Logout, Disconnect diff --git a/FunGame.Core.Api/Util/AssemblyHelper.cs b/FunGame.Core.Api/Util/AssemblyHelper.cs deleted file mode 100644 index efa8b05..0000000 --- a/FunGame.Core.Api/Util/AssemblyHelper.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Reflection; -using System.Diagnostics; -using FunGame.Core.Api.Model.Enum; -using System.Data.Common; - -namespace FunGame.Core.Api.Util -{ - /// - /// 在FunGame.Core.Api中添加新接口和新实现时,需要: - /// 在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType、InterfaceMethod - /// - public class AssemblyHelper - { - /** - * 定义需要反射的DLL - */ - public const string FUNGAME_CORE = "FunGame.Core"; - - /** - * 无需二次修改的 - */ - public static string EXEDocPath = System.Environment.CurrentDirectory.ToString() + "\\"; // 程序目录 - public static string PluginDocPath = System.Environment.CurrentDirectory.ToString() + "\\plugins\\"; // 插件目录 - - //////////////////////////////////////////////////////////////////// - /////////////// * 下 面 是 工 具 类 实 现 * //////////////// - /////////////////////////////////////////////////////////////////// - - /** - * 定义反射变量 - */ - private Assembly? Assembly; - private Type? Type; - private MethodInfo? Method; - private object? Instance; - - /// - /// 获取FunGame.Core.dll中接口的实现方法 - /// - /// 接口代号 - /// - private Type? GetFunGameCoreImplement(int Interface) - { - // 通过类名获取获取命名空间+类名称 - string ClassName = EnumHelper.GetImplementClassName(Interface); - List? Classes = null; - if (Assembly != null) - { - Classes = Assembly.GetTypes().Where(w => - w.Namespace == "FunGame.Core.Implement" && - w.Name.Contains(ClassName) - ).ToList(); - if (Classes != null && Classes.Count > 0) - return Classes[0]; - else return null; - } - else return null; - } - - /// - /// 公开方法:获取FUNGAME.CORE.DLL中指定方法的返回值 - /// - /// 接口代号 - /// 方法代号 - /// - public object? GetFunGameCoreValue(int Interface, int Method) - { - Assembly = Assembly.LoadFile(EXEDocPath + @FUNGAME_CORE + ".dll"); - Type = GetFunGameCoreImplement(Interface); // 通过类名获取获取命名空间+类名称 - string MethodName = EnumHelper.GetImplementMethodName(Method); // 获取方法名 - if (Assembly != null && Type != null) this.Method = Type.GetMethod(MethodName); // 从Type中查找方法名 - else return null; - Instance = Assembly.CreateInstance(Type.Namespace + "." + Type.Name); - if (Instance != null && this.Method != null) - { - object? value = this.Method.Invoke(Instance, Array.Empty()); // 实例方法的调用 - if (value != null) - return value; - else return null; - } - else return null; - } - } -} diff --git a/FunGame.Core.Api/Util/INIHelper.cs b/FunGame.Core.Api/Util/INIHelper.cs deleted file mode 100644 index ff334c9..0000000 --- a/FunGame.Core.Api/Util/INIHelper.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace FunGame.Core.Api.Util -{ - public class INIHelper - { - /* - * 声明API函数 - */ - public string INIPath = System.Environment.CurrentDirectory.ToString() + @"\FunGame.ini"; - [DllImport("kernel32", CharSet = CharSet.Unicode)] - private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); - [DllImport("kernel32", CharSet = CharSet.Unicode)] - private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); - - /// - /// 构造函数 - /// - /// ini文件路径 - public INIHelper(string INIPath = "") - { - if (INIPath != "") - this.INIPath = INIPath; - } - - /// - /// 写入ini文件 - /// - /// Section - /// 键 - /// 值 - public void WriteINI(string Section, string Key, string Value) - { - WritePrivateProfileString(Section, Key, Value, INIPath); - } - - /// - /// 读取ini文件 - /// - /// Section - /// 键 - /// 返回的值 - public string ReadINI(string Section, string Key) - { - StringBuilder temp = new StringBuilder(256); - int i = GetPrivateProfileString(Section, Key, "", temp, 256, INIPath); - return temp.ToString(); - } - - /// - /// 查询ini文件是否存在 - /// - /// 是否存在 - public bool ExistINIFile() - { - return File.Exists(INIPath); - } - - /// - /// 初始化服务器ini模板文件 - /// - public void InitServerConfigs() - { - /** - * Server - */ - WriteINI("Server", "Name", "FunGame Server"); - WriteINI("Server", "Password", ""); - WriteINI("Server", "Describe", "Just Another FunGame Server."); - WriteINI("Server", "Notice", "This is the FunGame Server's Notice."); - WriteINI("Server", "Key", ""); - WriteINI("Server", "Status", "1"); - /** - * Socket - */ - WriteINI("Socket", "Port", "22222"); - WriteINI("Socket", "MaxPlayer", "20"); - WriteINI("Socket", "MaxConnectFailed", "0"); - /** - * MySQL - */ - WriteINI("MySQL", "DBServer", "localhost"); - WriteINI("MySQL", "DBPort", "3306"); - WriteINI("MySQL", "DBName", "fungame"); - WriteINI("MySQL", "DBUser", "root"); - WriteINI("MySQL", "DBPassword", "pass"); - } - - /// - /// 初始化客户端ini模板文件 - /// - public void InitClientConfigs() - { - /** - * Config - */ - WriteINI("Config", "AutoConnect", "true"); - WriteINI("Config", "AutoLogin", "false"); - /** - * Account - */ - WriteINI("Account", "UserName", ""); - WriteINI("Account", "Password", ""); - WriteINI("Account", "AutoKey", ""); - } - } -} diff --git a/FunGame.Core.Api/Util/Utility.cs b/FunGame.Core.Api/Util/Utility.cs index bb63d9d..b665b49 100644 --- a/FunGame.Core.Api/Util/Utility.cs +++ b/FunGame.Core.Api/Util/Utility.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; +using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -76,4 +78,179 @@ namespace FunGame.Core.Api.Util else return ErrorType.WrongFormat; } } + + public class INIHelper + { + /* + * 声明API函数 + */ + [DllImport("kernel32", CharSet = CharSet.Unicode)] + private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); + [DllImport("kernel32", CharSet = CharSet.Unicode)] + private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); + + /// + /// 写入ini文件 + /// + /// Section + /// 键 + /// 值 + /// 文件名,缺省为FunGame.ini + public static void WriteINI(string Section, string Key, string Value, string FileName = @"FunGame.ini") + { + WritePrivateProfileString(Section, Key, Value, System.Environment.CurrentDirectory.ToString() + @"\" + FileName); + } + + /// + /// 读取ini文件 + /// + /// Section + /// 键 + /// 文件名,缺省为FunGame.ini + /// 读取到的值 + public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini") + { + StringBuilder str = new(256); + _ = GetPrivateProfileString(Section, Key, "", str, 256, System.Environment.CurrentDirectory.ToString() + @"\" + FileName); + return str.ToString(); + } + + /// + /// 查询ini文件是否存在 + /// + /// 文件名,缺省为FunGame.ini + /// 是否存在 + public static bool ExistINIFile(string FileName = @"FunGame.ini") + { + return File.Exists(System.Environment.CurrentDirectory.ToString() + @"\" + FileName); + } + + /// + /// 初始化ini模板文件 + /// + public static void Init(FunGameEnums.FunGame FunGameType) + { + switch(FunGameType) + { + case FunGameEnums.FunGame.FunGame_Core: + case FunGameEnums.FunGame.FunGame_Core_Api: + case FunGameEnums.FunGame.FunGame_Console: + case FunGameEnums.FunGame.FunGame_Desktop: + /** + * Config + */ + WriteINI("Config", "AutoConnect", "true"); + WriteINI("Config", "AutoLogin", "false"); + /** + * Account + */ + WriteINI("Account", "UserName", ""); + WriteINI("Account", "Password", ""); + WriteINI("Account", "AutoKey", ""); + break; + case FunGameEnums.FunGame.FunGame_Server: + /** + * Server + */ + WriteINI("Server", "Name", "FunGame Server"); + WriteINI("Server", "Password", ""); + WriteINI("Server", "Describe", "Just Another FunGame Server."); + WriteINI("Server", "Notice", "This is the FunGame Server's Notice."); + WriteINI("Server", "Key", ""); + WriteINI("Server", "Status", "1"); + /** + * Socket + */ + WriteINI("Socket", "Port", "22222"); + WriteINI("Socket", "MaxPlayer", "20"); + WriteINI("Socket", "MaxConnectFailed", "0"); + /** + * MySQL + */ + WriteINI("MySQL", "DBServer", "localhost"); + WriteINI("MySQL", "DBPort", "3306"); + WriteINI("MySQL", "DBName", "fungame"); + WriteINI("MySQL", "DBUser", "root"); + WriteINI("MySQL", "DBPassword", "pass"); + break; + } + } + } + + /// + /// 在FunGame.Core.Api中添加新接口和新实现时,需要: + /// 在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType、InterfaceMethod + /// + public class ReflectionHelper + { + /** + * 定义需要反射的DLL + */ + public const string FUNGAME_CORE = "FunGame.Core"; + + /** + * 无需二次修改的 + */ + public static string EXEDocPath = System.Environment.CurrentDirectory.ToString() + "\\"; // 程序目录 + public static string PluginDocPath = System.Environment.CurrentDirectory.ToString() + "\\plugins\\"; // 插件目录 + + //////////////////////////////////////////////////////////////////// + /////////////// * 下 面 是 工 具 类 实 现 * //////////////// + /////////////////////////////////////////////////////////////////// + + /** + * 定义反射变量 + */ + private Assembly? Assembly; + private Type? Type; + private MethodInfo? Method; + private object? Instance; + + /// + /// 获取FunGame.Core.dll中接口的实现方法 + /// + /// 接口代号 + /// + private Type? GetFunGameCoreImplement(int Interface) + { + // 通过类名获取获取命名空间+类名称 + string ClassName = EnumHelper.GetImplementClassName(Interface); + List? Classes = null; + if (Assembly != null) + { + Classes = Assembly.GetTypes().Where(w => + w.Namespace == "FunGame.Core.Implement" && + w.Name.Contains(ClassName) + ).ToList(); + if (Classes != null && Classes.Count > 0) + return Classes[0]; + else return null; + } + else return null; + } + + /// + /// 公开方法:获取FUNGAME.CORE.DLL中指定方法的返回值 + /// + /// 接口代号 + /// 方法代号 + /// + public object? GetFunGameCoreValue(int Interface, int Method) + { + Assembly = Assembly.LoadFile(EXEDocPath + @FUNGAME_CORE + ".dll"); + Type = GetFunGameCoreImplement(Interface); // 通过类名获取获取命名空间+类名称 + string MethodName = EnumHelper.GetImplementMethodName(Method); // 获取方法名 + if (Assembly != null && Type != null) this.Method = Type.GetMethod(MethodName); // 从Type中查找方法名 + else return null; + Instance = Assembly.CreateInstance(Type.Namespace + "." + Type.Name); + if (Instance != null && this.Method != null) + { + object? value = this.Method.Invoke(Instance, Array.Empty()); // 实例方法的调用 + if (value != null) + return value; + else return null; + } + else return null; + } + } } diff --git a/FunGame.Desktop/Models/Config/Config.cs b/FunGame.Desktop/Models/Config/Config.cs index 16a2e7f..1d90d48 100644 --- a/FunGame.Desktop/Models/Config/Config.cs +++ b/FunGame.Desktop/Models/Config/Config.cs @@ -15,9 +15,7 @@ namespace FunGame.Desktop.Models.Config * Game Configs */ public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Desktop; - - public static INIHelper INIHelper = new(); - public static AssemblyHelper AssemblyHelper = new(); + public static ReflectionHelper ReflectionHelper = new(); /** * FunGame Desktop Configs @@ -35,21 +33,21 @@ namespace FunGame.Desktop.Models.Config public static string FunGame_Notice = ""; // 公告 /** - * WebHelper Configs + * SocketHelper Configs */ - public const string WebHelper_SetGreen = "-WebHelper .set green"; - public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping"; - public const string WebHelper_SetRed = "-WebHelper .set red"; - public const string WebHelper_SetYellow = "-WebHelper .set yellow"; - public const string WebHelper_WaitConnectAndSetYellow = "-WebHelper .waitconnect .set yellow"; - public const string WebHelper_WaitLoginAndSetYellow = "-WebHelper .waitlogin .set yellow"; - public const string WebHelper_Disconnect = "-WebHelper .disconnect"; - public const string WebHelper_Disconnected = "-WebHelper .disconnected"; - public const string WebHelper_LogOut = "-WebHelper .logout"; - public const string WebHelper_GetUser = "-WebHelper .get user"; - public const string WebHelper_SetUser = "-WebHelper .set user"; - public const string WebHelper_SetNotice = "-WebHelper .set notice"; - public static int WebHelper_HeartBeatFaileds = 0; + public const string SocketHelper_SetGreen = "-SocketHelper .set green"; + public const string SocketHelper_SetGreenAndPing = "-SocketHelper .set greenandping"; + public const string SocketHelper_SetRed = "-SocketHelper .set red"; + public const string SocketHelper_SetYellow = "-SocketHelper .set yellow"; + public const string SocketHelper_WaitConnectAndSetYellow = "-SocketHelper .waitconnect .set yellow"; + public const string SocketHelper_WaitLoginAndSetYellow = "-SocketHelper .waitlogin .set yellow"; + public const string SocketHelper_Disconnect = "-SocketHelper .disconnect"; + public const string SocketHelper_Disconnected = "-SocketHelper .disconnected"; + public const string SocketHelper_LogOut = "-SocketHelper .logout"; + public const string SocketHelper_GetUser = "-SocketHelper .get user"; + public const string SocketHelper_SetUser = "-SocketHelper .set user"; + public const string SocketHelper_SetNotice = "-SocketHelper .set notice"; + public static int SocketHelper_HeartBeatFaileds = 0; /** * Socket Configs diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index 67a0bef..c771801 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -29,7 +29,7 @@ namespace FunGame.Desktop.UI * 定义全局对象 */ private Task? MatchFunGame = null; // 匹配线程 - private WebHelper? WebHelper = null; // WebHelper + private SocketHelper? SocketHelper = null; // ScoketHelper /** * 定义委托 @@ -37,7 +37,7 @@ namespace FunGame.Desktop.UI */ Action? StartMatch_Action = null; Action? CreateRoom_Action = null; - Action? WebHelper_Action = null; + Action? SocketHelper_Action = null; Action? Main_Action = null; public Main() @@ -63,13 +63,13 @@ namespace FunGame.Desktop.UI #region 公有方法 /// - /// 提供公共方法给WebHelper + /// 提供公共方法给SocketHelper /// - /// + /// /// /// /// - public object? GetMessage(WebHelper webHelper, string? msg, bool needTime = false, object[]? objs = null) + public object? GetMessage(SocketHelper SocketHelper, string? msg, bool needTime = false, object[]? objs = null) { try { @@ -77,105 +77,105 @@ namespace FunGame.Desktop.UI { switch (msg) { - case Config.WebHelper_SetGreen: + case Config.SocketHelper_SetGreen: Config.FunGame_isRetrying = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Green); SetButtonEnableIfLogon(true, ClientState.Online); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); Config.FunGame_isConnected = true; NOW_CONNECTEDRETRY = 0; break; - case Config.WebHelper_SetGreenAndPing: + case Config.SocketHelper_SetGreenAndPing: Config.FunGame_isRetrying = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Green, ping: GetServerPing(Config.SERVER_IPADRESS)); SetButtonEnableIfLogon(true, ClientState.Online); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); Config.FunGame_isConnected = true; NOW_CONNECTEDRETRY = 0; break; - case Config.WebHelper_SetYellow: + case Config.SocketHelper_SetYellow: Config.FunGame_isRetrying = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); Config.FunGame_isConnected = true; NOW_CONNECTEDRETRY = 0; break; - case Config.WebHelper_WaitConnectAndSetYellow: + case Config.SocketHelper_WaitConnectAndSetYellow: Config.FunGame_isRetrying = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); Config.FunGame_isConnected = true; NOW_CONNECTEDRETRY = 0; - if (WebHelper != null && Config.FunGame_isAutoConnect) + if (SocketHelper != null && Config.FunGame_isAutoConnect) { // 自动连接服务器 GetServerConnection(); } break; - case Config.WebHelper_WaitLoginAndSetYellow: + case Config.SocketHelper_WaitLoginAndSetYellow: Config.FunGame_isRetrying = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Yellow, true); SetButtonEnableIfLogon(false, ClientState.WaitLogin); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); Config.FunGame_isConnected = true; NOW_CONNECTEDRETRY = 0; break; - case Config.WebHelper_SetRed: - WebHelper_Action = (main) => + case Config.SocketHelper_SetRed: + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Red); SetButtonEnableIfLogon(false, ClientState.WaitConnect); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); Config.FunGame_isConnected = false; break; - case Config.WebHelper_Disconnected: + case Config.SocketHelper_Disconnected: Config.FunGame_isRetrying = false; Config.FunGame_isConnected = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Red); SetButtonEnableIfLogon(false, ClientState.WaitConnect); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); if (Config.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY) { Task.Run(() => @@ -193,37 +193,37 @@ namespace FunGame.Desktop.UI throw new Exception(GetNowShortTime() + "\nERROR:无法连接至服务器,请检查你的网络连接。"); else throw new Exception("ERROR:无法连接至服务器,请检查你的网络连接。"); - case Config.WebHelper_Disconnect: + case Config.SocketHelper_Disconnect: Config.FunGame_isAutoRetry = false; Config.FunGame_isRetrying = false; Config.FunGame_isAutoConnect = false; Config.FunGame_isAutoLogin = false; Config.FunGame_isConnected = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); LogoutAccount(); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); break; - case Config.WebHelper_LogOut: + case Config.SocketHelper_LogOut: Config.FunGame_isRetrying = false; Config.FunGame_isConnected = false; Config.FunGame_isAutoLogin = false; - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); LogoutAccount(); }; if (InvokeRequired) - BeginInvoke(WebHelper_Action, this); + BeginInvoke(SocketHelper_Action, this); else - WebHelper_Action(this); + SocketHelper_Action(this); if (Config.FunGame_isAutoConnect) { NOW_CONNECTEDRETRY = -1; @@ -234,11 +234,11 @@ namespace FunGame.Desktop.UI }); } break; - case Config.WebHelper_GetUser: + case Config.SocketHelper_GetUser: if (Usercfg.LoginUser != null) return Usercfg.LoginUser; return null; - case Config.WebHelper_SetUser: + case Config.SocketHelper_SetUser: if (objs != null && objs.Length > 1) { if (InvokeRequired) @@ -247,14 +247,14 @@ namespace FunGame.Desktop.UI SetLoginUser(objs); } return null; - case Config.WebHelper_SetNotice: + case Config.SocketHelper_SetNotice: Action action = () => { NoticeText.Text = Config.FunGame_Notice; - if (WebHelper != null && Config.FunGame_isAutoLogin) + if (SocketHelper != null && Config.FunGame_isAutoLogin) { // 自动登录 - WebHelper.WebHelpMethod((int)WebHelperMethod.Login); + SocketHelper.WebHelpMethod((int)SocketHelperMethod.Login); } }; if (InvokeRequired) @@ -264,9 +264,9 @@ namespace FunGame.Desktop.UI return null; default: if (needTime) - WritelnGameInfo(webHelper, GetNowShortTime() + msg); + WritelnGameInfo(SocketHelper, GetNowShortTime() + msg); else - WritelnGameInfo(webHelper, msg); + WritelnGameInfo(SocketHelper, msg); return null; } } @@ -274,8 +274,8 @@ namespace FunGame.Desktop.UI } catch (Exception e) { - WritelnGameInfo(webHelper, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); - GetMessage(webHelper, Config.WebHelper_SetRed); + WritelnGameInfo(SocketHelper, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); + GetMessage(SocketHelper, Config.SocketHelper_SetRed); } return null; } @@ -291,7 +291,7 @@ namespace FunGame.Desktop.UI { try { - string? ipaddress = (string?)Config.AssemblyHelper.GetFunGameCoreValue((int)InterfaceType.ClientConnectInterface, (int)InterfaceMethod.RemoteServerIP); // 获取服务器IP + string? ipaddress = (string?)Config.ReflectionHelper.GetFunGameCoreValue((int)InterfaceType.ClientConnectInterface, (int)InterfaceMethod.RemoteServerIP); // 获取服务器IP if (ipaddress != null) { string[] s = ipaddress.Split(':'); @@ -346,21 +346,21 @@ namespace FunGame.Desktop.UI WritelnGameInfo("ERROR:无法连接至服务器,请检查网络并重启游戏再试。"); return; } - WebHelper_Action = (main) => + SocketHelper_Action = (main) => { try { if (main != null) { - if (WebHelper != null) + if (SocketHelper != null) { - WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); - WebHelper = null; + SocketHelper.WebHelpMethod((int)SocketHelperMethod.CloseSocket); + SocketHelper = null; } Config.FunGame_isRetrying = true; Application.DoEvents(); - WebHelper = new WebHelper(main); - WebHelper.WebHelpMethod((int)WebHelperMethod.CreateSocket); // Invoke -> CreateSocket + SocketHelper = new SocketHelper(main); + SocketHelper.WebHelpMethod((int)SocketHelperMethod.CreateSocket); // Invoke -> CreateSocket } } catch @@ -372,11 +372,11 @@ namespace FunGame.Desktop.UI { if (InvokeRequired) { - BeginInvoke(WebHelper_Action, main); + BeginInvoke(SocketHelper_Action, main); } else { - WebHelper_Action(main); + SocketHelper_Action(main); } }); } @@ -399,10 +399,10 @@ namespace FunGame.Desktop.UI { try { - if (Config.INIHelper.ExistINIFile()) + if (INIHelper.ExistINIFile()) { - string isAutoConncet = Config.INIHelper.ReadINI("Config", "AutoConnect"); - string isAutoLogin = Config.INIHelper.ReadINI("Config", "AutoLogin"); + string isAutoConncet = INIHelper.ReadINI("Config", "AutoConnect"); + string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin"); if (isAutoConncet != null && !isAutoConncet.Equals("") && (isAutoConncet.Equals("false") || isAutoConncet.Equals("true"))) Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConncet); else throw new Exception("ERROR: 读取配置文件出错,参数格式不正确"); @@ -412,7 +412,7 @@ namespace FunGame.Desktop.UI } else { - Config.INIHelper.InitClientConfigs(); + INIHelper.Init(Config.FunGameType); WritelnGameInfo(">> 首次启动,已自动为你创建配置文件。"); GetFunGameConfig(); } @@ -463,13 +463,13 @@ namespace FunGame.Desktop.UI } /// - /// 由WebHelper委托向消息队列输出一行文字 + /// 由SocketHelper委托向消息队列输出一行文字 /// - /// + /// /// - private void WritelnGameInfo(WebHelper webHelper, string msg) + private void WritelnGameInfo(SocketHelper SocketHelper, string msg) { - if (webHelper != null && msg.Trim() != "") + if (SocketHelper != null && msg.Trim() != "") { Action tempAction = new Action(() => { @@ -934,10 +934,10 @@ namespace FunGame.Desktop.UI { if (ShowMessage.OKCancelMessage("你确定关闭游戏?", "退出") == (int)MessageResult.OK) { - if (WebHelper != null) + if (SocketHelper != null) { - WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); - WebHelper = null; + SocketHelper.WebHelpMethod((int)SocketHelperMethod.CloseSocket); + SocketHelper = null; } Environment.Exit(0); } @@ -1104,7 +1104,7 @@ namespace FunGame.Desktop.UI { if (ShowMessage.OKCancelMessage("你确定要退出登录吗?", "退出登录") == MessageResult.OK) { - if (WebHelper == null || !WebHelper.WebHelpMethod((int)WebHelperMethod.Logout)) + if (SocketHelper == null || !SocketHelper.WebHelpMethod((int)SocketHelperMethod.Logout)) ShowMessage.WarningMessage("请求无效:退出登录失败!"); } } @@ -1116,8 +1116,8 @@ namespace FunGame.Desktop.UI /// private void Login_Click(object sender, EventArgs e) { - if (WebHelper != null && Config.FunGame_isConnected) - WebHelper.WebHelpMethod((int)WebHelperMethod.Login); + if (SocketHelper != null && Config.FunGame_isConnected) + SocketHelper.WebHelpMethod((int)SocketHelperMethod.Login); else ShowMessage.WarningMessage("请先连接服务器!"); } @@ -1399,16 +1399,16 @@ namespace FunGame.Desktop.UI } break; case Config.FunGame_Disconnect: - if (Config.FunGame_isConnected && WebHelper != null) + if (Config.FunGame_isConnected && SocketHelper != null) { - WebHelper.WebHelpMethod((int)WebHelperMethod.Disconnect); + SocketHelper.WebHelpMethod((int)SocketHelperMethod.Disconnect); } break; case Config.FunGame_DisconnectWhenNotLogin: - if (Config.FunGame_isConnected && WebHelper != null) + if (Config.FunGame_isConnected && SocketHelper != null) { - WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); - GetMessage(WebHelper, Config.WebHelper_Disconnect); + SocketHelper.WebHelpMethod((int)SocketHelperMethod.CloseSocket); + GetMessage(SocketHelper, Config.SocketHelper_Disconnect); WritelnGameInfo(GetNowShortTime() + " >> 你已成功断开与服务器的连接。 "); } break; diff --git a/FunGame.Desktop/Utils/WebHelper.cs b/FunGame.Desktop/Utils/SocketHelper.cs similarity index 86% rename from FunGame.Desktop/Utils/WebHelper.cs rename to FunGame.Desktop/Utils/SocketHelper.cs index 50bf2e5..eb0ca79 100644 --- a/FunGame.Desktop/Utils/WebHelper.cs +++ b/FunGame.Desktop/Utils/SocketHelper.cs @@ -15,17 +15,17 @@ using FunGame.Desktop.UI; namespace FunGame.Desktop.Utils { - public class WebHelper + public class SocketHelper { private Socket? client; private EndPoint? server; Main Main; - Action? WebHelper_Action = null; + Action? SocketHelper_Action = null; Task? WaitHeartBeat = null; - public WebHelper(Main main) + public SocketHelper(Main main) { Main = main; } @@ -38,30 +38,30 @@ namespace FunGame.Desktop.Utils { switch (i) { - case (int)WebHelperMethod.CreateSocket: + case (int)SocketHelperMethod.CreateSocket: CreateSocket(); break; - case (int)WebHelperMethod.CloseSocket: + case (int)SocketHelperMethod.CloseSocket: Close(); break; - case (int)WebHelperMethod.StartWebHelper: - StartWebHelper(); + case (int)SocketHelperMethod.StartSocketHelper: + StartSocketHelper(); break; - case (int)WebHelperMethod.Login: + case (int)SocketHelperMethod.Login: if (client != null) { Send((int)SocketMessageType.CheckLogin, new object[] { Main, client, new User("Mili") }); return true; } return false; - case (int)WebHelperMethod.Logout: + case (int)SocketHelperMethod.Logout: if (client != null && Usercfg.LoginUser != null) { Send((int)SocketMessageType.Logout, new object[] { Main, client, Usercfg.LoginUser }); return true; } return false; - case (int)WebHelperMethod.Disconnect: + case (int)SocketHelperMethod.Disconnect: if (client != null) { Send((int)SocketMessageType.Disconnect, new object[] { Main, client }); @@ -88,14 +88,14 @@ namespace FunGame.Desktop.Utils client.Connect(server); if (IsConnected()) { - Main.GetMessage(this, Config.WebHelper_WaitLoginAndSetYellow); + Main.GetMessage(this, Config.SocketHelper_WaitLoginAndSetYellow); break; } } } - WebHelper_Action = (main, socket) => + SocketHelper_Action = (main, socket) => { - object? obj = main.GetMessage(this, Config.WebHelper_GetUser); + object? obj = main.GetMessage(this, Config.SocketHelper_GetUser); object[] objs; if (obj != null) objs = new object[] { main, socket, obj }; @@ -104,24 +104,24 @@ namespace FunGame.Desktop.Utils if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告 { main.GetMessage(this, " >> 连接服务器成功,请登录账号以体验FunGame。", true); - main.GetMessage(this, Config.WebHelper_SetNotice); + main.GetMessage(this, Config.SocketHelper_SetNotice); } }; Task t = Task.Factory.StartNew(() => { if (Main.InvokeRequired) { - Main.Invoke(WebHelper_Action, Main, client); + Main.Invoke(SocketHelper_Action, Main, client); } else { - WebHelper_Action(Main, client); + SocketHelper_Action(Main, client); } }); } catch (Exception e) { - Main.GetMessage(this, Config.WebHelper_Disconnected); + Main.GetMessage(this, Config.SocketHelper_Disconnected); Main.GetMessage(this, e.StackTrace); Close(); } @@ -175,25 +175,25 @@ namespace FunGame.Desktop.Utils case (int)SocketMessageType.Login: break; case (int)SocketMessageType.CheckLogin: - Main.GetMessage(this, Config.WebHelper_SetUser, false, objs); + Main.GetMessage(this, Config.SocketHelper_SetUser, false, objs); Main.GetMessage(this, read, true); - StartWebHelper(); // 开始创建TCP流 + StartSocketHelper(); // 开始创建TCP流 return true; case (int)SocketMessageType.Logout: - Main.GetMessage(this, Config.WebHelper_SetUser, false, objs); + Main.GetMessage(this, Config.SocketHelper_SetUser, false, objs); Main.GetMessage(this, read, true); - Main.GetMessage(this, Config.WebHelper_LogOut); + Main.GetMessage(this, Config.SocketHelper_LogOut); Close(); return true; case (int)SocketMessageType.Disconnect: Main.GetMessage(this, read, true); - Main.GetMessage(this, Config.WebHelper_Disconnect); + Main.GetMessage(this, Config.SocketHelper_Disconnect); Close(); return true; case (int)SocketMessageType.HeartBeat: if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1); - Config.WebHelper_HeartBeatFaileds = 0; - main.GetMessage(this, Config.WebHelper_SetGreenAndPing); + Config.SocketHelper_HeartBeatFaileds = 0; + main.GetMessage(this, Config.SocketHelper_SetGreenAndPing); return true; } main.GetMessage(this, read); @@ -204,13 +204,13 @@ namespace FunGame.Desktop.Utils } else { - main.GetMessage(this, Config.WebHelper_Disconnected); + main.GetMessage(this, Config.SocketHelper_Disconnected); throw new Exception("ERROR:服务器连接失败。"); } } catch (Exception e) { - main.GetMessage(this, Config.WebHelper_Disconnected); + main.GetMessage(this, Config.SocketHelper_Disconnected); main.GetMessage(this, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); Close(); } @@ -302,7 +302,7 @@ namespace FunGame.Desktop.Utils } else { - main.GetMessage(this, Config.WebHelper_Disconnected); + main.GetMessage(this, Config.SocketHelper_Disconnected); throw new Exception("ERROR:服务器连接失败。"); } } @@ -323,9 +323,9 @@ namespace FunGame.Desktop.Utils private void CatchException(Main main, Exception e, bool isDisconnected) { if (isDisconnected) - main.GetMessage(this, Config.WebHelper_Disconnected); + main.GetMessage(this, Config.SocketHelper_Disconnected); else - main.GetMessage(this, Config.WebHelper_SetRed); + main.GetMessage(this, Config.SocketHelper_SetRed); main.GetMessage(this, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); Close(); } @@ -335,8 +335,8 @@ namespace FunGame.Desktop.Utils // 超过三次没回应心跳,服务器连接失败。 try { - Config.WebHelper_HeartBeatFaileds++; - if (Config.WebHelper_HeartBeatFaileds >= 3) + Config.SocketHelper_HeartBeatFaileds++; + if (Config.SocketHelper_HeartBeatFaileds >= 3) throw new Exception("ERROR:服务器连接失败。"); } catch (Exception e) @@ -378,7 +378,7 @@ namespace FunGame.Desktop.Utils } } - private void StartWebHelper() + private void StartSocketHelper() { Task HeartBeatStream = Task.Factory.StartNew(() => {