(client) feat:添加数据序列化相关代码 (#37)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-07-16 13:05:04 +08:00
committed by TheRedApricot
parent 55cf9d954e
commit ffeb65ba6b
11 changed files with 2407 additions and 25 deletions

View File

@ -0,0 +1,13 @@
using System;
namespace Base
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class NeedSaveAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class UnSaveAttribute : Attribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bb4eb0442fc54e27955bb860d6dc2fd3
timeCreated: 1752635644

View File

@ -343,23 +343,6 @@ namespace Data
// 如果字段的类型直接是基类或其派生类,则返回 true
return fieldType != baseType && baseType.IsAssignableFrom(fieldType);
}
/// <summary>
/// 检查字段的类型是否继承自指定的类
/// </summary>
/// <param name="field">字段信息</param>
/// <param name="baseType">要检查的基类类型</param>
/// <returns>如果字段的类型是基类或其派生类,则返回 true</returns>
public static bool IsFieldTypeInheritedFrom(FieldInfo field, Type baseType)
{
// 获取字段的类型
var fieldType = field.FieldType;
// 如果字段的类型为 null 或不是基类的派生类,则返回 false
if (!baseType.IsAssignableFrom(fieldType))
return false;
// 如果字段的类型直接是基类或其派生类,则返回 true
return fieldType != baseType && baseType.IsAssignableFrom(fieldType);
}
}
}

View File

@ -0,0 +1,18 @@
namespace Entity
{
public class Monster
{
}
public class MonsterAttributes
{
public int health = 10;
public int moveSpeed = 1;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;
public int attackRange = 3;
public int attackTargetCount = 1;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 32995e4c8e0d40dfa39990f0671a3733
timeCreated: 1752576973

View File

@ -0,0 +1,10 @@
namespace Managers
{
public class ArchiveManager
{
public bool Save(string filename)
{
return false;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a6952f5a135a4809bdeb721b4761aa6f
timeCreated: 1752635779

View File

@ -0,0 +1,163 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace Managers
{
/// <summary>
/// 双缓冲静态类,用于逻辑计算线程与主线程之间的数据同步。
/// </summary>
/// <typeparam name="T">要同步的数据类型。</typeparam>
public static class DoubleBuffer<T>
{
/// <summary>
/// 当前用于写入的缓冲区。
/// </summary>
private static T _writeBuffer;
/// <summary>
/// 当前用于读取的缓冲区。
/// </summary>
private static T _readBuffer;
/// <summary>
/// 用于同步缓冲区交换的锁对象。
/// </summary>
private static readonly object _lock = new object();
/// <summary>
/// 初始化双缓冲类。
/// </summary>
/// <param name="initialValue">初始值,用于初始化两个缓冲区。</param>
public static void Initialize(T initialValue)
{
_writeBuffer = initialValue;
_readBuffer = initialValue;
}
/// <summary>
/// 写入数据到当前写缓冲区。
/// </summary>
/// <param name="data">要写入的数据。</param>
public static void Write(T data)
{
lock (_lock)
{
_writeBuffer = data; // 将数据写入写缓冲区
}
}
/// <summary>
/// 交换读写缓冲区,使主线程可以读取最新的数据。
/// </summary>
public static void SwapBuffers()
{
lock (_lock)
{
// 交换读写缓冲区
T temp = _readBuffer;
_readBuffer = _writeBuffer;
_writeBuffer = temp;
}
}
/// <summary>
/// 从当前读缓冲区读取数据。
/// </summary>
/// <returns>当前读缓冲区中的数据。</returns>
public static T Read()
{
lock (_lock)
{
return _readBuffer; // 返回读缓冲区中的数据
}
}
}
/// <summary>
/// 网络消息缓冲区类,用于在网络线程和主线程之间进行线程安全的消息传递。
/// </summary>
public static class NetworkMessageBuffer
{
/// <summary>
/// 缓冲区的最大消息数量。
/// </summary>
private static readonly int MaxMessageCount;
/// <summary>
/// 存储消息的线程安全队列。
/// </summary>
private static readonly ConcurrentQueue<string> MessageQueue;
/// <summary>
/// 用于控制缓冲区是否已满的信号量。
/// </summary>
private static readonly SemaphoreSlim BufferSemaphore;
/// <summary>
/// 初始化网络消息缓冲区类。
/// </summary>
static NetworkMessageBuffer()
{
MaxMessageCount = 100;
MessageQueue = new ConcurrentQueue<string>();
BufferSemaphore = new SemaphoreSlim(MaxMessageCount, MaxMessageCount);
}
/// <summary>
/// 向缓冲区添加一条消息。
/// 如果缓冲区已满,则等待直到有空间可用。
/// </summary>
/// <param name="message">要添加的消息内容。</param>
public static async void AddMessageAsync(string message)
{
// 等待缓冲区有可用空间
await BufferSemaphore.WaitAsync();
try
{
// 将消息添加到队列中
MessageQueue.Enqueue(message);
}
catch (Exception ex)
{
Console.WriteLine($"AddMessageAsync Error: {ex.Message}");
}
}
/// <summary>
/// 从缓冲区中读取一条消息。
/// 如果缓冲区为空,则返回 null。
/// </summary>
/// <returns>缓冲区中的第一条消息,如果缓冲区为空则返回 null。</returns>
public static string ReadMessage()
{
if (MessageQueue.TryDequeue(out string message))
{
// 释放一个缓冲区空间
BufferSemaphore.Release();
return message;
}
return null;
}
/// <summary>
/// 获取当前缓冲区中的消息数量。
/// </summary>
/// <returns>当前缓冲区中的消息数量。</returns>
public static int GetMessageCount()
{
return MessageQueue.Count;
}
/// <summary>
/// 清空缓冲区中的所有消息。
/// </summary>
public static void ClearBuffer()
{
while (MessageQueue.TryDequeue(out _)) { }
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 44d2ac59153c45a4bdab4c6eef6ddcec
timeCreated: 1752585193

File diff suppressed because it is too large Load Diff

View File

@ -49,4 +49,57 @@ message SignupRequest {
message SignupResponse {
RequestResult result = 1;
string message = 2;
}
// 定义 FilePack 消息
message FilePack {
string filePath = 1;
bytes content = 2;
}
// 定义 DataPackListPack 消息
message DataPackListPack {
repeated Pair packIDAndVersion = 1;
// 定义内部的 Pair 消息(用于替代 Tuple<string, string>
message Pair {
string key = 1;
string value = 2;
}
}
// 定义 TileMapTablePack 消息
message TileMapTablePack {
map<int32, string> tileMapKey = 1; // 使用 int32 替代 int
}
// 定义 MapDataPack 消息
message MapDataPack {
Vector2 position = 1;
Vector2 size = 2;
repeated int32 tileMapType = 3; // 使用 int32 替代 int
}
// 定义 MonsterAttributes 消息
message MonsterAttributes {
int32 health = 1;
int32 moveSpeed = 2;
int32 attack = 3;
int32 defense = 4;
int32 attackSpeed = 5;
int32 attackRange = 6;
int32 attackTargetCount = 7;
}
// 定义 MonsterPack 消息
message MonsterPack {
int32 monsterID = 1;
Vector2 position = 2;
MonsterAttributes attributes = 3;
}
// 定义 Vector2 消息
message Vector2 {
float x = 1;
float y = 2;
}