milimoe 59253948cb
为服务器添加插件 (#94)
* 添加 WebAPIPlugin 和 Loader

* 添加 接收服务器控制台的输入

* 添加 ServerPlugin,添加控制器

* 更新 SocketObejct
2024-10-15 20:13:48 +08:00

52 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text.Json.Serialization;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
[Serializable]
public readonly struct SocketObject
{
public SocketMessageType SocketType { get; } = SocketMessageType.Unknown;
public Guid Token { get; } = Guid.Empty;
public object[] Parameters { get; } = [];
[JsonIgnore]
public int Length => Parameters.Length;
// 从参数列表中获取指定索引的参数的Json字符串
// -- 此索引器仅返回Json字符串对象类型请使用反序列化方法GetParam<T>() --
// -- 当然也可以自己反序列化 --
// -- 基本类型可能有效,但仍建议使用反序列化方法 --
public object? this[int index]
{
get
{
if (index >= Parameters.Length) throw new IndexOutOfArrayLengthException();
object? obj = Parameters[index];
return JsonManager.GetObject(obj.ToString() ?? "");
}
}
[JsonConstructor]
public SocketObject(SocketMessageType socketType, Guid token, params object[] parameters)
{
SocketType = socketType;
Token = token;
if (parameters != null && parameters.Length > 0) Parameters = parameters;
}
/// <summary>
/// 从参数列表中获取指定类型和索引的参数
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="index">索引</param>
/// <returns>类型的参数</returns>
/// <exception cref="IndexOutOfArrayLengthException">索引超过数组上限</exception>
public T? GetParam<T>(int index)
{
return JsonManager.GetObject<T>(this, index);
}
}
}