diff --git a/FunGame.Core/Api/Utility/Implement.cs b/FunGame.Core/Api/Utility/Implement.cs index 3b3c02f..fa8c9fb 100644 --- a/FunGame.Core/Api/Utility/Implement.cs +++ b/FunGame.Core/Api/Utility/Implement.cs @@ -1,4 +1,4 @@ -using System; + using System; using System.Collections.Generic; using System.Linq; using System.Reflection; diff --git a/FunGame.Core/Api/Utility/Singleton.cs b/FunGame.Core/Api/Utility/Singleton.cs index 6ac073c..104affe 100644 --- a/FunGame.Core/Api/Utility/Singleton.cs +++ b/FunGame.Core/Api/Utility/Singleton.cs @@ -15,6 +15,11 @@ namespace Milimoe.FunGame.Core.Api.Utility { private static readonly Hashtable SingletonTable = new(); + public static bool IsExist(string key) + { + return SingletonTable.ContainsKey(key); + } + public static bool Add(object single) { string type = single.GetType().ToString(); @@ -26,13 +31,27 @@ namespace Milimoe.FunGame.Core.Api.Utility } catch { - throw new Exception("添加单例到单例表时遇到错误"); + throw new Exception("添加单例到单例表时遇到错误。"); } return true; } return false; } + public static bool Remove(object single) + { + string type = single.GetType().ToString(); + if (!SingletonTable.ContainsKey(type)) + { + return false; + } + else + { + SingletonTable[type] = null; + return true; + } + } + public static T? Get() { T? single = default; @@ -45,7 +64,25 @@ namespace Milimoe.FunGame.Core.Api.Utility } catch { - throw new Exception("不能从单例表中获取到指定的单例"); + throw new Exception("不能从单例表中获取到指定的单例。"); + } + if (single != null) return single; + } + return single; + } + + public static object? Get(string key) + { + object? single = default; + if (SingletonTable.ContainsKey(key)) + { + try + { + single = SingletonTable[key]; + } + catch + { + throw new Exception("不能从单例表中获取到指定的单例。"); } if (single != null) return single; } diff --git a/FunGame.Core/Library/Constant/ConstantSet.cs b/FunGame.Core/Library/Constant/ConstantSet.cs index 57385ce..1f1eaac 100644 --- a/FunGame.Core/Library/Constant/ConstantSet.cs +++ b/FunGame.Core/Library/Constant/ConstantSet.cs @@ -45,4 +45,15 @@ namespace Milimoe.FunGame.Core.Library.Constant public static string EXEFolderPath { get; } = Environment.CurrentDirectory.ToString() + "\\"; // 程序目录 public static string PluginFolderPath { get; } = Environment.CurrentDirectory.ToString() + "\\plugins\\"; // 插件目录 } + + public class FormSet + { + public const string Main = "Main"; + public const string Register = "Register"; + public const string Login = "Login"; + public const string Inventory = "Inventory"; + public const string Store = "Store"; + public const string RoomSetting = "RoomSetting"; + public const string UserCenter = "UserCenter"; + } } diff --git a/FunGame.Core/Library/Constant/TypeEnum.cs b/FunGame.Core/Library/Constant/TypeEnum.cs index f61ec0e..882c921 100644 --- a/FunGame.Core/Library/Constant/TypeEnum.cs +++ b/FunGame.Core/Library/Constant/TypeEnum.cs @@ -6,6 +6,23 @@ using System.Threading.Tasks; namespace Milimoe.FunGame.Core.Library.Constant { + public enum FormType + { + Main, + Register, + Login, + Inventory, + RoomSetting, + Store, + UserCenter + } + + public enum OpenFormType + { + General, + Dialog + } + public enum InterfaceType { IClient, diff --git a/FunGame.Desktop/FunGame.Desktop.csproj b/FunGame.Desktop/FunGame.Desktop.csproj index 02ef313..071b5eb 100644 --- a/FunGame.Desktop/FunGame.Desktop.csproj +++ b/FunGame.Desktop/FunGame.Desktop.csproj @@ -58,7 +58,7 @@ - + diff --git a/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs b/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs new file mode 100644 index 0000000..df9f82a --- /dev/null +++ b/FunGame.Desktop/Library/Component/GeneralForm.Designer.cs @@ -0,0 +1,47 @@ +namespace Milimoe.FunGame.Desktop.Library.Component +{ + partial class GeneralForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.SuspendLayout(); + // + // GeneralForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(536, 284); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; + this.Name = "GeneralForm"; + this.Text = "GeneralForm"; + this.ResumeLayout(false); + + } + + #endregion + } +} \ No newline at end of file diff --git a/FunGame.Desktop/Library/Component/GeneralForm.cs b/FunGame.Desktop/Library/Component/GeneralForm.cs new file mode 100644 index 0000000..7fc2aa7 --- /dev/null +++ b/FunGame.Desktop/Library/Component/GeneralForm.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Milimoe.FunGame.Desktop.Library.Component +{ + public partial class GeneralForm : Form + { + protected int loc_x, loc_y; // 窗口当前坐标 + + public GeneralForm() + { + InitializeComponent(); + } + + /// + /// 鼠标按下,开始移动主窗口 + /// + /// + /// + protected void Title_MouseDown(object sender, MouseEventArgs e) + { + //判断是否为鼠标左键 + if (e.Button == MouseButtons.Left) + { + //获取鼠标左键按下时的位置 + loc_x = e.Location.X; + loc_y = e.Location.Y; + } + } + + /// + /// 鼠标移动,正在移动主窗口 + /// + /// + /// + protected void Title_MouseMove(object sender, MouseEventArgs e) + { + if (e.Button == MouseButtons.Left) + { + //计算鼠标移动距离 + Left += e.Location.X - loc_x; + Top += e.Location.Y - loc_y; + } + } + } +} diff --git a/FunGame.Desktop/Library/Component/GeneralForm.resx b/FunGame.Desktop/Library/Component/GeneralForm.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/FunGame.Desktop/Library/Component/GeneralForm.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index e01c0f2..c37c93b 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics; using System.Net.Sockets; using System.Net; @@ -7,13 +8,10 @@ using System.Text; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Others; -using Milimoe.FunGame.Desktop.Utils; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Core.Library.Exception; -using System; using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Desktop.Model; namespace Milimoe.FunGame.Desktop.UI { diff --git a/FunGame.Desktop/Utility/OpenForm.cs b/FunGame.Desktop/Utility/OpenForm.cs new file mode 100644 index 0000000..a3b62ae --- /dev/null +++ b/FunGame.Desktop/Utility/OpenForm.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; +using System.Net.NetworkInformation; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Desktop.Others; +using Milimoe.FunGame.Desktop.UI; +using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Api.Factory; +using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Desktop.Library.Component; +using System.Drawing.Drawing2D; +using System.Windows.Forms; + +namespace Milimoe.FunGame.Desktop.Utility +{ + public class OpenForm + { + public static void SingleForm(FormType type, OpenFormType opentype = OpenFormType.General) + { + string formtype = ""; + switch(type) + { + case FormType.Register: + formtype = FormSet.Register; + break; + case FormType.Login: + formtype = FormSet.Login; + break; + case FormType.Inventory: + formtype = FormSet.Inventory; + break; + case FormType.RoomSetting: + formtype = FormSet.RoomSetting; + break; + case FormType.Store: + formtype = FormSet.Store; + break; + case FormType.UserCenter: + formtype = FormSet.UserCenter; + break; + case FormType.Main: + formtype = FormSet.Main; + break; + default: + break; + } + if (!Singleton.IsExist(formtype)) + { + NewSingleForm(type, opentype); + } + else + { + OpenSingleForm(formtype, opentype); + } + } + + private static void NewSingleForm(FormType type, OpenFormType opentype) + { + System.Windows.Forms.Form form = new(); + switch (type) + { + case FormType.Register: + form = new Register(); + break; + case FormType.Login: + form = new Login(); + break; + case FormType.Inventory: + break; + case FormType.RoomSetting: + break; + case FormType.Store: + break; + case FormType.UserCenter: + break; + case FormType.Main: + form = new Main(); + break; + default: + break; + } + if (Singleton.Add(form)) + { + if (opentype == OpenFormType.Dialog) form.ShowDialog(); + else form.Show(); + } + else throw new Exception("无法打开指定窗口。"); + } + + private static void OpenSingleForm(string key, OpenFormType opentype) + { + System.Windows.Forms.Form form = new(); + object? obj = Singleton.Get(key); + if (obj != null) + { + switch (key) + { + case FormSet.Register: + form = (Register)obj; + break; + case FormSet.Login: + form = (Login)obj; + break; + case FormSet.Inventory: + break; + case FormSet.RoomSetting: + break; + case FormSet.Store: + break; + case FormSet.UserCenter: + break; + case FormSet.Main: + form = (Main)obj; + break; + default: + break; + } + if (opentype == OpenFormType.Dialog) form.ShowDialog(); + else form.Show(); + } + else throw new Exception("无法打开指定窗口。"); + } + } +} diff --git a/FunGame.Desktop/Utility/SocketHelper.cs b/FunGame.Desktop/Utility/SocketHelper.cs deleted file mode 100644 index 5725fcf..0000000 --- a/FunGame.Desktop/Utility/SocketHelper.cs +++ /dev/null @@ -1,410 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Sockets; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using Milimoe.FunGame.Desktop.Library.Component; -using System.ComponentModel.DataAnnotations; -using System.Net.NetworkInformation; -using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Desktop.Others; -using Milimoe.FunGame.Desktop.UI; -using Milimoe.FunGame.Core.Api.Utility; -using Milimoe.FunGame.Core.Api.Factory; -using Milimoe.FunGame.Core.Library.Constant; - -namespace Milimoe.FunGame.Desktop.Utils -{ - public class SocketHelper - { - //private Socket? client; - //private EndPoint? server; - //Main Main; - - //Action? SocketHelper_Action = null; - - //Task? WaitHeartBeat = null; - - //public SocketHelper(Main main) - //{ - // Main = main; - //} - - ///// - ///// 选择SocketHelper分支方法 - ///// - ///// 分支方法ID - //public bool GetSocketHelperMethod(int i) - //{ - // switch (i) - // { - // case (int)SocketHelperMethod.CreateSocket: - // CreateSocket(); - // break; - // case (int)SocketHelperMethod.CloseSocket: - // Close(); - // break; - // case (int)SocketHelperMethod.StartSocketHelper: - // StartSocketHelper(); - // break; - // case (int)SocketHelperMethod.Login: - // if (client != null) - // { - // Send((int)SocketMessageType.CheckLogin, new object[] { Main, client, Factory.New("Mili") }); - // return true; - // } - // return false; - // 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)SocketHelperMethod.Disconnect: - // if (client != null) - // { - // Send((int)SocketMessageType.Disconnect, new object[] { Main, client }); - // return true; - // } - // return false; - // } - // return true; - //} - - ///// - ///// 创建客户端专属Socket - ///// - //private void CreateSocket() - //{ - // try - // { - // client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - // server = new IPEndPoint(IPAddress.Parse(Constant.SERVER_IPADRESS), Constant.SERVER_PORT); - // while (true) - // { - // if (!IsConnected()) - // { - // client.Connect(server); - // if (IsConnected()) - // { - // Main.GetMessage(this, MainControllerSet.WaitLoginAndSetYellow); - // break; - // } - // } - // } - // SocketHelper_Action = (main, socket) => - // { - // object? obj = main.GetMessage(this, MainControllerSet.GetUser); - // object[] objs; - // if (obj != null) - // objs = new object[] { main, socket, obj }; - // else - // objs = new object[] { main, socket }; - // if (Send((int)SocketMessageType.GetNotice, objs)) // 接触服务器并获取公告 - // { - // main.GetMessage(this, " >> 连接服务器成功,请登录账号以体验FunGame。", true); - // main.GetMessage(this, MainControllerSet.Connected); - // } - // }; - // Task t = Task.Factory.StartNew(() => - // { - // if (Main.InvokeRequired) - // { - // Main.Invoke(SocketHelper_Action, Main, client); - // } - // else - // { - // SocketHelper_Action(Main, client); - // } - // }); - // } - // catch (Exception e) - // { - // Main.GetMessage(this, MainControllerSet.Disconnected); - // Main.GetMessage(this, e.StackTrace); - // Close(); - // } - //} - - ///// - ///// 判断是否连接成功 - ///// - ///// 连接状态 - //public bool IsConnected() - //{ - // if (client != null) - // return client.Connected; - // return false; - //} - - //private bool Read(object[]? objs = null) - //{ - // Main main = Main; - // Socket? socket = null; - // try - // { - // if (objs != null) - // { - // if (objs.Length > 0) main = (Main)objs[0]; - // if (objs.Length > 1) socket = (Socket)objs[1]; - // } - // else - // { - // main = Main; - // socket = client; - // } - // if (socket != null) - // { - // // 从服务器接收消息 - // byte[] buffer = new byte[2048]; - // int length = socket.Receive(buffer); - // if (length > 0) - // { - // string msg = Constant.DEFAULT_ENCODING.GetString(buffer, 0, length); - // int type = GetType(msg); - // string typestring = EnumHelper.GetSocketTypeName(type); - // string read = GetMessage(msg); - // switch (type) - // { - // case (int)SocketMessageType.GetNotice: - // string[] reads = read.Split(';'); - // Config.FunGame_Notice = reads[1]; - // main.GetMessage(this, " >> 已连接至服务器: " + reads[0] + "。\n\n********** 服务器公告 **********\n\n" + Config.FunGame_Notice + "\n\n", true); - // return true; - // case (int)SocketMessageType.Login: - // break; - // case (int)SocketMessageType.CheckLogin: - // Main.GetMessage(this, MainControllerSet.SetUser, false, objs); - // Main.GetMessage(this, read, true); - // StartSocketHelper(); // 开始创建TCP流 - // return true; - // case (int)SocketMessageType.Logout: - // Main.GetMessage(this, MainControllerSet.SetUser, false, objs); - // Main.GetMessage(this, read, true); - // Main.GetMessage(this, MainControllerSet.LogOut); - // Close(); - // return true; - // case (int)SocketMessageType.Disconnect: - // Main.GetMessage(this, read, true); - // Main.GetMessage(this, MainControllerSet.Disconnect); - // Close(); - // return true; - // case (int)SocketMessageType.HeartBeat: - // if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1); - // //Constant.SocketHelper_HeartBeatFaileds = 0; - // main.GetMessage(this, MainControllerSet.SetGreenAndPing); - // return true; - // } - // main.GetMessage(this, read); - // return true; - // } - // else - // throw new Exception("ERROR:未收到任何来自服务器的信息,与服务器连接可能丢失。"); - // } - // else - // { - // main.GetMessage(this, MainControllerSet.Disconnected); - // throw new Exception("ERROR:服务器连接失败。"); - // } - // } - // catch (Exception e) - // { - // main.GetMessage(this, MainControllerSet.Disconnected); - // main.GetMessage(this, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); - // Close(); - // } - // return false; - //} - - //private bool Send(int i, object[]? objs = null) - //{ - // Main main = Main; - // Socket? socket = null; - // try - // { - // if (objs != null) - // { - // if (objs.Length > 0) main = (Main)objs[0]; - // if (objs.Length > 1) socket = (Socket)objs[1]; - // } - // else - // { - // main = Main; - // socket = client; - // } - // if (socket != null) - // { - // string msg = ""; - // SocketMessageType type = (SocketMessageType)i; - // // 发送消息给服务器端 - // switch (type) - // { - // case SocketMessageType.GetNotice: - // msg = MakeMessage(type, "获取公告"); - // if (Send(msg, socket) > 0) - // { - // return Read(objs); - // } - // else - // throw new Exception("ERROR:消息未送达服务器,与服务器连接可能丢失。"); - // case SocketMessageType.Login: - // break; - // case SocketMessageType.CheckLogin: - // User user; - // if (objs != null && objs.Length > 2) - // { - // user = (User)objs[2]; - // msg = MakeMessage(type, user.Userame); - // } - // else - // { - // Config.FunGame_isAutoRetry = false; - // throw new Exception("ERROR: 请登录账号。"); - // } - // break; - // case SocketMessageType.Logout: - // if (objs != null && objs.Length > 2) - // { - // user = (User)objs[2]; - // msg = MakeMessage(type, user.Userame); - // if (Send(msg, socket) > 0) - // return true; - // } - // return false; - // case SocketMessageType.Disconnect: - // msg = MakeMessage(type, "断开连接"); - // if (Send(msg, socket) > 0) - // return true; - // return false; - // case SocketMessageType.HeartBeat: - // msg = MakeMessage(type, "心跳检测"); - // if (Send(msg, socket) > 0) - // { - // WaitHeartBeat = Task.Run(() => - // { - // Thread.Sleep(4000); - // AddHeartBeatFaileds(main); - // }); - // return true; - // } - // AddHeartBeatFaileds(main); - // return false; - // default: - // return false; - // } - // if (Send(msg, socket) > 0) - // { - // return Read(objs); - // } - // else - // throw new Exception("ERROR:消息未送达服务器,与服务器连接可能丢失。"); - // } - // else - // { - // main.GetMessage(this, MainControllerSet.Disconnected); - // throw new Exception("ERROR:服务器连接失败。"); - // } - // } - // catch (Exception e) - // { - // CatchException(main, e, false); - // } - // return false; - //} - - //private int Send(string msg, Socket socket) - //{ - // byte[] buffer = Constant.DEFAULT_ENCODING.GetBytes(msg); - // int length = socket.Send(buffer); - // return length; - //} - - //private void CatchException(Main main, Exception e, bool isDisconnected) - //{ - // if (isDisconnected) - // main.GetMessage(this, MainControllerSet.Disconnected); - // else - // main.GetMessage(this, MainControllerSet.SetRed); - // main.GetMessage(this, e.Message != null ? e.Message + "\n" + e.StackTrace : "" + e.StackTrace); - // Close(); - //} - - //private void AddHeartBeatFaileds(Main main) - //{ - // // 超过三次没回应心跳,服务器连接失败。 - // try - // { - // //MainControllerSet.HeartBeatFaileds++; - // //if (MainControllerSet.HeartBeatFaileds >= 3) - // // throw new Exception("ERROR:服务器连接失败。"); - // } - // catch (Exception e) - // { - // CatchException(main, e, true); - // } - //} - - //private int GetType(string msg) - //{ - // int index = msg.IndexOf(';') - 1; - // if (index > 0) - // return Convert.ToInt32(msg[..index]); - // else - // return Convert.ToInt32(msg[..1]); - //} - - //private string GetMessage(string msg) - //{ - // int index = msg.IndexOf(';') + 1; - // return msg[index..]; - //} - - //private string MakeMessage(SocketMessageType type, string msg) - //{ - // return (int)type + ";" + msg; - //} - - //private void Close() - //{ - // if (client != null) - // { - // client.Close(); - // client = null; - // } - // if (server != null) - // { - // server = null; - // } - //} - - //private void StartSocketHelper() - //{ - // Task HeartBeatStream = Task.Factory.StartNew(CreateSendHeartBeatStream); - // Task StreamReader = Task.Factory.StartNew(CreateStreamReader); - //} - - //private void CreateSendHeartBeatStream() - //{ - // Thread.Sleep(100); - // Main.GetMessage(this, "Creating: SendHeartBeatStream...OK"); - // while (IsConnected()) - // { - // Send((int)SocketMessageType.HeartBeat); // 发送心跳包 - // Thread.Sleep(20000); - // } - //} - - //private void CreateStreamReader() - //{ - // Thread.Sleep(100); - // Main.GetMessage(this, "Creating: StreamReader...OK"); - // while (IsConnected()) - // { - // Read(); - // } - //} - } -}