更新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 (typeof(T) == typeof(Entity.User))
{
instance = Api.Factory.UserFactory.GetInstance("Mili");
instance = GetUser(objs);
}
else if (typeof(T) == typeof(Skill))
{
@ -90,5 +90,30 @@ namespace Milimoe.FunGame.Core.Api.Utility
return true;
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
};
string data = "getserverping";
byte[] buffer = Encoding.ASCII.GetBytes(data);
byte[] buffer = General.DefaultEncoding.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(addr, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
@ -116,7 +116,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <summary>
/// Json工具类
/// </summary>
public static class JsonUtility
internal static class JsonUtility
{
/// <summary>
/// 将JsonElement转换为泛型
@ -124,10 +124,9 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <typeparam name="T">泛型</typeparam>
/// <param name="element">JsonElement</param>
/// <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>(json);
return JsonSerializer.Deserialize<T>(element.GetRawText());
}
}
@ -222,7 +221,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <summary>
/// 使用HMACSHA512算法加密
/// </summary>
public class Encryption
internal class Encryption
{
/// <summary>
/// 使用HMACSHA512算法加密
@ -230,15 +229,29 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <param name="Message">需要加密的值</param>
/// <param name="Key">秘钥</param>
/// <returns></returns>
public static string HmacSha512(string Message, string Key)
internal static string HmacSha512(string Message, string Key)
{
byte[] MessageBytes = General.DefaultEncoding.GetBytes(Message);
Key = Convert.ToBase64String(General.DefaultEncoding.GetBytes(Key));
byte[] KeyBytes = General.DefaultEncoding.GetBytes(Key);
HMACSHA512 Hmacsha512 = new(KeyBytes);
byte[] Hash = Hmacsha512.ComputeHash(MessageBytes);
string Hamc = BitConverter.ToString(Hash).Replace("-", "");
return Hamc.ToLower();
string Hmac = BitConverter.ToString(Hash).Replace("-", "");
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
{
public static class ExceptionHelper
public static class ExceptionExtension
{
public static string GetErrorInfo(this System.Exception e)
{

View File

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

View File

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

View File

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

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.Library;
using Milimoe.FunGame.Desktop.Library.Base;
@ -26,7 +27,7 @@ namespace Milimoe.FunGame.Desktop.UI
UsernameText.Focus();
return;
}
password = Core.Api.Utility.Encryption.HmacSha512(password, username);
password = password.Encrypt(username);
if (!LoginController.LoginAccount(username, password))
ShowMessage.Message("登录失败!!", "登录失败");
}

View File

@ -173,7 +173,6 @@ namespace Milimoe.FunGame.Desktop.UI
break;
case MainSet.LogIn:
LoginAccount(objs);
break;
case MainSet.LogOut:
@ -627,7 +626,8 @@ namespace Milimoe.FunGame.Desktop.UI
Login.Visible = false;
Logout.Visible = true;
SetServerStatusLight((int)LightType.Green);
RunTime.Login?.Dispose();
RunTime.Login?.Close();
Thread.Sleep(100);
string welcome = $"欢迎回来, {Usercfg.LoginUserName}";
ShowMessage.Message(welcome, "登录成功", 5);
WritelnSystemInfo(welcome);
@ -1170,7 +1170,7 @@ namespace Milimoe.FunGame.Desktop.UI
/// <returns></returns>
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);