TH1/Unity/Assets/Scripts/TH1_Logic/Tools/TH1Serialization.cs

49 lines
1.5 KiB
C#

/*
* @Author: Codex
* @Description: AOT-safe MemoryPack entrypoints for hotfix types
* @Date: 2026年06月11日 星期四 00:00:00
* @Modify:
*/
using System;
using System.Buffers;
using MemoryPack;
namespace TH1_Logic.Tools
{
public static class TH1Serialization
{
public static byte[] Serialize<T>(T value)
{
return MemoryPackSerializer.Serialize(typeof(T), value);
}
public static byte[] Serialize(Type type, object value)
{
if (type == null) throw new ArgumentNullException(nameof(type));
return MemoryPackSerializer.Serialize(type, value);
}
public static void Serialize<T>(IBufferWriter<byte> writer, T value)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
MemoryPackSerializer.Serialize(typeof(T), writer, value);
}
public static T Deserialize<T>(byte[] bytes)
{
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
object value = MemoryPackSerializer.Deserialize(typeof(T), bytes);
return value is T data ? data : default;
}
public static object Deserialize(Type type, byte[] bytes)
{
if (type == null) throw new ArgumentNullException(nameof(type));
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
return MemoryPackSerializer.Deserialize(type, bytes);
}
}
}