登录框架+自动登录

This commit is contained in:
Mili 2023-02-23 22:15:14 +08:00
parent e4e471e3f1
commit 5a516d7242
10 changed files with 98 additions and 54 deletions

View File

@ -4,15 +4,15 @@ using System.Text;
namespace Milimoe.FunGame.Core.Api.Utility namespace Milimoe.FunGame.Core.Api.Utility
{ {
public class INIHelper public partial class INIHelper
{ {
/* /*
* API函数 * API函数
*/ */
[DllImport("kernel32", CharSet = CharSet.Unicode)] [LibraryImport("kernel32", StringMarshalling = StringMarshalling.Utf16)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); private static partial long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)] [LibraryImport("kernel32", StringMarshalling = StringMarshalling.Utf16)]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); private static partial int GetPrivateProfileString(string section, string key, string def, byte[] val, int size, string filePath);
/// <summary> /// <summary>
/// 写入ini文件 /// 写入ini文件
@ -35,9 +35,10 @@ namespace Milimoe.FunGame.Core.Api.Utility
/// <returns>读取到的值</returns> /// <returns>读取到的值</returns>
public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini") public static string ReadINI(string Section, string Key, string FileName = @"FunGame.ini")
{ {
StringBuilder str = new(256); byte[] val = new byte[1024];
_ = GetPrivateProfileString(Section, Key, "", str, 256, Environment.CurrentDirectory.ToString() + @"\" + FileName); _ = GetPrivateProfileString(Section, Key, "", val, 1024, Environment.CurrentDirectory.ToString() + @"\" + FileName);
return str.ToString(); string? read = val.ToString();
return read ?? "";
} }
/// <summary> /// <summary>

View File

@ -12,6 +12,7 @@
<PackageOutputPath>..\bin</PackageOutputPath> <PackageOutputPath>..\bin</PackageOutputPath>
<Title>FunGame.Core</Title> <Title>FunGame.Core</Title>
<RootNamespace>Milimoe.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace> <RootNamespace>Milimoe.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -1,40 +1,55 @@
using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Interface; using Milimoe.FunGame.Desktop.Library.Interface;
using Milimoe.FunGame.Desktop.Model; using Milimoe.FunGame.Desktop.Model;
using Milimoe.FunGame.Desktop.UI; using Milimoe.FunGame.Desktop.UI;
using System.Windows.Forms;
namespace Milimoe.FunGame.Desktop.Controller namespace Milimoe.FunGame.Desktop.Controller
{ {
public class LoginController : ILogin public class LoginController : ILogin
{ {
private LoginModel LoginModel { get; }
private Login Login { get; } private Login Login { get; }
public LoginController(Login Login) public LoginController(Login Login)
{ {
this.Login = Login; this.Login = Login;
LoginModel = new LoginModel(Login);
} }
public static bool LoginAccount(params object[]? objs) public static bool LoginAccount(params object[]? objs)
{ {
return LoginModel.LoginAccount(objs); RunTime.Login?.OnBeforeLoginEvent(new GeneralEventArgs());
bool result = LoginModel.LoginAccount(objs);
if (!result)
{
RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
}
RunTime.Login?.OnAfterLoginEvent(new GeneralEventArgs());
return result;
} }
public bool LoginAccount(string username, string password) public bool LoginAccount(string username, string password)
{ {
Login.OnBeforeLoginEvent(new GeneralEventArgs()); return LoginController.LoginAccount(username, password);
bool result = LoginModel.LoginAccount(username, password); }
public static bool CheckLogin(params object[]? objs)
{
bool result = LoginModel.CheckLogin(objs);
if (result) if (result)
{ {
Login.OnSucceedLoginEvent(new GeneralEventArgs()); RunTime.Login?.OnSucceedLoginEvent(new GeneralEventArgs());
} }
else else
{ {
Login.OnFailedLoginEvent(new GeneralEventArgs()); RunTime.Login?.OnFailedLoginEvent(new GeneralEventArgs());
} }
Login.OnAfterLoginEvent(new GeneralEventArgs());
return result; return result;
} }
public bool CheckLogin(Guid key)
{
return LoginController.CheckLogin(key);
}
} }
} }

View File

@ -17,6 +17,9 @@
public static string FunGame_Roomid { get; set; } = "-1"; // 房间号 public static string FunGame_Roomid { get; set; } = "-1"; // 房间号
public static string FunGame_ServerName { get; set; } = ""; // 服务器名称 public static string FunGame_ServerName { get; set; } = ""; // 服务器名称
public static string FunGame_Notice { get; set; } = ""; // 公告 public static string FunGame_Notice { get; set; } = ""; // 公告
public static string FunGame_AutoLoginUser { get; set; } = ""; // 自动登录的账号
public static string FunGame_AutoLoginPassword { get; set; } = ""; // 自动登录的密码
public static string FunGame_AutoLoginKey { get; set; } = ""; // 自动登录的秘钥
/*** GUID For Socket ***/ /*** GUID For Socket ***/
public static Guid Guid_Socket { get; set; } = Guid.Empty; public static Guid Guid_Socket { get; set; } = Guid.Empty;

View File

@ -3,5 +3,6 @@
public interface ILogin public interface ILogin
{ {
public bool LoginAccount(string username, string password); public bool LoginAccount(string username, string password);
public bool CheckLogin(Guid key);
} }
} }

View File

@ -4,20 +4,15 @@ using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Interface; using Milimoe.FunGame.Desktop.Library.Interface;
using Milimoe.FunGame.Desktop.UI; using Milimoe.FunGame.Desktop.UI;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace Milimoe.FunGame.Desktop.Model namespace Milimoe.FunGame.Desktop.Model
{ {
public class LoginModel : ILogin /// <summary>
/// 请不要越过Controller直接调用Model中的方法。
/// </summary>
public class LoginModel
{ {
private readonly Login Login;
private Core.Library.Common.Network.Socket? Socket;
public LoginModel(Login login)
{
Login = login;
Socket = RunTime.Socket;
}
public static bool LoginAccount(params object[]? objs) public static bool LoginAccount(params object[]? objs)
{ {
try try
@ -27,9 +22,11 @@ namespace Milimoe.FunGame.Desktop.Model
{ {
string username = ""; string username = "";
string password = ""; string password = "";
string autokey = "";
if (objs.Length > 0) username = (string)objs[0]; if (objs.Length > 0) username = (string)objs[0];
if (objs.Length > 1) password = (string)objs[1]; if (objs.Length > 1) password = (string)objs[1];
if (Socket.Send(SocketMessageType.Login, username, password) == SocketResult.Success) if (objs.Length > 2) autokey = (string)objs[2];
if (Socket.Send(SocketMessageType.Login, username, password, autokey) == SocketResult.Success)
{ {
return true; return true;
} }
@ -42,13 +39,19 @@ namespace Milimoe.FunGame.Desktop.Model
return false; return false;
} }
public bool LoginAccount(string username, string password) public static bool CheckLogin(params object[]? objs)
{ {
try try
{ {
if (LoginModel.LoginAccount(username, password)) Core.Library.Common.Network.Socket? Socket = RunTime.Socket;
if (Socket != null && objs != null)
{ {
return true; Guid key = Guid.Empty;
if (objs.Length > 0) key = (Guid)objs[0];
if (Socket.Send(SocketMessageType.CheckLogin, key) == SocketResult.Success)
{
return true;
}
} }
} }
catch (Exception e) catch (Exception e)

View File

@ -3,6 +3,7 @@ using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Exception; using Milimoe.FunGame.Core.Library.Exception;
using Milimoe.FunGame.Desktop.Controller;
using Milimoe.FunGame.Desktop.Library; using Milimoe.FunGame.Desktop.Library;
using Milimoe.FunGame.Desktop.Library.Component; using Milimoe.FunGame.Desktop.Library.Component;
using Milimoe.FunGame.Desktop.Library.Interface; using Milimoe.FunGame.Desktop.Library.Interface;
@ -263,6 +264,7 @@ namespace Milimoe.FunGame.Desktop.Model
break; break;
case SocketMessageType.Login: case SocketMessageType.Login:
SocketHandler_Login(objs);
break; break;
case SocketMessageType.CheckLogin: case SocketMessageType.CheckLogin:
@ -319,6 +321,14 @@ namespace Milimoe.FunGame.Desktop.Model
if (objs.Length > 0) Config.FunGame_Notice = NetworkUtility.ConvertJsonObject<string>(objs[0])!; if (objs.Length > 0) Config.FunGame_Notice = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
} }
private void SocketHandler_Login(object[] objs)
{
Guid key = Guid.Empty;
// 返回一个Key再发回去给服务器就行了
if (objs.Length > 0) key = NetworkUtility.ConvertJsonObject<Guid>(objs[0])!;
LoginController.CheckLogin(key);
}
private void SocketHandler_CheckLogin(object[] objs) private void SocketHandler_CheckLogin(object[] objs)
{ {
string msg = ""; string msg = "";

View File

@ -11,12 +11,10 @@ namespace Milimoe.FunGame.Desktop.UI
public partial class Login : BaseLogin public partial class Login : BaseLogin
{ {
private LoginController LoginController; private LoginController LoginController;
private Main Main;
public Login(Main Main) public Login()
{ {
InitializeComponent(); InitializeComponent();
this.Main = Main;
LoginController = new LoginController(this); LoginController = new LoginController(this);
} }
@ -33,11 +31,8 @@ namespace Milimoe.FunGame.Desktop.UI
return; return;
} }
password = Core.Api.Utility.Encryption.HmacSha512(password, username); password = Core.Api.Utility.Encryption.HmacSha512(password, username);
if (LoginController.LoginAccount(username, password)) if (!LoginController.LoginAccount(username, password))
{ ShowMessage.Message("登录失败!!", "登录失败");
Main.UpdateUI(MainSet.LogIn, new object[] { Core.Api.Utility.Factory.NewSingle<User>(username, password) });
}
else ShowMessage.Message("登录失败!!", "登录失败");
} }
catch (Exception e) catch (Exception e)
{ {

View File

@ -269,14 +269,31 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
if (INIHelper.ExistINIFile()) if (INIHelper.ExistINIFile())
{ {
string isAutoConncet = INIHelper.ReadINI("Config", "AutoConnect"); string isAutoConnect = INIHelper.ReadINI("Config", "AutoConnect");
string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin"); string isAutoLogin = INIHelper.ReadINI("Config", "AutoLogin");
if (isAutoConncet != null && !isAutoConncet.Equals("") && (isAutoConncet.Equals("false") || isAutoConncet.Equals("true"))) string strUserName = INIHelper.ReadINI("Config", "UserName");
Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConncet); string strPassword = INIHelper.ReadINI("Config", "Password");
string strAutoKey = INIHelper.ReadINI("Config", "AutoKey");
if (isAutoConnect != null && isAutoConnect.Trim() != "" && (isAutoConnect.ToLower().Equals("false") || isAutoConnect.ToLower().Equals("true")))
Config.FunGame_isAutoConnect = Convert.ToBoolean(isAutoConnect);
else throw new ReadConfigException(); else throw new ReadConfigException();
if (isAutoLogin != null && !isAutoLogin.Equals("") && (isAutoLogin.Equals("false") || isAutoLogin.Equals("true")))
if (isAutoLogin != null && isAutoLogin.Trim() != "" && (isAutoLogin.ToLower().Equals("false") || isAutoLogin.ToLower().Equals("true")))
Config.FunGame_isAutoLogin = Convert.ToBoolean(isAutoLogin); Config.FunGame_isAutoLogin = Convert.ToBoolean(isAutoLogin);
else throw new ReadConfigException(); else throw new ReadConfigException();
if (strUserName != null && strUserName.Trim() != "")
Config.FunGame_AutoLoginUser = strUserName.Trim();
else throw new ReadConfigException();
if (strPassword != null && strPassword.Trim() != "")
Config.FunGame_AutoLoginPassword = strPassword.Trim();
else throw new ReadConfigException();
if (strAutoKey != null && strAutoKey.Trim() != "")
Config.FunGame_AutoLoginKey = strAutoKey.Trim();
else throw new ReadConfigException();
} }
else else
{ {
@ -613,7 +630,10 @@ 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);
ShowMessage.Message($"欢迎回来,{Usercfg.LoginUserName}", "登录成功", 5); RunTime.Login?.Dispose();
string welcome = $"欢迎回来, {Usercfg.LoginUserName}";
ShowMessage.Message(welcome, "登录成功", 5);
WritelnSystemInfo(welcome);
} }
/// <summary> /// <summary>
@ -944,7 +964,7 @@ namespace Milimoe.FunGame.Desktop.UI
private void Login_Click(object sender, EventArgs e) private void Login_Click(object sender, EventArgs e)
{ {
if (MainController != null && Config.FunGame_isConnected) if (MainController != null && Config.FunGame_isConnected)
OpenForm.SingleForm(FormType.Login, OpenFormType.Dialog, this); OpenForm.SingleForm(FormType.Login, OpenFormType.Dialog);
else else
ShowMessage.WarningMessage("请先连接服务器!"); ShowMessage.WarningMessage("请先连接服务器!");
} }
@ -1155,8 +1175,8 @@ namespace Milimoe.FunGame.Desktop.UI
{ {
if (MainController != null && Config.FunGame_isAutoLogin) if (MainController != null && Config.FunGame_isAutoLogin)
{ {
// 自动登录 [TODO] // 自动登录
LoginController.LoginAccount(Config.FunGame_AutoLoginUser, Config.FunGame_AutoLoginPassword, Config.FunGame_AutoLoginKey);
} }
return EventResult.Success; return EventResult.Success;
} }

View File

@ -29,14 +29,9 @@ namespace Milimoe.FunGame.Desktop.Utility
RunTime.Register = (Register)form; RunTime.Register = (Register)form;
break; break;
case FormType.Login: case FormType.Login:
Main? main = default; form = new Login();
if (objs != null && objs.Length > 0) main = (Main)objs[0]; IsExist = RunTime.Login != null;
if (main != null) RunTime.Login = (Login)form;
{
form = new Login(main);
IsExist = RunTime.Login != null;
RunTime.Login = (Login)form;
}
break; break;
case FormType.Inventory: case FormType.Inventory:
form = new InventoryUI(); form = new InventoryUI();