更新Singleton和OpenForm

This commit is contained in:
Mili 2023-01-19 21:58:46 +08:00
parent d737702511
commit c82eafb294
11 changed files with 360 additions and 417 deletions

View File

@ -1,4 +1,4 @@
using System;
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

View File

@ -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>()
{
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;
}

View File

@ -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";
}
}

View File

@ -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,

View File

@ -58,7 +58,7 @@
<ItemGroup>
<Folder Include="UI\RoomSetting\" />
<Folder Include="UI\UserCenter\" />
<Folder Include="UI\Stock\" />
<Folder Include="UI\Store\" />
<Folder Include="UI\Inventory\" />
</ItemGroup>

View File

@ -0,0 +1,47 @@
namespace Milimoe.FunGame.Desktop.Library.Component
{
partial class GeneralForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
}
}

View File

@ -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();
}
/// <summary>
/// 鼠标按下,开始移动主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Title_MouseDown(object sender, MouseEventArgs e)
{
//判断是否为鼠标左键
if (e.Button == MouseButtons.Left)
{
//获取鼠标左键按下时的位置
loc_x = e.Location.X;
loc_y = e.Location.Y;
}
}
/// <summary>
/// 鼠标移动,正在移动主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -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
{

View File

@ -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("无法打开指定窗口。");
}
}
}

View File

@ -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<Main, Socket>? SocketHelper_Action = null;
//Task? WaitHeartBeat = null;
//public SocketHelper(Main main)
//{
// Main = main;
//}
///// <summary>
///// 选择SocketHelper分支方法
///// </summary>
///// <param name="i">分支方法ID</param>
//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<User>("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;
//}
///// <summary>
///// 创建客户端专属Socket
///// </summary>
//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();
// }
//}
///// <summary>
///// 判断是否连接成功
///// </summary>
///// <returns>连接状态</returns>
//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();
// }
//}
}
}