This commit is contained in:
Timothy Baldridge 2020-03-20 17:59:55 -06:00
parent 0dc18334ac
commit e4ecaa882c
6 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,9 @@
using System.IO;
namespace Wabbajack.Common.Serialization
{
public class Deserializer
{
public BinaryReader Reader { get; }
}
}

View File

@ -0,0 +1,9 @@
namespace Wabbajack.Common.Serialization
{
public interface IHandler
{
public void Write<T>(Serializer serializer, T data);
public T Read<T>(Deserializer deserialiser);
}
}

View File

@ -0,0 +1,34 @@
using System;
namespace Wabbajack.Common.Serialization {
public class UInt32Handler : IHandler {
public void Write<T>(Serializer serializer, UInt32 data)
{
serializer.Writer.Write(data);
}
public T Read<T>(Deserializer deserializer)
{
return deserializer.Reader.ReadUInt32();
}
}
public class Int32Handler : IHandler {
public void Write<T>(Serializer serializer, Int32 data)
{
serializer.Writer.Write(data);
}
public T Read<T>(Deserializer deserializer)
{
return deserializer.Reader.ReadInt32();
}
}
}

View File

@ -0,0 +1,34 @@
<#@ template language="C#" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
using System;
namespace Wabbajack.Common.Serialization {
<#
var types = new List<(Type, string)>()
{
(typeof(UInt32), "UInt32"),
(typeof(Int32), "Int32")
};
foreach (var type in types)
{
#>
public class <#=type.Item2#>Handler : IHandler {
public void Write<T>(Serializer serializer, <#=type.Item2#> data)
{
serializer.Writer.Write(data);
}
public T Read<T>(Deserializer deserializer)
{
return deserializer.Reader.Read<#=type.Item2#>();
}
}
<# } #>
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Wabbajack.Common.Serialization
{
public class Serializer
{
private Dictionary<string, int> _internedStrings = new Dictionary<string, int>();
private Dictionary<Type, HandlerRecord> _handlers = new Dictionary<Type, HandlerRecord>();
public BinaryWriter Writer { get; }
public Serializer(BinaryWriter bw)
{
Writer = bw;
}
public void RegisterWriteHandler<T>(string name, IHandler handler)
{
_handlers.Add(typeof(T), new HandlerRecord
{
TypeName = name,
TypeId = Intern(name),
Handler = handler
});
}
public async Task Write<T>(BinaryWriter bw, T data)
{
var handler = _handlers[typeof(T)];
handler.Handler.Write<T>(this, data);
}
private int Intern(string s)
{
if (_internedStrings.TryGetValue(s, out var idx))
return idx;
idx = _internedStrings.Count;
_internedStrings[s] = idx;
return idx;
}
}
class HandlerRecord
{
public string TypeName;
public int TypeId;
public IHandler Handler;
}
}

View File

@ -16,6 +16,10 @@
<None Update="7Zip\7z.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Serialization\PrimitiveHandlers.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>PrimitiveHandlers.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="KnownFolders\" />
@ -39,4 +43,9 @@
<ItemGroup>
<ProjectReference Include="..\Wabbajack.Common.CSP\Wabbajack.Common.CSP.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Serialization\PrimitiveHandlers.cs">
<DependentUpon>PrimitiveHandlers.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>