结构调整

This commit is contained in:
Mili 2022-09-18 17:20:19 +08:00
parent f2e55ad20d
commit 52a33c0ddb
8 changed files with 306 additions and 333 deletions

View File

@ -34,7 +34,6 @@ namespace FunGame.Core.Api.Interface
public interface ServerInterface public interface ServerInterface
{ {
public string DBConnection();
public Hashtable GetServerSettings();
} }
} }

View File

@ -123,11 +123,11 @@ namespace FunGame.Core.Api.Model.Enum
#region Method #region Method
public enum WebHelperMethod public enum SocketHelperMethod
{ {
CreateSocket, CreateSocket,
CloseSocket, CloseSocket,
StartWebHelper, StartSocketHelper,
Login, Login,
Logout, Logout,
Disconnect Disconnect

View File

@ -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
{
/// <summary>
/// 在FunGame.Core.Api中添加新接口和新实现时需要
/// 在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType、InterfaceMethod
/// </summary>
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;
/// <summary>
/// 获取FunGame.Core.dll中接口的实现方法
/// </summary>
/// <param name="Interface">接口代号</param>
/// <returns></returns>
private Type? GetFunGameCoreImplement(int Interface)
{
// 通过类名获取获取命名空间+类名称
string ClassName = EnumHelper.GetImplementClassName(Interface);
List<Type>? 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;
}
/// <summary>
/// 公开方法获取FUNGAME.CORE.DLL中指定方法的返回值
/// </summary>
/// <param name="Interface">接口代号</param>
/// <param name="Method">方法代号</param>
/// <returns></returns>
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<object>()); // 实例方法的调用
if (value != null)
return value;
else return null;
}
else return null;
}
}
}

View File

@ -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);
/// <summary>
/// 构造函数
/// </summary>
/// <param name="INIPath">ini文件路径</param>
public INIHelper(string INIPath = "")
{
if (INIPath != "")
this.INIPath = INIPath;
}
/// <summary>
/// 写入ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void WriteINI(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, INIPath);
}
/// <summary>
/// 读取ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <returns>返回的值</returns>
public string ReadINI(string Section, string Key)
{
StringBuilder temp = new StringBuilder(256);
int i = GetPrivateProfileString(Section, Key, "", temp, 256, INIPath);
return temp.ToString();
}
/// <summary>
/// 查询ini文件是否存在
/// </summary>
/// <returns>是否存在</returns>
public bool ExistINIFile()
{
return File.Exists(INIPath);
}
/// <summary>
/// 初始化服务器ini模板文件
/// </summary>
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");
}
/// <summary>
/// 初始化客户端ini模板文件
/// </summary>
public void InitClientConfigs()
{
/**
* Config
*/
WriteINI("Config", "AutoConnect", "true");
WriteINI("Config", "AutoLogin", "false");
/**
* Account
*/
WriteINI("Account", "UserName", "");
WriteINI("Account", "Password", "");
WriteINI("Account", "AutoKey", "");
}
}
}

View File

@ -2,6 +2,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -76,4 +78,179 @@ namespace FunGame.Core.Api.Util
else return ErrorType.WrongFormat; 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);
/// <summary>
/// 写入ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
/// <param name="FileName">文件名缺省为FunGame.ini</param>
public static void WriteINI(string Section, string Key, string Value, string FileName = @"FunGame.ini")
{
WritePrivateProfileString(Section, Key, Value, System.Environment.CurrentDirectory.ToString() + @"\" + FileName);
}
/// <summary>
/// 读取ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <param name="FileName">文件名缺省为FunGame.ini</param>
/// <returns>读取到的值</returns>
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();
}
/// <summary>
/// 查询ini文件是否存在
/// </summary>
/// <param name="FileName">文件名缺省为FunGame.ini</param>
/// <returns>是否存在</returns>
public static bool ExistINIFile(string FileName = @"FunGame.ini")
{
return File.Exists(System.Environment.CurrentDirectory.ToString() + @"\" + FileName);
}
/// <summary>
/// 初始化ini模板文件
/// </summary>
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;
}
}
}
/// <summary>
/// 在FunGame.Core.Api中添加新接口和新实现时需要
/// 在FunGame.Core.Api.Model.Enum.CommonEnums里同步添加InterfaceType、InterfaceMethod
/// </summary>
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;
/// <summary>
/// 获取FunGame.Core.dll中接口的实现方法
/// </summary>
/// <param name="Interface">接口代号</param>
/// <returns></returns>
private Type? GetFunGameCoreImplement(int Interface)
{
// 通过类名获取获取命名空间+类名称
string ClassName = EnumHelper.GetImplementClassName(Interface);
List<Type>? 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;
}
/// <summary>
/// 公开方法获取FUNGAME.CORE.DLL中指定方法的返回值
/// </summary>
/// <param name="Interface">接口代号</param>
/// <param name="Method">方法代号</param>
/// <returns></returns>
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<object>()); // 实例方法的调用
if (value != null)
return value;
else return null;
}
else return null;
}
}
} }

View File

@ -15,9 +15,7 @@ namespace FunGame.Desktop.Models.Config
* Game Configs * Game Configs
*/ */
public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Desktop; public static FunGameEnums.FunGame FunGameType = FunGameEnums.FunGame.FunGame_Desktop;
public static ReflectionHelper ReflectionHelper = new();
public static INIHelper INIHelper = new();
public static AssemblyHelper AssemblyHelper = new();
/** /**
* FunGame Desktop Configs * FunGame Desktop Configs
@ -35,21 +33,21 @@ namespace FunGame.Desktop.Models.Config
public static string FunGame_Notice = ""; // 公告 public static string FunGame_Notice = ""; // 公告
/** /**
* WebHelper Configs * SocketHelper Configs
*/ */
public const string WebHelper_SetGreen = "-WebHelper .set green"; public const string SocketHelper_SetGreen = "-SocketHelper .set green";
public const string WebHelper_SetGreenAndPing = "-WebHelper .set greenandping"; public const string SocketHelper_SetGreenAndPing = "-SocketHelper .set greenandping";
public const string WebHelper_SetRed = "-WebHelper .set red"; public const string SocketHelper_SetRed = "-SocketHelper .set red";
public const string WebHelper_SetYellow = "-WebHelper .set yellow"; public const string SocketHelper_SetYellow = "-SocketHelper .set yellow";
public const string WebHelper_WaitConnectAndSetYellow = "-WebHelper .waitconnect .set yellow"; public const string SocketHelper_WaitConnectAndSetYellow = "-SocketHelper .waitconnect .set yellow";
public const string WebHelper_WaitLoginAndSetYellow = "-WebHelper .waitlogin .set yellow"; public const string SocketHelper_WaitLoginAndSetYellow = "-SocketHelper .waitlogin .set yellow";
public const string WebHelper_Disconnect = "-WebHelper .disconnect"; public const string SocketHelper_Disconnect = "-SocketHelper .disconnect";
public const string WebHelper_Disconnected = "-WebHelper .disconnected"; public const string SocketHelper_Disconnected = "-SocketHelper .disconnected";
public const string WebHelper_LogOut = "-WebHelper .logout"; public const string SocketHelper_LogOut = "-SocketHelper .logout";
public const string WebHelper_GetUser = "-WebHelper .get user"; public const string SocketHelper_GetUser = "-SocketHelper .get user";
public const string WebHelper_SetUser = "-WebHelper .set user"; public const string SocketHelper_SetUser = "-SocketHelper .set user";
public const string WebHelper_SetNotice = "-WebHelper .set notice"; public const string SocketHelper_SetNotice = "-SocketHelper .set notice";
public static int WebHelper_HeartBeatFaileds = 0; public static int SocketHelper_HeartBeatFaileds = 0;
/** /**
* Socket Configs * Socket Configs

View File

@ -29,7 +29,7 @@ namespace FunGame.Desktop.UI
* *
*/ */
private Task? MatchFunGame = null; // 匹配线程 private Task? MatchFunGame = null; // 匹配线程
private WebHelper? WebHelper = null; // WebHelper private SocketHelper? SocketHelper = null; // ScoketHelper
/** /**
* *
@ -37,7 +37,7 @@ namespace FunGame.Desktop.UI
*/ */
Action<int, object[]?>? StartMatch_Action = null; Action<int, object[]?>? StartMatch_Action = null;
Action<int, object[]?>? CreateRoom_Action = null; Action<int, object[]?>? CreateRoom_Action = null;
Action<Main?>? WebHelper_Action = null; Action<Main?>? SocketHelper_Action = null;
Action<Main?>? Main_Action = null; Action<Main?>? Main_Action = null;
public Main() public Main()
@ -63,13 +63,13 @@ namespace FunGame.Desktop.UI
#region #region
/// <summary> /// <summary>
/// 提供公共方法给WebHelper /// 提供公共方法给SocketHelper
/// </summary> /// </summary>
/// <param name="webHelper"></param> /// <param name="SocketHelper"></param>
/// <param name="msg"></param> /// <param name="msg"></param>
/// <param name="needTime"></param> /// <param name="needTime"></param>
/// <returns></returns> /// <returns></returns>
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 try
{ {
@ -77,105 +77,105 @@ namespace FunGame.Desktop.UI
{ {
switch (msg) switch (msg)
{ {
case Config.WebHelper_SetGreen: case Config.SocketHelper_SetGreen:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Green); SetServerStatusLight((int)LightType.Green);
SetButtonEnableIfLogon(true, ClientState.Online); SetButtonEnableIfLogon(true, ClientState.Online);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
Config.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_SetGreenAndPing: case Config.SocketHelper_SetGreenAndPing:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Green, ping: GetServerPing(Config.SERVER_IPADRESS)); SetServerStatusLight((int)LightType.Green, ping: GetServerPing(Config.SERVER_IPADRESS));
SetButtonEnableIfLogon(true, ClientState.Online); SetButtonEnableIfLogon(true, ClientState.Online);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
Config.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_SetYellow: case Config.SocketHelper_SetYellow:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Yellow); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
Config.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_WaitConnectAndSetYellow: case Config.SocketHelper_WaitConnectAndSetYellow:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Yellow); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
Config.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
if (WebHelper != null && Config.FunGame_isAutoConnect) if (SocketHelper != null && Config.FunGame_isAutoConnect)
{ {
// 自动连接服务器 // 自动连接服务器
GetServerConnection(); GetServerConnection();
} }
break; break;
case Config.WebHelper_WaitLoginAndSetYellow: case Config.SocketHelper_WaitLoginAndSetYellow:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Yellow, true); SetServerStatusLight((int)LightType.Yellow, true);
SetButtonEnableIfLogon(false, ClientState.WaitLogin); SetButtonEnableIfLogon(false, ClientState.WaitLogin);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
Config.FunGame_isConnected = true; Config.FunGame_isConnected = true;
NOW_CONNECTEDRETRY = 0; NOW_CONNECTEDRETRY = 0;
break; break;
case Config.WebHelper_SetRed: case Config.SocketHelper_SetRed:
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
Config.FunGame_isConnected = false; Config.FunGame_isConnected = false;
break; break;
case Config.WebHelper_Disconnected: case Config.SocketHelper_Disconnected:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Config.FunGame_isConnected = false; Config.FunGame_isConnected = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Red);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
if (Config.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY) if (Config.FunGame_isAutoRetry && NOW_CONNECTEDRETRY <= MAX_CONNECTEDRETRY)
{ {
Task.Run(() => Task.Run(() =>
@ -193,37 +193,37 @@ namespace FunGame.Desktop.UI
throw new Exception(GetNowShortTime() + "\nERROR无法连接至服务器请检查你的网络连接。"); throw new Exception(GetNowShortTime() + "\nERROR无法连接至服务器请检查你的网络连接。");
else else
throw new Exception("ERROR无法连接至服务器请检查你的网络连接。"); throw new Exception("ERROR无法连接至服务器请检查你的网络连接。");
case Config.WebHelper_Disconnect: case Config.SocketHelper_Disconnect:
Config.FunGame_isAutoRetry = false; Config.FunGame_isAutoRetry = false;
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Config.FunGame_isAutoConnect = false; Config.FunGame_isAutoConnect = false;
Config.FunGame_isAutoLogin = false; Config.FunGame_isAutoLogin = false;
Config.FunGame_isConnected = false; Config.FunGame_isConnected = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Yellow); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount(); LogoutAccount();
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
break; break;
case Config.WebHelper_LogOut: case Config.SocketHelper_LogOut:
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Config.FunGame_isConnected = false; Config.FunGame_isConnected = false;
Config.FunGame_isAutoLogin = false; Config.FunGame_isAutoLogin = false;
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Yellow); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount(); LogoutAccount();
}; };
if (InvokeRequired) if (InvokeRequired)
BeginInvoke(WebHelper_Action, this); BeginInvoke(SocketHelper_Action, this);
else else
WebHelper_Action(this); SocketHelper_Action(this);
if (Config.FunGame_isAutoConnect) if (Config.FunGame_isAutoConnect)
{ {
NOW_CONNECTEDRETRY = -1; NOW_CONNECTEDRETRY = -1;
@ -234,11 +234,11 @@ namespace FunGame.Desktop.UI
}); });
} }
break; break;
case Config.WebHelper_GetUser: case Config.SocketHelper_GetUser:
if (Usercfg.LoginUser != null) if (Usercfg.LoginUser != null)
return Usercfg.LoginUser; return Usercfg.LoginUser;
return null; return null;
case Config.WebHelper_SetUser: case Config.SocketHelper_SetUser:
if (objs != null && objs.Length > 1) if (objs != null && objs.Length > 1)
{ {
if (InvokeRequired) if (InvokeRequired)
@ -247,14 +247,14 @@ namespace FunGame.Desktop.UI
SetLoginUser(objs); SetLoginUser(objs);
} }
return null; return null;
case Config.WebHelper_SetNotice: case Config.SocketHelper_SetNotice:
Action action = () => Action action = () =>
{ {
NoticeText.Text = Config.FunGame_Notice; 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) if (InvokeRequired)
@ -264,9 +264,9 @@ namespace FunGame.Desktop.UI
return null; return null;
default: default:
if (needTime) if (needTime)
WritelnGameInfo(webHelper, GetNowShortTime() + msg); WritelnGameInfo(SocketHelper, GetNowShortTime() + msg);
else else
WritelnGameInfo(webHelper, msg); WritelnGameInfo(SocketHelper, msg);
return null; return null;
} }
} }
@ -274,8 +274,8 @@ namespace FunGame.Desktop.UI
} }
catch (Exception e) catch (Exception e)
{ {
WritelnGameInfo(webHelper, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); WritelnGameInfo(SocketHelper, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace);
GetMessage(webHelper, Config.WebHelper_SetRed); GetMessage(SocketHelper, Config.SocketHelper_SetRed);
} }
return null; return null;
} }
@ -291,7 +291,7 @@ namespace FunGame.Desktop.UI
{ {
try 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) if (ipaddress != null)
{ {
string[] s = ipaddress.Split(':'); string[] s = ipaddress.Split(':');
@ -346,21 +346,21 @@ namespace FunGame.Desktop.UI
WritelnGameInfo("ERROR无法连接至服务器请检查网络并重启游戏再试。"); WritelnGameInfo("ERROR无法连接至服务器请检查网络并重启游戏再试。");
return; return;
} }
WebHelper_Action = (main) => SocketHelper_Action = (main) =>
{ {
try try
{ {
if (main != null) if (main != null)
{ {
if (WebHelper != null) if (SocketHelper != null)
{ {
WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); SocketHelper.WebHelpMethod((int)SocketHelperMethod.CloseSocket);
WebHelper = null; SocketHelper = null;
} }
Config.FunGame_isRetrying = true; Config.FunGame_isRetrying = true;
Application.DoEvents(); Application.DoEvents();
WebHelper = new WebHelper(main); SocketHelper = new SocketHelper(main);
WebHelper.WebHelpMethod((int)WebHelperMethod.CreateSocket); // Invoke -> CreateSocket SocketHelper.WebHelpMethod((int)SocketHelperMethod.CreateSocket); // Invoke -> CreateSocket
} }
} }
catch catch
@ -372,11 +372,11 @@ namespace FunGame.Desktop.UI
{ {
if (InvokeRequired) if (InvokeRequired)
{ {
BeginInvoke(WebHelper_Action, main); BeginInvoke(SocketHelper_Action, main);
} }
else else
{ {
WebHelper_Action(main); SocketHelper_Action(main);
} }
}); });
} }
@ -399,10 +399,10 @@ namespace FunGame.Desktop.UI
{ {
try try
{ {
if (Config.INIHelper.ExistINIFile()) if (INIHelper.ExistINIFile())
{ {
string isAutoConncet = Config.INIHelper.ReadINI("Config", "AutoConnect"); string isAutoConncet = INIHelper.ReadINI("Config", "AutoConnect");
string isAutoLogin = Config.INIHelper.ReadINI("Config", "AutoLogin"); string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin");
if (isAutoConncet != null && !isAutoConncet.Equals("") && (isAutoConncet.Equals("false") || isAutoConncet.Equals("true"))) if (isAutoConncet != null && !isAutoConncet.Equals("") && (isAutoConncet.Equals("false") || isAutoConncet.Equals("true")))
Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConncet); Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConncet);
else throw new Exception("ERROR: 读取配置文件出错,参数格式不正确"); else throw new Exception("ERROR: 读取配置文件出错,参数格式不正确");
@ -412,7 +412,7 @@ namespace FunGame.Desktop.UI
} }
else else
{ {
Config.INIHelper.InitClientConfigs(); INIHelper.Init(Config.FunGameType);
WritelnGameInfo(">> 首次启动,已自动为你创建配置文件。"); WritelnGameInfo(">> 首次启动,已自动为你创建配置文件。");
GetFunGameConfig(); GetFunGameConfig();
} }
@ -463,13 +463,13 @@ namespace FunGame.Desktop.UI
} }
/// <summary> /// <summary>
/// 由WebHelper委托向消息队列输出一行文字 /// 由SocketHelper委托向消息队列输出一行文字
/// </summary> /// </summary>
/// <param name="webHelper"></param> /// <param name="SocketHelper"></param>
/// <param name="msg"></param> /// <param name="msg"></param>
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(() => Action tempAction = new Action(() =>
{ {
@ -934,10 +934,10 @@ namespace FunGame.Desktop.UI
{ {
if (ShowMessage.OKCancelMessage("你确定关闭游戏?", "退出") == (int)MessageResult.OK) if (ShowMessage.OKCancelMessage("你确定关闭游戏?", "退出") == (int)MessageResult.OK)
{ {
if (WebHelper != null) if (SocketHelper != null)
{ {
WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); SocketHelper.WebHelpMethod((int)SocketHelperMethod.CloseSocket);
WebHelper = null; SocketHelper = null;
} }
Environment.Exit(0); Environment.Exit(0);
} }
@ -1104,7 +1104,7 @@ namespace FunGame.Desktop.UI
{ {
if (ShowMessage.OKCancelMessage("你确定要退出登录吗?", "退出登录") == MessageResult.OK) if (ShowMessage.OKCancelMessage("你确定要退出登录吗?", "退出登录") == MessageResult.OK)
{ {
if (WebHelper == null || !WebHelper.WebHelpMethod((int)WebHelperMethod.Logout)) if (SocketHelper == null || !SocketHelper.WebHelpMethod((int)SocketHelperMethod.Logout))
ShowMessage.WarningMessage("请求无效:退出登录失败!"); ShowMessage.WarningMessage("请求无效:退出登录失败!");
} }
} }
@ -1116,8 +1116,8 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void Login_Click(object sender, EventArgs e) private void Login_Click(object sender, EventArgs e)
{ {
if (WebHelper != null && Config.FunGame_isConnected) if (SocketHelper != null && Config.FunGame_isConnected)
WebHelper.WebHelpMethod((int)WebHelperMethod.Login); SocketHelper.WebHelpMethod((int)SocketHelperMethod.Login);
else else
ShowMessage.WarningMessage("请先连接服务器!"); ShowMessage.WarningMessage("请先连接服务器!");
} }
@ -1399,16 +1399,16 @@ namespace FunGame.Desktop.UI
} }
break; break;
case Config.FunGame_Disconnect: 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; break;
case Config.FunGame_DisconnectWhenNotLogin: case Config.FunGame_DisconnectWhenNotLogin:
if (Config.FunGame_isConnected && WebHelper != null) if (Config.FunGame_isConnected && SocketHelper != null)
{ {
WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); SocketHelper.WebHelpMethod((int)SocketHelperMethod.CloseSocket);
GetMessage(WebHelper, Config.WebHelper_Disconnect); GetMessage(SocketHelper, Config.SocketHelper_Disconnect);
WritelnGameInfo(GetNowShortTime() + " >> 你已成功断开与服务器的连接。 "); WritelnGameInfo(GetNowShortTime() + " >> 你已成功断开与服务器的连接。 ");
} }
break; break;

View File

@ -15,17 +15,17 @@ using FunGame.Desktop.UI;
namespace FunGame.Desktop.Utils namespace FunGame.Desktop.Utils
{ {
public class WebHelper public class SocketHelper
{ {
private Socket? client; private Socket? client;
private EndPoint? server; private EndPoint? server;
Main Main; Main Main;
Action<Main, Socket>? WebHelper_Action = null; Action<Main, Socket>? SocketHelper_Action = null;
Task? WaitHeartBeat = null; Task? WaitHeartBeat = null;
public WebHelper(Main main) public SocketHelper(Main main)
{ {
Main = main; Main = main;
} }
@ -38,30 +38,30 @@ namespace FunGame.Desktop.Utils
{ {
switch (i) switch (i)
{ {
case (int)WebHelperMethod.CreateSocket: case (int)SocketHelperMethod.CreateSocket:
CreateSocket(); CreateSocket();
break; break;
case (int)WebHelperMethod.CloseSocket: case (int)SocketHelperMethod.CloseSocket:
Close(); Close();
break; break;
case (int)WebHelperMethod.StartWebHelper: case (int)SocketHelperMethod.StartSocketHelper:
StartWebHelper(); StartSocketHelper();
break; break;
case (int)WebHelperMethod.Login: case (int)SocketHelperMethod.Login:
if (client != null) if (client != null)
{ {
Send((int)SocketMessageType.CheckLogin, new object[] { Main, client, new User("Mili") }); Send((int)SocketMessageType.CheckLogin, new object[] { Main, client, new User("Mili") });
return true; return true;
} }
return false; return false;
case (int)WebHelperMethod.Logout: case (int)SocketHelperMethod.Logout:
if (client != null && Usercfg.LoginUser != null) if (client != null && Usercfg.LoginUser != null)
{ {
Send((int)SocketMessageType.Logout, new object[] { Main, client, Usercfg.LoginUser }); Send((int)SocketMessageType.Logout, new object[] { Main, client, Usercfg.LoginUser });
return true; return true;
} }
return false; return false;
case (int)WebHelperMethod.Disconnect: case (int)SocketHelperMethod.Disconnect:
if (client != null) if (client != null)
{ {
Send((int)SocketMessageType.Disconnect, new object[] { Main, client }); Send((int)SocketMessageType.Disconnect, new object[] { Main, client });
@ -88,14 +88,14 @@ namespace FunGame.Desktop.Utils
client.Connect(server); client.Connect(server);
if (IsConnected()) if (IsConnected())
{ {
Main.GetMessage(this, Config.WebHelper_WaitLoginAndSetYellow); Main.GetMessage(this, Config.SocketHelper_WaitLoginAndSetYellow);
break; 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; object[] objs;
if (obj != null) if (obj != null)
objs = new object[] { main, socket, obj }; objs = new object[] { main, socket, obj };
@ -104,24 +104,24 @@ namespace FunGame.Desktop.Utils
if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告 if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告
{ {
main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true); main.GetMessage(this, " >> 连接服务器成功请登录账号以体验FunGame。", true);
main.GetMessage(this, Config.WebHelper_SetNotice); main.GetMessage(this, Config.SocketHelper_SetNotice);
} }
}; };
Task t = Task.Factory.StartNew(() => Task t = Task.Factory.StartNew(() =>
{ {
if (Main.InvokeRequired) if (Main.InvokeRequired)
{ {
Main.Invoke(WebHelper_Action, Main, client); Main.Invoke(SocketHelper_Action, Main, client);
} }
else else
{ {
WebHelper_Action(Main, client); SocketHelper_Action(Main, client);
} }
}); });
} }
catch (Exception e) catch (Exception e)
{ {
Main.GetMessage(this, Config.WebHelper_Disconnected); Main.GetMessage(this, Config.SocketHelper_Disconnected);
Main.GetMessage(this, e.StackTrace); Main.GetMessage(this, e.StackTrace);
Close(); Close();
} }
@ -175,25 +175,25 @@ namespace FunGame.Desktop.Utils
case (int)SocketMessageType.Login: case (int)SocketMessageType.Login:
break; break;
case (int)SocketMessageType.CheckLogin: 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); Main.GetMessage(this, read, true);
StartWebHelper(); // 开始创建TCP流 StartSocketHelper(); // 开始创建TCP流
return true; return true;
case (int)SocketMessageType.Logout: 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, read, true);
Main.GetMessage(this, Config.WebHelper_LogOut); Main.GetMessage(this, Config.SocketHelper_LogOut);
Close(); Close();
return true; return true;
case (int)SocketMessageType.Disconnect: case (int)SocketMessageType.Disconnect:
Main.GetMessage(this, read, true); Main.GetMessage(this, read, true);
Main.GetMessage(this, Config.WebHelper_Disconnect); Main.GetMessage(this, Config.SocketHelper_Disconnect);
Close(); Close();
return true; return true;
case (int)SocketMessageType.HeartBeat: case (int)SocketMessageType.HeartBeat:
if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1); if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1);
Config.WebHelper_HeartBeatFaileds = 0; Config.SocketHelper_HeartBeatFaileds = 0;
main.GetMessage(this, Config.WebHelper_SetGreenAndPing); main.GetMessage(this, Config.SocketHelper_SetGreenAndPing);
return true; return true;
} }
main.GetMessage(this, read); main.GetMessage(this, read);
@ -204,13 +204,13 @@ namespace FunGame.Desktop.Utils
} }
else else
{ {
main.GetMessage(this, Config.WebHelper_Disconnected); main.GetMessage(this, Config.SocketHelper_Disconnected);
throw new Exception("ERROR服务器连接失败。"); throw new Exception("ERROR服务器连接失败。");
} }
} }
catch (Exception e) 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); main.GetMessage(this, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace);
Close(); Close();
} }
@ -302,7 +302,7 @@ namespace FunGame.Desktop.Utils
} }
else else
{ {
main.GetMessage(this, Config.WebHelper_Disconnected); main.GetMessage(this, Config.SocketHelper_Disconnected);
throw new Exception("ERROR服务器连接失败。"); throw new Exception("ERROR服务器连接失败。");
} }
} }
@ -323,9 +323,9 @@ namespace FunGame.Desktop.Utils
private void CatchException(Main main, Exception e, bool isDisconnected) private void CatchException(Main main, Exception e, bool isDisconnected)
{ {
if (isDisconnected) if (isDisconnected)
main.GetMessage(this, Config.WebHelper_Disconnected); main.GetMessage(this, Config.SocketHelper_Disconnected);
else 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); main.GetMessage(this, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace);
Close(); Close();
} }
@ -335,8 +335,8 @@ namespace FunGame.Desktop.Utils
// 超过三次没回应心跳,服务器连接失败。 // 超过三次没回应心跳,服务器连接失败。
try try
{ {
Config.WebHelper_HeartBeatFaileds++; Config.SocketHelper_HeartBeatFaileds++;
if (Config.WebHelper_HeartBeatFaileds >= 3) if (Config.SocketHelper_HeartBeatFaileds >= 3)
throw new Exception("ERROR服务器连接失败。"); throw new Exception("ERROR服务器连接失败。");
} }
catch (Exception e) catch (Exception e)
@ -378,7 +378,7 @@ namespace FunGame.Desktop.Utils
} }
} }
private void StartWebHelper() private void StartSocketHelper()
{ {
Task HeartBeatStream = Task.Factory.StartNew(() => Task HeartBeatStream = Task.Factory.StartNew(() =>
{ {