mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
WIP
This commit is contained in:
parent
0dc18334ac
commit
e4ecaa882c
9
Wabbajack.Common/Serialization/Deserializer.cs
Normal file
9
Wabbajack.Common/Serialization/Deserializer.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Wabbajack.Common.Serialization
|
||||
{
|
||||
public class Deserializer
|
||||
{
|
||||
public BinaryReader Reader { get; }
|
||||
}
|
||||
}
|
9
Wabbajack.Common/Serialization/IHandler.cs
Normal file
9
Wabbajack.Common/Serialization/IHandler.cs
Normal 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);
|
||||
|
||||
}
|
||||
}
|
34
Wabbajack.Common/Serialization/PrimitiveHandlers.cs
Normal file
34
Wabbajack.Common/Serialization/PrimitiveHandlers.cs
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
34
Wabbajack.Common/Serialization/PrimitiveHandlers.tt
Normal file
34
Wabbajack.Common/Serialization/PrimitiveHandlers.tt
Normal 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#>();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
<# } #>
|
||||
|
||||
}
|
53
Wabbajack.Common/Serialization/Serializer.cs
Normal file
53
Wabbajack.Common/Serialization/Serializer.cs
Normal 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;
|
||||
|
||||
}
|
||||
}
|
@ -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>
|
Loading…
Reference in New Issue
Block a user