更新checklogin后续问题、showmessage, factory等

This commit is contained in:
Mili 2023-02-25 00:11:47 +08:00
parent b1cf47cdcb
commit fc43f91ad2
8 changed files with 93 additions and 44 deletions

View File

@ -43,7 +43,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
if (objs is null || objs.Length == 0) return (T)instance; if (objs is null || objs.Length == 0) return (T)instance;
if (typeof(T) == typeof(Entity.User)) if (typeof(T) == typeof(Entity.User))
{ {
instance = Api.Factory.UserFactory.GetInstance("Mili"); instance = GetUser(objs);
} }
else if (typeof(T) == typeof(Skill)) else if (typeof(T) == typeof(Skill))
{ {
@ -90,5 +90,30 @@ namespace Milimoe.FunGame.Core.Api.Utility
return true; return true;
return false; return false;
} }
private static User GetUser(params object[] objs)
{
string username = "", password = "";
object objTemp;
if (objs.Length > 0)
{
objTemp = objs[0];
if (objTemp.GetType() != typeof(string))
{
username = NetworkUtility.ConvertJsonObject<string>(objTemp) ?? "";
}
else username = (string)objs[0];
}
if (objs.Length > 1)
{
objTemp = objs[1];
if (objTemp.GetType() != typeof(string))
{
password = NetworkUtility.ConvertJsonObject<string>(objTemp) ?? "";
}
else password = (string)objs[1];
}
return Api.Factory.UserFactory.GetInstance(username, password);
}
} }
} }

View File

@ -91,7 +91,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
DontFragment = true DontFragment = true
}; };
string data = "getserverping"; string data = "getserverping";
byte[] buffer = Encoding.ASCII.GetBytes(data); byte[] buffer = General.DefaultEncoding.GetBytes(data);
int timeout = 120; int timeout = 120;
PingReply reply = pingSender.Send(addr, timeout, buffer, options); PingReply reply = pingSender.Send(addr, timeout, buffer, options);
if (reply.Status == IPStatus.Success) if (reply.Status == IPStatus.Success)
@ -116,7 +116,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <summary> /// <summary>
/// Json工具类 /// Json工具类
/// </summary> /// </summary>
public static class JsonUtility internal static class JsonUtility
{ {
/// <summary> /// <summary>
/// 将JsonElement转换为泛型 /// 将JsonElement转换为泛型
@ -124,10 +124,9 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <typeparam name="T">泛型</typeparam> /// <typeparam name="T">泛型</typeparam>
/// <param name="element">JsonElement</param> /// <param name="element">JsonElement</param>
/// <returns></returns> /// <returns></returns>
public static T? ToObject<T>(this JsonElement element) internal static T? ToObject<T>(this JsonElement element)
{ {
var json = element.GetRawText(); return JsonSerializer.Deserialize<T>(element.GetRawText());
return JsonSerializer.Deserialize<T>(json);
} }
} }
@ -222,7 +221,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <summary> /// <summary>
/// 使用HMACSHA512算法加密 /// 使用HMACSHA512算法加密
/// </summary> /// </summary>
public class Encryption internal class Encryption
{ {
/// <summary> /// <summary>
/// 使用HMACSHA512算法加密 /// 使用HMACSHA512算法加密
@ -230,15 +229,29 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <param name="Message">需要加密的值</param> /// <param name="Message">需要加密的值</param>
/// <param name="Key">秘钥</param> /// <param name="Key">秘钥</param>
/// <returns></returns> /// <returns></returns>
public static string HmacSha512(string Message, string Key) internal static string HmacSha512(string Message, string Key)
{ {
byte[] MessageBytes = General.DefaultEncoding.GetBytes(Message); byte[] MessageBytes = General.DefaultEncoding.GetBytes(Message);
Key = Convert.ToBase64String(General.DefaultEncoding.GetBytes(Key)); Key = Convert.ToBase64String(General.DefaultEncoding.GetBytes(Key));
byte[] KeyBytes = General.DefaultEncoding.GetBytes(Key); byte[] KeyBytes = General.DefaultEncoding.GetBytes(Key);
HMACSHA512 Hmacsha512 = new(KeyBytes); HMACSHA512 Hmacsha512 = new(KeyBytes);
byte[] Hash = Hmacsha512.ComputeHash(MessageBytes); byte[] Hash = Hmacsha512.ComputeHash(MessageBytes);
string Hamc = BitConverter.ToString(Hash).Replace("-", ""); string Hmac = BitConverter.ToString(Hash).Replace("-", "");
return Hamc.ToLower(); return Hmac.ToLower();
}
}
public static class StringExtension
{
/// <summary>
/// 使用HMACSHA512算法加密
/// </summary>
/// <param name="msg">需要加密的值</param>
/// <param name="key">秘钥</param>
/// <returns></returns>
public static string Encrypt(this string msg, string key)
{
return Encryption.HmacSha512(msg, Encryption.HmacSha512(msg, key));
} }
} }

View File

@ -1,6 +1,6 @@
namespace Milimoe.FunGame.Core.Library.Exception namespace Milimoe.FunGame.Core.Library.Exception
{ {
public static class ExceptionHelper public static class ExceptionExtension
{ {
public static string GetErrorInfo(this System.Exception e) public static string GetErrorInfo(this System.Exception e)
{ {

View File

@ -1,5 +1,6 @@
using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Component;
using Milimoe.FunGame.Desktop.Model; using Milimoe.FunGame.Desktop.Model;
namespace Milimoe.FunGame.Desktop.Controller namespace Milimoe.FunGame.Desktop.Controller
@ -12,21 +13,18 @@ namespace Milimoe.FunGame.Desktop.Controller
bool result = LoginModel.LoginAccount(objs); bool result = LoginModel.LoginAccount(objs);
if (!result) if (!result)
{ {
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
} }
RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs());
return result; return result;
} }
public static bool CheckLogin(params object[]? objs) public static bool CheckLogin(params object[]? objs)
{ {
bool result = LoginModel.CheckLogin(objs); bool result = LoginModel.CheckLogin(objs);
if (result) if (!result)
{
RunTime.Login?.OnSucceedLoginEvent(new GeneralEventArgs());
}
else
{ {
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs()); RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
} }
return result; return result;

View File

@ -6,7 +6,8 @@ namespace Milimoe.FunGame.Desktop.Library.Component
{ {
private MessageResult MessageResult = MessageResult.Cancel; private MessageResult MessageResult = MessageResult.Cancel;
private string InputResult = ""; private string InputResult = "";
private int AutoClose = 0; private readonly int AutoClose = 0;
private readonly Task? TaskAutoClose;
private const string TITLE_TIP = "提示"; private const string TITLE_TIP = "提示";
private const string TITLE_WARNING = "警告"; private const string TITLE_WARNING = "警告";
@ -112,29 +113,23 @@ namespace Milimoe.FunGame.Desktop.Library.Component
} }
if (AutoClose > 0) if (AutoClose > 0)
{ {
Action action = new(() => TaskAutoClose = Task.Factory.StartNew(() =>
{ {
Thread.Sleep(100);
string msg = MsgText.Text; string msg = MsgText.Text;
MsgText.Text = msg + "\n[ " + AutoClose + " 秒后自动关闭 ]"; int s = AutoClose;
while (AutoClose > 0) BeginInvoke(() => ChangeSecond(msg, s));
while (s > 0)
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
AutoClose--; s--;
MsgText.Text = msg + "\n[ " + AutoClose + " 秒后自动关闭 ]"; if (IsHandleCreated) BeginInvoke(() => ChangeSecond(msg, s));
} }
MessageResult = MessageResult.OK; MessageResult = MessageResult.OK;
Dispose(); Close();
}); });
Task.Run(() =>
{
if (InvokeRequired)
Invoke(action);
else
action();
});
Show();
} }
else ShowDialog(); ShowDialog();
} }
/// <summary> /// <summary>
@ -152,6 +147,7 @@ namespace Milimoe.FunGame.Desktop.Library.Component
BUTTON_RETRY => MessageResult.Retry, BUTTON_RETRY => MessageResult.Retry,
_ => MessageResult.Cancel _ => MessageResult.Cancel
}; };
TaskAutoClose?.Wait(1);
Dispose(); Dispose();
} }
@ -211,6 +207,11 @@ namespace Milimoe.FunGame.Desktop.Library.Component
return result; return result;
} }
private void ChangeSecond(string msg, int s)
{
MsgText.Text = msg + "\n[ " + s + " 秒后自动关闭 ]";
}
private void LeftButton_Click(object sender, EventArgs e) private void LeftButton_Click(object sender, EventArgs e)
{ {
SetButtonResult(LeftButton.Text); SetButtonResult(LeftButton.Text);

View File

@ -251,7 +251,6 @@ namespace Milimoe.FunGame.Desktop.Model
object[] ServerMessage = GetServerMessage(); object[] ServerMessage = GetServerMessage();
SocketMessageType type = (SocketMessageType)ServerMessage[0]; SocketMessageType type = (SocketMessageType)ServerMessage[0];
object[] objs = (object[])ServerMessage[1]; object[] objs = (object[])ServerMessage[1];
result = type; result = type;
switch (type) switch (type)
{ {
@ -269,6 +268,7 @@ namespace Milimoe.FunGame.Desktop.Model
case SocketMessageType.CheckLogin: case SocketMessageType.CheckLogin:
SocketHandler_CheckLogin(objs); SocketHandler_CheckLogin(objs);
RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs());
break; break;
case SocketMessageType.Logout: case SocketMessageType.Logout:
@ -326,16 +326,27 @@ namespace Milimoe.FunGame.Desktop.Model
Guid key = Guid.Empty; Guid key = Guid.Empty;
// 返回一个Key再发回去给服务器就行了 // 返回一个Key再发回去给服务器就行了
if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject<Guid>(objs[0])!; if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject<Guid>(objs[0])!;
LoginController.CheckLogin(key); if (key != Guid.Empty) LoginController.CheckLogin(key);
else
{
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs());
}
} }
private void SocketHandler_CheckLogin(object[] objs) private void SocketHandler_CheckLogin(object[] objs)
{ {
string msg = "";
// 返回的objs是该Login的User对象的各个属性 // 返回的objs是该Login的User对象的各个属性
if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject<string>(objs[0])!; if (objs != null && objs.Length > 0)
Main.GetMessage(msg); {
Main.UpdateUI(MainSet.SetUser, new object[] { Factory.New<User>(msg) }); // 创建User对象并返回到Main
Main.UpdateUI(MainSet.SetUser, new object[] { Factory.New<User>(objs) });
RunTime.Login?.OnSucceedLoginEvent(new GeneralEventArgs());
return;
}
ShowMessage.ErrorMessage("登录失败!!", "登录失败", 5);
RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
} }
private void SocketHandler_Disconnect(object[] objs) private void SocketHandler_Disconnect(object[] objs)

View File

@ -1,4 +1,5 @@
using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Controller; using Milimoe.FunGame.Desktop.Controller;
using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Base; using Milimoe.FunGame.Desktop.Library.Base;
@ -26,7 +27,7 @@ namespace Milimoe.FunGame.Desktop.UI
UsernameText.Focus(); UsernameText.Focus();
return; return;
} }
password = Core.Api.Utility.Encryption.HmacSha512(password, username); password = password.Encrypt(username);
if (!LoginController.LoginAccount(username, password)) if (!LoginController.LoginAccount(username, password))
ShowMessage.Message("登录失败!!", "登录失败"); ShowMessage.Message("登录失败!!", "登录失败");
} }

View File

@ -173,7 +173,6 @@ namespace Milimoe.FunGame.Desktop.UI
break; break;
case MainSet.LogIn: case MainSet.LogIn:
LoginAccount(objs);
break; break;
case MainSet.LogOut: case MainSet.LogOut:
@ -627,7 +626,8 @@ namespace Milimoe.FunGame.Desktop.UI
Login.Visible = false; Login.Visible = false;
Logout.Visible = true; Logout.Visible = true;
SetServerStatusLight((int)LightType.Green); SetServerStatusLight((int)LightType.Green);
RunTime.Login?.Dispose(); RunTime.Login?.Close();
Thread.Sleep(100);
string welcome = $"欢迎回来, {Usercfg.LoginUserName}"; string welcome = $"欢迎回来, {Usercfg.LoginUserName}";
ShowMessage.Message(welcome, "登录成功", 5); ShowMessage.Message(welcome, "登录成功", 5);
WritelnSystemInfo(welcome); WritelnSystemInfo(welcome);
@ -1170,7 +1170,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// <returns></returns> /// <returns></returns>
public EventResult SucceedConnectEvent(object sender, GeneralEventArgs e) public EventResult SucceedConnectEvent(object sender, GeneralEventArgs e)
{ {
if (MainController != null && Config.FunGame_isAutoLogin) if (MainController != null && Config.FunGame_isAutoLogin && Config.FunGame_AutoLoginUser != "" && Config.FunGame_AutoLoginPassword != "" && Config.FunGame_AutoLoginKey != "")
{ {
// 自动登录 // 自动登录
LoginController.LoginAccount(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey); LoginController.LoginAccount(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey);