mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-12-05 16:16:33 +00:00
全新json解析方式
This commit is contained in:
parent
231f28541f
commit
3d5aee0a45
@ -10,9 +10,9 @@ namespace Milimoe.FunGame.Core.Api.Factory
|
|||||||
return new User();
|
return new User();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static User GetInstance(DataSet? ds)
|
internal static User GetInstance(DataSet? DataSet)
|
||||||
{
|
{
|
||||||
return new User(ds);
|
return new User(DataSet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -115,34 +115,6 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 将JsonElement转换为泛型
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">泛型</typeparam>
|
|
||||||
/// <param name="obj">为JsonElement的对象</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static T? ConvertJsonObject<T>(object obj)
|
|
||||||
{
|
|
||||||
return ((JsonElement)obj).ToObject<T>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Json工具类
|
|
||||||
/// </summary>
|
|
||||||
internal static class JsonUtility
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 将JsonElement转换为泛型
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">泛型</typeparam>
|
|
||||||
/// <param name="element">JsonElement</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
internal static T? ToObject<T>(this JsonElement element)
|
|
||||||
{
|
|
||||||
return JsonSerializer.Deserialize<T>(element.GetRawText());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
using Milimoe.FunGame.Core.Interface.Entity;
|
using System.Collections;
|
||||||
using System.Collections;
|
using Milimoe.FunGame.Core.Interface.Entity;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Entity
|
namespace Milimoe.FunGame.Core.Entity
|
||||||
{
|
{
|
||||||
public abstract class BaseEntity : IBaseEntity
|
public abstract class BaseEntity : IBaseEntity
|
||||||
{
|
{
|
||||||
public long Id { get; set; } = 0;
|
public virtual long Id { get; set; } = 0;
|
||||||
public Guid Guid { get; set; } = Guid.Empty;
|
public virtual Guid Guid { get; set; } = Guid.Empty;
|
||||||
public string Name { get; set; } = "";
|
public virtual string Name { get; set; } = "";
|
||||||
|
|
||||||
public abstract bool Equals(IBaseEntity? other);
|
public abstract bool Equals(IBaseEntity? other);
|
||||||
public abstract IEnumerator<IBaseEntity> GetEnumerator();
|
public abstract IEnumerator<IBaseEntity> GetEnumerator();
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace Milimoe.FunGame.Core.Entity
|
|||||||
{
|
{
|
||||||
public class User : BaseEntity
|
public class User : BaseEntity
|
||||||
{
|
{
|
||||||
public new long Id { get; set; }
|
public override long Id { get; set; }
|
||||||
public string Username { get; set; } = "";
|
public string Username { get; set; } = "";
|
||||||
public string Password { get; set; } = "";
|
public string Password { get; set; } = "";
|
||||||
public DateTime RegTime { get; set; }
|
public DateTime RegTime { get; set; }
|
||||||
@ -28,11 +28,11 @@ namespace Milimoe.FunGame.Core.Entity
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal User(DataSet? ds)
|
internal User(DataSet? DataSet)
|
||||||
{
|
{
|
||||||
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
|
if (DataSet != null && DataSet.Tables.Count > 0 && DataSet.Tables[0].Rows.Count > 0)
|
||||||
{
|
{
|
||||||
DataRow row = ds.Tables[0].Rows[0];
|
DataRow row = DataSet.Tables[0].Rows[0];
|
||||||
Id = (long)row[UserQuery.Column_UID];
|
Id = (long)row[UserQuery.Column_UID];
|
||||||
Username = (string)row[UserQuery.Column_Username];
|
Username = (string)row[UserQuery.Column_Username];
|
||||||
Password = (string)row[UserQuery.Column_Password];
|
Password = (string)row[UserQuery.Column_Password];
|
||||||
@ -40,12 +40,12 @@ namespace Milimoe.FunGame.Core.Entity
|
|||||||
LastTime = (DateTime)row[UserQuery.Column_LastTime];
|
LastTime = (DateTime)row[UserQuery.Column_LastTime];
|
||||||
Email = (string)row[UserQuery.Column_Email];
|
Email = (string)row[UserQuery.Column_Email];
|
||||||
NickName = (string)row[UserQuery.Column_Nickname];
|
NickName = (string)row[UserQuery.Column_Nickname];
|
||||||
IsAdmin = (int)row[UserQuery.Column_IsAdmin] == 1;
|
IsAdmin = Convert.ToInt32(row[UserQuery.Column_IsAdmin]) == 1;
|
||||||
IsOperator = (int)row[UserQuery.Column_IsOperator] == 1;
|
IsOperator = Convert.ToInt32(row[UserQuery.Column_IsOperator]) == 1;
|
||||||
IsEnable = (int)row[UserQuery.Column_IsEnable] == 1;
|
IsEnable = Convert.ToInt32(row[UserQuery.Column_IsEnable]) == 1;
|
||||||
Credits = (decimal)row[UserQuery.Column_Credits];
|
Credits = Convert.ToDecimal(row[UserQuery.Column_Credits]);
|
||||||
Materials = (decimal)row[UserQuery.Column_Materials];
|
Materials = Convert.ToDecimal(row[UserQuery.Column_Materials]);
|
||||||
GameTime = (decimal)row[UserQuery.Column_GameTime];
|
GameTime = Convert.ToDecimal(row[UserQuery.Column_GameTime]);
|
||||||
AutoKey = (string)row[UserQuery.Column_AutoKey];
|
AutoKey = (string)row[UserQuery.Column_AutoKey];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
|
|
||||||
namespace Milimoe.FunGame.Core.Library.Common.Network
|
namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||||
@ -11,12 +12,21 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
public object[] Parameters { get; }
|
public object[] Parameters { get; }
|
||||||
public string JsonString { get; }
|
public string JsonString { get; }
|
||||||
|
|
||||||
|
private JArray? JArray;
|
||||||
|
|
||||||
public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters)
|
public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters)
|
||||||
{
|
{
|
||||||
this.MessageType = MessageType;
|
this.MessageType = MessageType;
|
||||||
this.Token = Token;
|
this.Token = Token;
|
||||||
this.Parameters = Parameters;
|
this.Parameters = Parameters;
|
||||||
this.JsonString = JsonConvert.SerializeObject(this);
|
this.JsonString = JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T? GetObject<T>(int i)
|
||||||
|
{
|
||||||
|
if (i >= Parameters.Length) throw new IndexOutOfArrayLengthException();
|
||||||
|
JArray ??= JArray.FromObject(Parameters);
|
||||||
|
return JArray[i].ToObject<T>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetString(SocketMessageType MessageType, Guid Token, params object[] Parameters)
|
public static string GetString(SocketMessageType MessageType, Guid Token, params object[] Parameters)
|
||||||
@ -29,4 +39,5 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
return JsonConvert.DeserializeObject<JsonObject>(JsonString);
|
return JsonConvert.DeserializeObject<JsonObject>(JsonString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,13 +8,24 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
public Guid Token { get; } = Guid.Empty;
|
public Guid Token { get; } = Guid.Empty;
|
||||||
public object[] Parameters { get; } = Array.Empty<object>();
|
public object[] Parameters { get; } = Array.Empty<object>();
|
||||||
public int Length { get; } = 0;
|
public int Length { get; } = 0;
|
||||||
|
private JsonObject Json { get; }
|
||||||
|
|
||||||
public SocketObject(SocketMessageType type, Guid token, params object[] parameters)
|
public SocketObject(JsonObject json)
|
||||||
{
|
{
|
||||||
SocketType = type;
|
Json = json;
|
||||||
Token = token;
|
SocketType = Json.MessageType;
|
||||||
Parameters = parameters;
|
Token = Json.Token;
|
||||||
Length = parameters.Length;
|
Parameters = Json.Parameters;
|
||||||
|
Length = Parameters.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SocketObject()
|
||||||
|
{
|
||||||
|
Json = new JsonObject(SocketMessageType.Unknown, Guid.Empty, Array.Empty<object>());
|
||||||
|
SocketType = Json.MessageType;
|
||||||
|
Token = Json.Token;
|
||||||
|
Parameters = Json.Parameters;
|
||||||
|
Length = Parameters.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -24,18 +35,6 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
|||||||
/// <param name="index">索引</param>
|
/// <param name="index">索引</param>
|
||||||
/// <returns>类型的参数</returns>
|
/// <returns>类型的参数</returns>
|
||||||
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
|
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
|
||||||
public T? GetParam<T>(int index)
|
public T? GetParam<T>(int index) => Json.GetObject<T>(index);
|
||||||
{
|
|
||||||
if (index < Parameters.Length)
|
|
||||||
{
|
|
||||||
if (typeof(T) == typeof(Guid))
|
|
||||||
{
|
|
||||||
object param = Guid.Parse((string)Parameters[index]);
|
|
||||||
return (T)param;
|
|
||||||
}
|
|
||||||
return (T)Parameters[index];
|
|
||||||
}
|
|
||||||
throw new IndexOutOfArrayLengthException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -170,7 +170,7 @@ namespace Milimoe.FunGame.Core.Service
|
|||||||
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
|
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
|
||||||
if (json != null)
|
if (json != null)
|
||||||
{
|
{
|
||||||
result = new Library.Common.Network.SocketObject(json.MessageType, json.Token, json.Parameters);
|
result = new Library.Common.Network.SocketObject(json);
|
||||||
}
|
}
|
||||||
// 客户端接收消息,广播ScoketObject到每个UIModel
|
// 客户端接收消息,广播ScoketObject到每个UIModel
|
||||||
OnSocketReceive(result);
|
OnSocketReceive(result);
|
||||||
@ -199,7 +199,7 @@ namespace Milimoe.FunGame.Core.Service
|
|||||||
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
|
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
|
||||||
if (json != null)
|
if (json != null)
|
||||||
{
|
{
|
||||||
result = new Library.Common.Network.SocketObject(json.MessageType, json.Token, json.Parameters);
|
result = new Library.Common.Network.SocketObject(json);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ using Milimoe.FunGame.Core.Library.Common.Event;
|
|||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||||
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.Component;
|
using Milimoe.FunGame.Desktop.Library.Component;
|
||||||
using Milimoe.FunGame.Desktop.Library;
|
using Milimoe.FunGame.Desktop.Library;
|
||||||
using Milimoe.FunGame.Desktop.UI;
|
using Milimoe.FunGame.Desktop.UI;
|
||||||
@ -227,7 +226,7 @@ namespace Milimoe.FunGame.Desktop.Model
|
|||||||
{
|
{
|
||||||
return Socket.Receive();
|
return Socket.Receive();
|
||||||
}
|
}
|
||||||
return new SocketObject(SocketMessageType.Unknown, Guid.Empty, Array.Empty<object>());
|
return new SocketObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SocketMessageType Receiving()
|
private SocketMessageType Receiving()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user