mirror of
https://github.com/kwsch/pkNX.git
synced 2026-08-26 04:20:28 -05:00
File scoped namespaces for all lib projects netstandard2.0 => net6; now uniform. bye netframework!
52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using FlatSharp;
|
|
|
|
namespace pkNX.Structures.FlatBuffers;
|
|
|
|
public static class FlatBufferConverter
|
|
{
|
|
public static T[] DeserializeFrom<T>(string[] files) where T : class
|
|
{
|
|
var result = new T[files.Length];
|
|
for (int i = 0; i < result.Length; i++)
|
|
{
|
|
var file = files[i];
|
|
result[i] = DeserializeFrom<T>(file);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static byte[][] SerializeFrom<T>(T[] obj) where T : class
|
|
{
|
|
var result = new byte[obj.Length][];
|
|
for (int i = 0; i < result.Length; i++)
|
|
{
|
|
var file = obj[i];
|
|
result[i] = SerializeFrom(file);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static T DeserializeFrom<T>(string file) where T : class
|
|
{
|
|
var data = File.ReadAllBytes(file);
|
|
return DeserializeFrom<T>(data);
|
|
}
|
|
|
|
public static T DeserializeFrom<T>(byte[] data) where T : class
|
|
{
|
|
return FlatBufferSerializer.Default.Parse<T>(data);
|
|
}
|
|
|
|
public static byte[] SerializeFrom<T>(T obj) where T : class
|
|
{
|
|
var size = FlatBufferSerializer.Default.GetMaxSize(obj);
|
|
var data = new byte[size];
|
|
var result = FlatBufferSerializer.Default.Serialize(obj, data);
|
|
if (result != data.Length)
|
|
Array.Resize(ref data, result);
|
|
return data;
|
|
}
|
|
}
|