Initial Commit (broken due to version update)
This commit is contained in:
parent
74003df84b
commit
3f7b3b7440
3132 changed files with 1103305 additions and 0 deletions
75
Assets/Scripts/MessagePack/Attributes.cs
Normal file
75
Assets/Scripts/MessagePack/Attributes.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
|
||||
public class MessagePackObjectAttribute : Attribute
|
||||
{
|
||||
public bool KeyAsPropertyName { get; private set; }
|
||||
|
||||
public MessagePackObjectAttribute(bool keyAsPropertyName = false)
|
||||
{
|
||||
this.KeyAsPropertyName = keyAsPropertyName;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
|
||||
public class KeyAttribute : Attribute
|
||||
{
|
||||
public int? IntKey { get; private set; }
|
||||
public string StringKey { get; private set; }
|
||||
|
||||
public KeyAttribute(int x)
|
||||
{
|
||||
this.IntKey = x;
|
||||
}
|
||||
|
||||
public KeyAttribute(string x)
|
||||
{
|
||||
this.StringKey = x;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
|
||||
public class IgnoreMemberAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
|
||||
public class UnionAttribute : Attribute
|
||||
{
|
||||
public int Key { get; private set; }
|
||||
public Type SubType { get; private set; }
|
||||
|
||||
public UnionAttribute(int key, Type subType)
|
||||
{
|
||||
this.Key = key;
|
||||
this.SubType = subType;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = true)]
|
||||
public class SerializationConstructorAttribute : Attribute
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
|
||||
public class MessagePackFormatterAttribute : Attribute
|
||||
{
|
||||
public Type FormatterType { get; private set; }
|
||||
public object[] Arguments { get; private set; }
|
||||
|
||||
public MessagePackFormatterAttribute(Type formatterType)
|
||||
{
|
||||
this.FormatterType = formatterType;
|
||||
}
|
||||
|
||||
public MessagePackFormatterAttribute(Type formatterType, params object[] arguments)
|
||||
{
|
||||
this.FormatterType = formatterType;
|
||||
this.Arguments = arguments;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/Attributes.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Attributes.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2c523d28e0e7e4f44842116a01f36ae3
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
118
Assets/Scripts/MessagePack/FloatBits.cs
Normal file
118
Assets/Scripts/MessagePack/FloatBits.cs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
// safe accessor of Single/Double's underlying byte.
|
||||
// This code is borrowed from MsgPack-Cli https://github.com/msgpack/msgpack-cli
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal struct Float32Bits
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public readonly float Value;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public readonly Byte Byte0;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly Byte Byte1;
|
||||
|
||||
[FieldOffset(2)]
|
||||
public readonly Byte Byte2;
|
||||
|
||||
[FieldOffset(3)]
|
||||
public readonly Byte Byte3;
|
||||
|
||||
public Float32Bits(float value)
|
||||
{
|
||||
this = default(Float32Bits);
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
public Float32Bits(byte[] bigEndianBytes, int offset)
|
||||
{
|
||||
this = default(Float32Bits);
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
this.Byte0 = bigEndianBytes[offset + 3];
|
||||
this.Byte1 = bigEndianBytes[offset + 2];
|
||||
this.Byte2 = bigEndianBytes[offset + 1];
|
||||
this.Byte3 = bigEndianBytes[offset];
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Byte0 = bigEndianBytes[offset];
|
||||
this.Byte1 = bigEndianBytes[offset + 1];
|
||||
this.Byte2 = bigEndianBytes[offset + 2];
|
||||
this.Byte3 = bigEndianBytes[offset + 3];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal struct Float64Bits
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public readonly double Value;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public readonly Byte Byte0;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly Byte Byte1;
|
||||
|
||||
[FieldOffset(2)]
|
||||
public readonly Byte Byte2;
|
||||
|
||||
[FieldOffset(3)]
|
||||
public readonly Byte Byte3;
|
||||
|
||||
[FieldOffset(4)]
|
||||
public readonly Byte Byte4;
|
||||
|
||||
[FieldOffset(5)]
|
||||
public readonly Byte Byte5;
|
||||
|
||||
[FieldOffset(6)]
|
||||
public readonly Byte Byte6;
|
||||
|
||||
[FieldOffset(7)]
|
||||
public readonly Byte Byte7;
|
||||
|
||||
public Float64Bits(double value)
|
||||
{
|
||||
this = default(Float64Bits);
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
public Float64Bits(byte[] bigEndianBytes, int offset)
|
||||
{
|
||||
this = default(Float64Bits);
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
this.Byte0 = bigEndianBytes[offset + 7];
|
||||
this.Byte1 = bigEndianBytes[offset + 6];
|
||||
this.Byte2 = bigEndianBytes[offset + 5];
|
||||
this.Byte3 = bigEndianBytes[offset + 4];
|
||||
this.Byte4 = bigEndianBytes[offset + 3];
|
||||
this.Byte5 = bigEndianBytes[offset + 2];
|
||||
this.Byte6 = bigEndianBytes[offset + 1];
|
||||
this.Byte7 = bigEndianBytes[offset];
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Byte0 = bigEndianBytes[offset];
|
||||
this.Byte1 = bigEndianBytes[offset + 1];
|
||||
this.Byte2 = bigEndianBytes[offset + 2];
|
||||
this.Byte3 = bigEndianBytes[offset + 3];
|
||||
this.Byte4 = bigEndianBytes[offset + 4];
|
||||
this.Byte5 = bigEndianBytes[offset + 5];
|
||||
this.Byte6 = bigEndianBytes[offset + 6];
|
||||
this.Byte7 = bigEndianBytes[offset + 7];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/FloatBits.cs.meta
Normal file
12
Assets/Scripts/MessagePack/FloatBits.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 345a76b4016103543ab8445c750365c4
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/Scripts/MessagePack/Formatters.meta
Normal file
9
Assets/Scripts/MessagePack/Formatters.meta
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 419e470fd6919bd468e25bcf581e081b
|
||||
folderAsset: yes
|
||||
timeCreated: 1487501809
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1072
Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs
Normal file
1072
Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 882ec8ca76f574f4d86e8e1fa98fbff2
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
449
Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs
Normal file
449
Assets/Scripts/MessagePack/Formatters/DictionaryFormatter.cs
Normal file
|
|
@ -0,0 +1,449 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
#if NETSTANDARD
|
||||
using System.Collections.Concurrent;
|
||||
#endif
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
#if NETSTANDARD
|
||||
|
||||
// unfortunately, can't use IDictionary<KVP> because supports IReadOnlyDictionary.
|
||||
public abstract class DictionaryFormatterBase<TKey, TValue, TIntermediate, TEnumerator, TDictionary> : IMessagePackFormatter<TDictionary>
|
||||
where TDictionary : IEnumerable<KeyValuePair<TKey, TValue>>
|
||||
where TEnumerator : IEnumerator<KeyValuePair<TKey, TValue>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, TDictionary value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
|
||||
|
||||
int count;
|
||||
{
|
||||
var col = value as ICollection<KeyValuePair<TKey, TValue>>;
|
||||
if (col != null)
|
||||
{
|
||||
count = col.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
var col2 = value as IReadOnlyCollection<KeyValuePair<TKey, TValue>>;
|
||||
if (col2 != null)
|
||||
{
|
||||
count = col2.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("DictionaryFormatterBase's TDictionary supports only ICollection<KVP> or IReadOnlyCollection<KVP>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count);
|
||||
|
||||
var e = GetSourceEnumerator(value);
|
||||
try
|
||||
{
|
||||
while (e.MoveNext())
|
||||
{
|
||||
var item = e.Current;
|
||||
offset += keyFormatter.Serialize(ref bytes, offset, item.Key, formatterResolver);
|
||||
offset += valueFormatter.Serialize(ref bytes, offset, item.Value, formatterResolver);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
e.Dispose();
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return default(TDictionary);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
|
||||
|
||||
var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var dict = Create(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = valueFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
Add(dict, i, key, value);
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return Complete(dict);
|
||||
}
|
||||
}
|
||||
|
||||
// abstraction for serialize
|
||||
|
||||
// Some collections can use struct iterator, this is optimization path
|
||||
protected abstract TEnumerator GetSourceEnumerator(TDictionary source);
|
||||
|
||||
// abstraction for deserialize
|
||||
protected abstract TIntermediate Create(int count);
|
||||
protected abstract void Add(TIntermediate collection, int index, TKey key, TValue value);
|
||||
protected abstract TDictionary Complete(TIntermediate intermediateCollection);
|
||||
}
|
||||
|
||||
public abstract class DictionaryFormatterBase<TKey, TValue, TIntermediate, TDictionary> : DictionaryFormatterBase<TKey, TValue, TIntermediate, IEnumerator<KeyValuePair<TKey, TValue>>, TDictionary>
|
||||
where TDictionary : IEnumerable<KeyValuePair<TKey, TValue>>
|
||||
{
|
||||
protected override IEnumerator<KeyValuePair<TKey, TValue>> GetSourceEnumerator(TDictionary source)
|
||||
{
|
||||
return source.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class DictionaryFormatterBase<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, TDictionary, TDictionary>
|
||||
where TDictionary : IDictionary<TKey, TValue>
|
||||
{
|
||||
protected override TDictionary Complete(TDictionary intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class DictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, Dictionary<TKey, TValue>.Enumerator, Dictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new Dictionary<TKey, TValue>(count);
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue>.Enumerator GetSourceEnumerator(Dictionary<TKey, TValue> source)
|
||||
{
|
||||
return source.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class GenericDictionaryFormatter<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, TDictionary>
|
||||
where TDictionary : IDictionary<TKey, TValue>, new()
|
||||
{
|
||||
protected override void Add(TDictionary collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override TDictionary Create(int count)
|
||||
{
|
||||
return new TDictionary();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class InterfaceDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new Dictionary<TKey, TValue>(count);
|
||||
}
|
||||
|
||||
protected override IDictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SortedListFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedList<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(SortedList<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override SortedList<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new SortedList<TKey, TValue>(count);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SortedDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedDictionary<TKey, TValue>, SortedDictionary<TKey, TValue>.Enumerator, SortedDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(SortedDictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override SortedDictionary<TKey, TValue> Complete(SortedDictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override SortedDictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new SortedDictionary<TKey, TValue>();
|
||||
}
|
||||
|
||||
protected override SortedDictionary<TKey, TValue>.Enumerator GetSourceEnumerator(SortedDictionary<TKey, TValue> source)
|
||||
{
|
||||
return source.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, ReadOnlyDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override ReadOnlyDictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return new ReadOnlyDictionary<TKey, TValue>(intermediateCollection);
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new Dictionary<TKey, TValue>(count);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class InterfaceReadOnlyDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override IReadOnlyDictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new Dictionary<TKey, TValue>(count);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ConcurrentDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(ConcurrentDictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.TryAdd(key, value);
|
||||
}
|
||||
|
||||
protected override ConcurrentDictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
// concurrent dictionary can't access defaultConcurrecyLevel so does not use count overload.
|
||||
return new ConcurrentDictionary<TKey, TValue>();
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
public abstract class DictionaryFormatterBase<TKey, TValue, TIntermediate, TDictionary> : IMessagePackFormatter<TDictionary>
|
||||
where TDictionary : IDictionary<TKey, TValue>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, TDictionary value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
|
||||
|
||||
var count = value.Count;
|
||||
|
||||
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count);
|
||||
|
||||
var e = value.GetEnumerator();
|
||||
try
|
||||
{
|
||||
while (e.MoveNext())
|
||||
{
|
||||
var item = e.Current;
|
||||
offset += keyFormatter.Serialize(ref bytes, offset, item.Key, formatterResolver);
|
||||
offset += valueFormatter.Serialize(ref bytes, offset, item.Value, formatterResolver);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
e.Dispose();
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public TDictionary Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return default(TDictionary);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>();
|
||||
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
|
||||
|
||||
var len = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var dict = Create(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var key = keyFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = valueFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
Add(dict, i, key, value);
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return Complete(dict);
|
||||
}
|
||||
}
|
||||
|
||||
// abstraction for deserialize
|
||||
protected abstract TIntermediate Create(int count);
|
||||
protected abstract void Add(TIntermediate collection, int index, TKey key, TValue value);
|
||||
protected abstract TDictionary Complete(TIntermediate intermediateCollection);
|
||||
}
|
||||
|
||||
public sealed class DictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, Dictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new Dictionary<TKey, TValue>(count);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class GenericDictionaryFormatter<TKey, TValue, TDictionary> : DictionaryFormatterBase<TKey, TValue, TDictionary, TDictionary>
|
||||
where TDictionary : IDictionary<TKey, TValue>, new()
|
||||
{
|
||||
protected override void Add(TDictionary collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override TDictionary Complete(TDictionary intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override TDictionary Create(int count)
|
||||
{
|
||||
return new TDictionary();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class InterfaceDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, Dictionary<TKey, TValue>, IDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(Dictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override Dictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new Dictionary<TKey, TValue>(count);
|
||||
}
|
||||
|
||||
protected override IDictionary<TKey, TValue> Complete(Dictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SortedListFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedList<TKey, TValue>, SortedList<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(SortedList<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override SortedList<TKey, TValue> Complete(SortedList<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override SortedList<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new SortedList<TKey, TValue>(count);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SortedDictionaryFormatter<TKey, TValue> : DictionaryFormatterBase<TKey, TValue, SortedDictionary<TKey, TValue>, SortedDictionary<TKey, TValue>>
|
||||
{
|
||||
protected override void Add(SortedDictionary<TKey, TValue> collection, int index, TKey key, TValue value)
|
||||
{
|
||||
collection.Add(key, value);
|
||||
}
|
||||
|
||||
protected override SortedDictionary<TKey, TValue> Complete(SortedDictionary<TKey, TValue> intermediateCollection)
|
||||
{
|
||||
return intermediateCollection;
|
||||
}
|
||||
|
||||
protected override SortedDictionary<TKey, TValue> Create(int count)
|
||||
{
|
||||
return new SortedDictionary<TKey, TValue>();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7a4ff3e539e0f104fbacfc340c297fb6
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using MessagePack.Resolvers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class DynamicObjectTypeFallbackFormatter : IMessagePackFormatter<object>
|
||||
{
|
||||
delegate int SerializeMethod(object dynamicFormatter, ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver);
|
||||
|
||||
readonly MessagePack.Internal.ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>> serializers = new Internal.ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>>();
|
||||
|
||||
readonly IFormatterResolver[] innerResolvers;
|
||||
|
||||
public DynamicObjectTypeFallbackFormatter(params IFormatterResolver[] innerResolvers)
|
||||
{
|
||||
this.innerResolvers = innerResolvers;
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
var type = value.GetType();
|
||||
var ti = type.GetTypeInfo();
|
||||
|
||||
if (type == typeof(object))
|
||||
{
|
||||
// serialize to empty map
|
||||
return MessagePackBinary.WriteMapHeader(ref bytes, offset, 0);
|
||||
}
|
||||
|
||||
KeyValuePair<object, SerializeMethod> formatterAndDelegate;
|
||||
if (!serializers.TryGetValue(type, out formatterAndDelegate))
|
||||
{
|
||||
lock (serializers)
|
||||
{
|
||||
if (!serializers.TryGetValue(type, out formatterAndDelegate))
|
||||
{
|
||||
object formatter = null;
|
||||
foreach (var innerResolver in innerResolvers)
|
||||
{
|
||||
formatter = innerResolver.GetFormatterDynamic(type);
|
||||
if (formatter != null) break;
|
||||
}
|
||||
if (formatter == null)
|
||||
{
|
||||
throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolvers:" + string.Join(", ", innerResolvers.Select(x => x.GetType().Name).ToArray()));
|
||||
}
|
||||
|
||||
var t = type;
|
||||
{
|
||||
var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(t);
|
||||
var param0 = Expression.Parameter(typeof(object), "formatter");
|
||||
var param1 = Expression.Parameter(typeof(byte[]).MakeByRefType(), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(int), "offset");
|
||||
var param3 = Expression.Parameter(typeof(object), "value");
|
||||
var param4 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");
|
||||
|
||||
var serializeMethodInfo = formatterType.GetRuntimeMethod("Serialize", new[] { typeof(byte[]).MakeByRefType(), typeof(int), t, typeof(IFormatterResolver) });
|
||||
|
||||
var body = Expression.Call(
|
||||
Expression.Convert(param0, formatterType),
|
||||
serializeMethodInfo,
|
||||
param1,
|
||||
param2,
|
||||
ti.IsValueType ? Expression.Unbox(param3, t) : Expression.Convert(param3, t),
|
||||
param4);
|
||||
|
||||
var lambda = Expression.Lambda<SerializeMethod>(body, param0, param1, param2, param3, param4).Compile();
|
||||
|
||||
formatterAndDelegate = new KeyValuePair<object, SerializeMethod>(formatter, lambda);
|
||||
}
|
||||
|
||||
serializers.TryAdd(t, formatterAndDelegate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return formatterAndDelegate.Value(formatterAndDelegate.Key, ref bytes, offset, value, formatterResolver);
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return PrimitiveObjectFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a5f7b1732c30ff94ab3db3e7a130f91a
|
||||
timeCreated: 1498137502
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
// Note:This implemenataion is 'not' fastest, should more improve.
|
||||
public sealed class EnumAsStringFormatter<T> : IMessagePackFormatter<T>
|
||||
{
|
||||
readonly Dictionary<string, T> nameValueMapping;
|
||||
readonly Dictionary<T, string> valueNameMapping;
|
||||
|
||||
public EnumAsStringFormatter()
|
||||
{
|
||||
var names = Enum.GetNames(typeof(T));
|
||||
var values = Enum.GetValues(typeof(T));
|
||||
|
||||
nameValueMapping = new Dictionary<string, T>(names.Length);
|
||||
valueNameMapping = new Dictionary<T, string>(names.Length);
|
||||
|
||||
for (int i = 0; i < names.Length; i++)
|
||||
{
|
||||
nameValueMapping[names[i]] = (T)values.GetValue(i);
|
||||
valueNameMapping[(T)values.GetValue(i)] = names[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
string name;
|
||||
if (!valueNameMapping.TryGetValue(value, out name))
|
||||
{
|
||||
name = value.ToString(); // fallback for flags etc, But Enum.ToString is too slow.
|
||||
}
|
||||
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, name);
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var name = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
|
||||
T value;
|
||||
if (!nameValueMapping.TryGetValue(name, out value))
|
||||
{
|
||||
value = (T)Enum.Parse(typeof(T), name); // Enum.Parse is too slow
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 78dc03e88b6b9c5479a3f2cd8aab369c
|
||||
timeCreated: 1489559017
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,801 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class ForceInt16BlockFormatter : IMessagePackFormatter<Int16>
|
||||
{
|
||||
public static readonly ForceInt16BlockFormatter Instance = new ForceInt16BlockFormatter();
|
||||
|
||||
ForceInt16BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int16 value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceInt16BlockFormatter : IMessagePackFormatter<Int16?>
|
||||
{
|
||||
public static readonly NullableForceInt16BlockFormatter Instance = new NullableForceInt16BlockFormatter();
|
||||
|
||||
NullableForceInt16BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int16? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public Int16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceInt16BlockArrayFormatter : IMessagePackFormatter<Int16[]>
|
||||
{
|
||||
public static readonly ForceInt16BlockArrayFormatter Instance = new ForceInt16BlockArrayFormatter();
|
||||
|
||||
ForceInt16BlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int16[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Int16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new Int16[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceInt32BlockFormatter : IMessagePackFormatter<Int32>
|
||||
{
|
||||
public static readonly ForceInt32BlockFormatter Instance = new ForceInt32BlockFormatter();
|
||||
|
||||
ForceInt32BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int32 value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceInt32BlockFormatter : IMessagePackFormatter<Int32?>
|
||||
{
|
||||
public static readonly NullableForceInt32BlockFormatter Instance = new NullableForceInt32BlockFormatter();
|
||||
|
||||
NullableForceInt32BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int32? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public Int32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceInt32BlockArrayFormatter : IMessagePackFormatter<Int32[]>
|
||||
{
|
||||
public static readonly ForceInt32BlockArrayFormatter Instance = new ForceInt32BlockArrayFormatter();
|
||||
|
||||
ForceInt32BlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int32[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Int32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new Int32[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceInt64BlockFormatter : IMessagePackFormatter<Int64>
|
||||
{
|
||||
public static readonly ForceInt64BlockFormatter Instance = new ForceInt64BlockFormatter();
|
||||
|
||||
ForceInt64BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int64 value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Int64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceInt64BlockFormatter : IMessagePackFormatter<Int64?>
|
||||
{
|
||||
public static readonly NullableForceInt64BlockFormatter Instance = new NullableForceInt64BlockFormatter();
|
||||
|
||||
NullableForceInt64BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int64? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public Int64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceInt64BlockArrayFormatter : IMessagePackFormatter<Int64[]>
|
||||
{
|
||||
public static readonly ForceInt64BlockArrayFormatter Instance = new ForceInt64BlockArrayFormatter();
|
||||
|
||||
ForceInt64BlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Int64[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Int64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new Int64[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceUInt16BlockFormatter : IMessagePackFormatter<UInt16>
|
||||
{
|
||||
public static readonly ForceUInt16BlockFormatter Instance = new ForceUInt16BlockFormatter();
|
||||
|
||||
ForceUInt16BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt16 value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt16 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceUInt16BlockFormatter : IMessagePackFormatter<UInt16?>
|
||||
{
|
||||
public static readonly NullableForceUInt16BlockFormatter Instance = new NullableForceUInt16BlockFormatter();
|
||||
|
||||
NullableForceUInt16BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt16? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public UInt16? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceUInt16BlockArrayFormatter : IMessagePackFormatter<UInt16[]>
|
||||
{
|
||||
public static readonly ForceUInt16BlockArrayFormatter Instance = new ForceUInt16BlockArrayFormatter();
|
||||
|
||||
ForceUInt16BlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt16[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public UInt16[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new UInt16[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceUInt32BlockFormatter : IMessagePackFormatter<UInt32>
|
||||
{
|
||||
public static readonly ForceUInt32BlockFormatter Instance = new ForceUInt32BlockFormatter();
|
||||
|
||||
ForceUInt32BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt32 value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt32 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceUInt32BlockFormatter : IMessagePackFormatter<UInt32?>
|
||||
{
|
||||
public static readonly NullableForceUInt32BlockFormatter Instance = new NullableForceUInt32BlockFormatter();
|
||||
|
||||
NullableForceUInt32BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt32? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public UInt32? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceUInt32BlockArrayFormatter : IMessagePackFormatter<UInt32[]>
|
||||
{
|
||||
public static readonly ForceUInt32BlockArrayFormatter Instance = new ForceUInt32BlockArrayFormatter();
|
||||
|
||||
ForceUInt32BlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt32[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public UInt32[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new UInt32[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceUInt64BlockFormatter : IMessagePackFormatter<UInt64>
|
||||
{
|
||||
public static readonly ForceUInt64BlockFormatter Instance = new ForceUInt64BlockFormatter();
|
||||
|
||||
ForceUInt64BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt64 value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public UInt64 Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceUInt64BlockFormatter : IMessagePackFormatter<UInt64?>
|
||||
{
|
||||
public static readonly NullableForceUInt64BlockFormatter Instance = new NullableForceUInt64BlockFormatter();
|
||||
|
||||
NullableForceUInt64BlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt64? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public UInt64? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceUInt64BlockArrayFormatter : IMessagePackFormatter<UInt64[]>
|
||||
{
|
||||
public static readonly ForceUInt64BlockArrayFormatter Instance = new ForceUInt64BlockArrayFormatter();
|
||||
|
||||
ForceUInt64BlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, UInt64[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public UInt64[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new UInt64[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceByteBlockFormatter : IMessagePackFormatter<Byte>
|
||||
{
|
||||
public static readonly ForceByteBlockFormatter Instance = new ForceByteBlockFormatter();
|
||||
|
||||
ForceByteBlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Byte value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public Byte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceByteBlockFormatter : IMessagePackFormatter<Byte?>
|
||||
{
|
||||
public static readonly NullableForceByteBlockFormatter Instance = new NullableForceByteBlockFormatter();
|
||||
|
||||
NullableForceByteBlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Byte? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public Byte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ForceSByteBlockFormatter : IMessagePackFormatter<SByte>
|
||||
{
|
||||
public static readonly ForceSByteBlockFormatter Instance = new ForceSByteBlockFormatter();
|
||||
|
||||
ForceSByteBlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, SByte value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public SByte Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableForceSByteBlockFormatter : IMessagePackFormatter<SByte?>
|
||||
{
|
||||
public static readonly NullableForceSByteBlockFormatter Instance = new NullableForceSByteBlockFormatter();
|
||||
|
||||
NullableForceSByteBlockFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, SByte? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public SByte? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceSByteBlockArrayFormatter : IMessagePackFormatter<SByte[]>
|
||||
{
|
||||
public static readonly ForceSByteBlockArrayFormatter Instance = new ForceSByteBlockArrayFormatter();
|
||||
|
||||
ForceSByteBlockArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, SByte[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public SByte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new SByte[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 853b67de5219ba944bc151d0ccd3edd5
|
||||
timeCreated: 1503901444
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
// marker
|
||||
public interface IMessagePackFormatter
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IMessagePackFormatter<T> : IMessagePackFormatter
|
||||
{
|
||||
int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver);
|
||||
T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2df5e729bd4efab45a275d958f71eedf
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
16
Assets/Scripts/MessagePack/Formatters/IgnoreFormatter.cs
Normal file
16
Assets/Scripts/MessagePack/Formatters/IgnoreFormatter.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class IgnoreFormatter<T> : IMessagePackFormatter<T>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
readSize = MessagePackBinary.ReadNextBlock(bytes, offset);
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c96ba78a1f786ed4594fe406160c8b0b
|
||||
timeCreated: 1504181170
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,294 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
// multi dimentional array serialize to [i, j, [seq]]
|
||||
|
||||
public sealed class TwoDimentionalArrayFormatter<T> : IMessagePackFormatter<T[,]>
|
||||
{
|
||||
const int ArrayLength = 3;
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, T[,] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var i = value.GetLength(0);
|
||||
var j = value.GetLength(1);
|
||||
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, ArrayLength);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, i);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, j);
|
||||
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
foreach (var item in value)
|
||||
{
|
||||
offset += formatter.Serialize(ref bytes, offset, item, formatterResolver);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public T[,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,] format");
|
||||
|
||||
var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var array = new T[iLength, jLength];
|
||||
|
||||
var i = 0;
|
||||
var j = -1;
|
||||
for (int loop = 0; loop < maxLen; loop++)
|
||||
{
|
||||
if (j < jLength - 1)
|
||||
{
|
||||
j++;
|
||||
}
|
||||
else
|
||||
{
|
||||
j = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
array[i, j] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ThreeDimentionalArrayFormatter<T> : IMessagePackFormatter<T[,,]>
|
||||
{
|
||||
const int ArrayLength = 4;
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, T[,,] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var i = value.GetLength(0);
|
||||
var j = value.GetLength(1);
|
||||
var k = value.GetLength(2);
|
||||
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, ArrayLength);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, i);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, j);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, k);
|
||||
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
foreach (var item in value)
|
||||
{
|
||||
offset += formatter.Serialize(ref bytes, offset, item, formatterResolver);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public T[,,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,,] format");
|
||||
|
||||
var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var kLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var array = new T[iLength, jLength, kLength];
|
||||
|
||||
var i = 0;
|
||||
var j = 0;
|
||||
var k = -1;
|
||||
for (int loop = 0; loop < maxLen; loop++)
|
||||
{
|
||||
if (k < kLength - 1)
|
||||
{
|
||||
k++;
|
||||
}
|
||||
else if (j < jLength - 1)
|
||||
{
|
||||
k = 0;
|
||||
j++;
|
||||
}
|
||||
else
|
||||
{
|
||||
k = 0;
|
||||
j = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
array[i, j, k] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FourDimentionalArrayFormatter<T> : IMessagePackFormatter<T[,,,]>
|
||||
{
|
||||
const int ArrayLength = 5;
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, T[,,,] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var i = value.GetLength(0);
|
||||
var j = value.GetLength(1);
|
||||
var k = value.GetLength(2);
|
||||
var l = value.GetLength(3);
|
||||
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, ArrayLength);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, i);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, j);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, k);
|
||||
offset += MessagePackBinary.WriteInt32(ref bytes, offset, l);
|
||||
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
foreach (var item in value)
|
||||
{
|
||||
offset += formatter.Serialize(ref bytes, offset, item, formatterResolver);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public T[,,,] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var formatter = formatterResolver.GetFormatterWithVerify<T>();
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
if (len != ArrayLength) throw new InvalidOperationException("Invalid T[,,,] format");
|
||||
|
||||
var iLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var jLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var kLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var lLength = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var maxLen = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var array = new T[iLength, jLength, kLength, lLength];
|
||||
|
||||
var i = 0;
|
||||
var j = 0;
|
||||
var k = 0;
|
||||
var l = -1;
|
||||
for (int loop = 0; loop < maxLen; loop++)
|
||||
{
|
||||
if (l < lLength - 1)
|
||||
{
|
||||
l++;
|
||||
}
|
||||
else if (k < kLength - 1)
|
||||
{
|
||||
l = 0;
|
||||
k++;
|
||||
}
|
||||
else if (j < jLength - 1)
|
||||
{
|
||||
l = 0;
|
||||
k = 0;
|
||||
j++;
|
||||
}
|
||||
else
|
||||
{
|
||||
l = 0;
|
||||
k = 0;
|
||||
j = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
array[i, j, k, l] = formatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3244dd5e70e5fda48afe09f2273b3ee1
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
71
Assets/Scripts/MessagePack/Formatters/NullableFormatter.cs
Normal file
71
Assets/Scripts/MessagePack/Formatters/NullableFormatter.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class NullableFormatter<T> : IMessagePackFormatter<T?>
|
||||
where T : struct
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, T? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Value, formatterResolver);
|
||||
}
|
||||
}
|
||||
|
||||
public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StaticNullableFormatter<T> : IMessagePackFormatter<T?>
|
||||
where T : struct
|
||||
{
|
||||
readonly IMessagePackFormatter<T> underlyingFormatter;
|
||||
|
||||
public StaticNullableFormatter(IMessagePackFormatter<T> underlyingFormatter)
|
||||
{
|
||||
this.underlyingFormatter = underlyingFormatter;
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, T? value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return underlyingFormatter.Serialize(ref bytes, offset, value.Value, formatterResolver);
|
||||
}
|
||||
}
|
||||
|
||||
public T? Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return underlyingFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1b8e7644d20320549860b6c234b10857
|
||||
timeCreated: 1487501809
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
260
Assets/Scripts/MessagePack/Formatters/OldSpecFormatter.cs
Normal file
260
Assets/Scripts/MessagePack/Formatters/OldSpecFormatter.cs
Normal file
|
|
@ -0,0 +1,260 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialize by .NET native DateTime binary format.
|
||||
/// </summary>
|
||||
public sealed class NativeDateTimeFormatter : IMessagePackFormatter<DateTime>
|
||||
{
|
||||
public static readonly NativeDateTimeFormatter Instance = new NativeDateTimeFormatter();
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, DateTime value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var dateData = value.ToBinary();
|
||||
return MessagePackBinary.WriteInt64(ref bytes, offset, dateData);
|
||||
}
|
||||
|
||||
public DateTime Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.GetMessagePackType(bytes, offset) == MessagePackType.Extension)
|
||||
{
|
||||
return DateTimeFormatter.Instance.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
}
|
||||
|
||||
var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
return DateTime.FromBinary(dateData);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NativeDateTimeArrayFormatter : IMessagePackFormatter<DateTime[]>
|
||||
{
|
||||
public static readonly NativeDateTimeArrayFormatter Instance = new NativeDateTimeArrayFormatter();
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, DateTime[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteInt64(ref bytes, offset, value[i].ToBinary());
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new DateTime[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
var dateData = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
array[i] = DateTime.FromBinary(dateData);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Old-Spec
|
||||
// bin 8, bin 16, bin 32, str 8, str 16, str 32 -> fixraw or raw 16 or raw 32
|
||||
// fixraw -> fixstr, raw16 -> str16, raw32 -> str32
|
||||
// https://github.com/msgpack/msgpack/blob/master/spec-old.md
|
||||
|
||||
/// <summary>
|
||||
/// Old-MessagePack spec's string formatter.
|
||||
/// </summary>
|
||||
public sealed class OldSpecStringFormatter : IMessagePackFormatter<string>
|
||||
{
|
||||
public static readonly OldSpecStringFormatter Instance = new OldSpecStringFormatter();
|
||||
|
||||
// Old spec does not exists str 8 format.
|
||||
public int Serialize(ref byte[] bytes, int offset, string value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null) return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, StringEncoding.UTF8.GetMaxByteCount(value.Length) + 5);
|
||||
|
||||
int useOffset;
|
||||
if (value.Length <= MessagePackRange.MaxFixStringLength)
|
||||
{
|
||||
useOffset = 1;
|
||||
}
|
||||
else if (value.Length <= ushort.MaxValue)
|
||||
{
|
||||
useOffset = 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
useOffset = 5;
|
||||
}
|
||||
|
||||
// skip length area
|
||||
var writeBeginOffset = offset + useOffset;
|
||||
var byteCount = StringEncoding.UTF8.GetBytes(value, 0, value.Length, bytes, writeBeginOffset);
|
||||
|
||||
// move body and write prefix
|
||||
if (byteCount <= MessagePackRange.MaxFixStringLength)
|
||||
{
|
||||
if (useOffset != 1)
|
||||
{
|
||||
Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 1, byteCount);
|
||||
}
|
||||
bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
|
||||
return byteCount + 1;
|
||||
}
|
||||
else if (byteCount <= ushort.MaxValue)
|
||||
{
|
||||
if (useOffset != 3)
|
||||
{
|
||||
Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 3, byteCount);
|
||||
}
|
||||
|
||||
bytes[offset] = MessagePackCode.Str16;
|
||||
bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
|
||||
bytes[offset + 2] = unchecked((byte)byteCount);
|
||||
return byteCount + 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (useOffset != 5)
|
||||
{
|
||||
Buffer.BlockCopy(bytes, writeBeginOffset, bytes, offset + 5, byteCount);
|
||||
}
|
||||
|
||||
bytes[offset] = MessagePackCode.Str32;
|
||||
bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
|
||||
bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
|
||||
bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
|
||||
bytes[offset + 4] = unchecked((byte)byteCount);
|
||||
return byteCount + 5;
|
||||
}
|
||||
}
|
||||
|
||||
public string Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Old-MessagePack spec's binary formatter.
|
||||
/// </summary>
|
||||
public sealed class OldSpecBinaryFormatter : IMessagePackFormatter<byte[]>
|
||||
{
|
||||
public static readonly OldSpecBinaryFormatter Instance = new OldSpecBinaryFormatter();
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null) return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
|
||||
var byteCount = value.Length;
|
||||
|
||||
if (byteCount <= MessagePackRange.MaxFixStringLength)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 1);
|
||||
|
||||
bytes[offset] = (byte)(MessagePackCode.MinFixStr | byteCount);
|
||||
Buffer.BlockCopy(value, 0, bytes, offset + 1, byteCount);
|
||||
return byteCount + 1;
|
||||
}
|
||||
else if (byteCount <= ushort.MaxValue)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 3);
|
||||
|
||||
bytes[offset] = MessagePackCode.Str16;
|
||||
bytes[offset + 1] = unchecked((byte)(byteCount >> 8));
|
||||
bytes[offset + 2] = unchecked((byte)byteCount);
|
||||
Buffer.BlockCopy(value, 0, bytes, offset + 3, byteCount);
|
||||
return byteCount + 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, byteCount + 5);
|
||||
|
||||
bytes[offset] = MessagePackCode.Str32;
|
||||
bytes[offset + 1] = unchecked((byte)(byteCount >> 24));
|
||||
bytes[offset + 2] = unchecked((byte)(byteCount >> 16));
|
||||
bytes[offset + 3] = unchecked((byte)(byteCount >> 8));
|
||||
bytes[offset + 4] = unchecked((byte)byteCount);
|
||||
Buffer.BlockCopy(value, 0, bytes, offset + 5, byteCount);
|
||||
return byteCount + 5;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (type == MessagePackType.Nil)
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else if (type == MessagePackType.Binary)
|
||||
{
|
||||
return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
}
|
||||
else if (type == MessagePackType.String)
|
||||
{
|
||||
var code = bytes[offset];
|
||||
unchecked
|
||||
{
|
||||
if (MessagePackCode.MinFixStr <= code && code <= MessagePackCode.MaxFixStr)
|
||||
{
|
||||
var length = bytes[offset] & 0x1F;
|
||||
readSize = length + 1;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 1, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
else if (code == MessagePackCode.Str8)
|
||||
{
|
||||
var length = (int)bytes[offset + 1];
|
||||
readSize = length + 2;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 2, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
else if (code == MessagePackCode.Str16)
|
||||
{
|
||||
var length = (bytes[offset + 1] << 8) + (bytes[offset + 2]);
|
||||
readSize = length + 3;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 3, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
else if (code == MessagePackCode.Str32)
|
||||
{
|
||||
var length = (int)((uint)(bytes[offset + 1] << 24) | (uint)(bytes[offset + 2] << 16) | (uint)(bytes[offset + 3] << 8) | (uint)bytes[offset + 4]);
|
||||
readSize = length + 5;
|
||||
var result = new byte[length];
|
||||
Buffer.BlockCopy(bytes, offset + 5, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 29a8b3571b399b14abf1dbe0f90491f6
|
||||
timeCreated: 1490454679
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
1331
Assets/Scripts/MessagePack/Formatters/PrimitiveFormatter.cs
Normal file
1331
Assets/Scripts/MessagePack/Formatters/PrimitiveFormatter.cs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3d26f3ff9ba5a22498c0c6c7363f3437
|
||||
timeCreated: 1488287483
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,246 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class PrimitiveObjectFormatter : IMessagePackFormatter<object>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<object> Instance = new PrimitiveObjectFormatter();
|
||||
|
||||
static readonly Dictionary<Type, int> typeToJumpCode = new Dictionary<Type, int>()
|
||||
{
|
||||
{ typeof(Boolean), 0 },
|
||||
{ typeof(Char), 1 },
|
||||
{ typeof(SByte), 2 },
|
||||
{ typeof(Byte), 3 },
|
||||
{ typeof(Int16), 4 },
|
||||
{ typeof(UInt16), 5 },
|
||||
{ typeof(Int32), 6 },
|
||||
{ typeof(UInt32), 7 },
|
||||
{ typeof(Int64), 8 },
|
||||
{ typeof(UInt64),9 },
|
||||
{ typeof(Single), 10 },
|
||||
{ typeof(Double), 11 },
|
||||
{ typeof(DateTime), 12 },
|
||||
{ typeof(string), 13 },
|
||||
{ typeof(byte[]), 14 }
|
||||
};
|
||||
|
||||
PrimitiveObjectFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#if !UNITY_WSA
|
||||
|
||||
public static bool IsSupportedType(Type type, TypeInfo typeInfo, object value)
|
||||
{
|
||||
if (value == null) return true;
|
||||
if (typeToJumpCode.ContainsKey(type)) return true;
|
||||
if (typeInfo.IsEnum) return true;
|
||||
|
||||
if (value is System.Collections.IDictionary) return true;
|
||||
if (value is System.Collections.ICollection) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
var t = value.GetType();
|
||||
|
||||
int code;
|
||||
if (typeToJumpCode.TryGetValue(t, out code))
|
||||
{
|
||||
switch (code)
|
||||
{
|
||||
case 0:
|
||||
return MessagePackBinary.WriteBoolean(ref bytes, offset, (bool)value);
|
||||
case 1:
|
||||
return MessagePackBinary.WriteChar(ref bytes, offset, (char)value);
|
||||
case 2:
|
||||
return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, (sbyte)value);
|
||||
case 3:
|
||||
return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, (byte)value);
|
||||
case 4:
|
||||
return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, (Int16)value);
|
||||
case 5:
|
||||
return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, (UInt16)value);
|
||||
case 6:
|
||||
return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, (Int32)value);
|
||||
case 7:
|
||||
return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, (UInt32)value);
|
||||
case 8:
|
||||
return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, (Int64)value);
|
||||
case 9:
|
||||
return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, (UInt64)value);
|
||||
case 10:
|
||||
return MessagePackBinary.WriteSingle(ref bytes, offset, (Single)value);
|
||||
case 11:
|
||||
return MessagePackBinary.WriteDouble(ref bytes, offset, (double)value);
|
||||
case 12:
|
||||
return MessagePackBinary.WriteDateTime(ref bytes, offset, (DateTime)value);
|
||||
case 13:
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, (string)value);
|
||||
case 14:
|
||||
return MessagePackBinary.WriteBytes(ref bytes, offset, (byte[])value);
|
||||
default:
|
||||
throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#if UNITY_WSA && !NETFX_CORE
|
||||
if (t.IsEnum)
|
||||
#else
|
||||
if (t.GetTypeInfo().IsEnum)
|
||||
#endif
|
||||
{
|
||||
var underlyingType = Enum.GetUnderlyingType(t);
|
||||
var code2 = typeToJumpCode[underlyingType];
|
||||
switch (code2)
|
||||
{
|
||||
case 2:
|
||||
return MessagePackBinary.WriteSByteForceSByteBlock(ref bytes, offset, (sbyte)value);
|
||||
case 3:
|
||||
return MessagePackBinary.WriteByteForceByteBlock(ref bytes, offset, (byte)value);
|
||||
case 4:
|
||||
return MessagePackBinary.WriteInt16ForceInt16Block(ref bytes, offset, (Int16)value);
|
||||
case 5:
|
||||
return MessagePackBinary.WriteUInt16ForceUInt16Block(ref bytes, offset, (UInt16)value);
|
||||
case 6:
|
||||
return MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, offset, (Int32)value);
|
||||
case 7:
|
||||
return MessagePackBinary.WriteUInt32ForceUInt32Block(ref bytes, offset, (UInt32)value);
|
||||
case 8:
|
||||
return MessagePackBinary.WriteInt64ForceInt64Block(ref bytes, offset, (Int64)value);
|
||||
case 9:
|
||||
return MessagePackBinary.WriteUInt64ForceUInt64Block(ref bytes, offset, (UInt64)value);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (value is System.Collections.IDictionary) // check IDictionary first
|
||||
{
|
||||
var d = value as System.Collections.IDictionary;
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, d.Count);
|
||||
foreach (System.Collections.DictionaryEntry item in d)
|
||||
{
|
||||
offset += Serialize(ref bytes, offset, item.Key, formatterResolver);
|
||||
offset += Serialize(ref bytes, offset, item.Value, formatterResolver);
|
||||
}
|
||||
return offset - startOffset;
|
||||
}
|
||||
else if (value is System.Collections.ICollection)
|
||||
{
|
||||
var c = value as System.Collections.ICollection;
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, c.Count);
|
||||
foreach (var item in c)
|
||||
{
|
||||
offset += Serialize(ref bytes, offset, item, formatterResolver);
|
||||
}
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name);
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
switch (type)
|
||||
{
|
||||
case MessagePackType.Integer:
|
||||
var code = bytes[offset];
|
||||
if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int8) return MessagePackBinary.ReadSByte(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int16) return MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int32) return MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.Int64) return MessagePackBinary.ReadInt64(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt8) return MessagePackBinary.ReadByte(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt16) return MessagePackBinary.ReadUInt16(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt32) return MessagePackBinary.ReadUInt32(bytes, offset, out readSize);
|
||||
else if (code == MessagePackCode.UInt64) return MessagePackBinary.ReadUInt64(bytes, offset, out readSize);
|
||||
throw new InvalidOperationException("Invalid primitive bytes.");
|
||||
case MessagePackType.Boolean:
|
||||
return MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
case MessagePackType.Float:
|
||||
if (MessagePackCode.Float32 == bytes[offset])
|
||||
{
|
||||
return MessagePackBinary.ReadSingle(bytes, offset, out readSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
}
|
||||
case MessagePackType.String:
|
||||
return MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
case MessagePackType.Binary:
|
||||
return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
case MessagePackType.Extension:
|
||||
var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
if (ext.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
|
||||
{
|
||||
return MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
}
|
||||
throw new InvalidOperationException("Invalid primitive bytes.");
|
||||
case MessagePackType.Array:
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
var startOffset = offset;
|
||||
offset += readSize;
|
||||
|
||||
var objectFormatter = formatterResolver.GetFormatter<object>();
|
||||
var array = new object[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
array[i] = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
case MessagePackType.Map:
|
||||
{
|
||||
var length = MessagePackBinary.ReadMapHeader(bytes, offset, out readSize);
|
||||
var startOffset = offset;
|
||||
offset += readSize;
|
||||
|
||||
var objectFormatter = formatterResolver.GetFormatter<object>();
|
||||
var hash = new Dictionary<object, object>(length);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var key = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = objectFormatter.Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
hash.Add(key, value);
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return hash;
|
||||
}
|
||||
case MessagePackType.Nil:
|
||||
readSize = 1;
|
||||
return null;
|
||||
default:
|
||||
throw new InvalidOperationException("Invalid primitive bytes.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: af0c4aff90f7e2748a7a7e0e46a88a3a
|
||||
timeCreated: 1491398527
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,578 @@
|
|||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
#if NETSTANDARD
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
// NET40 -> BigInteger, Complex, Tuple
|
||||
|
||||
// byte[] is special. represents bin type.
|
||||
public sealed class ByteArrayFormatter : IMessagePackFormatter<byte[]>
|
||||
{
|
||||
public static readonly ByteArrayFormatter Instance = new ByteArrayFormatter();
|
||||
|
||||
ByteArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, byte[] value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteBytes(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public byte[] Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadBytes(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableStringFormatter : IMessagePackFormatter<String>
|
||||
{
|
||||
public static readonly NullableStringFormatter Instance = new NullableStringFormatter();
|
||||
|
||||
NullableStringFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, String value, IFormatterResolver typeResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, value);
|
||||
}
|
||||
|
||||
public String Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
|
||||
{
|
||||
return MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullableStringArrayFormatter : IMessagePackFormatter<String[]>
|
||||
{
|
||||
public static readonly NullableStringArrayFormatter Instance = new NullableStringArrayFormatter();
|
||||
|
||||
NullableStringArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, String[] value, IFormatterResolver typeResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, value.Length);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteString(ref bytes, offset, value[i]);
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public String[] Deserialize(byte[] bytes, int offset, IFormatterResolver typeResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var array = new String[len];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadString(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DecimalFormatter : IMessagePackFormatter<Decimal>
|
||||
{
|
||||
public static readonly DecimalFormatter Instance = new DecimalFormatter();
|
||||
|
||||
DecimalFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, decimal value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return decimal.Parse(MessagePackBinary.ReadString(bytes, offset, out readSize), CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TimeSpanFormatter : IMessagePackFormatter<TimeSpan>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<TimeSpan> Instance = new TimeSpanFormatter();
|
||||
|
||||
TimeSpanFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, TimeSpan value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteInt64(ref bytes, offset, value.Ticks);
|
||||
}
|
||||
|
||||
public TimeSpan Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return new TimeSpan(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DateTimeOffsetFormatter : IMessagePackFormatter<DateTimeOffset>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<DateTimeOffset> Instance = new DateTimeOffsetFormatter();
|
||||
|
||||
DateTimeOffsetFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, DateTimeOffset value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2);
|
||||
offset += MessagePackBinary.WriteDateTime(ref bytes, offset, new DateTime(value.Ticks, DateTimeKind.Utc)); // current ticks as is
|
||||
offset += MessagePackBinary.WriteInt16(ref bytes, offset, (short)value.Offset.TotalMinutes); // offset is normalized in minutes
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public DateTimeOffset Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid DateTimeOffset format.");
|
||||
|
||||
var utc = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var dtOffsetMinutes = MessagePackBinary.ReadInt16(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
|
||||
return new DateTimeOffset(utc.Ticks, TimeSpan.FromMinutes(dtOffsetMinutes));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class GuidFormatter : IMessagePackFormatter<Guid>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<Guid> Instance = new GuidFormatter();
|
||||
|
||||
|
||||
GuidFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Guid value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, 38);
|
||||
|
||||
bytes[offset] = MessagePackCode.Str8;
|
||||
bytes[offset + 1] = unchecked((byte)36);
|
||||
new GuidBits(ref value).Write(bytes, offset + 2);
|
||||
return 38;
|
||||
}
|
||||
|
||||
public Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var segment = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
return new GuidBits(segment).Value;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class UriFormatter : IMessagePackFormatter<Uri>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<Uri> Instance = new UriFormatter();
|
||||
|
||||
|
||||
UriFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Uri value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public Uri Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Uri(MessagePackBinary.ReadString(bytes, offset, out readSize), UriKind.RelativeOrAbsolute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class VersionFormatter : IMessagePackFormatter<Version>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<Version> Instance = new VersionFormatter();
|
||||
|
||||
VersionFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Version value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public Version Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Version(MessagePackBinary.ReadString(bytes, offset, out readSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class KeyValuePairFormatter<TKey, TValue> : IMessagePackFormatter<KeyValuePair<TKey, TValue>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, KeyValuePair<TKey, TValue> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2);
|
||||
offset += formatterResolver.GetFormatterWithVerify<TKey>().Serialize(ref bytes, offset, value.Key, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<TValue>().Serialize(ref bytes, offset, value.Value, formatterResolver);
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public KeyValuePair<TKey, TValue> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid KeyValuePair format.");
|
||||
|
||||
var key = formatterResolver.GetFormatterWithVerify<TKey>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var value = formatterResolver.GetFormatterWithVerify<TValue>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new KeyValuePair<TKey, TValue>(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StringBuilderFormatter : IMessagePackFormatter<StringBuilder>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<StringBuilder> Instance = new StringBuilderFormatter();
|
||||
|
||||
StringBuilderFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, StringBuilder value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessagePackBinary.WriteString(ref bytes, offset, value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public StringBuilder Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new StringBuilder(MessagePackBinary.ReadString(bytes, offset, out readSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class BitArrayFormatter : IMessagePackFormatter<BitArray>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<BitArray> Instance = new BitArrayFormatter();
|
||||
|
||||
BitArrayFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, BitArray value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var len = value.Length;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
offset += MessagePackBinary.WriteBoolean(ref bytes, offset, value.Get(i));
|
||||
}
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public BitArray Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
|
||||
var len = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var array = new BitArray(len);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
array[i] = MessagePackBinary.ReadBoolean(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
}
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
|
||||
public sealed class BigIntegerFormatter : IMessagePackFormatter<System.Numerics.BigInteger>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<System.Numerics.BigInteger> Instance = new BigIntegerFormatter();
|
||||
|
||||
BigIntegerFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, System.Numerics.BigInteger value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return MessagePackBinary.WriteBytes(ref bytes, offset, value.ToByteArray());
|
||||
}
|
||||
|
||||
public System.Numerics.BigInteger Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
return new System.Numerics.BigInteger(MessagePackBinary.ReadBytes(bytes, offset, out readSize));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ComplexFormatter : IMessagePackFormatter<System.Numerics.Complex>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<System.Numerics.Complex> Instance = new ComplexFormatter();
|
||||
|
||||
ComplexFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, System.Numerics.Complex value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2);
|
||||
offset += MessagePackBinary.WriteDouble(ref bytes, offset, value.Real);
|
||||
offset += MessagePackBinary.WriteDouble(ref bytes, offset, value.Imaginary);
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public System.Numerics.Complex Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
if (count != 2) throw new InvalidOperationException("Invalid Complex format.");
|
||||
|
||||
var real = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var imaginary = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new System.Numerics.Complex(real, imaginary);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LazyFormatter<T> : IMessagePackFormatter<Lazy<T>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Lazy<T> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Value, formatterResolver);
|
||||
}
|
||||
}
|
||||
|
||||
public Lazy<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// deserialize immediately(no delay, because capture byte[] causes memory leak)
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return new Lazy<T>(() => v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TaskUnitFormatter : IMessagePackFormatter<Task>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<Task> Instance = new TaskUnitFormatter();
|
||||
static readonly Task CompletedTask = Task.FromResult<object>(null);
|
||||
|
||||
TaskUnitFormatter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, Task value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
value.Wait(); // wait...!
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
}
|
||||
|
||||
public Task Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (!MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Invalid input");
|
||||
}
|
||||
else
|
||||
{
|
||||
readSize = 1;
|
||||
return CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class TaskValueFormatter<T> : IMessagePackFormatter<Task<T>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Task<T> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
// value.Result -> wait...!
|
||||
return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Result, formatterResolver);
|
||||
}
|
||||
}
|
||||
|
||||
public Task<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return Task.FromResult(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ValueTaskFormatter<T> : IMessagePackFormatter<ValueTask<T>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTask<T> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
return formatterResolver.GetFormatterWithVerify<T>().Serialize(ref bytes, offset, value.Result, formatterResolver);
|
||||
}
|
||||
|
||||
public ValueTask<T> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
var v = formatterResolver.GetFormatterWithVerify<T>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
return new ValueTask<T>(v);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 27708bccdbaa27b4387eaa3b87bb9140
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
437
Assets/Scripts/MessagePack/Formatters/TupleFormatter.cs
Normal file
437
Assets/Scripts/MessagePack/Formatters/TupleFormatter.cs
Normal file
|
|
@ -0,0 +1,437 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
||||
public sealed class TupleFormatter<T1> : IMessagePackFormatter<Tuple<T1>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 1);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 1) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1>(item1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2> : IMessagePackFormatter<Tuple<T1, T2>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 2) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2>(item1, item2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2, T3> : IMessagePackFormatter<Tuple<T1, T2, T3>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 3);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 3) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2, T3>(item1, item2, item3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2, T3, T4> : IMessagePackFormatter<Tuple<T1, T2, T3, T4>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 4);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 4) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2, T3, T4>(item1, item2, item3, item4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2, T3, T4, T5> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 5);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 5) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2, T3, T4, T5, T6> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5, T6>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5, T6> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 6);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5, T6> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 6) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2, T3, T4, T5, T6, T7> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5, T6, T7>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5, T6, T7> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 7);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T7>().Serialize(ref bytes, offset, value.Item7, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 7) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class TupleFormatter<T1, T2, T3, T4, T5, T6, T7, TRest> : IMessagePackFormatter<Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 8);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T7>().Serialize(ref bytes, offset, value.Item7, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<TRest>().Serialize(ref bytes, offset, value.Rest, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 8) throw new InvalidOperationException("Invalid Tuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item8 = formatterResolver.GetFormatterWithVerify<TRest>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(item1, item2, item3, item4, item5, item6, item7, item8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
12
Assets/Scripts/MessagePack/Formatters/TupleFormatter.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Formatters/TupleFormatter.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0c85da68b90b760498edb47140d09a99
|
||||
timeCreated: 1488287483
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
323
Assets/Scripts/MessagePack/Formatters/TypelessFormatter.cs
Normal file
323
Assets/Scripts/MessagePack/Formatters/TypelessFormatter.cs
Normal file
|
|
@ -0,0 +1,323 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// For `object` field that holds derived from `object` value, ex: var arr = new object[] { 1, "a", new Model() };
|
||||
/// </summary>
|
||||
public sealed class TypelessFormatter : IMessagePackFormatter<object>
|
||||
{
|
||||
public const sbyte ExtensionTypeCode = 100;
|
||||
|
||||
static readonly Regex SubtractFullNameRegex = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+", RegexOptions.Compiled);
|
||||
|
||||
delegate int SerializeMethod(object dynamicContractlessFormatter, ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver);
|
||||
delegate object DeserializeMethod(object dynamicContractlessFormatter, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize);
|
||||
|
||||
public static readonly IMessagePackFormatter<object> Instance = new TypelessFormatter();
|
||||
|
||||
static readonly ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>> serializers = new ThreadsafeTypeKeyHashTable<KeyValuePair<object, SerializeMethod>>();
|
||||
static readonly ThreadsafeTypeKeyHashTable<KeyValuePair<object, DeserializeMethod>> deserializers = new ThreadsafeTypeKeyHashTable<KeyValuePair<object, DeserializeMethod>>();
|
||||
static readonly ThreadsafeTypeKeyHashTable<byte[]> typeNameCache = new ThreadsafeTypeKeyHashTable<byte[]>();
|
||||
static readonly AsymmetricKeyHashTable<byte[], ArraySegment<byte>, Type> typeCache = new AsymmetricKeyHashTable<byte[], ArraySegment<byte>, Type>(new StringArraySegmentByteAscymmetricEqualityComparer());
|
||||
|
||||
static readonly HashSet<string> blacklistCheck;
|
||||
static readonly HashSet<Type> useBuiltinTypes = new HashSet<Type>()
|
||||
{
|
||||
typeof(Boolean),
|
||||
// typeof(Char),
|
||||
typeof(SByte),
|
||||
typeof(Byte),
|
||||
typeof(Int16),
|
||||
typeof(UInt16),
|
||||
typeof(Int32),
|
||||
typeof(UInt32),
|
||||
typeof(Int64),
|
||||
typeof(UInt64),
|
||||
typeof(Single),
|
||||
typeof(Double),
|
||||
typeof(string),
|
||||
typeof(byte[]),
|
||||
|
||||
typeof(Boolean[]),
|
||||
// typeof(Char[]),
|
||||
typeof(SByte[]),
|
||||
typeof(Int16[]),
|
||||
typeof(UInt16[]),
|
||||
typeof(Int32[]),
|
||||
typeof(UInt32[]),
|
||||
typeof(Int64[]),
|
||||
typeof(UInt64[]),
|
||||
typeof(Single[]),
|
||||
typeof(Double[]),
|
||||
typeof(string[]),
|
||||
|
||||
typeof(Boolean?),
|
||||
// typeof(Char?),
|
||||
typeof(SByte?),
|
||||
typeof(Byte?),
|
||||
typeof(Int16?),
|
||||
typeof(UInt16?),
|
||||
typeof(Int32?),
|
||||
typeof(UInt32?),
|
||||
typeof(Int64?),
|
||||
typeof(UInt64?),
|
||||
typeof(Single?),
|
||||
typeof(Double?),
|
||||
};
|
||||
|
||||
// mscorlib or System.Private.CoreLib
|
||||
static bool isMscorlib = typeof(int).AssemblyQualifiedName.Contains("mscorlib");
|
||||
|
||||
/// <summary>
|
||||
/// When type name does not have Version, Culture, Public token - sometimes can not find type, example - ExpandoObject
|
||||
/// In that can set to `false`
|
||||
/// </summary>
|
||||
public static volatile bool RemoveAssemblyVersion = true;
|
||||
|
||||
static TypelessFormatter()
|
||||
{
|
||||
blacklistCheck = new HashSet<string>()
|
||||
{
|
||||
"System.CodeDom.Compiler.TempFileCollection",
|
||||
"System.IO.FileSystemInfo",
|
||||
"System.Management.IWbemClassObjectFreeThreaded"
|
||||
};
|
||||
|
||||
serializers.TryAdd(typeof(object), _ => new KeyValuePair<object, SerializeMethod>(null, (object p1, ref byte[] p2, int p3, object p4, IFormatterResolver p5) => 0));
|
||||
deserializers.TryAdd(typeof(object), _ => new KeyValuePair<object, DeserializeMethod>(null, (object p1, byte[] p2, int p3, IFormatterResolver p4, out int p5) =>
|
||||
{
|
||||
p5 = 0;
|
||||
return new object();
|
||||
}));
|
||||
}
|
||||
|
||||
// see:http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
|
||||
// subtract Version, Culture and PublicKeyToken from AssemblyQualifiedName
|
||||
static string BuildTypeName(Type type)
|
||||
{
|
||||
if (RemoveAssemblyVersion)
|
||||
{
|
||||
string full = type.AssemblyQualifiedName;
|
||||
|
||||
var shortened = SubtractFullNameRegex.Replace(full, "");
|
||||
if (Type.GetType(shortened, false) == null)
|
||||
{
|
||||
// if type cannot be found with shortened name - use full name
|
||||
shortened = full;
|
||||
}
|
||||
|
||||
return shortened;
|
||||
}
|
||||
else
|
||||
{
|
||||
return type.AssemblyQualifiedName;
|
||||
}
|
||||
}
|
||||
|
||||
public int Serialize(ref byte[] bytes, int offset, object value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return MessagePackBinary.WriteNil(ref bytes, offset);
|
||||
}
|
||||
|
||||
var type = value.GetType();
|
||||
|
||||
byte[] typeName;
|
||||
if (!typeNameCache.TryGetValue(type, out typeName))
|
||||
{
|
||||
if (blacklistCheck.Contains(type.FullName))
|
||||
{
|
||||
throw new InvalidOperationException("Type is in blacklist:" + type.FullName);
|
||||
}
|
||||
|
||||
var ti = type.GetTypeInfo();
|
||||
if (ti.IsAnonymous() || useBuiltinTypes.Contains(type))
|
||||
{
|
||||
typeName = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
typeName = StringEncoding.UTF8.GetBytes(BuildTypeName(type));
|
||||
}
|
||||
typeNameCache.TryAdd(type, typeName);
|
||||
}
|
||||
|
||||
if (typeName == null)
|
||||
{
|
||||
return Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter<object>().Serialize(ref bytes, offset, value, formatterResolver);
|
||||
}
|
||||
|
||||
// don't use GetOrAdd for avoid closure capture.
|
||||
KeyValuePair<object, SerializeMethod> formatterAndDelegate;
|
||||
if (!serializers.TryGetValue(type, out formatterAndDelegate))
|
||||
{
|
||||
lock (serializers) // double check locking...
|
||||
{
|
||||
if (!serializers.TryGetValue(type, out formatterAndDelegate))
|
||||
{
|
||||
var ti = type.GetTypeInfo();
|
||||
|
||||
var formatter = formatterResolver.GetFormatterDynamic(type);
|
||||
if (formatter == null)
|
||||
{
|
||||
throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolver:" + formatterResolver.GetType().Name);
|
||||
}
|
||||
|
||||
var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(type);
|
||||
var param0 = Expression.Parameter(typeof(object), "formatter");
|
||||
var param1 = Expression.Parameter(typeof(byte[]).MakeByRefType(), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(int), "offset");
|
||||
var param3 = Expression.Parameter(typeof(object), "value");
|
||||
var param4 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");
|
||||
|
||||
var serializeMethodInfo = formatterType.GetRuntimeMethod("Serialize", new[] { typeof(byte[]).MakeByRefType(), typeof(int), type, typeof(IFormatterResolver) });
|
||||
|
||||
var body = Expression.Call(
|
||||
Expression.Convert(param0, formatterType),
|
||||
serializeMethodInfo,
|
||||
param1,
|
||||
param2,
|
||||
ti.IsValueType ? Expression.Unbox(param3, type) : Expression.Convert(param3, type),
|
||||
param4);
|
||||
|
||||
var lambda = Expression.Lambda<SerializeMethod>(body, param0, param1, param2, param3, param4).Compile();
|
||||
|
||||
formatterAndDelegate = new KeyValuePair<object, SerializeMethod>(formatter, lambda);
|
||||
serializers.TryAdd(type, formatterAndDelegate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mark as extension with code 100
|
||||
var startOffset = offset;
|
||||
offset += 6; // mark will be written at the end, when size is known
|
||||
offset += MessagePackBinary.WriteStringBytes(ref bytes, offset, typeName);
|
||||
offset += formatterAndDelegate.Value(formatterAndDelegate.Key, ref bytes, offset, value, formatterResolver);
|
||||
MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref bytes, startOffset, (sbyte)TypelessFormatter.ExtensionTypeCode, offset - startOffset - 6);
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public object Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
readSize = 1;
|
||||
return null;
|
||||
}
|
||||
|
||||
int startOffset = offset;
|
||||
var packType = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (packType == MessagePackType.Extension)
|
||||
{
|
||||
var ext = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
if (ext.TypeCode == TypelessFormatter.ExtensionTypeCode)
|
||||
{
|
||||
// it has type name serialized
|
||||
offset += readSize;
|
||||
var typeName = MessagePackBinary.ReadStringSegment(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
var result = DeserializeByTypeName(typeName, bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
readSize = offset - startOffset;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// fallback
|
||||
return Resolvers.TypelessFormatterFallbackResolver.Instance.GetFormatter<object>().Deserialize(bytes, startOffset, formatterResolver, out readSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does not support deserializing of anonymous types
|
||||
/// Type should be covered by preceeding resolvers in complex/standard resolver
|
||||
/// </summary>
|
||||
private object DeserializeByTypeName(ArraySegment<byte> typeName, byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
// try get type with assembly name, throw if not found
|
||||
Type type;
|
||||
if (!typeCache.TryGetValue(typeName, out type))
|
||||
{
|
||||
var buffer = new byte[typeName.Count];
|
||||
Buffer.BlockCopy(typeName.Array, typeName.Offset, buffer, 0, buffer.Length);
|
||||
var str = StringEncoding.UTF8.GetString(buffer);
|
||||
type = Type.GetType(str, false);
|
||||
if (type == null)
|
||||
{
|
||||
if (isMscorlib && str.Contains("System.Private.CoreLib"))
|
||||
{
|
||||
str = str.Replace("System.Private.CoreLib", "mscorlib");
|
||||
type = Type.GetType(str, true); // throw
|
||||
}
|
||||
else if (!isMscorlib && str.Contains("mscorlib"))
|
||||
{
|
||||
str = str.Replace("mscorlib", "System.Private.CoreLib");
|
||||
type = Type.GetType(str, true); // throw
|
||||
}
|
||||
else
|
||||
{
|
||||
type = Type.GetType(str, true); // re-throw
|
||||
}
|
||||
}
|
||||
typeCache.TryAdd(buffer, type);
|
||||
}
|
||||
|
||||
KeyValuePair<object, DeserializeMethod> formatterAndDelegate;
|
||||
if (!deserializers.TryGetValue(type, out formatterAndDelegate))
|
||||
{
|
||||
lock (deserializers)
|
||||
{
|
||||
if (!deserializers.TryGetValue(type, out formatterAndDelegate))
|
||||
{
|
||||
var ti = type.GetTypeInfo();
|
||||
|
||||
var formatter = formatterResolver.GetFormatterDynamic(type);
|
||||
if (formatter == null)
|
||||
{
|
||||
throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolver:" + formatterResolver.GetType().Name);
|
||||
}
|
||||
|
||||
var formatterType = typeof(IMessagePackFormatter<>).MakeGenericType(type);
|
||||
var param0 = Expression.Parameter(typeof(object), "formatter");
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(int), "offset");
|
||||
var param3 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");
|
||||
var param4 = Expression.Parameter(typeof(int).MakeByRefType(), "readSize");
|
||||
|
||||
var deserializeMethodInfo = formatterType.GetRuntimeMethod("Deserialize", new[] { typeof(byte[]), typeof(int), typeof(IFormatterResolver), typeof(int).MakeByRefType() });
|
||||
|
||||
var deserialize = Expression.Call(
|
||||
Expression.Convert(param0, formatterType),
|
||||
deserializeMethodInfo,
|
||||
param1,
|
||||
param2,
|
||||
param3,
|
||||
param4);
|
||||
|
||||
Expression body = deserialize;
|
||||
if (ti.IsValueType)
|
||||
body = Expression.Convert(deserialize, typeof(object));
|
||||
var lambda = Expression.Lambda<DeserializeMethod>(body, param0, param1, param2, param3, param4).Compile();
|
||||
|
||||
formatterAndDelegate = new KeyValuePair<object, DeserializeMethod>(formatter, lambda);
|
||||
deserializers.TryAdd(type, formatterAndDelegate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return formatterAndDelegate.Value(formatterAndDelegate.Key, bytes, offset, formatterResolver, out readSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: aeb799582923b154690b60d32744e838
|
||||
timeCreated: 1498546040
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
126
Assets/Scripts/MessagePack/Formatters/UnsafeBinaryFormatters.cs
Normal file
126
Assets/Scripts/MessagePack/Formatters/UnsafeBinaryFormatters.cs
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
public sealed class BinaryGuidFormatter : IMessagePackFormatter<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Unsafe binary Guid formatter. this is only allows on LittleEndian environment.
|
||||
/// </summary>
|
||||
public static readonly IMessagePackFormatter<Guid> Instance = new BinaryGuidFormatter();
|
||||
|
||||
BinaryGuidFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
// Guid's underlying _a,...,_k field is sequential and same layuout as .NET Framework and Mono(Unity).
|
||||
// But target machines must be same endian so restrict only for little endian.
|
||||
|
||||
public unsafe int Serialize(ref byte[] bytes, int offset, Guid value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian) throw new Exception("BinaryGuidFormatter only allows on little endian env.");
|
||||
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, 18);
|
||||
fixed (byte* dst = &bytes[offset])
|
||||
{
|
||||
var src = &value;
|
||||
|
||||
dst[0] = MessagePackCode.Bin8;
|
||||
dst[1] = 16;
|
||||
|
||||
*(Guid*)(dst + 2) = *src;
|
||||
}
|
||||
|
||||
return 18;
|
||||
}
|
||||
|
||||
public unsafe Guid Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian) throw new Exception("BinaryGuidFormatter only allows on little endian env.");
|
||||
|
||||
if (!(offset + 18 <= bytes.Length))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (byte* src = &bytes[offset])
|
||||
{
|
||||
if (src[0] != MessagePackCode.Bin8)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
|
||||
}
|
||||
if (src[1] != 16)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Guid Size.");
|
||||
}
|
||||
|
||||
var target = *(Guid*)(src + 2);
|
||||
readSize = 18;
|
||||
return target;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class BinaryDecimalFormatter : IMessagePackFormatter<Decimal>
|
||||
{
|
||||
/// <summary>
|
||||
/// Unsafe binary Decimal formatter. this is only allows on LittleEndian environment.
|
||||
/// </summary>
|
||||
public static readonly IMessagePackFormatter<Decimal> Instance = new BinaryDecimalFormatter();
|
||||
|
||||
BinaryDecimalFormatter()
|
||||
{
|
||||
}
|
||||
|
||||
// decimal underlying "flags, hi, lo, mid" fields are sequential and same layuout with .NET Framework and Mono(Unity)
|
||||
// But target machines must be same endian so restrict only for little endian.
|
||||
|
||||
public unsafe int Serialize(ref byte[] bytes, int offset, Decimal value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian) throw new Exception("BinaryGuidFormatter only allows on little endian env.");
|
||||
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, 18);
|
||||
fixed (byte* dst = &bytes[offset])
|
||||
{
|
||||
var src = &value;
|
||||
|
||||
dst[0] = MessagePackCode.Bin8;
|
||||
dst[1] = 16;
|
||||
|
||||
*(Decimal*)(dst + 2) = *src;
|
||||
}
|
||||
|
||||
return 18;
|
||||
}
|
||||
|
||||
public unsafe Decimal Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (!BitConverter.IsLittleEndian) throw new Exception("BinaryDecimalFormatter only allows on little endian env.");
|
||||
|
||||
if (!(offset + 18 <= bytes.Length))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
fixed (byte* src = &bytes[offset])
|
||||
{
|
||||
if (src[0] != MessagePackCode.Bin8)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("code is invalid. code:{0} format:{1}", bytes[offset], MessagePackCode.ToFormatName(bytes[offset])));
|
||||
}
|
||||
if (src[1] != 16)
|
||||
{
|
||||
throw new InvalidOperationException("Invalid Guid Size.");
|
||||
}
|
||||
|
||||
var target = *(Decimal*)(src + 2);
|
||||
readSize = 18;
|
||||
return target;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 294dec8ab7467e9468a123dfdea751dd
|
||||
timeCreated: 1505965654
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
371
Assets/Scripts/MessagePack/Formatters/ValueTupleFormatter.cs
Normal file
371
Assets/Scripts/MessagePack/Formatters/ValueTupleFormatter.cs
Normal file
|
|
@ -0,0 +1,371 @@
|
|||
#if NETSTANDARD
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Formatters
|
||||
{
|
||||
|
||||
public sealed class ValueTupleFormatter<T1> : IMessagePackFormatter<ValueTuple<T1>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 1);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 1) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1>(item1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2> : IMessagePackFormatter<ValueTuple<T1, T2>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 2);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 2) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2>(item1, item2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2, T3> : IMessagePackFormatter<ValueTuple<T1, T2, T3>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 3);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 3) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3>(item1, item2, item3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2, T3, T4> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 4);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 4) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4>(item1, item2, item3, item4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 5);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 5) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5>(item1, item2, item3, item4, item5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5, T6> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5, T6>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5, T6> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 6);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 6) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5, T6>(item1, item2, item3, item4, item5, item6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5, T6, T7> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5, T6, T7> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 7);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T7>().Serialize(ref bytes, offset, value.Item7, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6, T7> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 7) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5, T6, T7>(item1, item2, item3, item4, item5, item6, item7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public sealed class ValueTupleFormatter<T1, T2, T3, T4, T5, T6, T7, TRest> : IMessagePackFormatter<ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>> where TRest : struct
|
||||
{
|
||||
public int Serialize(ref byte[] bytes, int offset, ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> value, IFormatterResolver formatterResolver)
|
||||
{
|
||||
var startOffset = offset;
|
||||
offset += MessagePackBinary.WriteArrayHeader(ref bytes, offset, 8);
|
||||
|
||||
offset += formatterResolver.GetFormatterWithVerify<T1>().Serialize(ref bytes, offset, value.Item1, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T2>().Serialize(ref bytes, offset, value.Item2, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T3>().Serialize(ref bytes, offset, value.Item3, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T4>().Serialize(ref bytes, offset, value.Item4, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T5>().Serialize(ref bytes, offset, value.Item5, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T6>().Serialize(ref bytes, offset, value.Item6, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<T7>().Serialize(ref bytes, offset, value.Item7, formatterResolver);
|
||||
offset += formatterResolver.GetFormatterWithVerify<TRest>().Serialize(ref bytes, offset, value.Rest, formatterResolver);
|
||||
|
||||
return offset - startOffset;
|
||||
}
|
||||
|
||||
public ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
|
||||
{
|
||||
if (MessagePackBinary.IsNil(bytes, offset))
|
||||
{
|
||||
throw new InvalidOperationException("Data is Nil, ValueTuple can not be null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var startOffset = offset;
|
||||
var count = MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
|
||||
if (count != 8) throw new InvalidOperationException("Invalid ValueTuple count");
|
||||
offset += readSize;
|
||||
|
||||
var item1 = formatterResolver.GetFormatterWithVerify<T1>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item2 = formatterResolver.GetFormatterWithVerify<T2>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item3 = formatterResolver.GetFormatterWithVerify<T3>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item4 = formatterResolver.GetFormatterWithVerify<T4>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item5 = formatterResolver.GetFormatterWithVerify<T5>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item6 = formatterResolver.GetFormatterWithVerify<T6>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item7 = formatterResolver.GetFormatterWithVerify<T7>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
var item8 = formatterResolver.GetFormatterWithVerify<TRest>().Deserialize(bytes, offset, formatterResolver, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
readSize = offset - startOffset;
|
||||
return new ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>(item1, item2, item3, item4, item5, item6, item7, item8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cf9648ed9c9e36a4b878654893321198
|
||||
timeCreated: 1488287483
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
60
Assets/Scripts/MessagePack/IFormatterResolver.cs
Normal file
60
Assets/Scripts/MessagePack/IFormatterResolver.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
|
||||
using MessagePack.Formatters;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
public interface IFormatterResolver
|
||||
{
|
||||
IMessagePackFormatter<T> GetFormatter<T>();
|
||||
}
|
||||
|
||||
public static class FormatterResolverExtensions
|
||||
{
|
||||
public static IMessagePackFormatter<T> GetFormatterWithVerify<T>(this IFormatterResolver resolver)
|
||||
{
|
||||
IMessagePackFormatter<T> formatter;
|
||||
try
|
||||
{
|
||||
formatter = resolver.GetFormatter<T>();
|
||||
}
|
||||
catch (TypeInitializationException ex)
|
||||
{
|
||||
Exception inner = ex;
|
||||
while (inner.InnerException != null)
|
||||
{
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
|
||||
throw inner;
|
||||
}
|
||||
|
||||
if (formatter == null)
|
||||
{
|
||||
throw new FormatterNotRegisteredException(typeof(T).FullName + " is not registered in this resolver. resolver:" + resolver.GetType().Name);
|
||||
}
|
||||
|
||||
return formatter;
|
||||
}
|
||||
|
||||
#if !UNITY_WSA
|
||||
|
||||
public static object GetFormatterDynamic(this IFormatterResolver resolver, Type type)
|
||||
{
|
||||
var methodInfo = typeof(IFormatterResolver).GetRuntimeMethod("GetFormatter", Type.EmptyTypes);
|
||||
|
||||
var formatter = methodInfo.MakeGenericMethod(type).Invoke(resolver, null);
|
||||
return formatter;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
public class FormatterNotRegisteredException : Exception
|
||||
{
|
||||
public FormatterNotRegisteredException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/IFormatterResolver.cs.meta
Normal file
12
Assets/Scripts/MessagePack/IFormatterResolver.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1f129219f75d54e4c9724475b2a222bb
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
public interface IMessagePackSerializationCallbackReceiver
|
||||
{
|
||||
void OnBeforeSerialize();
|
||||
void OnAfterDeserialize();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 66c409f4f185d7242aa98c827521e880
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/Scripts/MessagePack/Internal.meta
Normal file
9
Assets/Scripts/MessagePack/Internal.meta
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b5d7c71f646ae9e4ea575ef78fc57bb8
|
||||
folderAsset: yes
|
||||
timeCreated: 1487501809
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
277
Assets/Scripts/MessagePack/Internal/AsymmetricKeyHashTable.cs
Normal file
277
Assets/Scripts/MessagePack/Internal/AsymmetricKeyHashTable.cs
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
// Safe for multiple-read, single-write.
|
||||
|
||||
// Add and Get Key is asymmetric.
|
||||
|
||||
internal interface IAsymmetricEqualityComparer<TKey1, TKey2>
|
||||
{
|
||||
int GetHashCode(TKey1 key1);
|
||||
int GetHashCode(TKey2 key2);
|
||||
bool Equals(TKey1 x, TKey1 y); // when used rehash
|
||||
bool Equals(TKey1 x, TKey2 y); // when used get
|
||||
}
|
||||
|
||||
internal class StringArraySegmentByteAscymmetricEqualityComparer : IAsymmetricEqualityComparer<byte[], ArraySegment<byte>>
|
||||
{
|
||||
static readonly bool Is32Bit = (IntPtr.Size == 4);
|
||||
|
||||
public bool Equals(byte[] x, byte[] y)
|
||||
{
|
||||
if (x.Length != y.Length) return false;
|
||||
|
||||
for (int i = 0; i < x.Length; i++)
|
||||
{
|
||||
if (x[i] != y[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Equals(byte[] x, ArraySegment<byte> y)
|
||||
{
|
||||
return ByteArrayComparer.Equals(y.Array, y.Offset, y.Count, x);
|
||||
}
|
||||
|
||||
public int GetHashCode(byte[] key1)
|
||||
{
|
||||
return GetHashCode(new ArraySegment<byte>(key1, 0, key1.Length));
|
||||
}
|
||||
|
||||
public int GetHashCode(ArraySegment<byte> key2)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
if (Is32Bit)
|
||||
{
|
||||
return (int)FarmHash.Hash32(key2.Array, key2.Offset, key2.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (int)FarmHash.Hash64(key2.Array, key2.Offset, key2.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class AsymmetricKeyHashTable<TKey1, TKey2, TValue>
|
||||
{
|
||||
Entry[] buckets;
|
||||
int size; // only use in writer lock
|
||||
|
||||
readonly object writerLock = new object();
|
||||
readonly float loadFactor;
|
||||
readonly IAsymmetricEqualityComparer<TKey1, TKey2> comparer;
|
||||
|
||||
public AsymmetricKeyHashTable(IAsymmetricEqualityComparer<TKey1, TKey2> comparer)
|
||||
: this(4, 0.72f, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
public AsymmetricKeyHashTable(int capacity, float loadFactor, IAsymmetricEqualityComparer<TKey1, TKey2> comparer)
|
||||
{
|
||||
var tableSize = CalculateCapacity(capacity, loadFactor);
|
||||
this.buckets = new Entry[tableSize];
|
||||
this.loadFactor = loadFactor;
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
public TValue AddOrGet(TKey1 key1, Func<TKey1, TValue> valueFactory)
|
||||
{
|
||||
TValue v;
|
||||
TryAddInternal(key1, valueFactory, out v);
|
||||
return v;
|
||||
}
|
||||
|
||||
public bool TryAdd(TKey1 key, TValue value)
|
||||
{
|
||||
return TryAdd(key, _ => value); // closure capture
|
||||
}
|
||||
|
||||
public bool TryAdd(TKey1 key, Func<TKey1, TValue> valueFactory)
|
||||
{
|
||||
TValue _;
|
||||
return TryAddInternal(key, valueFactory, out _);
|
||||
}
|
||||
|
||||
bool TryAddInternal(TKey1 key, Func<TKey1, TValue> valueFactory, out TValue resultingValue)
|
||||
{
|
||||
lock (writerLock)
|
||||
{
|
||||
var nextCapacity = CalculateCapacity(size + 1, loadFactor);
|
||||
|
||||
if (buckets.Length < nextCapacity)
|
||||
{
|
||||
// rehash
|
||||
var nextBucket = new Entry[nextCapacity];
|
||||
for (int i = 0; i < buckets.Length; i++)
|
||||
{
|
||||
var e = buckets[i];
|
||||
while (e != null)
|
||||
{
|
||||
var newEntry = new Entry { Key = e.Key, Value = e.Value, Hash = e.Hash };
|
||||
AddToBuckets(nextBucket, key, newEntry, null, out resultingValue);
|
||||
e = e.Next;
|
||||
}
|
||||
}
|
||||
|
||||
// add entry(if failed to add, only do resize)
|
||||
var successAdd = AddToBuckets(nextBucket, key, null, valueFactory, out resultingValue);
|
||||
|
||||
// replace field(threadsafe for read)
|
||||
VolatileWrite(ref buckets, nextBucket);
|
||||
|
||||
if (successAdd) size++;
|
||||
return successAdd;
|
||||
}
|
||||
else
|
||||
{
|
||||
// add entry(insert last is thread safe for read)
|
||||
var successAdd = AddToBuckets(buckets, key, null, valueFactory, out resultingValue);
|
||||
if (successAdd) size++;
|
||||
return successAdd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AddToBuckets(Entry[] buckets, TKey1 newKey, Entry newEntryOrNull, Func<TKey1, TValue> valueFactory, out TValue resultingValue)
|
||||
{
|
||||
var h = (newEntryOrNull != null) ? newEntryOrNull.Hash : comparer.GetHashCode(newKey);
|
||||
if (buckets[h & (buckets.Length - 1)] == null)
|
||||
{
|
||||
if (newEntryOrNull != null)
|
||||
{
|
||||
resultingValue = newEntryOrNull.Value;
|
||||
VolatileWrite(ref buckets[h & (buckets.Length - 1)], newEntryOrNull);
|
||||
}
|
||||
else
|
||||
{
|
||||
resultingValue = valueFactory(newKey);
|
||||
VolatileWrite(ref buckets[h & (buckets.Length - 1)], new Entry { Key = newKey, Value = resultingValue, Hash = h });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var searchLastEntry = buckets[h & (buckets.Length - 1)];
|
||||
while (true)
|
||||
{
|
||||
if (comparer.Equals(searchLastEntry.Key, newKey))
|
||||
{
|
||||
resultingValue = searchLastEntry.Value;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (searchLastEntry.Next == null)
|
||||
{
|
||||
if (newEntryOrNull != null)
|
||||
{
|
||||
resultingValue = newEntryOrNull.Value;
|
||||
VolatileWrite(ref searchLastEntry.Next, newEntryOrNull);
|
||||
}
|
||||
else
|
||||
{
|
||||
resultingValue = valueFactory(newKey);
|
||||
VolatileWrite(ref searchLastEntry.Next, new Entry { Key = newKey, Value = resultingValue, Hash = h });
|
||||
}
|
||||
break;
|
||||
}
|
||||
searchLastEntry = searchLastEntry.Next;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey2 key, out TValue value)
|
||||
{
|
||||
var table = buckets;
|
||||
var hash = comparer.GetHashCode(key);
|
||||
var entry = table[hash & table.Length - 1];
|
||||
|
||||
if (entry == null) goto NOT_FOUND;
|
||||
|
||||
if (comparer.Equals(entry.Key, key))
|
||||
{
|
||||
value = entry.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
var next = entry.Next;
|
||||
while (next != null)
|
||||
{
|
||||
if (comparer.Equals(next.Key, key))
|
||||
{
|
||||
value = next.Value;
|
||||
return true;
|
||||
}
|
||||
next = next.Next;
|
||||
}
|
||||
|
||||
NOT_FOUND:
|
||||
value = default(TValue);
|
||||
return false;
|
||||
}
|
||||
|
||||
static int CalculateCapacity(int collectionSize, float loadFactor)
|
||||
{
|
||||
var initialCapacity = (int)(((float)collectionSize) / loadFactor);
|
||||
var capacity = 1;
|
||||
while (capacity < initialCapacity)
|
||||
{
|
||||
capacity <<= 1;
|
||||
}
|
||||
|
||||
if (capacity < 8)
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
return capacity;
|
||||
}
|
||||
|
||||
static void VolatileWrite(ref Entry location, Entry value)
|
||||
{
|
||||
System.Threading.Volatile.Write(ref location, value);
|
||||
}
|
||||
|
||||
static void VolatileWrite(ref Entry[] location, Entry[] value)
|
||||
{
|
||||
System.Threading.Volatile.Write(ref location, value);
|
||||
}
|
||||
|
||||
class Entry
|
||||
{
|
||||
public TKey1 Key;
|
||||
public TValue Value;
|
||||
public int Hash;
|
||||
public Entry Next;
|
||||
|
||||
// from debugger only
|
||||
public override string ToString()
|
||||
{
|
||||
return "Count:" + Count;
|
||||
}
|
||||
|
||||
internal int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
var count = 1;
|
||||
var n = this;
|
||||
while (n.Next != null)
|
||||
{
|
||||
count++;
|
||||
n = n.Next;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0cb7ee211ccebcd408e2e3f53d8b9f8a
|
||||
timeCreated: 1503499035
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
679
Assets/Scripts/MessagePack/Internal/AutomataDictionary.cs
Normal file
679
Assets/Scripts/MessagePack/Internal/AutomataDictionary.cs
Normal file
|
|
@ -0,0 +1,679 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
// Key = long, Value = int for UTF8String Dictionary
|
||||
|
||||
public class AutomataDictionary : IEnumerable<KeyValuePair<string, int>>
|
||||
{
|
||||
readonly AutomataNode root;
|
||||
|
||||
public AutomataDictionary()
|
||||
{
|
||||
root = new AutomataNode(0);
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
public unsafe void Add(string str, int value)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(str);
|
||||
fixed (byte* buffer = &bytes[0])
|
||||
{
|
||||
var node = root;
|
||||
|
||||
var p = buffer;
|
||||
var rest = bytes.Length;
|
||||
while (rest != 0)
|
||||
{
|
||||
var key = AutomataKeyGen.GetKey(ref p, ref rest);
|
||||
|
||||
if (rest == 0)
|
||||
{
|
||||
node = node.Add(key, value, str);
|
||||
}
|
||||
else
|
||||
{
|
||||
node = node.Add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe bool TryGetValue(byte[] bytes, int offset, int count, out int value)
|
||||
{
|
||||
fixed (byte* p = &bytes[0])
|
||||
{
|
||||
var p1 = p;
|
||||
var node = root;
|
||||
var rest = count;
|
||||
|
||||
while (rest != 0 && node != null)
|
||||
{
|
||||
node = node.SearchNext(ref p1, ref rest);
|
||||
}
|
||||
|
||||
if (node == null)
|
||||
{
|
||||
value = -1;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = node.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
// for Unity, use safe only.
|
||||
|
||||
public void Add(string str, int value)
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(str);
|
||||
var offset = 0;
|
||||
|
||||
var node = root;
|
||||
|
||||
var rest = bytes.Length;
|
||||
while (rest != 0)
|
||||
{
|
||||
var key = AutomataKeyGen.GetKeySafe(bytes, ref offset, ref rest);
|
||||
|
||||
if (rest == 0)
|
||||
{
|
||||
node = node.Add(key, value, str);
|
||||
}
|
||||
else
|
||||
{
|
||||
node = node.Add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
public bool TryGetValueSafe(ArraySegment<byte> key, out int value)
|
||||
{
|
||||
var node = root;
|
||||
var bytes = key.Array;
|
||||
var offset = key.Offset;
|
||||
var rest = key.Count;
|
||||
|
||||
while (rest != 0 && node != null)
|
||||
{
|
||||
node = node.SearchNextSafe(bytes, ref offset, ref rest);
|
||||
}
|
||||
|
||||
if (node == null)
|
||||
{
|
||||
value = -1;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = node.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// for debugging
|
||||
public override string ToString()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
ToStringCore(root.YieldChildren(), sb, 0);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
static void ToStringCore(IEnumerable<AutomataNode> nexts, StringBuilder sb, int depth)
|
||||
{
|
||||
foreach (var item in nexts)
|
||||
{
|
||||
if (depth != 0)
|
||||
{
|
||||
sb.Append(' ', depth * 2);
|
||||
}
|
||||
sb.Append("[" + item.Key + "]");
|
||||
if (item.Value != -1)
|
||||
{
|
||||
sb.Append("(" + item.originalKey + ")");
|
||||
sb.Append(" = ");
|
||||
sb.Append(item.Value);
|
||||
}
|
||||
sb.AppendLine();
|
||||
ToStringCore(item.YieldChildren(), sb, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
|
||||
{
|
||||
return YieldCore(this.root.YieldChildren()).GetEnumerator();
|
||||
}
|
||||
|
||||
static IEnumerable<KeyValuePair<string, int>> YieldCore(IEnumerable<AutomataNode> nexts)
|
||||
{
|
||||
foreach (var item in nexts)
|
||||
{
|
||||
if (item.Value != -1) yield return new KeyValuePair<string, int>(item.originalKey, item.Value);
|
||||
foreach (var x in YieldCore(item.YieldChildren())) yield return x;
|
||||
}
|
||||
}
|
||||
|
||||
// IL Emit
|
||||
|
||||
public void EmitMatch(ILGenerator il, LocalBuilder p, LocalBuilder rest, LocalBuilder key, Action<KeyValuePair<string, int>> onFound, Action onNotFound)
|
||||
{
|
||||
root.EmitSearchNext(il, p, rest, key, onFound, onNotFound);
|
||||
}
|
||||
|
||||
class AutomataNode : IComparable<AutomataNode>
|
||||
{
|
||||
static readonly AutomataNode[] emptyNodes = new AutomataNode[0];
|
||||
static readonly ulong[] emptyKeys = new ulong[0];
|
||||
|
||||
public ulong Key;
|
||||
public int Value;
|
||||
public string originalKey;
|
||||
|
||||
AutomataNode[] nexts;
|
||||
ulong[] nextKeys;
|
||||
int count;
|
||||
|
||||
public bool HasChildren { get { return count != 0; } }
|
||||
|
||||
public AutomataNode(ulong key)
|
||||
{
|
||||
this.Key = key;
|
||||
this.Value = -1;
|
||||
this.nexts = emptyNodes;
|
||||
this.nextKeys = emptyKeys;
|
||||
this.count = 0;
|
||||
this.originalKey = null;
|
||||
}
|
||||
|
||||
public AutomataNode Add(ulong key)
|
||||
{
|
||||
var index = Array.BinarySearch(nextKeys, 0, count, key);
|
||||
if (index < 0)
|
||||
{
|
||||
if (nexts.Length == count)
|
||||
{
|
||||
Array.Resize<AutomataNode>(ref nexts, (count == 0) ? 4 : (count * 2));
|
||||
Array.Resize<ulong>(ref nextKeys, (count == 0) ? 4 : (count * 2));
|
||||
}
|
||||
count++;
|
||||
|
||||
var nextNode = new AutomataNode(key);
|
||||
nexts[count - 1] = nextNode;
|
||||
nextKeys[count - 1] = key;
|
||||
Array.Sort(nexts, 0, count);
|
||||
Array.Sort(nextKeys, 0, count);
|
||||
return nextNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
return nexts[index];
|
||||
}
|
||||
}
|
||||
|
||||
public AutomataNode Add(ulong key, int value, string originalKey)
|
||||
{
|
||||
var v = Add(key);
|
||||
v.Value = value;
|
||||
v.originalKey = originalKey;
|
||||
return v;
|
||||
}
|
||||
|
||||
public unsafe AutomataNode SearchNext(ref byte* p, ref int rest)
|
||||
{
|
||||
var key = AutomataKeyGen.GetKey(ref p, ref rest);
|
||||
if (count < 4)
|
||||
{
|
||||
// linear search
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (nextKeys[i] == key)
|
||||
{
|
||||
return nexts[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// binary search
|
||||
var index = BinarySearch(nextKeys, 0, count, key);
|
||||
if (index >= 0)
|
||||
{
|
||||
return nexts[index];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public unsafe AutomataNode SearchNextSafe(byte[] p, ref int offset, ref int rest)
|
||||
{
|
||||
var key = AutomataKeyGen.GetKeySafe(p, ref offset, ref rest);
|
||||
if (count < 4)
|
||||
{
|
||||
// linear search
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (nextKeys[i] == key)
|
||||
{
|
||||
return nexts[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// binary search
|
||||
var index = BinarySearch(nextKeys, 0, count, key);
|
||||
if (index >= 0)
|
||||
{
|
||||
return nexts[index];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static int BinarySearch(ulong[] array, int index, int length, ulong value)
|
||||
{
|
||||
int lo = index;
|
||||
int hi = index + length - 1;
|
||||
while (lo <= hi)
|
||||
{
|
||||
int i = lo + ((hi - lo) >> 1);
|
||||
|
||||
var arrayValue = array[i];
|
||||
int order;
|
||||
if (arrayValue < value) order = -1;
|
||||
else if (arrayValue > value) order = 1;
|
||||
else order = 0;
|
||||
|
||||
if (order == 0) return i;
|
||||
if (order < 0)
|
||||
{
|
||||
lo = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
hi = i - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~lo;
|
||||
}
|
||||
|
||||
public int CompareTo(AutomataNode other)
|
||||
{
|
||||
return this.Key.CompareTo(other.Key);
|
||||
}
|
||||
|
||||
public IEnumerable<AutomataNode> YieldChildren()
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
yield return nexts[i];
|
||||
}
|
||||
}
|
||||
|
||||
// SearchNext(ref byte* p, ref int rest, ref ulong key)
|
||||
public void EmitSearchNext(ILGenerator il, LocalBuilder p, LocalBuilder rest, LocalBuilder key, Action<KeyValuePair<string, int>> onFound, Action onNotFound)
|
||||
{
|
||||
// key = AutomataKeyGen.GetKey(ref p, ref rest);
|
||||
il.EmitLdloca(p);
|
||||
il.EmitLdloca(rest);
|
||||
il.EmitCall(AutomataKeyGen.GetKeyMethod);
|
||||
il.EmitStloc(key);
|
||||
|
||||
// match children.
|
||||
EmitSearchNextCore(il, p, rest, key, onFound, onNotFound, nexts, count);
|
||||
}
|
||||
|
||||
static void EmitSearchNextCore(ILGenerator il, LocalBuilder p, LocalBuilder rest, LocalBuilder key, Action<KeyValuePair<string, int>> onFound, Action onNotFound, AutomataNode[] nexts, int count)
|
||||
{
|
||||
if (count < 4)
|
||||
{
|
||||
// linear-search
|
||||
var valueExists = nexts.Take(count).Where(x => x.Value != -1).ToArray();
|
||||
var childrenExists = nexts.Take(count).Where(x => x.HasChildren).ToArray();
|
||||
var gotoSearchNext = il.DefineLabel();
|
||||
var gotoNotFound = il.DefineLabel();
|
||||
|
||||
{
|
||||
il.EmitLdloc(rest);
|
||||
if (childrenExists.Length != 0 && valueExists.Length == 0)
|
||||
{
|
||||
|
||||
il.Emit(OpCodes.Brfalse, gotoNotFound); // if(rest == 0)
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Brtrue, gotoSearchNext); // if(rest != 0)
|
||||
}
|
||||
}
|
||||
{
|
||||
var ifValueNexts = Enumerable.Range(0, Math.Max(valueExists.Length - 1, 0)).Select(_ => il.DefineLabel()).ToArray();
|
||||
for (int i = 0; i < valueExists.Length; i++)
|
||||
{
|
||||
var notFoundLabel = il.DefineLabel();
|
||||
if (i != 0)
|
||||
{
|
||||
il.MarkLabel(ifValueNexts[i - 1]);
|
||||
}
|
||||
|
||||
il.EmitLdloc(key);
|
||||
il.EmitULong(valueExists[i].Key);
|
||||
il.Emit(OpCodes.Bne_Un, notFoundLabel);
|
||||
// found
|
||||
onFound(new KeyValuePair<string, int>(valueExists[i].originalKey, valueExists[i].Value));
|
||||
|
||||
// notfound
|
||||
il.MarkLabel(notFoundLabel);
|
||||
if (i != valueExists.Length - 1)
|
||||
{
|
||||
il.Emit(OpCodes.Br, ifValueNexts[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
onNotFound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
il.MarkLabel(gotoSearchNext);
|
||||
var ifRecNext = Enumerable.Range(0, Math.Max(childrenExists.Length - 1, 0)).Select(_ => il.DefineLabel()).ToArray();
|
||||
for (int i = 0; i < childrenExists.Length; i++)
|
||||
{
|
||||
var notFoundLabel = il.DefineLabel();
|
||||
if (i != 0)
|
||||
{
|
||||
il.MarkLabel(ifRecNext[i - 1]);
|
||||
}
|
||||
|
||||
il.EmitLdloc(key);
|
||||
il.EmitULong(childrenExists[i].Key);
|
||||
il.Emit(OpCodes.Bne_Un, notFoundLabel);
|
||||
// found
|
||||
childrenExists[i].EmitSearchNext(il, p, rest, key, onFound, onNotFound);
|
||||
// notfound
|
||||
il.MarkLabel(notFoundLabel);
|
||||
if (i != childrenExists.Length - 1)
|
||||
{
|
||||
il.Emit(OpCodes.Br, ifRecNext[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
onNotFound();
|
||||
}
|
||||
}
|
||||
|
||||
il.MarkLabel(gotoNotFound);
|
||||
onNotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
// binary-search
|
||||
var midline = count / 2;
|
||||
var mid = nexts[midline].Key;
|
||||
var l = nexts.Take(count).Take(midline).ToArray();
|
||||
var r = nexts.Take(count).Skip(midline).ToArray();
|
||||
|
||||
var gotoRight = il.DefineLabel();
|
||||
|
||||
// if(key < mid)
|
||||
il.EmitLdloc(key);
|
||||
il.EmitULong(mid);
|
||||
il.Emit(OpCodes.Bge, gotoRight);
|
||||
EmitSearchNextCore(il, p, rest, key, onFound, onNotFound, l, l.Length);
|
||||
|
||||
// else
|
||||
il.MarkLabel(gotoRight);
|
||||
EmitSearchNextCore(il, p, rest, key, onFound, onNotFound, r, r.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class AutomataKeyGen
|
||||
{
|
||||
public static readonly MethodInfo GetKeyMethod = typeof(AutomataKeyGen).GetRuntimeMethod("GetKey", new[] { typeof(byte*).MakeByRefType(), typeof(int).MakeByRefType() });
|
||||
// public static readonly MethodInfo GetKeySafeMethod = typeof(AutomataKeyGen).GetRuntimeMethod("GetKeySafe", new[] { typeof(byte[]), typeof(int).MakeByRefType(), typeof(int).MakeByRefType() });
|
||||
|
||||
public static unsafe ulong GetKey(ref byte* p, ref int rest)
|
||||
{
|
||||
int readSize;
|
||||
ulong key;
|
||||
|
||||
unchecked
|
||||
{
|
||||
if (rest >= 8)
|
||||
{
|
||||
key = *(ulong*)p;
|
||||
readSize = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (rest)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
key = *(byte*)p;
|
||||
readSize = 1;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
key = *(ushort*)p;
|
||||
readSize = 2;
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
var a = *p;
|
||||
var b = *(ushort*)(p + 1);
|
||||
key = ((ulong)a | (ulong)b << 8);
|
||||
readSize = 3;
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
key = *(uint*)p;
|
||||
readSize = 4;
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
var a = *p;
|
||||
var b = *(uint*)(p + 1);
|
||||
key = ((ulong)a | (ulong)b << 8);
|
||||
readSize = 5;
|
||||
break;
|
||||
}
|
||||
case 6:
|
||||
{
|
||||
ulong a = *(ushort*)p;
|
||||
ulong b = *(uint*)(p + 2);
|
||||
key = (a | (b << 16));
|
||||
readSize = 6;
|
||||
break;
|
||||
}
|
||||
case 7:
|
||||
{
|
||||
var a = *(byte*)p;
|
||||
var b = *(ushort*)(p + 1);
|
||||
var c = *(uint*)(p + 3);
|
||||
key = ((ulong)a | (ulong)b << 8 | (ulong)c << 24);
|
||||
readSize = 7;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new InvalidOperationException("Not Supported Length");
|
||||
}
|
||||
}
|
||||
|
||||
p += readSize;
|
||||
rest -= readSize;
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public static ulong GetKeySafe(byte[] bytes, ref int offset, ref int rest)
|
||||
{
|
||||
int readSize;
|
||||
ulong key;
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
if (rest >= 8)
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 16 | (ulong)bytes[offset + 3] << 24
|
||||
| (ulong)bytes[offset + 4] << 32 | (ulong)bytes[offset + 5] << 40 | (ulong)bytes[offset + 6] << 48 | (ulong)bytes[offset + 7] << 56;
|
||||
readSize = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (rest)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
key = bytes[offset];
|
||||
readSize = 1;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8;
|
||||
readSize = 2;
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 16;
|
||||
readSize = 3;
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 16 | (ulong)bytes[offset + 3] << 24;
|
||||
readSize = 4;
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 16 | (ulong)bytes[offset + 3] << 24
|
||||
| (ulong)bytes[offset + 4] << 32;
|
||||
readSize = 5;
|
||||
break;
|
||||
}
|
||||
case 6:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 16 | (ulong)bytes[offset + 3] << 24
|
||||
| (ulong)bytes[offset + 4] << 32 | (ulong)bytes[offset + 5] << 40;
|
||||
readSize = 6;
|
||||
break;
|
||||
}
|
||||
case 7:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 0 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 16 | (ulong)bytes[offset + 3] << 24
|
||||
| (ulong)bytes[offset + 4] << 32 | (ulong)bytes[offset + 5] << 40 | (ulong)bytes[offset + 6] << 48;
|
||||
readSize = 7;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new InvalidOperationException("Not Supported Length");
|
||||
}
|
||||
}
|
||||
|
||||
offset += readSize;
|
||||
rest -= readSize;
|
||||
return key;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
if (rest >= 8)
|
||||
{
|
||||
key = (ulong)bytes[offset] << 56 | (ulong)bytes[offset + 1] << 48 | (ulong)bytes[offset + 2] << 40 | (ulong)bytes[offset + 3] << 32
|
||||
| (ulong)bytes[offset + 4] << 24 | (ulong)bytes[offset + 5] << 16 | (ulong)bytes[offset + 6] << 8 | (ulong)bytes[offset + 7];
|
||||
readSize = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (rest)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
key = bytes[offset];
|
||||
readSize = 1;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 8 | (ulong)bytes[offset + 1] << 0;
|
||||
readSize = 2;
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 16 | (ulong)bytes[offset + 1] << 8 | (ulong)bytes[offset + 2] << 0;
|
||||
readSize = 3;
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 24 | (ulong)bytes[offset + 1] << 16 | (ulong)bytes[offset + 2] << 8 | (ulong)bytes[offset + 3] << 0;
|
||||
readSize = 4;
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 32 | (ulong)bytes[offset + 1] << 24 | (ulong)bytes[offset + 2] << 16 | (ulong)bytes[offset + 3] << 8
|
||||
| (ulong)bytes[offset + 4] << 0;
|
||||
readSize = 5;
|
||||
break;
|
||||
}
|
||||
case 6:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 40 | (ulong)bytes[offset + 1] << 32 | (ulong)bytes[offset + 2] << 24 | (ulong)bytes[offset + 3] << 16
|
||||
| (ulong)bytes[offset + 4] << 8 | (ulong)bytes[offset + 5] << 0;
|
||||
readSize = 6;
|
||||
break;
|
||||
}
|
||||
case 7:
|
||||
{
|
||||
key = (ulong)bytes[offset] << 48 | (ulong)bytes[offset + 1] << 40 | (ulong)bytes[offset + 2] << 32 | (ulong)bytes[offset + 3] << 24
|
||||
| (ulong)bytes[offset + 4] << 16 | (ulong)bytes[offset + 5] << 8 | (ulong)bytes[offset + 6] << 0;
|
||||
readSize = 7;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new InvalidOperationException("Not Supported Length");
|
||||
}
|
||||
}
|
||||
|
||||
offset += readSize;
|
||||
rest -= readSize;
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9422203c151b16c4e8bd7c4b9e96f7ab
|
||||
timeCreated: 1503901444
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
139
Assets/Scripts/MessagePack/Internal/ByteArrayComparer.cs
Normal file
139
Assets/Scripts/MessagePack/Internal/ByteArrayComparer.cs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
public static class ByteArrayComparer
|
||||
{
|
||||
#if ENABLE_UNSAFE_MSGPACK
|
||||
|
||||
#if NETSTANDARD
|
||||
|
||||
static readonly bool Is32Bit = (IntPtr.Size == 4);
|
||||
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
public static int GetHashCode(byte[] bytes, int offset, int count)
|
||||
{
|
||||
if (Is32Bit)
|
||||
{
|
||||
return unchecked((int)FarmHash.Hash32(bytes, offset, count));
|
||||
}
|
||||
else
|
||||
{
|
||||
return unchecked((int)FarmHash.Hash64(bytes, offset, count));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
public static unsafe bool Equals(byte[] xs, int xsOffset, int xsCount, byte[] ys)
|
||||
{
|
||||
return Equals(xs, xsOffset, xsCount, ys, 0, ys.Length);
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
public static unsafe bool Equals(byte[] xs, int xsOffset, int xsCount, byte[] ys, int ysOffset, int ysCount)
|
||||
{
|
||||
if (xs == null || ys == null || xsCount != ysCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
fixed (byte* p1 = &xs[xsOffset])
|
||||
fixed (byte* p2 = &ys[ysOffset])
|
||||
{
|
||||
switch (xsCount)
|
||||
{
|
||||
case 0:
|
||||
return true;
|
||||
case 1:
|
||||
return *p1 == *p2;
|
||||
case 2:
|
||||
return *(short*)p1 == *(short*)p2;
|
||||
case 3:
|
||||
if (*(byte*)p1 != *(byte*)p2) return false;
|
||||
return *(short*)(p1 + 1) == *(short*)(p2 + 1);
|
||||
case 4:
|
||||
return *(int*)p1 == *(int*)p2;
|
||||
case 5:
|
||||
if (*(byte*)p1 != *(byte*)p2) return false;
|
||||
return *(int*)(p1 + 1) == *(int*)(p2 + 1);
|
||||
case 6:
|
||||
if (*(short*)p1 != *(short*)p2) return false;
|
||||
return *(int*)(p1 + 2) == *(int*)(p2 + 2);
|
||||
case 7:
|
||||
if (*(byte*)p1 != *(byte*)p2) return false;
|
||||
if (*(short*)(p1 + 1) != *(short*)(p2 + 1)) return false;
|
||||
return *(int*)(p1 + 3) == *(int*)(p2 + 3);
|
||||
default:
|
||||
{
|
||||
var x1 = p1;
|
||||
var x2 = p2;
|
||||
|
||||
byte* xEnd = p1 + xsCount - 8;
|
||||
byte* yEnd = p2 + ysCount - 8;
|
||||
|
||||
while (x1 < xEnd)
|
||||
{
|
||||
if (*(long*)x1 != *(long*)x2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
x1 += 8;
|
||||
x2 += 8;
|
||||
}
|
||||
|
||||
return *(long*)xEnd == *(long*)yEnd;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
public static bool Equals(byte[] xs, int xsOffset, int xsCount, byte[] ys)
|
||||
{
|
||||
if (xs == null || ys == null || xsCount != ys.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ys.Length; i++)
|
||||
{
|
||||
if (xs[xsOffset++] != ys[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
public static bool Equals(byte[] xs, int xsOffset, int xsCount, byte[] ys, int ysOffset, int ysCount)
|
||||
{
|
||||
if (xs == null || ys == null || xsCount != ysCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < xsCount; i++)
|
||||
{
|
||||
if (xs[xsOffset++] != ys[ysOffset++]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f1ca7a6e056df3949a27f2cf69173ea6
|
||||
timeCreated: 1503499035
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
209
Assets/Scripts/MessagePack/Internal/ByteArrayStringHashTable.cs
Normal file
209
Assets/Scripts/MessagePack/Internal/ByteArrayStringHashTable.cs
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
// like ArraySegment<byte> hashtable.
|
||||
// Add is safe for construction phase only and requires capacity(does not do rehash)
|
||||
// and specialized for internal use(nongenerics, TValue is int)
|
||||
|
||||
// internal, but code generator requires this class
|
||||
public class ByteArrayStringHashTable : IEnumerable<KeyValuePair<string, int>>
|
||||
{
|
||||
readonly Entry[][] buckets; // immutable array(faster than linkedlist)
|
||||
readonly ulong indexFor;
|
||||
|
||||
public ByteArrayStringHashTable(int capacity)
|
||||
: this(capacity, 0.42f) // default: 0.75f -> 0.42f
|
||||
{
|
||||
}
|
||||
|
||||
public ByteArrayStringHashTable(int capacity, float loadFactor)
|
||||
{
|
||||
var tableSize = CalculateCapacity(capacity, loadFactor);
|
||||
this.buckets = new Entry[tableSize][];
|
||||
this.indexFor = (ulong)buckets.Length - 1;
|
||||
}
|
||||
|
||||
public void Add(string key, int value)
|
||||
{
|
||||
if (!TryAddInternal(Encoding.UTF8.GetBytes(key), value))
|
||||
{
|
||||
throw new ArgumentException("Key was already exists. Key:" + key);
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(byte[] key, int value)
|
||||
{
|
||||
if (!TryAddInternal(key, value))
|
||||
{
|
||||
throw new ArgumentException("Key was already exists. Key:" + key);
|
||||
}
|
||||
}
|
||||
|
||||
bool TryAddInternal(byte[] key, int value)
|
||||
{
|
||||
var h = ByteArrayGetHashCode(key, 0, key.Length);
|
||||
var entry = new Entry { Key = key, Value = value };
|
||||
|
||||
var array = buckets[h & (indexFor)];
|
||||
if (array == null)
|
||||
{
|
||||
buckets[h & (indexFor)] = new[] { entry };
|
||||
}
|
||||
else
|
||||
{
|
||||
// check duplicate
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
var e = array[i].Key;
|
||||
if (ByteArrayComparer.Equals(key, 0, key.Length, e))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var newArray = new Entry[array.Length + 1];
|
||||
Array.Copy(array, newArray, array.Length);
|
||||
array = newArray;
|
||||
array[array.Length - 1] = entry;
|
||||
buckets[h & (indexFor)] = array;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetValue(ArraySegment<byte> key, out int value)
|
||||
{
|
||||
var table = buckets;
|
||||
var hash = ByteArrayGetHashCode(key.Array, key.Offset, key.Count);
|
||||
var entry = table[hash & indexFor];
|
||||
|
||||
if (entry == null) goto NOT_FOUND;
|
||||
|
||||
{
|
||||
#if NETSTANDARD
|
||||
ref var v = ref entry[0];
|
||||
#else
|
||||
var v = entry[0];
|
||||
#endif
|
||||
if (ByteArrayComparer.Equals(key.Array, key.Offset, key.Count, v.Key))
|
||||
{
|
||||
value = v.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i < entry.Length; i++)
|
||||
{
|
||||
#if NETSTANDARD
|
||||
ref var v = ref entry[i];
|
||||
#else
|
||||
var v = entry[i];
|
||||
#endif
|
||||
if (ByteArrayComparer.Equals(key.Array, key.Offset, key.Count, v.Key))
|
||||
{
|
||||
value = v.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
NOT_FOUND:
|
||||
value = default(int);
|
||||
return false;
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
static readonly bool Is32Bit = (IntPtr.Size == 4);
|
||||
#endif
|
||||
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
static ulong ByteArrayGetHashCode(byte[] x, int offset, int count)
|
||||
{
|
||||
#if NETSTANDARD
|
||||
// FarmHash https://github.com/google/farmhash
|
||||
if (x == null) return 0;
|
||||
|
||||
if (Is32Bit)
|
||||
{
|
||||
return (ulong)FarmHash.Hash32(x, offset, count);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FarmHash.Hash64(x, offset, count);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// FNV1-1a 32bit https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
||||
uint hash = 0;
|
||||
if (x != null)
|
||||
{
|
||||
var max = offset + count;
|
||||
|
||||
hash = 2166136261;
|
||||
for (int i = offset; i < max; i++)
|
||||
{
|
||||
hash = unchecked((x[i] ^ hash) * 16777619);
|
||||
}
|
||||
}
|
||||
|
||||
return (ulong)hash;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
static int CalculateCapacity(int collectionSize, float loadFactor)
|
||||
{
|
||||
var initialCapacity = (int)(((float)collectionSize) / loadFactor);
|
||||
var capacity = 1;
|
||||
while (capacity < initialCapacity)
|
||||
{
|
||||
capacity <<= 1;
|
||||
}
|
||||
|
||||
if (capacity < 8)
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
return capacity;
|
||||
}
|
||||
|
||||
// only for Debug use
|
||||
public IEnumerator<KeyValuePair<string, int>> GetEnumerator()
|
||||
{
|
||||
var b = this.buckets;
|
||||
|
||||
foreach (var item in b)
|
||||
{
|
||||
if (item == null) continue;
|
||||
foreach (var item2 in item)
|
||||
{
|
||||
yield return new KeyValuePair<string, int>(Encoding.UTF8.GetString(item2.Key), item2.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
struct Entry
|
||||
{
|
||||
public byte[] Key;
|
||||
public int Value;
|
||||
|
||||
// for debugging
|
||||
public override string ToString()
|
||||
{
|
||||
return "(" + Encoding.UTF8.GetString(Key) + ", " + Value + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 150b5dab543f3e647922c5017293f831
|
||||
timeCreated: 1501832163
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
47
Assets/Scripts/MessagePack/Internal/DynamicAssembly.cs
Normal file
47
Assets/Scripts/MessagePack/Internal/DynamicAssembly.cs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal class DynamicAssembly
|
||||
{
|
||||
#if NET_35
|
||||
readonly string moduleName;
|
||||
#endif
|
||||
readonly AssemblyBuilder assemblyBuilder;
|
||||
readonly ModuleBuilder moduleBuilder;
|
||||
|
||||
public ModuleBuilder ModuleBuilder { get { return moduleBuilder; } }
|
||||
|
||||
public DynamicAssembly(string moduleName)
|
||||
{
|
||||
#if NET_35
|
||||
this.moduleName = moduleName;
|
||||
this.assemblyBuilder = System.AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.RunAndSave);
|
||||
this.moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName, moduleName + ".dll");
|
||||
#else
|
||||
#if NETSTANDARD
|
||||
this.assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.Run);
|
||||
#else
|
||||
this.assemblyBuilder = System.AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.Run);
|
||||
#endif
|
||||
|
||||
this.moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NET_35
|
||||
|
||||
public AssemblyBuilder Save()
|
||||
{
|
||||
assemblyBuilder.Save(moduleName + ".dll");
|
||||
return assemblyBuilder;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
12
Assets/Scripts/MessagePack/Internal/DynamicAssembly.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Internal/DynamicAssembly.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2fd360de733c1504bb9a002668a6a73c
|
||||
timeCreated: 1488099023
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
88
Assets/Scripts/MessagePack/Internal/ExpressionUtility.cs
Normal file
88
Assets/Scripts/MessagePack/Internal/ExpressionUtility.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
public static class ExpressionUtility
|
||||
{
|
||||
// Method
|
||||
|
||||
static MethodInfo GetMethodInfoCore(LambdaExpression expression)
|
||||
{
|
||||
if (expression == null)
|
||||
{
|
||||
throw new ArgumentNullException("expression");
|
||||
}
|
||||
|
||||
return (expression.Body as MethodCallExpression).Method;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get MethodInfo from Expression for Static(with result) method.
|
||||
/// </summary>
|
||||
public static MethodInfo GetMethodInfo<T>(Expression<Func<T>> expression)
|
||||
{
|
||||
return GetMethodInfoCore(expression);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get MethodInfo from Expression for Static(void) method.
|
||||
/// </summary>
|
||||
public static MethodInfo GetMethodInfo(Expression<Action> expression)
|
||||
{
|
||||
return GetMethodInfoCore(expression);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get MethodInfo from Expression for Instance(with result) method.
|
||||
/// </summary>
|
||||
public static MethodInfo GetMethodInfo<T, TR>(Expression<Func<T, TR>> expression)
|
||||
{
|
||||
return GetMethodInfoCore(expression);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get MethodInfo from Expression for Instance(void) method.
|
||||
/// </summary>
|
||||
public static MethodInfo GetMethodInfo<T>(Expression<Action<T>> expression)
|
||||
{
|
||||
return GetMethodInfoCore(expression);
|
||||
}
|
||||
|
||||
// WithArgument(for ref, out) helper
|
||||
|
||||
/// <summary>
|
||||
/// Get MethodInfo from Expression for Instance(with result) method.
|
||||
/// </summary>
|
||||
public static MethodInfo GetMethodInfo<T, TArg1, TR>(Expression<Func<T, TArg1, TR>> expression)
|
||||
{
|
||||
return GetMethodInfoCore(expression);
|
||||
}
|
||||
|
||||
// Property
|
||||
|
||||
static MemberInfo GetMemberInfoCore<T>(Expression<T> source)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentNullException("source");
|
||||
}
|
||||
|
||||
var memberExpression = source.Body as MemberExpression;
|
||||
return memberExpression.Member;
|
||||
}
|
||||
|
||||
public static PropertyInfo GetPropertyInfo<T, TR>(Expression<Func<T, TR>> expression)
|
||||
{
|
||||
return GetMemberInfoCore(expression) as PropertyInfo;
|
||||
}
|
||||
|
||||
// Field
|
||||
|
||||
public static FieldInfo GetFieldInfo<T, TR>(Expression<Func<T, TR>> expression)
|
||||
{
|
||||
return GetMemberInfoCore(expression) as FieldInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9ce5e75c332bb854c9bfd13f612c7ef0
|
||||
timeCreated: 1505965655
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
604
Assets/Scripts/MessagePack/Internal/FarmHash.cs
Normal file
604
Assets/Scripts/MessagePack/Internal/FarmHash.cs
Normal file
|
|
@ -0,0 +1,604 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
public static class FarmHash
|
||||
{
|
||||
// entry point of 32bit
|
||||
|
||||
#region Hash32
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe uint Hash32(byte[] bytes, int offset, int count)
|
||||
{
|
||||
if (count <= 4)
|
||||
{
|
||||
return Hash32Len0to4(bytes, offset, (uint)count);
|
||||
}
|
||||
|
||||
fixed (byte* p = &bytes[offset])
|
||||
{
|
||||
return Hash32(p, (uint)count);
|
||||
}
|
||||
}
|
||||
|
||||
// port of farmhash.cc, 32bit only
|
||||
|
||||
// Magic numbers for 32-bit hashing. Copied from Murmur3.
|
||||
const uint c1 = 0xcc9e2d51;
|
||||
const uint c2 = 0x1b873593;
|
||||
|
||||
static unsafe uint Fetch32(byte* p)
|
||||
{
|
||||
return *(uint*)p;
|
||||
}
|
||||
|
||||
static uint Rotate32(uint val, int shift)
|
||||
{
|
||||
return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
|
||||
}
|
||||
|
||||
// A 32-bit to 32-bit integer hash copied from Murmur3.
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static uint fmix(uint h)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
h ^= h >> 16;
|
||||
h *= 0x85ebca6b;
|
||||
h ^= h >> 13;
|
||||
h *= 0xc2b2ae35;
|
||||
h ^= h >> 16;
|
||||
return h;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static uint Mur(uint a, uint h)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
// Helper from Murmur3 for combining two 32-bit values.
|
||||
a *= c1;
|
||||
a = Rotate32(a, 17);
|
||||
a *= c2;
|
||||
h ^= a;
|
||||
h = Rotate32(h, 19);
|
||||
return h * 5 + 0xe6546b64;
|
||||
}
|
||||
}
|
||||
|
||||
// 0-4
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe uint Hash32Len0to4(byte[] s, int offset, uint len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint b = 0;
|
||||
uint c = 9;
|
||||
var max = offset + len;
|
||||
for (int i = offset; i < max; i++)
|
||||
{
|
||||
b = b * c1 + s[i];
|
||||
c ^= b;
|
||||
}
|
||||
return fmix(Mur(b, Mur(len, c)));
|
||||
}
|
||||
}
|
||||
|
||||
// 5-12
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe uint Hash32Len5to12(byte* s, uint len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint a = len, b = len * 5, c = 9, d = b;
|
||||
a += Fetch32(s);
|
||||
b += Fetch32(s + len - 4);
|
||||
c += Fetch32(s + ((len >> 1) & 4));
|
||||
return fmix(Mur(c, Mur(b, Mur(a, d))));
|
||||
}
|
||||
}
|
||||
|
||||
// 13-24
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe uint Hash32Len13to24(byte* s, uint len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
uint a = Fetch32(s - 4 + (len >> 1));
|
||||
uint b = Fetch32(s + 4);
|
||||
uint c = Fetch32(s + len - 8);
|
||||
uint d = Fetch32(s + (len >> 1));
|
||||
uint e = Fetch32(s);
|
||||
uint f = Fetch32(s + len - 4);
|
||||
uint h = d * c1 + len;
|
||||
a = Rotate32(a, 12) + f;
|
||||
h = Mur(c, h) + a;
|
||||
a = Rotate32(a, 3) + c;
|
||||
h = Mur(e, h) + a;
|
||||
a = Rotate32(a + f, 12) + d;
|
||||
h = Mur(b, h) + a;
|
||||
return fmix(h);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe uint Hash32(byte* s, uint len)
|
||||
{
|
||||
if (len <= 24)
|
||||
{
|
||||
return len <= 12 ? Hash32Len5to12(s, len) : Hash32Len13to24(s, len);
|
||||
}
|
||||
|
||||
unchecked
|
||||
{
|
||||
// len > 24
|
||||
uint h = len, g = c1 * len, f = g;
|
||||
uint a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
|
||||
uint a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
|
||||
uint a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
|
||||
uint a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
|
||||
uint a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
|
||||
h ^= a0;
|
||||
h = Rotate32(h, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
h ^= a2;
|
||||
h = Rotate32(h, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
g ^= a1;
|
||||
g = Rotate32(g, 19);
|
||||
g = g * 5 + 0xe6546b64;
|
||||
g ^= a3;
|
||||
g = Rotate32(g, 19);
|
||||
g = g * 5 + 0xe6546b64;
|
||||
f += a4;
|
||||
f = Rotate32(f, 19) + 113;
|
||||
uint iters = (len - 1) / 20;
|
||||
do
|
||||
{
|
||||
uint a = Fetch32(s);
|
||||
uint b = Fetch32(s + 4);
|
||||
uint c = Fetch32(s + 8);
|
||||
uint d = Fetch32(s + 12);
|
||||
uint e = Fetch32(s + 16);
|
||||
h += a;
|
||||
g += b;
|
||||
f += c;
|
||||
h = Mur(d, h) + e;
|
||||
g = Mur(c, g) + a;
|
||||
f = Mur(b + e * c1, f) + d;
|
||||
f += g;
|
||||
g += f;
|
||||
s += 20;
|
||||
} while (--iters != 0);
|
||||
g = Rotate32(g, 11) * c1;
|
||||
g = Rotate32(g, 17) * c1;
|
||||
f = Rotate32(f, 11) * c1;
|
||||
f = Rotate32(f, 17) * c1;
|
||||
h = Rotate32(h + g, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
h = Rotate32(h, 17) * c1;
|
||||
h = Rotate32(h + f, 19);
|
||||
h = h * 5 + 0xe6546b64;
|
||||
h = Rotate32(h, 17) * c1;
|
||||
return h;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region hash64
|
||||
|
||||
// entry point
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ulong Hash64(byte[] bytes, int offset, int count)
|
||||
{
|
||||
fixed (byte* p = &bytes[offset])
|
||||
{
|
||||
return Hash64(p, (uint)count);
|
||||
}
|
||||
}
|
||||
|
||||
// port from farmhash.cc
|
||||
|
||||
struct pair
|
||||
{
|
||||
public ulong first;
|
||||
public ulong second;
|
||||
|
||||
public pair(ulong first, ulong second)
|
||||
{
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static pair make_pair(ulong first, ulong second)
|
||||
{
|
||||
return new pair(first, second);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static void swap(ref ulong x, ref ulong z)
|
||||
{
|
||||
var temp = z;
|
||||
z = x;
|
||||
x = temp;
|
||||
}
|
||||
|
||||
// Some primes between 2^63 and 2^64 for various uses.
|
||||
const ulong k0 = 0xc3a5c85c97cb3127UL;
|
||||
const ulong k1 = 0xb492b66fbe98f273UL;
|
||||
const ulong k2 = 0x9ae16a3b2f90404fUL;
|
||||
|
||||
static unsafe ulong Fetch64(byte* p)
|
||||
{
|
||||
return *(ulong*)p;
|
||||
}
|
||||
|
||||
static ulong Rotate64(ulong val, int shift)
|
||||
{
|
||||
return shift == 0 ? val : (val >> shift) | (val << (64 - shift));
|
||||
}
|
||||
|
||||
// farmhashna.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static ulong ShiftMix(ulong val)
|
||||
{
|
||||
return val ^ (val >> 47);
|
||||
}
|
||||
|
||||
// farmhashna.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static ulong HashLen16(ulong u, ulong v, ulong mul)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
// Murmur-inspired hashing.
|
||||
ulong a = (u ^ v) * mul;
|
||||
a ^= a >> 47;
|
||||
ulong b = (v ^ a) * mul;
|
||||
b ^= b >> 47;
|
||||
b *= mul;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
// farmhashxo.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong Hash64(byte* s, uint len)
|
||||
{
|
||||
if (len <= 16)
|
||||
{
|
||||
// farmhashna::
|
||||
return HashLen0to16(s, len);
|
||||
}
|
||||
|
||||
if (len <= 32)
|
||||
{
|
||||
// farmhashna::
|
||||
return HashLen17to32(s, len);
|
||||
}
|
||||
|
||||
if (len <= 64)
|
||||
{
|
||||
return HashLen33to64(s, len);
|
||||
}
|
||||
|
||||
if (len <= 96)
|
||||
{
|
||||
return HashLen65to96(s, len);
|
||||
}
|
||||
|
||||
if (len <= 256)
|
||||
{
|
||||
// farmhashna::
|
||||
return Hash64NA(s, len);
|
||||
}
|
||||
|
||||
// farmhashuo::
|
||||
return Hash64UO(s, len);
|
||||
}
|
||||
|
||||
// 0-16 farmhashna.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong HashLen0to16(byte* s, uint len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
if (len >= 8)
|
||||
{
|
||||
ulong mul = k2 + len * 2;
|
||||
ulong a = Fetch64(s) + k2;
|
||||
ulong b = Fetch64(s + len - 8);
|
||||
ulong c = Rotate64(b, 37) * mul + a;
|
||||
ulong d = (Rotate64(a, 25) + b) * mul;
|
||||
return HashLen16(c, d, mul);
|
||||
}
|
||||
if (len >= 4)
|
||||
{
|
||||
ulong mul = k2 + len * 2;
|
||||
ulong a = Fetch32(s);
|
||||
return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
|
||||
}
|
||||
if (len > 0)
|
||||
{
|
||||
ushort a = s[0];
|
||||
ushort b = s[len >> 1];
|
||||
ushort c = s[len - 1];
|
||||
uint y = a + ((uint)b << 8);
|
||||
uint z = len + ((uint)c << 2);
|
||||
return ShiftMix(y * k2 ^ z * k0) * k2;
|
||||
}
|
||||
return k2;
|
||||
}
|
||||
}
|
||||
|
||||
// 17-32 farmhashna.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong HashLen17to32(byte* s, uint len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
ulong mul = k2 + len * 2;
|
||||
ulong a = Fetch64(s) * k1;
|
||||
ulong b = Fetch64(s + 8);
|
||||
ulong c = Fetch64(s + len - 8) * mul;
|
||||
ulong d = Fetch64(s + len - 16) * k2;
|
||||
return HashLen16(Rotate64(a + b, 43) + Rotate64(c, 30) + d,
|
||||
a + Rotate64(b + k2, 18) + c, mul);
|
||||
}
|
||||
}
|
||||
|
||||
// farmhashxo.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong H32(byte* s, uint len, ulong mul, ulong seed0 = 0, ulong seed1 = 0)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
ulong a = Fetch64(s) * k1;
|
||||
ulong b = Fetch64(s + 8);
|
||||
ulong c = Fetch64(s + len - 8) * mul;
|
||||
ulong d = Fetch64(s + len - 16) * k2;
|
||||
ulong u = Rotate64(a + b, 43) + Rotate64(c, 30) + d + seed0;
|
||||
ulong v = a + Rotate64(b + k2, 18) + c + seed1;
|
||||
a = ShiftMix((u ^ v) * mul);
|
||||
b = ShiftMix((v ^ a) * mul);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
// 33-64 farmhashxo.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong HashLen33to64(byte* s, uint len)
|
||||
{
|
||||
const ulong mul0 = k2 - 30;
|
||||
|
||||
unchecked
|
||||
{
|
||||
ulong mul1 = k2 - 30 + 2 * len;
|
||||
ulong h0 = H32(s, 32, mul0);
|
||||
ulong h1 = H32(s + len - 32, 32, mul1);
|
||||
return (h1 * mul1 + h0) * mul1;
|
||||
}
|
||||
}
|
||||
|
||||
// 65-96 farmhashxo.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong HashLen65to96(byte* s, uint len)
|
||||
{
|
||||
const ulong mul0 = k2 - 114;
|
||||
|
||||
unchecked
|
||||
{
|
||||
ulong mul1 = k2 - 114 + 2 * len;
|
||||
ulong h0 = H32(s, 32, mul0);
|
||||
ulong h1 = H32(s + 32, 32, mul1);
|
||||
ulong h2 = H32(s + len - 32, 32, mul1, h0, h1);
|
||||
return (h2 * 9 + (h0 >> 17) + (h1 >> 21)) * mul1;
|
||||
}
|
||||
}
|
||||
|
||||
// farmhashna.cc
|
||||
// Return a 16-byte hash for 48 bytes. Quick and dirty.
|
||||
// Callers do best to use "random-looking" values for a and b.
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe pair WeakHashLen32WithSeeds(ulong w, ulong x, ulong y, ulong z, ulong a, ulong b)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
a += w;
|
||||
b = Rotate64(b + a + z, 21);
|
||||
ulong c = a;
|
||||
a += x;
|
||||
a += y;
|
||||
b += Rotate64(a, 44);
|
||||
return make_pair(a + z, b + c);
|
||||
}
|
||||
}
|
||||
|
||||
// farmhashna.cc
|
||||
// Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe pair WeakHashLen32WithSeeds(byte* s, ulong a, ulong b)
|
||||
{
|
||||
return WeakHashLen32WithSeeds(Fetch64(s),
|
||||
Fetch64(s + 8),
|
||||
Fetch64(s + 16),
|
||||
Fetch64(s + 24),
|
||||
a,
|
||||
b);
|
||||
}
|
||||
|
||||
// na(97-256) farmhashna.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong Hash64NA(byte* s, uint len)
|
||||
{
|
||||
const ulong seed = 81;
|
||||
|
||||
unchecked
|
||||
{
|
||||
// For strings over 64 bytes we loop. Internal state consists of
|
||||
// 56 bytes: v, w, x, y, and z.
|
||||
ulong x = seed;
|
||||
ulong y = seed * k1 + 113;
|
||||
ulong z = ShiftMix(y * k2 + 113) * k2;
|
||||
var v = make_pair(0, 0);
|
||||
var w = make_pair(0, 0);
|
||||
x = x * k2 + Fetch64(s);
|
||||
|
||||
// Set end so that after the loop we have 1 to 64 bytes left to process.
|
||||
byte* end = s + ((len - 1) / 64) * 64;
|
||||
byte* last64 = end + ((len - 1) & 63) - 63;
|
||||
|
||||
do
|
||||
{
|
||||
x = Rotate64(x + y + v.first + Fetch64(s + 8), 37) * k1;
|
||||
y = Rotate64(y + v.second + Fetch64(s + 48), 42) * k1;
|
||||
x ^= w.second;
|
||||
y += v.first + Fetch64(s + 40);
|
||||
z = Rotate64(z + w.first, 33) * k1;
|
||||
v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
|
||||
w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
|
||||
swap(ref z, ref x);
|
||||
s += 64;
|
||||
} while (s != end);
|
||||
ulong mul = k1 + ((z & 0xff) << 1);
|
||||
// Make s point to the last 64 bytes of input.
|
||||
s = last64;
|
||||
w.first += ((len - 1) & 63);
|
||||
v.first += w.first;
|
||||
w.first += v.first;
|
||||
x = Rotate64(x + y + v.first + Fetch64(s + 8), 37) * mul;
|
||||
y = Rotate64(y + v.second + Fetch64(s + 48), 42) * mul;
|
||||
x ^= w.second * 9;
|
||||
y += v.first * 9 + Fetch64(s + 40);
|
||||
z = Rotate64(z + w.first, 33) * mul;
|
||||
v = WeakHashLen32WithSeeds(s, v.second * mul, x + w.first);
|
||||
w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
|
||||
swap(ref z, ref x);
|
||||
return HashLen16(HashLen16(v.first, w.first, mul) + ShiftMix(y) * k0 + z,
|
||||
HashLen16(v.second, w.second, mul) + x,
|
||||
mul);
|
||||
}
|
||||
}
|
||||
|
||||
// farmhashuo.cc
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static ulong H(ulong x, ulong y, ulong mul, int r)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
ulong a = (x ^ y) * mul;
|
||||
a ^= (a >> 47);
|
||||
ulong b = (y ^ a) * mul;
|
||||
return Rotate64(b, r) * mul;
|
||||
}
|
||||
}
|
||||
|
||||
// uo(257-) farmhashuo.cc, Hash64WithSeeds
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static unsafe ulong Hash64UO(byte* s, uint len)
|
||||
{
|
||||
const ulong seed0 = 81;
|
||||
const ulong seed1 = 0;
|
||||
|
||||
unchecked
|
||||
{
|
||||
// For strings over 64 bytes we loop. Internal state consists of
|
||||
// 64 bytes: u, v, w, x, y, and z.
|
||||
ulong x = seed0;
|
||||
ulong y = seed1 * k2 + 113;
|
||||
ulong z = ShiftMix(y * k2) * k2;
|
||||
var v = make_pair(seed0, seed1);
|
||||
var w = make_pair(0, 0);
|
||||
ulong u = x - z;
|
||||
x *= k2;
|
||||
ulong mul = k2 + (u & 0x82);
|
||||
|
||||
// Set end so that after the loop we have 1 to 64 bytes left to process.
|
||||
byte* end = s + ((len - 1) / 64) * 64;
|
||||
byte* last64 = end + ((len - 1) & 63) - 63;
|
||||
|
||||
do
|
||||
{
|
||||
ulong a0 = Fetch64(s);
|
||||
ulong a1 = Fetch64(s + 8);
|
||||
ulong a2 = Fetch64(s + 16);
|
||||
ulong a3 = Fetch64(s + 24);
|
||||
ulong a4 = Fetch64(s + 32);
|
||||
ulong a5 = Fetch64(s + 40);
|
||||
ulong a6 = Fetch64(s + 48);
|
||||
ulong a7 = Fetch64(s + 56);
|
||||
x += a0 + a1;
|
||||
y += a2;
|
||||
z += a3;
|
||||
v.first += a4;
|
||||
v.second += a5 + a1;
|
||||
w.first += a6;
|
||||
w.second += a7;
|
||||
|
||||
x = Rotate64(x, 26);
|
||||
x *= 9;
|
||||
y = Rotate64(y, 29);
|
||||
z *= mul;
|
||||
v.first = Rotate64(v.first, 33);
|
||||
v.second = Rotate64(v.second, 30);
|
||||
w.first ^= x;
|
||||
w.first *= 9;
|
||||
z = Rotate64(z, 32);
|
||||
z += w.second;
|
||||
w.second += z;
|
||||
z *= 9;
|
||||
swap(ref u, ref y);
|
||||
|
||||
z += a0 + a6;
|
||||
v.first += a2;
|
||||
v.second += a3;
|
||||
w.first += a4;
|
||||
w.second += a5 + a6;
|
||||
x += a1;
|
||||
y += a7;
|
||||
|
||||
y += v.first;
|
||||
v.first += x - y;
|
||||
v.second += w.first;
|
||||
w.first += v.second;
|
||||
w.second += x - y;
|
||||
x += w.second;
|
||||
w.second = Rotate64(w.second, 34);
|
||||
swap(ref u, ref z);
|
||||
s += 64;
|
||||
} while (s != end);
|
||||
// Make s point to the last 64 bytes of input.
|
||||
s = last64;
|
||||
u *= 9;
|
||||
v.second = Rotate64(v.second, 28);
|
||||
v.first = Rotate64(v.first, 20);
|
||||
w.first += ((len - 1) & 63);
|
||||
u += y;
|
||||
y += u;
|
||||
x = Rotate64(y - x + v.first + Fetch64(s + 8), 37) * mul;
|
||||
y = Rotate64(y ^ v.second ^ Fetch64(s + 48), 42) * mul;
|
||||
x ^= w.second * 9;
|
||||
y += v.first + Fetch64(s + 40);
|
||||
z = Rotate64(z + w.first, 33) * mul;
|
||||
v = WeakHashLen32WithSeeds(s, v.second * mul, x + w.first);
|
||||
w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
|
||||
return H(HashLen16(v.first + x, w.first ^ y, mul) + z - u,
|
||||
H(v.second + y, w.second + z, k2, 30) ^ x,
|
||||
k2,
|
||||
31);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
12
Assets/Scripts/MessagePack/Internal/FarmHash.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Internal/FarmHash.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7b03526046058694e99952f4732e0672
|
||||
timeCreated: 1501832163
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
372
Assets/Scripts/MessagePack/Internal/GuidBits.cs
Normal file
372
Assets/Scripts/MessagePack/Internal/GuidBits.cs
Normal file
|
|
@ -0,0 +1,372 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
[StructLayout(LayoutKind.Explicit, Pack = 1)]
|
||||
internal struct GuidBits
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public readonly Guid Value;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public readonly byte Byte0;
|
||||
[FieldOffset(1)]
|
||||
public readonly byte Byte1;
|
||||
[FieldOffset(2)]
|
||||
public readonly byte Byte2;
|
||||
[FieldOffset(3)]
|
||||
public readonly byte Byte3;
|
||||
[FieldOffset(4)]
|
||||
public readonly byte Byte4;
|
||||
[FieldOffset(5)]
|
||||
public readonly byte Byte5;
|
||||
[FieldOffset(6)]
|
||||
public readonly byte Byte6;
|
||||
[FieldOffset(7)]
|
||||
public readonly byte Byte7;
|
||||
[FieldOffset(8)]
|
||||
public readonly byte Byte8;
|
||||
[FieldOffset(9)]
|
||||
public readonly byte Byte9;
|
||||
[FieldOffset(10)]
|
||||
public readonly byte Byte10;
|
||||
[FieldOffset(11)]
|
||||
public readonly byte Byte11;
|
||||
[FieldOffset(12)]
|
||||
public readonly byte Byte12;
|
||||
[FieldOffset(13)]
|
||||
public readonly byte Byte13;
|
||||
[FieldOffset(14)]
|
||||
public readonly byte Byte14;
|
||||
[FieldOffset(15)]
|
||||
public readonly byte Byte15;
|
||||
|
||||
// string.Join(", ", Enumerable.Range(0, 256).Select(x => (int)BitConverter.ToString(new byte[] { (byte)x }).ToLower()[0]))
|
||||
static byte[] byteToHexStringHigh = new byte[256] { 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102, 102 };
|
||||
|
||||
// string.Join(", ", Enumerable.Range(0, 256).Select(x => (int)BitConverter.ToString(new byte[] { (byte)x }).ToLower()[1]))
|
||||
static byte[] byteToHexStringLow = new byte[256] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
|
||||
|
||||
public GuidBits(ref Guid value)
|
||||
{
|
||||
this = default(GuidBits);
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
// 4-pattern, lower/upper and '-' or no
|
||||
public GuidBits(ArraySegment<byte> utf8string)
|
||||
{
|
||||
this = default(GuidBits);
|
||||
|
||||
var array = utf8string.Array;
|
||||
var offset = utf8string.Offset;
|
||||
|
||||
// 32
|
||||
if (utf8string.Count == 32)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
this.Byte0 = Parse(array, offset + 6);
|
||||
this.Byte1 = Parse(array, offset + 4);
|
||||
this.Byte2 = Parse(array, offset + 2);
|
||||
this.Byte3 = Parse(array, offset + 0);
|
||||
|
||||
this.Byte4 = Parse(array, offset + 10);
|
||||
this.Byte5 = Parse(array, offset + 8);
|
||||
|
||||
this.Byte6 = Parse(array, offset + 14);
|
||||
this.Byte7 = Parse(array, offset + 12);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Byte0 = Parse(array, offset + 0);
|
||||
this.Byte1 = Parse(array, offset + 2);
|
||||
this.Byte2 = Parse(array, offset + 4);
|
||||
this.Byte3 = Parse(array, offset + 6);
|
||||
|
||||
this.Byte4 = Parse(array, offset + 8);
|
||||
this.Byte5 = Parse(array, offset + 10);
|
||||
|
||||
this.Byte6 = Parse(array, offset + 12);
|
||||
this.Byte7 = Parse(array, offset + 14);
|
||||
}
|
||||
this.Byte8 = Parse(array, offset + 16);
|
||||
this.Byte9 = Parse(array, offset + 18);
|
||||
|
||||
this.Byte10 = Parse(array, offset + 20);
|
||||
this.Byte11 = Parse(array, offset + 22);
|
||||
this.Byte12 = Parse(array, offset + 24);
|
||||
this.Byte13 = Parse(array, offset + 26);
|
||||
this.Byte14 = Parse(array, offset + 28);
|
||||
this.Byte15 = Parse(array, offset + 30);
|
||||
return;
|
||||
}
|
||||
else if (utf8string.Count == 36)
|
||||
{
|
||||
// '-' => 45
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
this.Byte0 = Parse(array, offset + 6);
|
||||
this.Byte1 = Parse(array, offset + 4);
|
||||
this.Byte2 = Parse(array, offset + 2);
|
||||
this.Byte3 = Parse(array, offset + 0);
|
||||
|
||||
if (array[offset + 8] != '-') goto ERROR;
|
||||
|
||||
this.Byte4 = Parse(array, offset + 11);
|
||||
this.Byte5 = Parse(array, offset + 9);
|
||||
|
||||
if (array[offset + 13] != '-') goto ERROR;
|
||||
|
||||
this.Byte6 = Parse(array, offset + 16);
|
||||
this.Byte7 = Parse(array, offset + 14);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Byte0 = Parse(array, offset + 0);
|
||||
this.Byte1 = Parse(array, offset + 2);
|
||||
this.Byte2 = Parse(array, offset + 4);
|
||||
this.Byte3 = Parse(array, offset + 6);
|
||||
|
||||
if (array[offset + 8] != '-') goto ERROR;
|
||||
|
||||
this.Byte4 = Parse(array, offset + 9);
|
||||
this.Byte5 = Parse(array, offset + 11);
|
||||
|
||||
if (array[offset + 13] != '-') goto ERROR;
|
||||
|
||||
this.Byte6 = Parse(array, offset + 14);
|
||||
this.Byte7 = Parse(array, offset + 16);
|
||||
}
|
||||
|
||||
if (array[offset + 18] != '-') goto ERROR;
|
||||
|
||||
this.Byte8 = Parse(array, offset + 19);
|
||||
this.Byte9 = Parse(array, offset + 21);
|
||||
|
||||
if (array[offset + 23] != '-') goto ERROR;
|
||||
|
||||
this.Byte10 = Parse(array, offset + 24);
|
||||
this.Byte11 = Parse(array, offset + 26);
|
||||
this.Byte12 = Parse(array, offset + 28);
|
||||
this.Byte13 = Parse(array, offset + 30);
|
||||
this.Byte14 = Parse(array, offset + 32);
|
||||
this.Byte15 = Parse(array, offset + 34);
|
||||
return;
|
||||
}
|
||||
|
||||
ERROR:
|
||||
throw new ArgumentException("Invalid Guid Pattern.");
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
static byte Parse(byte[] bytes, int highOffset)
|
||||
{
|
||||
return unchecked((byte)(SwitchParse(bytes[highOffset]) * 16 + SwitchParse(bytes[highOffset + 1])));
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
|
||||
#endif
|
||||
static byte SwitchParse(byte b)
|
||||
{
|
||||
// '0'(48) ~ '9'(57) => -48
|
||||
// 'A'(65) ~ 'F'(70) => -55
|
||||
// 'a'(97) ~ 'f'(102) => -87
|
||||
switch (b)
|
||||
{
|
||||
case 48:
|
||||
case 49:
|
||||
case 50:
|
||||
case 51:
|
||||
case 52:
|
||||
case 53:
|
||||
case 54:
|
||||
case 55:
|
||||
case 56:
|
||||
case 57:
|
||||
return unchecked((byte)((b - 48)));
|
||||
case 65:
|
||||
case 66:
|
||||
case 67:
|
||||
case 68:
|
||||
case 69:
|
||||
case 70:
|
||||
return unchecked((byte)((b - 55)));
|
||||
case 97:
|
||||
case 98:
|
||||
case 99:
|
||||
case 100:
|
||||
case 101:
|
||||
case 102:
|
||||
return unchecked((byte)((b - 87)));
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
case 13:
|
||||
case 14:
|
||||
case 15:
|
||||
case 16:
|
||||
case 17:
|
||||
case 18:
|
||||
case 19:
|
||||
case 20:
|
||||
case 21:
|
||||
case 22:
|
||||
case 23:
|
||||
case 24:
|
||||
case 25:
|
||||
case 26:
|
||||
case 27:
|
||||
case 28:
|
||||
case 29:
|
||||
case 30:
|
||||
case 31:
|
||||
case 32:
|
||||
case 33:
|
||||
case 34:
|
||||
case 35:
|
||||
case 36:
|
||||
case 37:
|
||||
case 38:
|
||||
case 39:
|
||||
case 40:
|
||||
case 41:
|
||||
case 42:
|
||||
case 43:
|
||||
case 44:
|
||||
case 45:
|
||||
case 46:
|
||||
case 47:
|
||||
case 58:
|
||||
case 59:
|
||||
case 60:
|
||||
case 61:
|
||||
case 62:
|
||||
case 63:
|
||||
case 64:
|
||||
case 71:
|
||||
case 72:
|
||||
case 73:
|
||||
case 74:
|
||||
case 75:
|
||||
case 76:
|
||||
case 77:
|
||||
case 78:
|
||||
case 79:
|
||||
case 80:
|
||||
case 81:
|
||||
case 82:
|
||||
case 83:
|
||||
case 84:
|
||||
case 85:
|
||||
case 86:
|
||||
case 87:
|
||||
case 88:
|
||||
case 89:
|
||||
case 90:
|
||||
case 91:
|
||||
case 92:
|
||||
case 93:
|
||||
case 94:
|
||||
case 95:
|
||||
case 96:
|
||||
default:
|
||||
throw new ArgumentException("Invalid Guid Pattern.");
|
||||
}
|
||||
}
|
||||
|
||||
// 4(x2) - 2(x2) - 2(x2) - 2(x2) - 6(x2)
|
||||
public void Write(byte[] buffer, int offset)
|
||||
{
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
// int(_a)
|
||||
buffer[offset + 6] = byteToHexStringHigh[Byte0];
|
||||
buffer[offset + 7] = byteToHexStringLow[Byte0];
|
||||
buffer[offset + 4] = byteToHexStringHigh[Byte1];
|
||||
buffer[offset + 5] = byteToHexStringLow[Byte1];
|
||||
buffer[offset + 2] = byteToHexStringHigh[Byte2];
|
||||
buffer[offset + 3] = byteToHexStringLow[Byte2];
|
||||
buffer[offset + 0] = byteToHexStringHigh[Byte3];
|
||||
buffer[offset + 1] = byteToHexStringLow[Byte3];
|
||||
|
||||
buffer[offset + 8] = (byte)'-';
|
||||
|
||||
// short(_b)
|
||||
buffer[offset + 11] = byteToHexStringHigh[Byte4];
|
||||
buffer[offset + 12] = byteToHexStringLow[Byte4];
|
||||
buffer[offset + 9] = byteToHexStringHigh[Byte5];
|
||||
buffer[offset + 10] = byteToHexStringLow[Byte5];
|
||||
|
||||
buffer[offset + 13] = (byte)'-';
|
||||
|
||||
// short(_c)
|
||||
buffer[offset + 16] = byteToHexStringHigh[Byte6];
|
||||
buffer[offset + 17] = byteToHexStringLow[Byte6];
|
||||
buffer[offset + 14] = byteToHexStringHigh[Byte7];
|
||||
buffer[offset + 15] = byteToHexStringLow[Byte7];
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[offset + 0] = byteToHexStringHigh[Byte0];
|
||||
buffer[offset + 1] = byteToHexStringLow[Byte0];
|
||||
buffer[offset + 2] = byteToHexStringHigh[Byte1];
|
||||
buffer[offset + 3] = byteToHexStringLow[Byte1];
|
||||
buffer[offset + 4] = byteToHexStringHigh[Byte2];
|
||||
buffer[offset + 5] = byteToHexStringLow[Byte2];
|
||||
buffer[offset + 6] = byteToHexStringHigh[Byte3];
|
||||
buffer[offset + 7] = byteToHexStringLow[Byte3];
|
||||
|
||||
buffer[offset + 8] = (byte)'-';
|
||||
|
||||
buffer[offset + 9] = byteToHexStringHigh[Byte4];
|
||||
buffer[offset + 10] = byteToHexStringLow[Byte4];
|
||||
buffer[offset + 11] = byteToHexStringHigh[Byte5];
|
||||
buffer[offset + 12] = byteToHexStringLow[Byte5];
|
||||
|
||||
buffer[offset + 13] = (byte)'-';
|
||||
|
||||
buffer[offset + 14] = byteToHexStringHigh[Byte6];
|
||||
buffer[offset + 15] = byteToHexStringLow[Byte6];
|
||||
buffer[offset + 16] = byteToHexStringHigh[Byte7];
|
||||
buffer[offset + 17] = byteToHexStringLow[Byte7];
|
||||
}
|
||||
|
||||
buffer[offset + 18] = (byte)'-';
|
||||
|
||||
buffer[offset + 19] = byteToHexStringHigh[Byte8];
|
||||
buffer[offset + 20] = byteToHexStringLow[Byte8];
|
||||
buffer[offset + 21] = byteToHexStringHigh[Byte9];
|
||||
buffer[offset + 22] = byteToHexStringLow[Byte9];
|
||||
|
||||
buffer[offset + 23] = (byte)'-';
|
||||
|
||||
buffer[offset + 24] = byteToHexStringHigh[Byte10];
|
||||
buffer[offset + 25] = byteToHexStringLow[Byte10];
|
||||
buffer[offset + 26] = byteToHexStringHigh[Byte11];
|
||||
buffer[offset + 27] = byteToHexStringLow[Byte11];
|
||||
buffer[offset + 28] = byteToHexStringHigh[Byte12];
|
||||
buffer[offset + 29] = byteToHexStringLow[Byte12];
|
||||
buffer[offset + 30] = byteToHexStringHigh[Byte13];
|
||||
buffer[offset + 31] = byteToHexStringLow[Byte13];
|
||||
buffer[offset + 32] = byteToHexStringHigh[Byte14];
|
||||
buffer[offset + 33] = byteToHexStringLow[Byte14];
|
||||
buffer[offset + 34] = byteToHexStringHigh[Byte15];
|
||||
buffer[offset + 35] = byteToHexStringLow[Byte15];
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/Internal/GuidBits.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Internal/GuidBits.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d41e5f9c2ae8be2469a8d817a109b975
|
||||
timeCreated: 1505965655
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
393
Assets/Scripts/MessagePack/Internal/ILGeneratorExtensions.cs
Normal file
393
Assets/Scripts/MessagePack/Internal/ILGeneratorExtensions.cs
Normal file
|
|
@ -0,0 +1,393 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal struct ArgumentField
|
||||
{
|
||||
readonly int i;
|
||||
readonly bool @ref;
|
||||
readonly ILGenerator il;
|
||||
|
||||
public ArgumentField(ILGenerator il, int i, bool @ref = false)
|
||||
{
|
||||
this.il = il;
|
||||
this.i = i;
|
||||
this.@ref = @ref;
|
||||
}
|
||||
|
||||
public ArgumentField(ILGenerator il, int i, Type type)
|
||||
{
|
||||
this.il = il;
|
||||
this.i = i;
|
||||
var ti = type.GetTypeInfo();
|
||||
this.@ref = (ti.IsClass || ti.IsInterface || ti.IsAbstract) ? false : true;
|
||||
}
|
||||
|
||||
public void EmitLoad()
|
||||
{
|
||||
if (@ref)
|
||||
{
|
||||
il.EmitLdarga(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.EmitLdarg(i);
|
||||
}
|
||||
}
|
||||
|
||||
public void EmitLdarg()
|
||||
{
|
||||
il.EmitLdarg(i);
|
||||
}
|
||||
|
||||
public void EmitLdarga()
|
||||
{
|
||||
il.EmitLdarga(i);
|
||||
}
|
||||
|
||||
public void EmitStore()
|
||||
{
|
||||
il.EmitStarg(i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides optimized generation code and helpers.
|
||||
/// </summary>
|
||||
internal static class ILGeneratorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads the local variable at a specific index onto the evaluation stack.
|
||||
/// </summary>
|
||||
public static void EmitLdloc(this ILGenerator il, int index)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
il.Emit(OpCodes.Ldloc_0);
|
||||
break;
|
||||
case 1:
|
||||
il.Emit(OpCodes.Ldloc_1);
|
||||
break;
|
||||
case 2:
|
||||
il.Emit(OpCodes.Ldloc_2);
|
||||
break;
|
||||
case 3:
|
||||
il.Emit(OpCodes.Ldloc_3);
|
||||
break;
|
||||
default:
|
||||
if (index <= 255)
|
||||
{
|
||||
il.Emit(OpCodes.Ldloc_S, (byte)index);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Ldloc, (short)index);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitLdloc(this ILGenerator il, LocalBuilder local)
|
||||
{
|
||||
EmitLdloc(il, local.LocalIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pops the current value from the top of the evaluation stack and stores it in a the local variable list at a specified index.
|
||||
/// </summary>
|
||||
public static void EmitStloc(this ILGenerator il, int index)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
il.Emit(OpCodes.Stloc_0);
|
||||
break;
|
||||
case 1:
|
||||
il.Emit(OpCodes.Stloc_1);
|
||||
break;
|
||||
case 2:
|
||||
il.Emit(OpCodes.Stloc_2);
|
||||
break;
|
||||
case 3:
|
||||
il.Emit(OpCodes.Stloc_3);
|
||||
break;
|
||||
default:
|
||||
if (index <= 255)
|
||||
{
|
||||
il.Emit(OpCodes.Stloc_S, (byte)index);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Stloc, (short)index);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitStloc(this ILGenerator il, LocalBuilder local)
|
||||
{
|
||||
EmitStloc(il, local.LocalIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the address of the local variable at a specific index onto the evaluation statck.
|
||||
/// </summary>
|
||||
public static void EmitLdloca(this ILGenerator il, int index)
|
||||
{
|
||||
if (index <= 255)
|
||||
{
|
||||
il.Emit(OpCodes.Ldloca_S, (byte)index);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Ldloca, (short)index);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitLdloca(this ILGenerator il, LocalBuilder local)
|
||||
{
|
||||
EmitLdloca(il, local.LocalIndex);
|
||||
}
|
||||
|
||||
public static void EmitTrue(this ILGenerator il)
|
||||
{
|
||||
EmitBoolean(il, true);
|
||||
}
|
||||
|
||||
public static void EmitFalse(this ILGenerator il)
|
||||
{
|
||||
EmitBoolean(il, false);
|
||||
}
|
||||
|
||||
public static void EmitBoolean(this ILGenerator il, bool value)
|
||||
{
|
||||
EmitLdc_I4(il, value ? 1 : 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pushes a supplied value of type int32 onto the evaluation stack as an int32.
|
||||
/// </summary>
|
||||
public static void EmitLdc_I4(this ILGenerator il, int value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case -1:
|
||||
il.Emit(OpCodes.Ldc_I4_M1);
|
||||
break;
|
||||
case 0:
|
||||
il.Emit(OpCodes.Ldc_I4_0);
|
||||
break;
|
||||
case 1:
|
||||
il.Emit(OpCodes.Ldc_I4_1);
|
||||
break;
|
||||
case 2:
|
||||
il.Emit(OpCodes.Ldc_I4_2);
|
||||
break;
|
||||
case 3:
|
||||
il.Emit(OpCodes.Ldc_I4_3);
|
||||
break;
|
||||
case 4:
|
||||
il.Emit(OpCodes.Ldc_I4_4);
|
||||
break;
|
||||
case 5:
|
||||
il.Emit(OpCodes.Ldc_I4_5);
|
||||
break;
|
||||
case 6:
|
||||
il.Emit(OpCodes.Ldc_I4_6);
|
||||
break;
|
||||
case 7:
|
||||
il.Emit(OpCodes.Ldc_I4_7);
|
||||
break;
|
||||
case 8:
|
||||
il.Emit(OpCodes.Ldc_I4_8);
|
||||
break;
|
||||
default:
|
||||
if (value >= -128 && value <= 127)
|
||||
{
|
||||
il.Emit(OpCodes.Ldc_I4_S, (sbyte)value);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Ldc_I4, value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitUnboxOrCast(this ILGenerator il, Type type)
|
||||
{
|
||||
if (type.GetTypeInfo().IsValueType)
|
||||
{
|
||||
il.Emit(OpCodes.Unbox_Any, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitBoxOrDoNothing(this ILGenerator il, Type type)
|
||||
{
|
||||
if (type.GetTypeInfo().IsValueType)
|
||||
{
|
||||
il.Emit(OpCodes.Box, type);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitLdarg(this ILGenerator il, int index)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
break;
|
||||
case 1:
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
break;
|
||||
case 2:
|
||||
il.Emit(OpCodes.Ldarg_2);
|
||||
break;
|
||||
case 3:
|
||||
il.Emit(OpCodes.Ldarg_3);
|
||||
break;
|
||||
default:
|
||||
if (index <= 255)
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_S, (byte)index);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg, index);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitLoadThis(this ILGenerator il)
|
||||
{
|
||||
EmitLdarg(il, 0);
|
||||
}
|
||||
|
||||
public static void EmitLdarga(this ILGenerator il, int index)
|
||||
{
|
||||
if (index <= 255)
|
||||
{
|
||||
il.Emit(OpCodes.Ldarga_S, (byte)index);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Ldarga, index);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitStarg(this ILGenerator il, int index)
|
||||
{
|
||||
if (index <= 255)
|
||||
{
|
||||
il.Emit(OpCodes.Starg_S, (byte)index);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Starg, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper for Pop op.
|
||||
/// </summary>
|
||||
public static void EmitPop(this ILGenerator il, int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
il.Emit(OpCodes.Pop);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitCall(this ILGenerator il, MethodInfo methodInfo)
|
||||
{
|
||||
if (methodInfo.IsFinal || !methodInfo.IsVirtual)
|
||||
{
|
||||
il.Emit(OpCodes.Call, methodInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
il.Emit(OpCodes.Callvirt, methodInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EmitLdfld(this ILGenerator il, FieldInfo fieldInfo)
|
||||
{
|
||||
il.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
}
|
||||
|
||||
public static void EmitLdsfld(this ILGenerator il, FieldInfo fieldInfo)
|
||||
{
|
||||
il.Emit(OpCodes.Ldsfld, fieldInfo);
|
||||
}
|
||||
|
||||
public static void EmitRet(this ILGenerator il)
|
||||
{
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
public static void EmitIntZeroReturn(this ILGenerator il)
|
||||
{
|
||||
il.EmitLdc_I4(0);
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
public static void EmitNullReturn(this ILGenerator il)
|
||||
{
|
||||
il.Emit(OpCodes.Ldnull);
|
||||
il.Emit(OpCodes.Ret);
|
||||
}
|
||||
|
||||
public static void EmitULong(this ILGenerator il, ulong value)
|
||||
{
|
||||
il.Emit(OpCodes.Ldc_I8, unchecked((long)value));
|
||||
}
|
||||
|
||||
public static void EmitThrowNotimplemented(this ILGenerator il)
|
||||
{
|
||||
il.Emit(OpCodes.Newobj, typeof(System.NotImplementedException).GetTypeInfo().DeclaredConstructors.First(x => x.GetParameters().Length == 0));
|
||||
il.Emit(OpCodes.Throw);
|
||||
}
|
||||
|
||||
/// <summary>for var i = 0, i ..., i++ </summary>
|
||||
public static void EmitIncrementFor(this ILGenerator il, LocalBuilder conditionGreater, Action<LocalBuilder> emitBody)
|
||||
{
|
||||
var loopBegin = il.DefineLabel();
|
||||
var condtionLabel = il.DefineLabel();
|
||||
|
||||
// var i = 0
|
||||
var forI = il.DeclareLocal(typeof(int));
|
||||
il.EmitLdc_I4(0);
|
||||
il.EmitStloc(forI);
|
||||
il.Emit(OpCodes.Br, condtionLabel);
|
||||
|
||||
il.MarkLabel(loopBegin);
|
||||
emitBody(forI);
|
||||
|
||||
// i++
|
||||
il.EmitLdloc(forI);
|
||||
il.EmitLdc_I4(1);
|
||||
il.Emit(OpCodes.Add);
|
||||
il.EmitStloc(forI);
|
||||
|
||||
//// i < ***
|
||||
il.MarkLabel(condtionLabel);
|
||||
il.EmitLdloc(forI);
|
||||
il.EmitLdloc(conditionGreater);
|
||||
il.Emit(OpCodes.Blt, loopBegin);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f4704d47f3414974f88f9f21e3dc281c
|
||||
timeCreated: 1488099024
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
57
Assets/Scripts/MessagePack/Internal/ReflectionExtensions.cs
Normal file
57
Assets/Scripts/MessagePack/Internal/ReflectionExtensions.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#if !UNITY_WSA
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class ReflectionExtensions
|
||||
{
|
||||
public static bool IsNullable(this System.Reflection.TypeInfo type)
|
||||
{
|
||||
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Nullable<>);
|
||||
}
|
||||
|
||||
public static bool IsPublic(this System.Reflection.TypeInfo type)
|
||||
{
|
||||
return type.IsPublic;
|
||||
}
|
||||
|
||||
public static bool IsAnonymous(this System.Reflection.TypeInfo type)
|
||||
{
|
||||
return type.GetCustomAttribute<CompilerGeneratedAttribute>() != null
|
||||
&& type.IsGenericType && type.Name.Contains("AnonymousType")
|
||||
&& (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
|
||||
&& (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
|
||||
}
|
||||
|
||||
public static bool IsIndexer(this System.Reflection.PropertyInfo propertyInfo)
|
||||
{
|
||||
return propertyInfo.GetIndexParameters().Length > 0;
|
||||
}
|
||||
|
||||
#if NETSTANDARD
|
||||
|
||||
public static bool IsConstructedGenericType(this System.Reflection.TypeInfo type)
|
||||
{
|
||||
return type.AsType().IsConstructedGenericType;
|
||||
}
|
||||
|
||||
public static MethodInfo GetGetMethod(this PropertyInfo propInfo)
|
||||
{
|
||||
return propInfo.GetMethod;
|
||||
}
|
||||
|
||||
public static MethodInfo GetSetMethod(this PropertyInfo propInfo)
|
||||
{
|
||||
return propInfo.SetMethod;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e61aa0b2e2300ee4a91c0c699b1533a3
|
||||
timeCreated: 1487501810
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,232 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
// Safe for multiple-read, single-write.
|
||||
internal class ThreadsafeTypeKeyHashTable<TValue>
|
||||
{
|
||||
Entry[] buckets;
|
||||
int size; // only use in writer lock
|
||||
|
||||
readonly object writerLock = new object();
|
||||
readonly float loadFactor;
|
||||
|
||||
// IEqualityComparer.Equals is overhead if key only Type, don't use it.
|
||||
// readonly IEqualityComparer<TKey> comparer;
|
||||
|
||||
public ThreadsafeTypeKeyHashTable(int capacity = 4, float loadFactor = 0.75f)
|
||||
{
|
||||
var tableSize = CalculateCapacity(capacity, loadFactor);
|
||||
this.buckets = new Entry[tableSize];
|
||||
this.loadFactor = loadFactor;
|
||||
}
|
||||
|
||||
public bool TryAdd(Type key, TValue value)
|
||||
{
|
||||
return TryAdd(key, _ => value); // create lambda capture
|
||||
}
|
||||
|
||||
public bool TryAdd(Type key, Func<Type, TValue> valueFactory)
|
||||
{
|
||||
TValue _;
|
||||
return TryAddInternal(key, valueFactory, out _);
|
||||
}
|
||||
|
||||
bool TryAddInternal(Type key, Func<Type, TValue> valueFactory, out TValue resultingValue)
|
||||
{
|
||||
lock (writerLock)
|
||||
{
|
||||
var nextCapacity = CalculateCapacity(size + 1, loadFactor);
|
||||
|
||||
if (buckets.Length < nextCapacity)
|
||||
{
|
||||
// rehash
|
||||
var nextBucket = new Entry[nextCapacity];
|
||||
for (int i = 0; i < buckets.Length; i++)
|
||||
{
|
||||
var e = buckets[i];
|
||||
while (e != null)
|
||||
{
|
||||
var newEntry = new Entry { Key = e.Key, Value = e.Value, Hash = e.Hash };
|
||||
AddToBuckets(nextBucket, key, newEntry, null, out resultingValue);
|
||||
e = e.Next;
|
||||
}
|
||||
}
|
||||
|
||||
// add entry(if failed to add, only do resize)
|
||||
var successAdd = AddToBuckets(nextBucket, key, null, valueFactory, out resultingValue);
|
||||
|
||||
// replace field(threadsafe for read)
|
||||
VolatileWrite(ref buckets, nextBucket);
|
||||
|
||||
if (successAdd) size++;
|
||||
return successAdd;
|
||||
}
|
||||
else
|
||||
{
|
||||
// add entry(insert last is thread safe for read)
|
||||
var successAdd = AddToBuckets(buckets, key, null, valueFactory, out resultingValue);
|
||||
if (successAdd) size++;
|
||||
return successAdd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AddToBuckets(Entry[] buckets, Type newKey, Entry newEntryOrNull, Func<Type, TValue> valueFactory, out TValue resultingValue)
|
||||
{
|
||||
var h = (newEntryOrNull != null) ? newEntryOrNull.Hash : newKey.GetHashCode();
|
||||
if (buckets[h & (buckets.Length - 1)] == null)
|
||||
{
|
||||
if (newEntryOrNull != null)
|
||||
{
|
||||
resultingValue = newEntryOrNull.Value;
|
||||
VolatileWrite(ref buckets[h & (buckets.Length - 1)], newEntryOrNull);
|
||||
}
|
||||
else
|
||||
{
|
||||
resultingValue = valueFactory(newKey);
|
||||
VolatileWrite(ref buckets[h & (buckets.Length - 1)], new Entry { Key = newKey, Value = resultingValue, Hash = h });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var searchLastEntry = buckets[h & (buckets.Length - 1)];
|
||||
while (true)
|
||||
{
|
||||
if (searchLastEntry.Key == newKey)
|
||||
{
|
||||
resultingValue = searchLastEntry.Value;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (searchLastEntry.Next == null)
|
||||
{
|
||||
if (newEntryOrNull != null)
|
||||
{
|
||||
resultingValue = newEntryOrNull.Value;
|
||||
VolatileWrite(ref searchLastEntry.Next, newEntryOrNull);
|
||||
}
|
||||
else
|
||||
{
|
||||
resultingValue = valueFactory(newKey);
|
||||
VolatileWrite(ref searchLastEntry.Next, new Entry { Key = newKey, Value = resultingValue, Hash = h });
|
||||
}
|
||||
break;
|
||||
}
|
||||
searchLastEntry = searchLastEntry.Next;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetValue(Type key, out TValue value)
|
||||
{
|
||||
var table = buckets;
|
||||
var hash = key.GetHashCode();
|
||||
var entry = table[hash & table.Length - 1];
|
||||
|
||||
if (entry == null) goto NOT_FOUND;
|
||||
|
||||
if (entry.Key == key)
|
||||
{
|
||||
value = entry.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
var next = entry.Next;
|
||||
while (next != null)
|
||||
{
|
||||
if (next.Key == key)
|
||||
{
|
||||
value = next.Value;
|
||||
return true;
|
||||
}
|
||||
next = next.Next;
|
||||
}
|
||||
|
||||
NOT_FOUND:
|
||||
value = default(TValue);
|
||||
return false;
|
||||
}
|
||||
|
||||
public TValue GetOrAdd(Type key, Func<Type, TValue> valueFactory)
|
||||
{
|
||||
TValue v;
|
||||
if (TryGetValue(key, out v))
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
TryAddInternal(key, valueFactory, out v);
|
||||
return v;
|
||||
}
|
||||
|
||||
static int CalculateCapacity(int collectionSize, float loadFactor)
|
||||
{
|
||||
var initialCapacity = (int)(((float)collectionSize) / loadFactor);
|
||||
var capacity = 1;
|
||||
while (capacity < initialCapacity)
|
||||
{
|
||||
capacity <<= 1;
|
||||
}
|
||||
|
||||
if (capacity < 8)
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
|
||||
return capacity;
|
||||
}
|
||||
|
||||
static void VolatileWrite(ref Entry location, Entry value)
|
||||
{
|
||||
#if NETSTANDARD
|
||||
System.Threading.Volatile.Write(ref location, value);
|
||||
#elif UNITY_WSA || NET_4_6
|
||||
System.Threading.Volatile.Write(ref location, value);
|
||||
#else
|
||||
System.Threading.Thread.MemoryBarrier();
|
||||
location = value;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void VolatileWrite(ref Entry[] location, Entry[] value)
|
||||
{
|
||||
#if NETSTANDARD
|
||||
System.Threading.Volatile.Write(ref location, value);
|
||||
#elif UNITY_WSA || NET_4_6
|
||||
System.Threading.Volatile.Write(ref location, value);
|
||||
#else
|
||||
System.Threading.Thread.MemoryBarrier();
|
||||
location = value;
|
||||
#endif
|
||||
}
|
||||
|
||||
class Entry
|
||||
{
|
||||
public Type Key;
|
||||
public TValue Value;
|
||||
public int Hash;
|
||||
public Entry Next;
|
||||
|
||||
// debug only
|
||||
public override string ToString()
|
||||
{
|
||||
return Key + "(" + Count() + ")";
|
||||
}
|
||||
|
||||
int Count()
|
||||
{
|
||||
var count = 1;
|
||||
var n = this;
|
||||
while (n.Next != null)
|
||||
{
|
||||
count++;
|
||||
n = n.Next;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fb97ef9df4c7e264b911fac3efcc52de
|
||||
timeCreated: 1501832163
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
355
Assets/Scripts/MessagePack/Internal/TinyJsonReader.cs
Normal file
355
Assets/Scripts/MessagePack/Internal/TinyJsonReader.cs
Normal file
|
|
@ -0,0 +1,355 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
// simple, tiny JSON reader for MessagePackSerializer.FromJson.
|
||||
// this is simple, compact and enough fast but not optimized extremely.
|
||||
|
||||
internal enum TinyJsonToken
|
||||
{
|
||||
None,
|
||||
StartObject, // {
|
||||
EndObject, // }
|
||||
StartArray, // [
|
||||
EndArray, // ]
|
||||
Number, // -0~9
|
||||
String, // "___"
|
||||
True, // true
|
||||
False, // false
|
||||
Null, // null
|
||||
}
|
||||
|
||||
internal enum ValueType : byte
|
||||
{
|
||||
Null,
|
||||
True,
|
||||
False,
|
||||
Double,
|
||||
Long,
|
||||
ULong,
|
||||
Decimal,
|
||||
String
|
||||
}
|
||||
|
||||
internal class TinyJsonException : Exception
|
||||
{
|
||||
public TinyJsonException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
internal class TinyJsonReader : IDisposable
|
||||
{
|
||||
readonly TextReader reader;
|
||||
readonly bool disposeInnerReader;
|
||||
StringBuilder reusableBuilder;
|
||||
|
||||
public TinyJsonToken TokenType { get; private set; }
|
||||
public ValueType ValueType { get; private set; }
|
||||
public double DoubleValue { get; private set; }
|
||||
public long LongValue { get; private set; }
|
||||
public ulong ULongValue { get; private set; }
|
||||
public decimal DecimalValue { get; private set; }
|
||||
public string StringValue { get; private set; }
|
||||
|
||||
public TinyJsonReader(TextReader reader, bool disposeInnerReader = true)
|
||||
{
|
||||
this.reader = reader;
|
||||
this.disposeInnerReader = disposeInnerReader;
|
||||
}
|
||||
|
||||
public bool Read()
|
||||
{
|
||||
ReadNextToken();
|
||||
ReadValue();
|
||||
return TokenType != TinyJsonToken.None;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (reader != null && disposeInnerReader)
|
||||
{
|
||||
reader.Dispose();
|
||||
}
|
||||
TokenType = TinyJsonToken.None;
|
||||
ValueType = ValueType.Null;
|
||||
}
|
||||
|
||||
void SkipWhiteSpace()
|
||||
{
|
||||
var c = reader.Peek();
|
||||
while (c != -1 && Char.IsWhiteSpace((char)c))
|
||||
{
|
||||
reader.Read();
|
||||
c = reader.Peek();
|
||||
}
|
||||
}
|
||||
|
||||
char ReadChar()
|
||||
{
|
||||
return (char)reader.Read();
|
||||
}
|
||||
|
||||
static bool IsWordBreak(char c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case ' ':
|
||||
case '{':
|
||||
case '}':
|
||||
case '[':
|
||||
case ']':
|
||||
case ',':
|
||||
case ':':
|
||||
case '\"':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void ReadNextToken()
|
||||
{
|
||||
SkipWhiteSpace();
|
||||
|
||||
var intChar = reader.Peek();
|
||||
if (intChar == -1)
|
||||
{
|
||||
TokenType = TinyJsonToken.None;
|
||||
return;
|
||||
}
|
||||
|
||||
var c = (char)intChar;
|
||||
switch (c)
|
||||
{
|
||||
case '{':
|
||||
TokenType = TinyJsonToken.StartObject;
|
||||
return;
|
||||
case '}':
|
||||
TokenType = TinyJsonToken.EndObject;
|
||||
return;
|
||||
case '[':
|
||||
TokenType = TinyJsonToken.StartArray;
|
||||
return;
|
||||
case ']':
|
||||
TokenType = TinyJsonToken.EndArray;
|
||||
return;
|
||||
case '"':
|
||||
TokenType = TinyJsonToken.String;
|
||||
return;
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
case '-':
|
||||
TokenType = TinyJsonToken.Number;
|
||||
return;
|
||||
case 't':
|
||||
TokenType = TinyJsonToken.True;
|
||||
return;
|
||||
case 'f':
|
||||
TokenType = TinyJsonToken.False;
|
||||
return;
|
||||
case 'n':
|
||||
TokenType = TinyJsonToken.Null;
|
||||
return;
|
||||
case ',':
|
||||
case ':':
|
||||
reader.Read();
|
||||
ReadNextToken();
|
||||
return;
|
||||
default:
|
||||
throw new TinyJsonException("Invalid String:" + c);
|
||||
}
|
||||
}
|
||||
|
||||
void ReadValue()
|
||||
{
|
||||
ValueType = ValueType.Null;
|
||||
|
||||
switch (TokenType)
|
||||
{
|
||||
case TinyJsonToken.None:
|
||||
break;
|
||||
case TinyJsonToken.StartObject:
|
||||
case TinyJsonToken.EndObject:
|
||||
case TinyJsonToken.StartArray:
|
||||
case TinyJsonToken.EndArray:
|
||||
reader.Read();
|
||||
break;
|
||||
case TinyJsonToken.Number:
|
||||
ReadNumber();
|
||||
break;
|
||||
case TinyJsonToken.String:
|
||||
ReadString();
|
||||
break;
|
||||
case TinyJsonToken.True:
|
||||
if (ReadChar() != 't') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'r') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'u') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'e') throw new TinyJsonException("Invalid Token");
|
||||
ValueType = ValueType.True;
|
||||
break;
|
||||
case TinyJsonToken.False:
|
||||
if (ReadChar() != 'f') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'a') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'l') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 's') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'e') throw new TinyJsonException("Invalid Token");
|
||||
ValueType = ValueType.False;
|
||||
break;
|
||||
case TinyJsonToken.Null:
|
||||
if (ReadChar() != 'n') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'u') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'l') throw new TinyJsonException("Invalid Token");
|
||||
if (ReadChar() != 'l') throw new TinyJsonException("Invalid Token");
|
||||
ValueType = ValueType.Null;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("InvalidTokenState:" + TokenType);
|
||||
}
|
||||
}
|
||||
|
||||
void ReadNumber()
|
||||
{
|
||||
StringBuilder numberWord;
|
||||
if (reusableBuilder == null)
|
||||
{
|
||||
reusableBuilder = new StringBuilder();
|
||||
numberWord = reusableBuilder;
|
||||
}
|
||||
else
|
||||
{
|
||||
numberWord = reusableBuilder;
|
||||
numberWord.Length = 0; // Clear
|
||||
}
|
||||
|
||||
var isDouble = false;
|
||||
var intChar = reader.Peek();
|
||||
while (intChar != -1 && !IsWordBreak((char)intChar))
|
||||
{
|
||||
var c = ReadChar();
|
||||
numberWord.Append(c);
|
||||
if (c == '.') isDouble = true;
|
||||
intChar = reader.Peek();
|
||||
}
|
||||
|
||||
var number = numberWord.ToString();
|
||||
if (isDouble)
|
||||
{
|
||||
double parsedDouble;
|
||||
Double.TryParse(number, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, System.Globalization.CultureInfo.InvariantCulture, out parsedDouble);
|
||||
ValueType = ValueType.Double;
|
||||
DoubleValue = parsedDouble;
|
||||
}
|
||||
else
|
||||
{
|
||||
long parsedInt;
|
||||
if (Int64.TryParse(number, NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out parsedInt))
|
||||
{
|
||||
ValueType = ValueType.Long;
|
||||
LongValue = parsedInt;
|
||||
return;
|
||||
}
|
||||
|
||||
ulong parsedULong;
|
||||
if (ulong.TryParse(number, NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out parsedULong))
|
||||
{
|
||||
ValueType = ValueType.ULong;
|
||||
ULongValue = parsedULong;
|
||||
return;
|
||||
}
|
||||
|
||||
Decimal parsedDecimal;
|
||||
if (decimal.TryParse(number, NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out parsedDecimal))
|
||||
{
|
||||
ValueType = ValueType.Decimal;
|
||||
DecimalValue = parsedDecimal;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ReadString()
|
||||
{
|
||||
reader.Read(); // skip ["]
|
||||
|
||||
StringBuilder sb;
|
||||
if (reusableBuilder == null)
|
||||
{
|
||||
reusableBuilder = new StringBuilder();
|
||||
sb = reusableBuilder;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb = reusableBuilder;
|
||||
sb.Length = 0; // Clear
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (reader.Peek() == -1) throw new TinyJsonException("Invalid Json String");
|
||||
|
||||
var c = ReadChar();
|
||||
switch (c)
|
||||
{
|
||||
case '"': // endtoken
|
||||
goto END;
|
||||
case '\\': // escape character
|
||||
if (reader.Peek() == -1) throw new TinyJsonException("Invalid Json String");
|
||||
|
||||
c = ReadChar();
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
case '\\':
|
||||
case '/':
|
||||
sb.Append(c);
|
||||
break;
|
||||
case 'b':
|
||||
sb.Append('\b');
|
||||
break;
|
||||
case 'f':
|
||||
sb.Append('\f');
|
||||
break;
|
||||
case 'n':
|
||||
sb.Append('\n');
|
||||
break;
|
||||
case 'r':
|
||||
sb.Append('\r');
|
||||
break;
|
||||
case 't':
|
||||
sb.Append('\t');
|
||||
break;
|
||||
case 'u':
|
||||
var hex = new char[4];
|
||||
hex[0] = ReadChar();
|
||||
hex[1] = ReadChar();
|
||||
hex[2] = ReadChar();
|
||||
hex[3] = ReadChar();
|
||||
sb.Append((char)Convert.ToInt32(new string(hex), 16));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default: // string
|
||||
sb.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
END:
|
||||
ValueType = ValueType.String;
|
||||
StringValue = sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/Internal/TinyJsonReader.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Internal/TinyJsonReader.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 757d9ab32ae519a45b020eab3038332c
|
||||
timeCreated: 1498137502
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
167
Assets/Scripts/MessagePack/Internal/UnsafeMemory.Low.cs
Normal file
167
Assets/Scripts/MessagePack/Internal/UnsafeMemory.Low.cs
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
// for string key property name write optimization.
|
||||
|
||||
public static class UnsafeMemory
|
||||
{
|
||||
public static readonly bool Is32Bit = (IntPtr.Size == 4);
|
||||
}
|
||||
|
||||
public static partial class UnsafeMemory32
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw1(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(byte*)pDst = *(byte*)pSrc;
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw2(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(short*)pDst = *(short*)pSrc;
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw3(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(byte*)pDst = *(byte*)pSrc;
|
||||
*(short*)(pDst + 1) = *(short*)(pSrc + 1);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public static partial class UnsafeMemory64
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw1(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(byte*)pDst = *(byte*)pSrc;
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw2(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(short*)pDst = *(short*)pSrc;
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw3(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(byte*)pDst = *(byte*)pSrc;
|
||||
*(short*)(pDst + 1) = *(short*)(pSrc + 1);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw4(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw5(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 1) = *(int*)(pSrc + 1);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw6(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 2) = *(int*)(pSrc + 2);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw7(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 3) = *(int*)(pSrc + 3);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
12
Assets/Scripts/MessagePack/Internal/UnsafeMemory.Low.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Internal/UnsafeMemory.Low.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 66d2fe79920989d4792d401c6a51e9b8
|
||||
timeCreated: 1504181170
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
894
Assets/Scripts/MessagePack/Internal/UnsafeMemory.cs
Normal file
894
Assets/Scripts/MessagePack/Internal/UnsafeMemory.cs
Normal file
|
|
@ -0,0 +1,894 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
public static partial class UnsafeMemory32
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw4(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw5(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 1) = *(int*)(pSrc + 1);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw6(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 2) = *(int*)(pSrc + 2);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw7(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 3) = *(int*)(pSrc + 3);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw8(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw9(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 5) = *(int*)(pSrc + 5);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw10(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 6) = *(int*)(pSrc + 6);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw11(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 7) = *(int*)(pSrc + 7);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw12(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw13(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 9) = *(int*)(pSrc + 9);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw14(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 10) = *(int*)(pSrc + 10);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw15(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 11) = *(int*)(pSrc + 11);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw16(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw17(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 13) = *(int*)(pSrc + 13);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw18(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 14) = *(int*)(pSrc + 14);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw19(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 15) = *(int*)(pSrc + 15);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw20(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw21(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 17) = *(int*)(pSrc + 17);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw22(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 18) = *(int*)(pSrc + 18);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw23(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 19) = *(int*)(pSrc + 19);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw24(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw25(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 21) = *(int*)(pSrc + 21);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw26(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 22) = *(int*)(pSrc + 22);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw27(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 23) = *(int*)(pSrc + 23);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw28(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 24) = *(int*)(pSrc + 24);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw29(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 24) = *(int*)(pSrc + 24);
|
||||
*(int*)(pDst + 25) = *(int*)(pSrc + 25);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw30(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 24) = *(int*)(pSrc + 24);
|
||||
*(int*)(pDst + 26) = *(int*)(pSrc + 26);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw31(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(int*)(pDst + 0) = *(int*)(pSrc + 0);
|
||||
*(int*)(pDst + 4) = *(int*)(pSrc + 4);
|
||||
*(int*)(pDst + 8) = *(int*)(pSrc + 8);
|
||||
*(int*)(pDst + 12) = *(int*)(pSrc + 12);
|
||||
*(int*)(pDst + 16) = *(int*)(pSrc + 16);
|
||||
*(int*)(pDst + 20) = *(int*)(pSrc + 20);
|
||||
*(int*)(pDst + 24) = *(int*)(pSrc + 24);
|
||||
*(int*)(pDst + 27) = *(int*)(pSrc + 27);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static partial class UnsafeMemory64
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw8(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw9(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 1) = *(long*)(pSrc + 1);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw10(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 2) = *(long*)(pSrc + 2);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw11(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 3) = *(long*)(pSrc + 3);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw12(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 4) = *(long*)(pSrc + 4);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw13(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 5) = *(long*)(pSrc + 5);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw14(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 6) = *(long*)(pSrc + 6);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw15(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 7) = *(long*)(pSrc + 7);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw16(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw17(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 9) = *(long*)(pSrc + 9);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw18(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 10) = *(long*)(pSrc + 10);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw19(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 11) = *(long*)(pSrc + 11);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw20(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 12) = *(long*)(pSrc + 12);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw21(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 13) = *(long*)(pSrc + 13);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw22(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 14) = *(long*)(pSrc + 14);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw23(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 15) = *(long*)(pSrc + 15);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw24(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw25(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 17) = *(long*)(pSrc + 17);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw26(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 18) = *(long*)(pSrc + 18);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw27(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 19) = *(long*)(pSrc + 19);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw28(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 20) = *(long*)(pSrc + 20);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw29(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 21) = *(long*)(pSrc + 21);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw30(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 22) = *(long*)(pSrc + 22);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe int WriteRaw31(ref byte[] dst, int dstOffset, byte[] src)
|
||||
{
|
||||
MessagePackBinary.EnsureCapacity(ref dst, dstOffset, src.Length);
|
||||
|
||||
fixed (byte* pSrc = &src[0])
|
||||
fixed (byte* pDst = &dst[dstOffset])
|
||||
{
|
||||
*(long*)(pDst + 0) = *(long*)(pSrc + 0);
|
||||
*(long*)(pDst + 8) = *(long*)(pSrc + 8);
|
||||
*(long*)(pDst + 16) = *(long*)(pSrc + 16);
|
||||
*(long*)(pDst + 23) = *(long*)(pSrc + 23);
|
||||
}
|
||||
|
||||
return src.Length;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
12
Assets/Scripts/MessagePack/Internal/UnsafeMemory.cs.meta
Normal file
12
Assets/Scripts/MessagePack/Internal/UnsafeMemory.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d0e5250baf56d1141abd42ed1ba1b6f6
|
||||
timeCreated: 1504181170
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/Scripts/MessagePack/LZ4.meta
Normal file
9
Assets/Scripts/MessagePack/LZ4.meta
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6879702e59bcf3d4db35a0eae88bb8e0
|
||||
folderAsset: yes
|
||||
timeCreated: 1489057987
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/Scripts/MessagePack/LZ4/Codec.meta
Normal file
9
Assets/Scripts/MessagePack/LZ4/Codec.meta
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c783993d40a4bb944a2e2a2f8323886f
|
||||
folderAsset: yes
|
||||
timeCreated: 1489069054
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
115
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Helper.cs
Normal file
115
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Helper.cs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
using System;
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#if NETSTANDARD
|
||||
|
||||
public static int Encode(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Encode32Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Encode64Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Decode(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Decode32Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Decode64Unsafe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// use 'Safe' code for Unity because in IL2CPP gots strange behaviour.
|
||||
|
||||
public static int Encode(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Encode32Safe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Encode64Safe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Decode(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Decode32Safe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Decode64Safe(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
internal static class HashTablePool
|
||||
{
|
||||
[ThreadStatic]
|
||||
static ushort[] ushortPool;
|
||||
|
||||
[ThreadStatic]
|
||||
static uint[] uintPool;
|
||||
|
||||
[ThreadStatic]
|
||||
static int[] intPool;
|
||||
|
||||
public static ushort[] GetUShortHashTablePool()
|
||||
{
|
||||
if (ushortPool == null)
|
||||
{
|
||||
ushortPool = new ushort[HASH64K_TABLESIZE];
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Clear(ushortPool, 0, ushortPool.Length);
|
||||
}
|
||||
return ushortPool;
|
||||
}
|
||||
|
||||
public static uint[] GetUIntHashTablePool()
|
||||
{
|
||||
if (uintPool == null)
|
||||
{
|
||||
uintPool = new uint[HASH_TABLESIZE];
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Clear(uintPool, 0, uintPool.Length);
|
||||
}
|
||||
return uintPool;
|
||||
}
|
||||
|
||||
public static int[] GetIntHashTablePool()
|
||||
{
|
||||
if (intPool == null)
|
||||
{
|
||||
intPool = new int[HASH_TABLESIZE];
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Clear(intPool, 0, intPool.Length);
|
||||
}
|
||||
return intPool;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Helper.cs.meta
Normal file
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Helper.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7b5dd6b75c45f4046936c088aa4276c3
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
437
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe.cs
Normal file
437
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe.cs
Normal file
|
|
@ -0,0 +1,437 @@
|
|||
#region license
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
// ReSharper disable CheckNamespace
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
/// <summary>Safe LZ4 codec.</summary>
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#region Helper
|
||||
|
||||
// ReSharper disable UnusedParameter.Local
|
||||
|
||||
[Conditional("DEBUG")]
|
||||
private static void Assert(bool condition, string errorMessage)
|
||||
{
|
||||
if (!condition) throw new ArgumentException(errorMessage);
|
||||
Debug.Assert(condition, errorMessage);
|
||||
}
|
||||
|
||||
// ReSharper restore UnusedParameter.Local
|
||||
|
||||
#endregion
|
||||
|
||||
#region Byte manipulation
|
||||
|
||||
// ReSharper disable RedundantCast
|
||||
|
||||
internal static void Poke2(byte[] buffer, int offset, ushort value)
|
||||
{
|
||||
buffer[offset] = (byte)value;
|
||||
buffer[offset + 1] = (byte)(value >> 8);
|
||||
}
|
||||
|
||||
internal static ushort Peek2(byte[] buffer, int offset)
|
||||
{
|
||||
// NOTE: It's faster than BitConverter.ToUInt16 (suprised? me too)
|
||||
return (ushort)(((uint)buffer[offset]) | ((uint)buffer[offset + 1] << 8));
|
||||
}
|
||||
|
||||
internal static uint Peek4(byte[] buffer, int offset)
|
||||
{
|
||||
// NOTE: It's faster than BitConverter.ToUInt32 (suprised? me too)
|
||||
return
|
||||
((uint)buffer[offset]) |
|
||||
((uint)buffer[offset + 1] << 8) |
|
||||
((uint)buffer[offset + 2] << 16) |
|
||||
((uint)buffer[offset + 3] << 24);
|
||||
}
|
||||
|
||||
private static uint Xor4(byte[] buffer, int offset1, int offset2)
|
||||
{
|
||||
// return Peek4(buffer, offset1) ^ Peek4(buffer, offset2);
|
||||
var value1 =
|
||||
((uint)buffer[offset1]) |
|
||||
((uint)buffer[offset1 + 1] << 8) |
|
||||
((uint)buffer[offset1 + 2] << 16) |
|
||||
((uint)buffer[offset1 + 3] << 24);
|
||||
var value2 =
|
||||
((uint)buffer[offset2]) |
|
||||
((uint)buffer[offset2 + 1] << 8) |
|
||||
((uint)buffer[offset2 + 2] << 16) |
|
||||
((uint)buffer[offset2 + 3] << 24);
|
||||
return value1 ^ value2;
|
||||
}
|
||||
|
||||
private static ulong Xor8(byte[] buffer, int offset1, int offset2)
|
||||
{
|
||||
// return Peek8(buffer, offset1) ^ Peek8(buffer, offset2);
|
||||
var value1 =
|
||||
((ulong)buffer[offset1]) |
|
||||
((ulong)buffer[offset1 + 1] << 8) |
|
||||
((ulong)buffer[offset1 + 2] << 16) |
|
||||
((ulong)buffer[offset1 + 3] << 24) |
|
||||
((ulong)buffer[offset1 + 4] << 32) |
|
||||
((ulong)buffer[offset1 + 5] << 40) |
|
||||
((ulong)buffer[offset1 + 6] << 48) |
|
||||
((ulong)buffer[offset1 + 7] << 56);
|
||||
var value2 =
|
||||
((ulong)buffer[offset2]) |
|
||||
((ulong)buffer[offset2 + 1] << 8) |
|
||||
((ulong)buffer[offset2 + 2] << 16) |
|
||||
((ulong)buffer[offset2 + 3] << 24) |
|
||||
((ulong)buffer[offset2 + 4] << 32) |
|
||||
((ulong)buffer[offset2 + 5] << 40) |
|
||||
((ulong)buffer[offset2 + 6] << 48) |
|
||||
((ulong)buffer[offset2 + 7] << 56);
|
||||
return value1 ^ value2;
|
||||
}
|
||||
|
||||
private static bool Equal2(byte[] buffer, int offset1, int offset2)
|
||||
{
|
||||
// return Peek2(buffer, offset1) == Peek2(buffer, offset2);
|
||||
if (buffer[offset1] != buffer[offset2]) return false;
|
||||
return buffer[offset1 + 1] == buffer[offset2 + 1];
|
||||
}
|
||||
|
||||
private static bool Equal4(byte[] buffer, int offset1, int offset2)
|
||||
{
|
||||
// return Peek4(buffer, offset1) == Peek4(buffer, offset2);
|
||||
if (buffer[offset1] != buffer[offset2]) return false;
|
||||
if (buffer[offset1 + 1] != buffer[offset2 + 1]) return false;
|
||||
if (buffer[offset1 + 2] != buffer[offset2 + 2]) return false;
|
||||
return buffer[offset1 + 3] == buffer[offset2 + 3];
|
||||
}
|
||||
|
||||
// ReSharper restore RedundantCast
|
||||
|
||||
#endregion
|
||||
|
||||
#region Byte block copy
|
||||
|
||||
private static void Copy4(byte[] buf, int src, int dst)
|
||||
{
|
||||
Assert(dst > src, "Copying backwards is not implemented");
|
||||
buf[dst + 3] = buf[src + 3];
|
||||
buf[dst + 2] = buf[src + 2];
|
||||
buf[dst + 1] = buf[src + 1];
|
||||
buf[dst] = buf[src];
|
||||
}
|
||||
|
||||
private static void Copy8(byte[] buf, int src, int dst)
|
||||
{
|
||||
Assert(dst > src, "Copying backwards is not implemented");
|
||||
buf[dst + 7] = buf[src + 7];
|
||||
buf[dst + 6] = buf[src + 6];
|
||||
buf[dst + 5] = buf[src + 5];
|
||||
buf[dst + 4] = buf[src + 4];
|
||||
buf[dst + 3] = buf[src + 3];
|
||||
buf[dst + 2] = buf[src + 2];
|
||||
buf[dst + 1] = buf[src + 1];
|
||||
buf[dst] = buf[src];
|
||||
}
|
||||
|
||||
private static void BlockCopy(byte[] src, int src_0, byte[] dst, int dst_0, int len)
|
||||
{
|
||||
Assert(src != dst, "BlockCopy does not handle copying to the same buffer");
|
||||
|
||||
if (len >= BLOCK_COPY_LIMIT)
|
||||
{
|
||||
Buffer.BlockCopy(src, src_0, dst, dst_0, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (len >= 8)
|
||||
{
|
||||
dst[dst_0] = src[src_0];
|
||||
dst[dst_0 + 1] = src[src_0 + 1];
|
||||
dst[dst_0 + 2] = src[src_0 + 2];
|
||||
dst[dst_0 + 3] = src[src_0 + 3];
|
||||
dst[dst_0 + 4] = src[src_0 + 4];
|
||||
dst[dst_0 + 5] = src[src_0 + 5];
|
||||
dst[dst_0 + 6] = src[src_0 + 6];
|
||||
dst[dst_0 + 7] = src[src_0 + 7];
|
||||
len -= 8;
|
||||
src_0 += 8;
|
||||
dst_0 += 8;
|
||||
}
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
dst[dst_0] = src[src_0];
|
||||
dst[dst_0 + 1] = src[src_0 + 1];
|
||||
dst[dst_0 + 2] = src[src_0 + 2];
|
||||
dst[dst_0 + 3] = src[src_0 + 3];
|
||||
len -= 4;
|
||||
src_0 += 4;
|
||||
dst_0 += 4;
|
||||
}
|
||||
|
||||
while (len-- > 0)
|
||||
{
|
||||
dst[dst_0++] = src[src_0++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int WildCopy(byte[] src, int src_0, byte[] dst, int dst_0, int dst_end)
|
||||
{
|
||||
var len = dst_end - dst_0;
|
||||
|
||||
Assert(src != dst, "BlockCopy does not handle copying to the same buffer");
|
||||
Assert(len > 0, "Length have to be greater than 0");
|
||||
|
||||
if (len >= BLOCK_COPY_LIMIT)
|
||||
{
|
||||
Buffer.BlockCopy(src, src_0, dst, dst_0, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
// apparently (tested) this is an overkill
|
||||
// it seems to be faster without this 8-byte loop
|
||||
//while (len >= 8)
|
||||
//{
|
||||
// dst[dst_0] = src[src_0];
|
||||
// dst[dst_0 + 1] = src[src_0 + 1];
|
||||
// dst[dst_0 + 2] = src[src_0 + 2];
|
||||
// dst[dst_0 + 3] = src[src_0 + 3];
|
||||
// dst[dst_0 + 4] = src[src_0 + 4];
|
||||
// dst[dst_0 + 5] = src[src_0 + 5];
|
||||
// dst[dst_0 + 6] = src[src_0 + 6];
|
||||
// dst[dst_0 + 7] = src[src_0 + 7];
|
||||
// len -= 8; src_0 += 8; dst_0 += 8;
|
||||
//}
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
dst[dst_0] = src[src_0];
|
||||
dst[dst_0 + 1] = src[src_0 + 1];
|
||||
dst[dst_0 + 2] = src[src_0 + 2];
|
||||
dst[dst_0 + 3] = src[src_0 + 3];
|
||||
len -= 4;
|
||||
src_0 += 4;
|
||||
dst_0 += 4;
|
||||
}
|
||||
|
||||
while (len-- > 0)
|
||||
{
|
||||
dst[dst_0++] = src[src_0++];
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
private static int SecureCopy(byte[] buffer, int src, int dst, int dst_end)
|
||||
{
|
||||
var diff = dst - src;
|
||||
var length = dst_end - dst;
|
||||
var len = length;
|
||||
|
||||
Assert(diff >= 4, "Target must be at least 4 bytes further than source");
|
||||
Assert(BLOCK_COPY_LIMIT > 4, "This method requires BLOCK_COPY_LIMIT > 4");
|
||||
Assert(len > 0, "Length have to be greater than 0");
|
||||
|
||||
if (diff >= BLOCK_COPY_LIMIT)
|
||||
{
|
||||
if (diff >= length)
|
||||
{
|
||||
Buffer.BlockCopy(buffer, src, buffer, dst, length);
|
||||
return length; // done
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
Buffer.BlockCopy(buffer, src, buffer, dst, diff);
|
||||
src += diff;
|
||||
dst += diff;
|
||||
len -= diff;
|
||||
} while (len >= diff);
|
||||
}
|
||||
|
||||
// apparently (tested) this is an overkill
|
||||
// it seems to be faster without this 8-byte loop
|
||||
//while (len >= 8)
|
||||
//{
|
||||
// buffer[dst] = buffer[src];
|
||||
// buffer[dst + 1] = buffer[src + 1];
|
||||
// buffer[dst + 2] = buffer[src + 2];
|
||||
// buffer[dst + 3] = buffer[src + 3];
|
||||
// buffer[dst + 4] = buffer[src + 4];
|
||||
// buffer[dst + 5] = buffer[src + 5];
|
||||
// buffer[dst + 6] = buffer[src + 6];
|
||||
// buffer[dst + 7] = buffer[src + 7];
|
||||
// dst += 8; src += 8; len -= 8;
|
||||
//}
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
buffer[dst] = buffer[src];
|
||||
buffer[dst + 1] = buffer[src + 1];
|
||||
buffer[dst + 2] = buffer[src + 2];
|
||||
buffer[dst + 3] = buffer[src + 3];
|
||||
dst += 4;
|
||||
src += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
while (len-- > 0)
|
||||
{
|
||||
buffer[dst++] = buffer[src++];
|
||||
}
|
||||
|
||||
return length; // done
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>Encodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static int Encode32Safe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
if (inputLength < LZ4_64KLIMIT)
|
||||
{
|
||||
var hashTable = HashTablePool.GetUShortHashTablePool();
|
||||
return LZ4_compress64kCtx_safe32(hashTable, input, output, inputOffset, outputOffset, inputLength, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
var hashTable = HashTablePool.GetIntHashTablePool();
|
||||
return LZ4_compressCtx_safe32(hashTable, input, output, inputOffset, outputOffset, inputLength, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Encodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static int Encode64Safe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
if (inputLength < LZ4_64KLIMIT)
|
||||
{
|
||||
var hashTable = HashTablePool.GetUShortHashTablePool();
|
||||
return LZ4_compress64kCtx_safe64(hashTable, input, output, inputOffset, outputOffset, inputLength, outputLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
var hashTable = HashTablePool.GetIntHashTablePool();
|
||||
return LZ4_compressCtx_safe64(hashTable, input, output, inputOffset, outputOffset, inputLength, outputLength);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Decodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static int Decode32Safe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
var length = LZ4_uncompress_safe32(input, output, inputOffset, outputOffset, outputLength);
|
||||
if (length != inputLength)
|
||||
throw new ArgumentException("LZ4 block is corrupted, or invalid length has been given.");
|
||||
return outputLength;
|
||||
}
|
||||
|
||||
/// <summary>Decodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static int Decode64Safe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
var length = LZ4_uncompress_safe64(input, output, inputOffset, outputOffset, outputLength);
|
||||
if (length != inputLength)
|
||||
throw new ArgumentException("LZ4 block is corrupted, or invalid length has been given.");
|
||||
return outputLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper restore InconsistentNaming
|
||||
// ReSharper restore CheckNamespace
|
||||
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe.cs.meta
Normal file
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 350a9b63158d43c45be12b6ed93fa227
|
||||
timeCreated: 1490454679
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
665
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe32.Dirty.cs
Normal file
665
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe32.Dirty.cs
Normal file
|
|
@ -0,0 +1,665 @@
|
|||
#region LZ4 original
|
||||
|
||||
/*
|
||||
LZ4 - Fast LZ compression algorithm
|
||||
Copyright (C) 2011-2012, Yann Collet.
|
||||
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
You can contact the author at :
|
||||
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
||||
- LZ4 source repository : http://code.google.com/p/lz4/
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4 port
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
// ReSharper disable CheckNamespace
|
||||
// ReSharper disable InconsistentNaming
|
||||
// ReSharper disable TooWideLocalVariableScope
|
||||
// ReSharper disable JoinDeclarationAndInitializer
|
||||
// ReSharper disable RedundantIfElseBlock
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#region LZ4_compressCtx
|
||||
|
||||
private static int LZ4_compressCtx_safe32(
|
||||
int[] hash_table,
|
||||
byte[] src,
|
||||
byte[] dst,
|
||||
int src_0,
|
||||
int dst_0,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var debruijn32 = DEBRUIJN_TABLE_32;
|
||||
int _i;
|
||||
|
||||
// ---- preprocessed source start here ----
|
||||
// r93
|
||||
var src_p = src_0;
|
||||
var src_base = src_0;
|
||||
var src_anchor = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst_0;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_32 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
hash_table[(((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST)] = (src_p - src_base);
|
||||
src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
int src_ref;
|
||||
int dst_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = (((Peek4(src, src_p_fwd)) * 2654435761u) >> HASH_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (src_p - src_base);
|
||||
} while ((src_ref < src_p - MAX_DISTANCE) || (!Equal4(src, src_ref, src_p)));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (src_ref > src_0) && (src[src_p - 1] == src[src_ref - 1]))
|
||||
{
|
||||
src_p--;
|
||||
src_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (src_p - src_anchor);
|
||||
dst_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
var len = length - RUN_MASK;
|
||||
dst[dst_token] = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
dst[dst_p++] = (byte)len;
|
||||
BlockCopy(src, src_anchor, dst, dst_p, length);
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
else
|
||||
dst[dst_p++] = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
if (length > 0)
|
||||
{
|
||||
_i = dst_p + length;
|
||||
WildCopy(src, src_anchor, dst, dst_p, _i);
|
||||
dst_p = _i;
|
||||
}
|
||||
|
||||
_next_match:
|
||||
// Encode Offset
|
||||
Poke2(dst, dst_p, (ushort)(src_p - src_ref));
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
src_ref += MINMATCH; // MinMatch already verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (int)Xor4(src, src_ref, src_p);
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_32;
|
||||
src_ref += STEPSIZE_32;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn32[((uint)((diff) & -(diff)) * 0x077CB531u) >> 27];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_1) && (Equal2(src, src_ref, src_p)))
|
||||
{
|
||||
src_p += 2;
|
||||
src_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (src[src_ref] == src[src_p])) src_p++;
|
||||
|
||||
_endCount:
|
||||
// Encode MatchLength
|
||||
length = (src_p - src_anchor);
|
||||
|
||||
if (dst_p + (length >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (length >= ML_MASK)
|
||||
{
|
||||
dst[dst_token] += ML_MASK;
|
||||
length -= ML_MASK;
|
||||
for (; length > 509; length -= 510)
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
if (length > 254)
|
||||
{
|
||||
length -= 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
dst[dst_p++] = (byte)length;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] += (byte)length;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[(((Peek4(src, src_p - 2)) * 2654435761u) >> HASH_ADJUST)] = (src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = (((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (src_p - src_base);
|
||||
|
||||
if ((src_ref > src_p - (MAX_DISTANCE + 1)) && (Equal4(src, src_ref, src_p)))
|
||||
{
|
||||
dst_token = dst_p++;
|
||||
dst[dst_token] = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
// Encode Last Literals
|
||||
{
|
||||
var lastRun = (src_end - src_anchor);
|
||||
|
||||
if (dst_p + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > dst_end) return 0;
|
||||
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
dst[dst_p++] = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) dst[dst_p++] = 255;
|
||||
dst[dst_p++] = (byte)lastRun;
|
||||
}
|
||||
else dst[dst_p++] = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src, src_anchor, dst, dst_p, src_end - src_anchor);
|
||||
dst_p += src_end - src_anchor;
|
||||
}
|
||||
|
||||
// End
|
||||
return ((dst_p) - dst_0);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_compress64kCtx
|
||||
|
||||
private static int LZ4_compress64kCtx_safe32(
|
||||
ushort[] hash_table,
|
||||
byte[] src,
|
||||
byte[] dst,
|
||||
int src_0,
|
||||
int dst_0,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var debruijn32 = DEBRUIJN_TABLE_32;
|
||||
int _i;
|
||||
|
||||
// ---- preprocessed source start here ----
|
||||
// r93
|
||||
var src_p = src_0;
|
||||
var src_anchor = src_p;
|
||||
var src_base = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst_0;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_32 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int len, length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
int src_ref;
|
||||
int dst_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = (((Peek4(src, src_p_fwd)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
} while (!Equal4(src, src_ref, src_p));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (src_ref > src_0) && (src[src_p - 1] == src[src_ref - 1]))
|
||||
{
|
||||
src_p--;
|
||||
src_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (src_p - src_anchor);
|
||||
dst_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
len = length - RUN_MASK;
|
||||
dst[dst_token] = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
dst[dst_p++] = (byte)len;
|
||||
BlockCopy(src, src_anchor, dst, dst_p, length);
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_p++] = (byte)len;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
if (length > 0)
|
||||
{
|
||||
_i = dst_p + length;
|
||||
WildCopy(src, src_anchor, dst, dst_p, _i);
|
||||
dst_p = _i;
|
||||
}
|
||||
|
||||
_next_match:
|
||||
// Encode Offset
|
||||
Poke2(dst, dst_p, (ushort)(src_p - src_ref));
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
src_ref += MINMATCH; // MinMatch verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (int)Xor4(src, src_ref, src_p);
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_32;
|
||||
src_ref += STEPSIZE_32;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn32[((uint)((diff) & -(diff)) * 0x077CB531u) >> 27];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_1) && (Equal2(src, src_ref, src_p)))
|
||||
{
|
||||
src_p += 2;
|
||||
src_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (src[src_ref] == src[src_p])) src_p++;
|
||||
|
||||
_endCount:
|
||||
|
||||
// Encode MatchLength
|
||||
len = (src_p - src_anchor);
|
||||
|
||||
if (dst_p + (len >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (len >= ML_MASK)
|
||||
{
|
||||
dst[dst_token] += ML_MASK;
|
||||
len -= ML_MASK;
|
||||
for (; len > 509; len -= 510)
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
if (len > 254)
|
||||
{
|
||||
len -= 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
dst[dst_p++] = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] += (byte)len;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[(((Peek4(src, src_p - 2)) * 2654435761u) >> HASH64K_ADJUST)] = (ushort)(src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
|
||||
if (Equal4(src, src_ref, src_p))
|
||||
{
|
||||
dst_token = dst_p++;
|
||||
dst[dst_token] = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
// Encode Last Literals
|
||||
var lastRun = (src_end - src_anchor);
|
||||
if (dst_p + lastRun + 1 + (lastRun - RUN_MASK + 255) / 255 > dst_end) return 0;
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
dst[dst_p++] = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) dst[dst_p++] = 255;
|
||||
dst[dst_p++] = (byte)lastRun;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_p++] = (byte)(lastRun << ML_BITS);
|
||||
}
|
||||
BlockCopy(src, src_anchor, dst, dst_p, src_end - src_anchor);
|
||||
dst_p += src_end - src_anchor;
|
||||
|
||||
// End
|
||||
return ((dst_p) - dst_0);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_uncompress
|
||||
|
||||
private static int LZ4_uncompress_safe32(
|
||||
byte[] src,
|
||||
byte[] dst,
|
||||
int src_0,
|
||||
int dst_0,
|
||||
int dst_len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var dec32table = DECODER_TABLE_32;
|
||||
int _i;
|
||||
|
||||
// ---- preprocessed source start here ----
|
||||
// r93
|
||||
var src_p = src_0;
|
||||
int dst_ref;
|
||||
|
||||
var dst_p = dst_0;
|
||||
var dst_end = dst_p + dst_len;
|
||||
int dst_cpy;
|
||||
|
||||
var dst_LASTLITERALS = dst_end - LASTLITERALS;
|
||||
var dst_COPYLENGTH = dst_end - COPYLENGTH;
|
||||
var dst_COPYLENGTH_STEPSIZE_4 = dst_end - COPYLENGTH - (STEPSIZE_32 - 4);
|
||||
|
||||
byte token;
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
int length;
|
||||
|
||||
// get runlength
|
||||
token = src[src_p++];
|
||||
if ((length = (token >> ML_BITS)) == RUN_MASK)
|
||||
{
|
||||
int len;
|
||||
for (; (len = src[src_p++]) == 255; length += 255)
|
||||
{
|
||||
/* do nothing */
|
||||
}
|
||||
length += len;
|
||||
}
|
||||
|
||||
// copy literals
|
||||
dst_cpy = dst_p + length;
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH)
|
||||
{
|
||||
if (dst_cpy != dst_end) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
|
||||
BlockCopy(src, src_p, dst, dst_p, length);
|
||||
src_p += length;
|
||||
break; // EOF
|
||||
}
|
||||
if (dst_p < dst_cpy)
|
||||
{
|
||||
_i = WildCopy(src, src_p, dst, dst_p, dst_cpy);
|
||||
src_p += _i;
|
||||
dst_p += _i;
|
||||
}
|
||||
src_p -= (dst_p - dst_cpy);
|
||||
dst_p = dst_cpy;
|
||||
|
||||
// get offset
|
||||
dst_ref = (dst_cpy) - Peek2(src, src_p);
|
||||
src_p += 2;
|
||||
if (dst_ref < dst_0) goto _output_error; // Error : offset outside destination buffer
|
||||
|
||||
// get matchlength
|
||||
if ((length = (token & ML_MASK)) == ML_MASK)
|
||||
{
|
||||
for (; src[src_p] == 255; length += 255) src_p++;
|
||||
length += src[src_p++];
|
||||
}
|
||||
|
||||
// copy repeated sequence
|
||||
if ((dst_p - dst_ref) < STEPSIZE_32)
|
||||
{
|
||||
const int dec64 = 0;
|
||||
dst[dst_p + 0] = dst[dst_ref + 0];
|
||||
dst[dst_p + 1] = dst[dst_ref + 1];
|
||||
dst[dst_p + 2] = dst[dst_ref + 2];
|
||||
dst[dst_p + 3] = dst[dst_ref + 3];
|
||||
dst_p += 4;
|
||||
dst_ref += 4;
|
||||
dst_ref -= dec32table[dst_p - dst_ref];
|
||||
Copy4(dst, dst_ref, dst_p);
|
||||
dst_p += STEPSIZE_32 - 4;
|
||||
dst_ref -= dec64;
|
||||
}
|
||||
else
|
||||
{
|
||||
Copy4(dst, dst_ref, dst_p);
|
||||
dst_p += 4;
|
||||
dst_ref += 4;
|
||||
}
|
||||
dst_cpy = dst_p + length - (STEPSIZE_32 - 4);
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH_STEPSIZE_4)
|
||||
{
|
||||
if (dst_cpy > dst_LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
|
||||
if (dst_p < dst_COPYLENGTH)
|
||||
{
|
||||
_i = SecureCopy(dst, dst_ref, dst_p, dst_COPYLENGTH);
|
||||
dst_ref += _i;
|
||||
dst_p += _i;
|
||||
}
|
||||
|
||||
while (dst_p < dst_cpy) dst[dst_p++] = dst[dst_ref++];
|
||||
dst_p = dst_cpy;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dst_p < dst_cpy)
|
||||
{
|
||||
SecureCopy(dst, dst_ref, dst_p, dst_cpy);
|
||||
}
|
||||
dst_p = dst_cpy; // correction
|
||||
}
|
||||
|
||||
// end of decoding
|
||||
return ((src_p) - src_0);
|
||||
|
||||
// write overflow error detected
|
||||
_output_error:
|
||||
return (-((src_p) - src_0));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper restore RedundantIfElseBlock
|
||||
// ReSharper restore JoinDeclarationAndInitializer
|
||||
// ReSharper restore TooWideLocalVariableScope
|
||||
// ReSharper restore InconsistentNaming
|
||||
// ReSharper restore CheckNamespace
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9bcd40e694d03534381975789978c1f1
|
||||
timeCreated: 1490454679
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
678
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe64.Dirty.cs
Normal file
678
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Safe64.Dirty.cs
Normal file
|
|
@ -0,0 +1,678 @@
|
|||
#region LZ4 original
|
||||
|
||||
/*
|
||||
LZ4 - Fast LZ compression algorithm
|
||||
Copyright (C) 2011-2012, Yann Collet.
|
||||
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
You can contact the author at :
|
||||
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
||||
- LZ4 source repository : http://code.google.com/p/lz4/
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4 port
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
// ReSharper disable CheckNamespace
|
||||
// ReSharper disable InconsistentNaming
|
||||
// ReSharper disable TooWideLocalVariableScope
|
||||
// ReSharper disable JoinDeclarationAndInitializer
|
||||
// ReSharper disable RedundantIfElseBlock
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#region LZ4_compressCtx
|
||||
|
||||
private static int LZ4_compressCtx_safe64(
|
||||
int[] hash_table,
|
||||
byte[] src,
|
||||
byte[] dst,
|
||||
int src_0,
|
||||
int dst_0,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var debruijn64 = DEBRUIJN_TABLE_64;
|
||||
int _i;
|
||||
|
||||
// ---- preprocessed source start here ----
|
||||
// r93
|
||||
var src_p = src_0;
|
||||
var src_base = src_0;
|
||||
var src_anchor = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst_0;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_3 = src_LASTLITERALS - 3;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_64 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
hash_table[(((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST)] = (src_p - src_base);
|
||||
src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
int src_ref;
|
||||
int dst_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = (((Peek4(src, src_p_fwd)) * 2654435761u) >> HASH_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (src_p - src_base);
|
||||
} while ((src_ref < src_p - MAX_DISTANCE) || (!Equal4(src, src_ref, src_p)));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (src_ref > src_0) && (src[src_p - 1] == src[src_ref - 1]))
|
||||
{
|
||||
src_p--;
|
||||
src_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (src_p - src_anchor);
|
||||
dst_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
var len = length - RUN_MASK;
|
||||
dst[dst_token] = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
dst[dst_p++] = (byte)len;
|
||||
BlockCopy(src, src_anchor, dst, dst_p, length);
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
else
|
||||
dst[dst_p++] = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
if (length > 0)
|
||||
{
|
||||
_i = dst_p + length;
|
||||
WildCopy(src, src_anchor, dst, dst_p, _i);
|
||||
dst_p = _i;
|
||||
}
|
||||
|
||||
_next_match:
|
||||
// Encode Offset
|
||||
Poke2(dst, dst_p, (ushort)(src_p - src_ref));
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
src_ref += MINMATCH; // MinMatch already verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (long)Xor8(src, src_ref, src_p);
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_64;
|
||||
src_ref += STEPSIZE_64;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn64[((ulong)((diff) & -(diff)) * 0x0218A392CDABBD3FL) >> 58];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_3) && (Equal4(src, src_ref, src_p)))
|
||||
{
|
||||
src_p += 4;
|
||||
src_ref += 4;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS_1) && (Equal2(src, src_ref, src_p)))
|
||||
{
|
||||
src_p += 2;
|
||||
src_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (src[src_ref] == src[src_p])) src_p++;
|
||||
|
||||
_endCount:
|
||||
// Encode MatchLength
|
||||
length = (src_p - src_anchor);
|
||||
|
||||
if (dst_p + (length >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (length >= ML_MASK)
|
||||
{
|
||||
dst[dst_token] += ML_MASK;
|
||||
length -= ML_MASK;
|
||||
for (; length > 509; length -= 510)
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
if (length > 254)
|
||||
{
|
||||
length -= 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
dst[dst_p++] = (byte)length;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] += (byte)length;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[(((Peek4(src, src_p - 2)) * 2654435761u) >> HASH_ADJUST)] = (src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = (((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (src_p - src_base);
|
||||
|
||||
if ((src_ref > src_p - (MAX_DISTANCE + 1)) && (Equal4(src, src_ref, src_p)))
|
||||
{
|
||||
dst_token = dst_p++;
|
||||
dst[dst_token] = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
// Encode Last Literals
|
||||
{
|
||||
var lastRun = (src_end - src_anchor);
|
||||
|
||||
if (dst_p + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > dst_end) return 0;
|
||||
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
dst[dst_p++] = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) dst[dst_p++] = 255;
|
||||
dst[dst_p++] = (byte)lastRun;
|
||||
}
|
||||
else dst[dst_p++] = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src, src_anchor, dst, dst_p, src_end - src_anchor);
|
||||
dst_p += src_end - src_anchor;
|
||||
}
|
||||
|
||||
// End
|
||||
return (dst_p - dst_0);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_compress64kCtx
|
||||
|
||||
private static int LZ4_compress64kCtx_safe64(
|
||||
ushort[] hash_table,
|
||||
byte[] src,
|
||||
byte[] dst,
|
||||
int src_0,
|
||||
int dst_0,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var debruijn64 = DEBRUIJN_TABLE_64;
|
||||
int _i;
|
||||
|
||||
// ---- preprocessed source start here ----
|
||||
// r93
|
||||
var src_p = src_0;
|
||||
var src_anchor = src_p;
|
||||
var src_base = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst_0;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_3 = src_LASTLITERALS - 3;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_64 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int len, length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
int src_ref;
|
||||
int dst_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = (((Peek4(src, src_p_fwd)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
} while (!Equal4(src, src_ref, src_p));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (src_ref > src_0) && (src[src_p - 1] == src[src_ref - 1]))
|
||||
{
|
||||
src_p--;
|
||||
src_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (src_p - src_anchor);
|
||||
dst_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
len = length - RUN_MASK;
|
||||
dst[dst_token] = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
dst[dst_p++] = (byte)len;
|
||||
BlockCopy(src, src_anchor, dst, dst_p, length);
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
else
|
||||
dst[dst_p++] = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
if (length > 0) /*?*/
|
||||
{
|
||||
_i = dst_p + length;
|
||||
WildCopy(src, src_anchor, dst, dst_p, _i);
|
||||
dst_p = _i;
|
||||
}
|
||||
|
||||
_next_match:
|
||||
// Encode Offset
|
||||
Poke2(dst, dst_p, (ushort)(src_p - src_ref));
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
src_ref += MINMATCH; // MinMatch verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (long)Xor8(src, src_ref, src_p);
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_64;
|
||||
src_ref += STEPSIZE_64;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn64[((ulong)((diff) & -(diff)) * 0x0218A392CDABBD3FL) >> 58];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_3) && (Equal4(src, src_ref, src_p)))
|
||||
{
|
||||
src_p += 4;
|
||||
src_ref += 4;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS_1) && (Equal2(src, src_ref, src_p)))
|
||||
{
|
||||
src_p += 2;
|
||||
src_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (src[src_ref] == src[src_p])) src_p++;
|
||||
|
||||
_endCount:
|
||||
|
||||
// Encode MatchLength
|
||||
len = (src_p - src_anchor);
|
||||
|
||||
if (dst_p + (len >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (len >= ML_MASK)
|
||||
{
|
||||
dst[dst_token] += ML_MASK;
|
||||
len -= ML_MASK;
|
||||
for (; len > 509; len -= 510)
|
||||
{
|
||||
dst[dst_p++] = 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
if (len > 254)
|
||||
{
|
||||
len -= 255;
|
||||
dst[dst_p++] = 255;
|
||||
}
|
||||
dst[dst_p++] = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
dst[dst_token] += (byte)len;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[(((Peek4(src, src_p - 2)) * 2654435761u) >> HASH64K_ADJUST)] = (ushort)(src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
|
||||
if (Equal4(src, src_ref, src_p))
|
||||
{
|
||||
dst_token = dst_p++;
|
||||
dst[dst_token] = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = (((Peek4(src, src_p)) * 2654435761u) >> HASH64K_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
// Encode Last Literals
|
||||
{
|
||||
var lastRun = (src_end - src_anchor);
|
||||
if (dst_p + lastRun + 1 + (lastRun - RUN_MASK + 255) / 255 > dst_end) return 0;
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
dst[dst_p++] = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) dst[dst_p++] = 255;
|
||||
dst[dst_p++] = (byte)lastRun;
|
||||
}
|
||||
else dst[dst_p++] = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src, src_anchor, dst, dst_p, src_end - src_anchor);
|
||||
dst_p += src_end - src_anchor;
|
||||
}
|
||||
|
||||
// End
|
||||
return (dst_p - dst_0);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_uncompress
|
||||
|
||||
private static int LZ4_uncompress_safe64(
|
||||
byte[] src,
|
||||
byte[] dst,
|
||||
int src_0,
|
||||
int dst_0,
|
||||
int dst_len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
var dec32table = DECODER_TABLE_32;
|
||||
var dec64table = DECODER_TABLE_64;
|
||||
int _i;
|
||||
|
||||
// ---- preprocessed source start here ----
|
||||
// r93
|
||||
var src_p = src_0;
|
||||
int dst_ref;
|
||||
|
||||
var dst_p = dst_0;
|
||||
var dst_end = dst_p + dst_len;
|
||||
int dst_cpy;
|
||||
|
||||
var dst_LASTLITERALS = dst_end - LASTLITERALS;
|
||||
var dst_COPYLENGTH = dst_end - COPYLENGTH;
|
||||
var dst_COPYLENGTH_STEPSIZE_4 = dst_end - COPYLENGTH - (STEPSIZE_64 - 4);
|
||||
|
||||
uint token;
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
int length;
|
||||
|
||||
// get runlength
|
||||
token = src[src_p++];
|
||||
if ((length = (byte)(token >> ML_BITS)) == RUN_MASK)
|
||||
{
|
||||
int len;
|
||||
for (; (len = src[src_p++]) == 255; length += 255)
|
||||
{
|
||||
/* do nothing */
|
||||
}
|
||||
length += len;
|
||||
}
|
||||
|
||||
// copy literals
|
||||
dst_cpy = dst_p + length;
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH)
|
||||
{
|
||||
if (dst_cpy != dst_end) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
|
||||
BlockCopy(src, src_p, dst, dst_p, length);
|
||||
src_p += length;
|
||||
break; // EOF
|
||||
}
|
||||
if (dst_p < dst_cpy) /*?*/
|
||||
{
|
||||
_i = WildCopy(src, src_p, dst, dst_p, dst_cpy);
|
||||
src_p += _i;
|
||||
dst_p += _i;
|
||||
}
|
||||
src_p -= (dst_p - dst_cpy);
|
||||
dst_p = dst_cpy;
|
||||
|
||||
// get offset
|
||||
dst_ref = (dst_cpy) - Peek2(src, src_p);
|
||||
src_p += 2;
|
||||
if (dst_ref < dst_0) goto _output_error; // Error : offset outside destination buffer
|
||||
|
||||
// get matchlength
|
||||
if ((length = (byte)(token & ML_MASK)) == ML_MASK)
|
||||
{
|
||||
for (; src[src_p] == 255; length += 255) src_p++;
|
||||
length += src[src_p++];
|
||||
}
|
||||
|
||||
// copy repeated sequence
|
||||
if ((dst_p - dst_ref) < STEPSIZE_64)
|
||||
{
|
||||
var dec64 = dec64table[dst_p - dst_ref];
|
||||
|
||||
dst[dst_p + 0] = dst[dst_ref + 0];
|
||||
dst[dst_p + 1] = dst[dst_ref + 1];
|
||||
dst[dst_p + 2] = dst[dst_ref + 2];
|
||||
dst[dst_p + 3] = dst[dst_ref + 3];
|
||||
dst_p += 4;
|
||||
dst_ref += 4;
|
||||
dst_ref -= dec32table[dst_p - dst_ref];
|
||||
Copy4(dst, dst_ref, dst_p);
|
||||
dst_p += STEPSIZE_64 - 4;
|
||||
dst_ref -= dec64;
|
||||
}
|
||||
else
|
||||
{
|
||||
Copy8(dst, dst_ref, dst_p);
|
||||
dst_p += 8;
|
||||
dst_ref += 8;
|
||||
}
|
||||
dst_cpy = dst_p + length - (STEPSIZE_64 - 4);
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH_STEPSIZE_4)
|
||||
{
|
||||
if (dst_cpy > dst_LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
|
||||
if (dst_p < dst_COPYLENGTH)
|
||||
{
|
||||
_i = SecureCopy(dst, dst_ref, dst_p, dst_COPYLENGTH);
|
||||
dst_ref += _i;
|
||||
dst_p += _i;
|
||||
}
|
||||
|
||||
while (dst_p < dst_cpy) dst[dst_p++] = dst[dst_ref++];
|
||||
dst_p = dst_cpy;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dst_p < dst_cpy)
|
||||
{
|
||||
SecureCopy(dst, dst_ref, dst_p, dst_cpy);
|
||||
}
|
||||
dst_p = dst_cpy; // correction
|
||||
}
|
||||
|
||||
// end of decoding
|
||||
return ((src_p) - src_0);
|
||||
|
||||
_output_error:
|
||||
// write overflow error detected
|
||||
return (-((src_p) - src_0));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper restore RedundantIfElseBlock
|
||||
// ReSharper restore JoinDeclarationAndInitializer
|
||||
// ReSharper restore TooWideLocalVariableScope
|
||||
// ReSharper restore InconsistentNaming
|
||||
// ReSharper restore CheckNamespace
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bcaa47d405206684c9506db70c32210e
|
||||
timeCreated: 1490454679
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
222
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe.cs
Normal file
222
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe.cs
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
#if ENABLE_UNSAFE_MSGPACK
|
||||
|
||||
#region license
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
/// <summary>Unsafe LZ4 codec.</summary>
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
/// <summary>Copies block of memory.</summary>
|
||||
/// <param name="src">The source.</param>
|
||||
/// <param name="dst">The destination.</param>
|
||||
/// <param name="len">The length (in bytes).</param>
|
||||
private static unsafe void BlockCopy(byte* src, byte* dst, int len)
|
||||
{
|
||||
while (len >= 8)
|
||||
{
|
||||
*(ulong*)dst = *(ulong*)src;
|
||||
dst += 8;
|
||||
src += 8;
|
||||
len -= 8;
|
||||
}
|
||||
if (len >= 4)
|
||||
{
|
||||
*(uint*)dst = *(uint*)src;
|
||||
dst += 4;
|
||||
src += 4;
|
||||
len -= 4;
|
||||
}
|
||||
if (len >= 2)
|
||||
{
|
||||
*(ushort*)dst = *(ushort*)src;
|
||||
dst += 2;
|
||||
src += 2;
|
||||
len -= 2;
|
||||
}
|
||||
if (len >= 1)
|
||||
{
|
||||
*dst = *src; /* d++; s++; l--; */
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Encodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Encode32Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(input, inputOffset, inputLength, output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
{
|
||||
if (inputLength < LZ4_64KLIMIT)
|
||||
{
|
||||
var uHashTable = HashTablePool.GetUShortHashTablePool();
|
||||
fixed (ushort* hash1 = &uHashTable[0])
|
||||
{
|
||||
return LZ4_compress64kCtx_32(hash1, inputPtr, outputPtr, inputLength, outputLength);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var bHashTable = HashTablePool.GetUIntHashTablePool();
|
||||
fixed (uint* hash2 = &bHashTable[0])
|
||||
{
|
||||
return LZ4_compressCtx_32(hash2, inputPtr, outputPtr, inputLength, outputLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Decodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Decode32Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
{
|
||||
var length = LZ4_uncompress_32(inputPtr, outputPtr, outputLength);
|
||||
if (length != inputLength)
|
||||
throw new ArgumentException("LZ4 block is corrupted, or invalid length has been given.");
|
||||
return outputLength;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Encodes the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Encode64Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
{
|
||||
if (inputLength < LZ4_64KLIMIT)
|
||||
{
|
||||
var uHashTable = HashTablePool.GetUShortHashTablePool();
|
||||
fixed (ushort* h1 = &uHashTable[0])
|
||||
{
|
||||
return LZ4_compress64kCtx_64(h1, inputPtr, outputPtr, inputLength, outputLength);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var uiHashTable = HashTablePool.GetUIntHashTablePool();
|
||||
fixed (uint* h2 = &uiHashTable[0])
|
||||
{
|
||||
return LZ4_compressCtx_64(h2, inputPtr, outputPtr, inputLength, outputLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Decode64s the specified input.</summary>
|
||||
/// <param name="input">The input.</param>
|
||||
/// <param name="inputOffset">The input offset.</param>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <param name="output">The output.</param>
|
||||
/// <param name="outputOffset">The output offset.</param>
|
||||
/// <param name="outputLength">Length of the output.</param>
|
||||
/// <returns>Number of bytes written.</returns>
|
||||
public static unsafe int Decode64Unsafe(
|
||||
byte[] input,
|
||||
int inputOffset,
|
||||
int inputLength,
|
||||
byte[] output,
|
||||
int outputOffset,
|
||||
int outputLength)
|
||||
{
|
||||
CheckArguments(
|
||||
input, inputOffset, inputLength,
|
||||
output, outputOffset, outputLength);
|
||||
|
||||
if (outputLength == 0) return 0;
|
||||
|
||||
fixed (byte* inputPtr = &input[inputOffset])
|
||||
fixed (byte* outputPtr = &output[outputOffset])
|
||||
{
|
||||
var length = LZ4_uncompress_64(inputPtr, outputPtr, outputLength);
|
||||
if (length != inputLength)
|
||||
throw new ArgumentException("LZ4 block is corrupted, or invalid length has been given.");
|
||||
return outputLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe.cs.meta
Normal file
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fa7e8688b34abd749bff81ec21bc614c
|
||||
timeCreated: 1489131597
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
680
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe32.Dirty.cs
Normal file
680
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe32.Dirty.cs
Normal file
|
|
@ -0,0 +1,680 @@
|
|||
#if ENABLE_UNSAFE_MSGPACK
|
||||
|
||||
#region LZ4 original
|
||||
|
||||
/*
|
||||
LZ4 - Fast LZ compression algorithm
|
||||
Copyright (C) 2011-2012, Yann Collet.
|
||||
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
You can contact the author at :
|
||||
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
||||
- LZ4 source repository : http://code.google.com/p/lz4/
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4 port
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
// ReSharper disable TooWideLocalVariableScope
|
||||
// ReSharper disable JoinDeclarationAndInitializer
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#region LZ4_compressCtx_32
|
||||
|
||||
private static unsafe int LZ4_compressCtx_32(
|
||||
uint* hash_table,
|
||||
byte* src,
|
||||
byte* dst,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
byte* _p;
|
||||
|
||||
fixed (int* debruijn32 = &DEBRUIJN_TABLE_32[0])
|
||||
{
|
||||
// r93
|
||||
var src_p = src;
|
||||
var src_base = src_p;
|
||||
var src_anchor = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_32 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
hash_table[((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST)] = (uint)(src_p - src_base);
|
||||
src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
byte* xxx_ref;
|
||||
byte* xxx_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = ((((*(uint*)(src_p_fwd))) * 2654435761u) >> HASH_ADJUST);
|
||||
xxx_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (uint)(src_p - src_base);
|
||||
} while ((xxx_ref < src_p - MAX_DISTANCE) || ((*(uint*)(xxx_ref)) != (*(uint*)(src_p))));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (xxx_ref > src) && (src_p[-1] == xxx_ref[-1]))
|
||||
{
|
||||
src_p--;
|
||||
xxx_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (int)(src_p - src_anchor);
|
||||
xxx_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
var len = length - RUN_MASK;
|
||||
*xxx_token = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
*dst_p++ = (byte)len;
|
||||
BlockCopy(src_anchor, dst_p, (length));
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
*dst_p++ = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
*xxx_token = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
_p = dst_p + (length);
|
||||
do
|
||||
{
|
||||
*(uint*)dst_p = *(uint*)src_anchor;
|
||||
dst_p += 4;
|
||||
src_anchor += 4;
|
||||
*(uint*)dst_p = *(uint*)src_anchor;
|
||||
dst_p += 4;
|
||||
src_anchor += 4;
|
||||
} while (dst_p < _p);
|
||||
dst_p = _p;
|
||||
|
||||
_next_match:
|
||||
|
||||
// Encode Offset
|
||||
*(ushort*)dst_p = (ushort)(src_p - xxx_ref);
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
xxx_ref += MINMATCH; // MinMatch already verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (*(int*)(xxx_ref)) ^ (*(int*)(src_p));
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_32;
|
||||
xxx_ref += STEPSIZE_32;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn32[(((uint)((diff) & -(diff)) * 0x077CB531u)) >> 27];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_1) && ((*(ushort*)(xxx_ref)) == (*(ushort*)(src_p))))
|
||||
{
|
||||
src_p += 2;
|
||||
xxx_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (*xxx_ref == *src_p)) src_p++;
|
||||
|
||||
_endCount:
|
||||
|
||||
// Encode MatchLength
|
||||
length = (int)(src_p - src_anchor);
|
||||
|
||||
if (dst_p + (length >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (length >= ML_MASK)
|
||||
{
|
||||
*xxx_token += ML_MASK;
|
||||
length -= ML_MASK;
|
||||
for (; length > 509; length -= 510)
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
if (length > 254)
|
||||
{
|
||||
length -= 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
*dst_p++ = (byte)length;
|
||||
}
|
||||
else
|
||||
{
|
||||
*xxx_token += (byte)length;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[((((*(uint*)(src_p - 2))) * 2654435761u) >> HASH_ADJUST)] = (uint)(src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = ((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST);
|
||||
xxx_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (uint)(src_p - src_base);
|
||||
|
||||
if ((xxx_ref > src_p - (MAX_DISTANCE + 1)) && ((*(uint*)(xxx_ref)) == (*(uint*)(src_p))))
|
||||
{
|
||||
xxx_token = dst_p++;
|
||||
*xxx_token = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
|
||||
// Encode Last Literals
|
||||
{
|
||||
var lastRun = (int)(src_end - src_anchor);
|
||||
|
||||
if (dst_p + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > dst_end) return 0;
|
||||
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
*dst_p++ = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) *dst_p++ = 255;
|
||||
*dst_p++ = (byte)lastRun;
|
||||
}
|
||||
else *dst_p++ = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src_anchor, dst_p, (int)(src_end - src_anchor));
|
||||
dst_p += src_end - src_anchor;
|
||||
}
|
||||
|
||||
// End
|
||||
return (int)((dst_p) - dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_compress64kCtx_32
|
||||
|
||||
private static unsafe int LZ4_compress64kCtx_32(
|
||||
ushort* hash_table,
|
||||
byte* src,
|
||||
byte* dst,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
byte* _p;
|
||||
fixed (int* debruijn32 = &DEBRUIJN_TABLE_32[0])
|
||||
{
|
||||
// r93
|
||||
var src_p = src;
|
||||
var src_anchor = src_p;
|
||||
var src_base = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_32 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int len, length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
byte* xxx_ref;
|
||||
byte* xxx_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = ((((*(uint*)(src_p_fwd))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
xxx_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
} while ((*(uint*)(xxx_ref)) != (*(uint*)(src_p)));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (xxx_ref > src) && (src_p[-1] == xxx_ref[-1]))
|
||||
{
|
||||
src_p--;
|
||||
xxx_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (int)(src_p - src_anchor);
|
||||
xxx_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
len = length - RUN_MASK;
|
||||
*xxx_token = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
*dst_p++ = (byte)len;
|
||||
BlockCopy(src_anchor, dst_p, (length));
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
*dst_p++ = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
*xxx_token = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
_p = dst_p + (length);
|
||||
do
|
||||
{
|
||||
*(uint*)dst_p = *(uint*)src_anchor;
|
||||
dst_p += 4;
|
||||
src_anchor += 4;
|
||||
*(uint*)dst_p = *(uint*)src_anchor;
|
||||
dst_p += 4;
|
||||
src_anchor += 4;
|
||||
} while (dst_p < _p);
|
||||
dst_p = _p;
|
||||
|
||||
_next_match:
|
||||
|
||||
// Encode Offset
|
||||
*(ushort*)dst_p = (ushort)(src_p - xxx_ref);
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
xxx_ref += MINMATCH; // MinMatch verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (*(int*)(xxx_ref)) ^ (*(int*)(src_p));
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_32;
|
||||
xxx_ref += STEPSIZE_32;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn32[(((uint)((diff) & -(diff)) * 0x077CB531u)) >> 27];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_1) && ((*(ushort*)(xxx_ref)) == (*(ushort*)(src_p))))
|
||||
{
|
||||
src_p += 2;
|
||||
xxx_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (*xxx_ref == *src_p)) src_p++;
|
||||
|
||||
_endCount:
|
||||
|
||||
// Encode MatchLength
|
||||
len = (int)(src_p - src_anchor);
|
||||
|
||||
if (dst_p + (len >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (len >= ML_MASK)
|
||||
{
|
||||
*xxx_token += ML_MASK;
|
||||
len -= ML_MASK;
|
||||
for (; len > 509; len -= 510)
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
if (len > 254)
|
||||
{
|
||||
len -= 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
*dst_p++ = (byte)len;
|
||||
}
|
||||
else *xxx_token += (byte)len;
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[((((*(uint*)(src_p - 2))) * 2654435761u) >> HASH64K_ADJUST)] = (ushort)(src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = ((((*(uint*)(src_p))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
xxx_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
|
||||
if ((*(uint*)(xxx_ref)) == (*(uint*)(src_p)))
|
||||
{
|
||||
xxx_token = dst_p++;
|
||||
*xxx_token = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
|
||||
// Encode Last Literals
|
||||
{
|
||||
var lastRun = (int)(src_end - src_anchor);
|
||||
if (dst_p + lastRun + 1 + (lastRun - RUN_MASK + 255) / 255 > dst_end) return 0;
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
*dst_p++ = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) *dst_p++ = 255;
|
||||
*dst_p++ = (byte)lastRun;
|
||||
}
|
||||
else *dst_p++ = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src_anchor, dst_p, (int)(src_end - src_anchor));
|
||||
dst_p += src_end - src_anchor;
|
||||
}
|
||||
|
||||
// End
|
||||
return (int)((dst_p) - dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_uncompress_32
|
||||
|
||||
private static unsafe int LZ4_uncompress_32(
|
||||
byte* src,
|
||||
byte* dst,
|
||||
int dst_len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
fixed (int* dec32table = &DECODER_TABLE_32[0])
|
||||
{
|
||||
// r93
|
||||
var src_p = src;
|
||||
byte* xxx_ref;
|
||||
|
||||
var dst_p = dst;
|
||||
var dst_end = dst_p + dst_len;
|
||||
byte* dst_cpy;
|
||||
|
||||
var dst_LASTLITERALS = dst_end - LASTLITERALS;
|
||||
var dst_COPYLENGTH = dst_end - COPYLENGTH;
|
||||
var dst_COPYLENGTH_STEPSIZE_4 = dst_end - COPYLENGTH - (STEPSIZE_32 - 4);
|
||||
|
||||
uint xxx_token;
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
int length;
|
||||
|
||||
// get runlength
|
||||
xxx_token = *src_p++;
|
||||
if ((length = (int)(xxx_token >> ML_BITS)) == RUN_MASK)
|
||||
{
|
||||
int len;
|
||||
for (; (len = *src_p++) == 255; length += 255)
|
||||
{
|
||||
/* do nothing */
|
||||
}
|
||||
length += len;
|
||||
}
|
||||
|
||||
// copy literals
|
||||
dst_cpy = dst_p + length;
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH)
|
||||
{
|
||||
if (dst_cpy != dst_end) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
|
||||
BlockCopy(src_p, dst_p, (length));
|
||||
src_p += length;
|
||||
break; // EOF
|
||||
}
|
||||
do
|
||||
{
|
||||
*(uint*)dst_p = *(uint*)src_p;
|
||||
dst_p += 4;
|
||||
src_p += 4;
|
||||
*(uint*)dst_p = *(uint*)src_p;
|
||||
dst_p += 4;
|
||||
src_p += 4;
|
||||
} while (dst_p < dst_cpy);
|
||||
src_p -= (dst_p - dst_cpy);
|
||||
dst_p = dst_cpy;
|
||||
|
||||
// get offset
|
||||
xxx_ref = (dst_cpy) - (*(ushort*)(src_p));
|
||||
src_p += 2;
|
||||
if (xxx_ref < dst) goto _output_error; // Error : offset outside destination buffer
|
||||
|
||||
// get matchlength
|
||||
if ((length = (int)(xxx_token & ML_MASK)) == ML_MASK)
|
||||
{
|
||||
for (; *src_p == 255; length += 255) src_p++;
|
||||
length += *src_p++;
|
||||
}
|
||||
|
||||
// copy repeated sequence
|
||||
if ((dst_p - xxx_ref) < STEPSIZE_32)
|
||||
{
|
||||
const int dec64 = 0;
|
||||
|
||||
dst_p[0] = xxx_ref[0];
|
||||
dst_p[1] = xxx_ref[1];
|
||||
dst_p[2] = xxx_ref[2];
|
||||
dst_p[3] = xxx_ref[3];
|
||||
dst_p += 4;
|
||||
xxx_ref += 4;
|
||||
xxx_ref -= dec32table[dst_p - xxx_ref];
|
||||
(*(uint*)(dst_p)) = (*(uint*)(xxx_ref));
|
||||
dst_p += STEPSIZE_32 - 4;
|
||||
xxx_ref -= dec64;
|
||||
}
|
||||
else
|
||||
{
|
||||
*(uint*)dst_p = *(uint*)xxx_ref;
|
||||
dst_p += 4;
|
||||
xxx_ref += 4;
|
||||
}
|
||||
dst_cpy = dst_p + length - (STEPSIZE_32 - 4);
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH_STEPSIZE_4)
|
||||
{
|
||||
if (dst_cpy > dst_LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
|
||||
{
|
||||
do
|
||||
{
|
||||
*(uint*)dst_p = *(uint*)xxx_ref;
|
||||
dst_p += 4;
|
||||
xxx_ref += 4;
|
||||
*(uint*)dst_p = *(uint*)xxx_ref;
|
||||
dst_p += 4;
|
||||
xxx_ref += 4;
|
||||
} while (dst_p < dst_COPYLENGTH);
|
||||
}
|
||||
|
||||
while (dst_p < dst_cpy) *dst_p++ = *xxx_ref++;
|
||||
dst_p = dst_cpy;
|
||||
continue;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
*(uint*)dst_p = *(uint*)xxx_ref;
|
||||
dst_p += 4;
|
||||
xxx_ref += 4;
|
||||
*(uint*)dst_p = *(uint*)xxx_ref;
|
||||
dst_p += 4;
|
||||
xxx_ref += 4;
|
||||
} while (dst_p < dst_cpy);
|
||||
dst_p = dst_cpy; // correction
|
||||
}
|
||||
|
||||
// end of decoding
|
||||
return (int)((src_p) - src);
|
||||
|
||||
// write overflow error detected
|
||||
_output_error:
|
||||
return (int)(-((src_p) - src));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper restore JoinDeclarationAndInitializer
|
||||
// ReSharper restore TooWideLocalVariableScope
|
||||
// ReSharper restore InconsistentNaming
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 12789d40b95f6b848921f2f2470b24de
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
682
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe64.Dirty.cs
Normal file
682
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.Unsafe64.Dirty.cs
Normal file
|
|
@ -0,0 +1,682 @@
|
|||
#if ENABLE_UNSAFE_MSGPACK
|
||||
|
||||
#region LZ4 original
|
||||
|
||||
/*
|
||||
LZ4 - Fast LZ compression algorithm
|
||||
Copyright (C) 2011-2012, Yann Collet.
|
||||
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
You can contact the author at :
|
||||
- LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
|
||||
- LZ4 source repository : http://code.google.com/p/lz4/
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4 port
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
// ReSharper disable TooWideLocalVariableScope
|
||||
// ReSharper disable JoinDeclarationAndInitializer
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#region LZ4_compressCtx_64
|
||||
|
||||
private static unsafe int LZ4_compressCtx_64(
|
||||
uint* hash_table,
|
||||
byte* src,
|
||||
byte* dst,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
byte* _p;
|
||||
|
||||
fixed (int* debruijn64 = &DEBRUIJN_TABLE_64[0])
|
||||
{
|
||||
// r93
|
||||
var src_p = src;
|
||||
var src_base = src_p;
|
||||
var src_anchor = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_3 = src_LASTLITERALS - 3;
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_64 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int length;
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
hash_table[((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST)] = (uint)(src_p - src_base);
|
||||
src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
byte* src_ref;
|
||||
byte* dst_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = ((((*(uint*)(src_p_fwd))) * 2654435761u) >> HASH_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (uint)(src_p - src_base);
|
||||
} while ((src_ref < src_p - MAX_DISTANCE) || ((*(uint*)(src_ref)) != (*(uint*)(src_p))));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (src_ref > src) && (src_p[-1] == src_ref[-1]))
|
||||
{
|
||||
src_p--;
|
||||
src_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (int)(src_p - src_anchor);
|
||||
dst_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
var len = length - RUN_MASK;
|
||||
*dst_token = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
*dst_p++ = (byte)len;
|
||||
BlockCopy(src_anchor, dst_p, (length));
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
*dst_p++ = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
*dst_token = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
_p = dst_p + (length);
|
||||
{
|
||||
do
|
||||
{
|
||||
*(ulong*)dst_p = *(ulong*)src_anchor;
|
||||
dst_p += 8;
|
||||
src_anchor += 8;
|
||||
} while (dst_p < _p);
|
||||
}
|
||||
dst_p = _p;
|
||||
|
||||
_next_match:
|
||||
|
||||
// Encode Offset
|
||||
*(ushort*)dst_p = (ushort)(src_p - src_ref);
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
src_ref += MINMATCH; // MinMatch already verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (*(long*)(src_ref)) ^ (*(long*)(src_p));
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_64;
|
||||
src_ref += STEPSIZE_64;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn64[(((ulong)((diff) & -(diff)) * 0x0218A392CDABBD3FL)) >> 58];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_3) && ((*(uint*)(src_ref)) == (*(uint*)(src_p))))
|
||||
{
|
||||
src_p += 4;
|
||||
src_ref += 4;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS_1) && ((*(ushort*)(src_ref)) == (*(ushort*)(src_p))))
|
||||
{
|
||||
src_p += 2;
|
||||
src_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (*src_ref == *src_p)) src_p++;
|
||||
|
||||
_endCount:
|
||||
|
||||
// Encode MatchLength
|
||||
length = (int)(src_p - src_anchor);
|
||||
|
||||
if (dst_p + (length >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (length >= ML_MASK)
|
||||
{
|
||||
*dst_token += ML_MASK;
|
||||
length -= ML_MASK;
|
||||
for (; length > 509; length -= 510)
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
if (length > 254)
|
||||
{
|
||||
length -= 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
*dst_p++ = (byte)length;
|
||||
}
|
||||
else
|
||||
{
|
||||
*dst_token += (byte)length;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[((((*(uint*)(src_p - 2))) * 2654435761u) >> HASH_ADJUST)] = (uint)(src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = ((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (uint)(src_p - src_base);
|
||||
|
||||
if ((src_ref > src_p - (MAX_DISTANCE + 1)) && ((*(uint*)(src_ref)) == (*(uint*)(src_p))))
|
||||
{
|
||||
dst_token = dst_p++;
|
||||
*dst_token = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
|
||||
// Encode Last Literals
|
||||
var lastRun = (int)(src_end - src_anchor);
|
||||
if (dst_p + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > dst_end) return 0;
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
*dst_p++ = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) *dst_p++ = 255;
|
||||
*dst_p++ = (byte)lastRun;
|
||||
}
|
||||
else *dst_p++ = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src_anchor, dst_p, (int)(src_end - src_anchor));
|
||||
dst_p += src_end - src_anchor;
|
||||
|
||||
// End
|
||||
return (int)(dst_p - dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_compress64kCtx_64
|
||||
|
||||
private static unsafe int LZ4_compress64kCtx_64(
|
||||
ushort* hash_table,
|
||||
byte* src,
|
||||
byte* dst,
|
||||
int src_len,
|
||||
int dst_maxlen)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
byte* _p;
|
||||
|
||||
fixed (int* debruijn64 = &DEBRUIJN_TABLE_64[0])
|
||||
{
|
||||
// r93
|
||||
var src_p = src;
|
||||
var src_anchor = src_p;
|
||||
var src_base = src_p;
|
||||
var src_end = src_p + src_len;
|
||||
var src_mflimit = src_end - MFLIMIT;
|
||||
|
||||
var dst_p = dst;
|
||||
var dst_end = dst_p + dst_maxlen;
|
||||
|
||||
var src_LASTLITERALS = src_end - LASTLITERALS;
|
||||
var src_LASTLITERALS_1 = src_LASTLITERALS - 1;
|
||||
|
||||
var src_LASTLITERALS_3 = src_LASTLITERALS - 3;
|
||||
|
||||
var src_LASTLITERALS_STEPSIZE_1 = src_LASTLITERALS - (STEPSIZE_64 - 1);
|
||||
var dst_LASTLITERALS_1 = dst_end - (1 + LASTLITERALS);
|
||||
var dst_LASTLITERALS_3 = dst_end - (2 + 1 + LASTLITERALS);
|
||||
|
||||
int len, length;
|
||||
|
||||
uint h, h_fwd;
|
||||
|
||||
// Init
|
||||
if (src_len < MINLENGTH) goto _last_literals;
|
||||
|
||||
// First Byte
|
||||
src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
var findMatchAttempts = (1 << SKIPSTRENGTH) + 3;
|
||||
var src_p_fwd = src_p;
|
||||
byte* src_ref;
|
||||
byte* dst_token;
|
||||
|
||||
// Find a match
|
||||
do
|
||||
{
|
||||
h = h_fwd;
|
||||
var step = findMatchAttempts++ >> SKIPSTRENGTH;
|
||||
src_p = src_p_fwd;
|
||||
src_p_fwd = src_p + step;
|
||||
|
||||
if (src_p_fwd > src_mflimit) goto _last_literals;
|
||||
|
||||
h_fwd = ((((*(uint*)(src_p_fwd))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
} while ((*(uint*)(src_ref)) != (*(uint*)(src_p)));
|
||||
|
||||
// Catch up
|
||||
while ((src_p > src_anchor) && (src_ref > src) && (src_p[-1] == src_ref[-1]))
|
||||
{
|
||||
src_p--;
|
||||
src_ref--;
|
||||
}
|
||||
|
||||
// Encode Literal length
|
||||
length = (int)(src_p - src_anchor);
|
||||
dst_token = dst_p++;
|
||||
|
||||
if (dst_p + length + (length >> 8) > dst_LASTLITERALS_3) return 0; // Check output limit
|
||||
|
||||
if (length >= RUN_MASK)
|
||||
{
|
||||
len = length - RUN_MASK;
|
||||
*dst_token = (RUN_MASK << ML_BITS);
|
||||
if (len > 254)
|
||||
{
|
||||
do
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
len -= 255;
|
||||
} while (len > 254);
|
||||
*dst_p++ = (byte)len;
|
||||
BlockCopy(src_anchor, dst_p, (length));
|
||||
dst_p += length;
|
||||
goto _next_match;
|
||||
}
|
||||
*dst_p++ = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
*dst_token = (byte)(length << ML_BITS);
|
||||
}
|
||||
|
||||
// Copy Literals
|
||||
{
|
||||
_p = dst_p + (length);
|
||||
{
|
||||
do
|
||||
{
|
||||
*(ulong*)dst_p = *(ulong*)src_anchor;
|
||||
dst_p += 8;
|
||||
src_anchor += 8;
|
||||
} while (dst_p < _p);
|
||||
}
|
||||
dst_p = _p;
|
||||
}
|
||||
|
||||
_next_match:
|
||||
|
||||
// Encode Offset
|
||||
*(ushort*)dst_p = (ushort)(src_p - src_ref);
|
||||
dst_p += 2;
|
||||
|
||||
// Start Counting
|
||||
src_p += MINMATCH;
|
||||
src_ref += MINMATCH; // MinMatch verified
|
||||
src_anchor = src_p;
|
||||
|
||||
while (src_p < src_LASTLITERALS_STEPSIZE_1)
|
||||
{
|
||||
var diff = (*(long*)(src_ref)) ^ (*(long*)(src_p));
|
||||
if (diff == 0)
|
||||
{
|
||||
src_p += STEPSIZE_64;
|
||||
src_ref += STEPSIZE_64;
|
||||
continue;
|
||||
}
|
||||
src_p += debruijn64[(((ulong)((diff) & -(diff)) * 0x0218A392CDABBD3FL)) >> 58];
|
||||
goto _endCount;
|
||||
}
|
||||
|
||||
if ((src_p < src_LASTLITERALS_3) && ((*(uint*)(src_ref)) == (*(uint*)(src_p))))
|
||||
{
|
||||
src_p += 4;
|
||||
src_ref += 4;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS_1) && ((*(ushort*)(src_ref)) == (*(ushort*)(src_p))))
|
||||
{
|
||||
src_p += 2;
|
||||
src_ref += 2;
|
||||
}
|
||||
if ((src_p < src_LASTLITERALS) && (*src_ref == *src_p)) src_p++;
|
||||
|
||||
_endCount:
|
||||
|
||||
// Encode MatchLength
|
||||
len = (int)(src_p - src_anchor);
|
||||
|
||||
if (dst_p + (len >> 8) > dst_LASTLITERALS_1) return 0; // Check output limit
|
||||
|
||||
if (len >= ML_MASK)
|
||||
{
|
||||
*dst_token += ML_MASK;
|
||||
len -= ML_MASK;
|
||||
for (; len > 509; len -= 510)
|
||||
{
|
||||
*dst_p++ = 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
if (len > 254)
|
||||
{
|
||||
len -= 255;
|
||||
*dst_p++ = 255;
|
||||
}
|
||||
*dst_p++ = (byte)len;
|
||||
}
|
||||
else
|
||||
{
|
||||
*dst_token += (byte)len;
|
||||
}
|
||||
|
||||
// Test end of chunk
|
||||
if (src_p > src_mflimit)
|
||||
{
|
||||
src_anchor = src_p;
|
||||
break;
|
||||
}
|
||||
|
||||
// Fill table
|
||||
hash_table[((((*(uint*)(src_p - 2))) * 2654435761u) >> HASH64K_ADJUST)] = (ushort)(src_p - 2 - src_base);
|
||||
|
||||
// Test next position
|
||||
|
||||
h = ((((*(uint*)(src_p))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
src_ref = src_base + hash_table[h];
|
||||
hash_table[h] = (ushort)(src_p - src_base);
|
||||
|
||||
if ((*(uint*)(src_ref)) == (*(uint*)(src_p)))
|
||||
{
|
||||
dst_token = dst_p++;
|
||||
*dst_token = 0;
|
||||
goto _next_match;
|
||||
}
|
||||
|
||||
// Prepare next loop
|
||||
src_anchor = src_p++;
|
||||
h_fwd = ((((*(uint*)(src_p))) * 2654435761u) >> HASH64K_ADJUST);
|
||||
}
|
||||
|
||||
_last_literals:
|
||||
|
||||
// Encode Last Literals
|
||||
var lastRun = (int)(src_end - src_anchor);
|
||||
if (dst_p + lastRun + 1 + (lastRun - RUN_MASK + 255) / 255 > dst_end) return 0;
|
||||
if (lastRun >= RUN_MASK)
|
||||
{
|
||||
*dst_p++ = (RUN_MASK << ML_BITS);
|
||||
lastRun -= RUN_MASK;
|
||||
for (; lastRun > 254; lastRun -= 255) *dst_p++ = 255;
|
||||
*dst_p++ = (byte)lastRun;
|
||||
}
|
||||
else *dst_p++ = (byte)(lastRun << ML_BITS);
|
||||
BlockCopy(src_anchor, dst_p, (int)(src_end - src_anchor));
|
||||
dst_p += src_end - src_anchor;
|
||||
|
||||
// End
|
||||
return (int)(dst_p - dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region LZ4_uncompress_64
|
||||
|
||||
private static unsafe int LZ4_uncompress_64(
|
||||
byte* src,
|
||||
byte* dst,
|
||||
int dst_len)
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
fixed (int* dec32table = &DECODER_TABLE_32[0])
|
||||
fixed (int* dec64table = &DECODER_TABLE_64[0])
|
||||
{
|
||||
// r93
|
||||
var src_p = src;
|
||||
byte* dst_ref;
|
||||
|
||||
var dst_p = dst;
|
||||
var dst_end = dst_p + dst_len;
|
||||
byte* dst_cpy;
|
||||
|
||||
var dst_LASTLITERALS = dst_end - LASTLITERALS;
|
||||
var dst_COPYLENGTH = dst_end - COPYLENGTH;
|
||||
var dst_COPYLENGTH_STEPSIZE_4 = dst_end - COPYLENGTH - (STEPSIZE_64 - 4);
|
||||
|
||||
byte token;
|
||||
|
||||
// Main Loop
|
||||
while (true)
|
||||
{
|
||||
int length;
|
||||
|
||||
// get runlength
|
||||
token = *src_p++;
|
||||
if ((length = (token >> ML_BITS)) == RUN_MASK)
|
||||
{
|
||||
int len;
|
||||
for (; (len = *src_p++) == 255; length += 255)
|
||||
{
|
||||
/* do nothing */
|
||||
}
|
||||
length += len;
|
||||
}
|
||||
|
||||
// copy literals
|
||||
dst_cpy = dst_p + length;
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH)
|
||||
{
|
||||
if (dst_cpy != dst_end) goto _output_error; // Error : not enough place for another match (min 4) + 5 literals
|
||||
BlockCopy(src_p, dst_p, (length));
|
||||
src_p += length;
|
||||
break; // EOF
|
||||
}
|
||||
do
|
||||
{
|
||||
*(ulong*)dst_p = *(ulong*)src_p;
|
||||
dst_p += 8;
|
||||
src_p += 8;
|
||||
} while (dst_p < dst_cpy);
|
||||
src_p -= (dst_p - dst_cpy);
|
||||
dst_p = dst_cpy;
|
||||
|
||||
// get offset
|
||||
dst_ref = (dst_cpy) - (*(ushort*)(src_p));
|
||||
src_p += 2;
|
||||
if (dst_ref < dst) goto _output_error; // Error : offset outside destination buffer
|
||||
|
||||
// get matchlength
|
||||
if ((length = (token & ML_MASK)) == ML_MASK)
|
||||
{
|
||||
for (; *src_p == 255; length += 255) src_p++;
|
||||
length += *src_p++;
|
||||
}
|
||||
|
||||
// copy repeated sequence
|
||||
if ((dst_p - dst_ref) < STEPSIZE_64)
|
||||
{
|
||||
var dec64 = dec64table[dst_p - dst_ref];
|
||||
|
||||
dst_p[0] = dst_ref[0];
|
||||
dst_p[1] = dst_ref[1];
|
||||
dst_p[2] = dst_ref[2];
|
||||
dst_p[3] = dst_ref[3];
|
||||
dst_p += 4;
|
||||
dst_ref += 4;
|
||||
dst_ref -= dec32table[dst_p - dst_ref];
|
||||
(*(uint*)(dst_p)) = (*(uint*)(dst_ref));
|
||||
dst_p += STEPSIZE_64 - 4;
|
||||
dst_ref -= dec64;
|
||||
}
|
||||
else
|
||||
{
|
||||
*(ulong*)dst_p = *(ulong*)dst_ref;
|
||||
dst_p += 8;
|
||||
dst_ref += 8;
|
||||
}
|
||||
dst_cpy = dst_p + length - (STEPSIZE_64 - 4);
|
||||
|
||||
if (dst_cpy > dst_COPYLENGTH_STEPSIZE_4)
|
||||
{
|
||||
if (dst_cpy > dst_LASTLITERALS) goto _output_error; // Error : last 5 bytes must be literals
|
||||
while (dst_p < dst_COPYLENGTH)
|
||||
{
|
||||
*(ulong*)dst_p = *(ulong*)dst_ref;
|
||||
dst_p += 8;
|
||||
dst_ref += 8;
|
||||
}
|
||||
|
||||
while (dst_p < dst_cpy) *dst_p++ = *dst_ref++;
|
||||
dst_p = dst_cpy;
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
do
|
||||
{
|
||||
*(ulong*)dst_p = *(ulong*)dst_ref;
|
||||
dst_p += 8;
|
||||
dst_ref += 8;
|
||||
} while (dst_p < dst_cpy);
|
||||
}
|
||||
dst_p = dst_cpy; // correction
|
||||
}
|
||||
|
||||
// end of decoding
|
||||
return (int)((src_p) - src);
|
||||
|
||||
// write overflow error detected
|
||||
_output_error:
|
||||
return (int)(-((src_p) - src));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper restore JoinDeclarationAndInitializer
|
||||
// ReSharper restore TooWideLocalVariableScope
|
||||
// ReSharper restore InconsistentNaming
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: de72c794ac869bf4f95a95cb75a2db79
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
158
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.cs
Normal file
158
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.cs
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
#region license
|
||||
|
||||
/*
|
||||
Copyright (c) 2013, Milosz Krajewski
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||||
that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
||||
and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
||||
and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
||||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace MessagePack.LZ4
|
||||
{
|
||||
public static partial class LZ4Codec
|
||||
{
|
||||
#region configuration
|
||||
|
||||
/// <summary>
|
||||
/// Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
|
||||
/// Increasing memory usage improves compression ratio
|
||||
/// Reduced memory usage can improve speed, due to cache effect
|
||||
/// Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
|
||||
/// </summary>
|
||||
private const int MEMORY_USAGE = 12; // modified use 12.
|
||||
|
||||
/// <summary>
|
||||
/// Decreasing this value will make the algorithm skip faster data segments considered "incompressible"
|
||||
/// This may decrease compression ratio dramatically, but will be faster on incompressible data
|
||||
/// Increasing this value will make the algorithm search more before declaring a segment "incompressible"
|
||||
/// This could improve compression a bit, but will be slower on incompressible data
|
||||
/// The default value (6) is recommended
|
||||
/// </summary>
|
||||
private const int NOTCOMPRESSIBLE_DETECTIONLEVEL = 6;
|
||||
|
||||
#endregion
|
||||
|
||||
#region consts
|
||||
|
||||
private const int MINMATCH = 4;
|
||||
|
||||
#pragma warning disable 162, 429
|
||||
// ReSharper disable once UnreachableCode
|
||||
private const int SKIPSTRENGTH =
|
||||
NOTCOMPRESSIBLE_DETECTIONLEVEL > 2
|
||||
? NOTCOMPRESSIBLE_DETECTIONLEVEL
|
||||
: 2;
|
||||
#pragma warning restore 162, 429
|
||||
|
||||
private const int COPYLENGTH = 8;
|
||||
private const int LASTLITERALS = 5;
|
||||
private const int MFLIMIT = COPYLENGTH + MINMATCH;
|
||||
private const int MINLENGTH = MFLIMIT + 1;
|
||||
private const int MAXD_LOG = 16;
|
||||
private const int MAXD = 1 << MAXD_LOG;
|
||||
private const int MAXD_MASK = MAXD - 1;
|
||||
private const int MAX_DISTANCE = (1 << MAXD_LOG) - 1;
|
||||
private const int ML_BITS = 4;
|
||||
private const int ML_MASK = (1 << ML_BITS) - 1;
|
||||
private const int RUN_BITS = 8 - ML_BITS;
|
||||
private const int RUN_MASK = (1 << RUN_BITS) - 1;
|
||||
private const int STEPSIZE_64 = 8;
|
||||
private const int STEPSIZE_32 = 4;
|
||||
|
||||
private const int LZ4_64KLIMIT = (1 << 16) + (MFLIMIT - 1);
|
||||
|
||||
private const int HASH_LOG = MEMORY_USAGE - 2;
|
||||
private const int HASH_TABLESIZE = 1 << HASH_LOG;
|
||||
private const int HASH_ADJUST = (MINMATCH * 8) - HASH_LOG;
|
||||
|
||||
private const int HASH64K_LOG = HASH_LOG + 1;
|
||||
private const int HASH64K_TABLESIZE = 1 << HASH64K_LOG;
|
||||
private const int HASH64K_ADJUST = (MINMATCH * 8) - HASH64K_LOG;
|
||||
|
||||
private const int HASHHC_LOG = MAXD_LOG - 1;
|
||||
private const int HASHHC_TABLESIZE = 1 << HASHHC_LOG;
|
||||
private const int HASHHC_ADJUST = (MINMATCH * 8) - HASHHC_LOG;
|
||||
//private const int HASHHC_MASK = HASHHC_TABLESIZE - 1;
|
||||
|
||||
private static readonly int[] DECODER_TABLE_32 = { 0, 3, 2, 3, 0, 0, 0, 0 };
|
||||
private static readonly int[] DECODER_TABLE_64 = { 0, 0, 0, -1, 0, 1, 2, 3 };
|
||||
|
||||
private static readonly int[] DEBRUIJN_TABLE_32 = {
|
||||
0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1,
|
||||
3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1
|
||||
};
|
||||
|
||||
private static readonly int[] DEBRUIJN_TABLE_64 = {
|
||||
0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7,
|
||||
0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7,
|
||||
7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6,
|
||||
7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7
|
||||
};
|
||||
|
||||
private const int MAX_NB_ATTEMPTS = 256;
|
||||
private const int OPTIMAL_ML = (ML_MASK - 1) + MINMATCH;
|
||||
|
||||
private const int BLOCK_COPY_LIMIT = 16;
|
||||
|
||||
#endregion
|
||||
|
||||
#region public interface (common)
|
||||
|
||||
/// <summary>Gets maximum the length of the output.</summary>
|
||||
/// <param name="inputLength">Length of the input.</param>
|
||||
/// <returns>Maximum number of bytes needed for compressed buffer.</returns>
|
||||
public static int MaximumOutputLength(int inputLength)
|
||||
{
|
||||
return inputLength + (inputLength / 255) + 16;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region internal interface (common)
|
||||
|
||||
internal static void CheckArguments(
|
||||
byte[] input, int inputOffset, int inputLength,
|
||||
byte[] output, int outputOffset, int outputLength)
|
||||
{
|
||||
if (inputLength == 0)
|
||||
{
|
||||
outputLength = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (input == null) throw new ArgumentNullException("input");
|
||||
if ((uint)inputOffset > (uint)input.Length) throw new ArgumentOutOfRangeException("inputOffset");
|
||||
if ((uint)inputLength > (uint)input.Length - (uint)inputOffset) throw new ArgumentOutOfRangeException("inputLength");
|
||||
|
||||
if (output == null) throw new ArgumentNullException("output");
|
||||
if ((uint)outputOffset > (uint)output.Length) throw new ArgumentOutOfRangeException("outputOffset");
|
||||
if ((uint)outputLength > (uint)output.Length - (uint)outputOffset) throw new ArgumentOutOfRangeException("outputLength");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper restore InconsistentNaming
|
||||
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.cs.meta
Normal file
12
Assets/Scripts/MessagePack/LZ4/Codec/LZ4Codec.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1493ebdaae2d2ec44808ae4cf6418b31
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
292
Assets/Scripts/MessagePack/LZ4/LZ4MessagePackSerializer.JSON.cs
Normal file
292
Assets/Scripts/MessagePack/LZ4/LZ4MessagePackSerializer.JSON.cs
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
using MessagePack.Formatters;
|
||||
using MessagePack.Internal;
|
||||
using MessagePack.LZ4;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
// JSON API
|
||||
public static partial class LZ4MessagePackSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Dump to JSON string.
|
||||
/// </summary>
|
||||
public static string ToJson<T>(T obj)
|
||||
{
|
||||
return ToJson(Serialize(obj));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dump to JSON string.
|
||||
/// </summary>
|
||||
public static string ToJson<T>(T obj, IFormatterResolver resolver)
|
||||
{
|
||||
return ToJson(Serialize(obj, resolver));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dump message-pack binary to JSON string.
|
||||
/// </summary>
|
||||
public static string ToJson(byte[] bytes)
|
||||
{
|
||||
if (bytes == null || bytes.Length == 0) return "";
|
||||
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes, 0) == MessagePackType.Extension)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes, 0, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
{
|
||||
// decode lz4
|
||||
var offset = readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = LZ4MemoryPool.GetBuffer();
|
||||
if (buffer.Length < length)
|
||||
{
|
||||
buffer = new byte[length];
|
||||
}
|
||||
|
||||
// LZ4 Decode
|
||||
LZ4Codec.Decode(bytes, offset, bytes.Length - offset, buffer, 0, length);
|
||||
|
||||
bytes = buffer; // use LZ4 bytes
|
||||
}
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
ToJsonCore(bytes, 0, sb);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static byte[] FromJson(string str)
|
||||
{
|
||||
using (var sr = new StringReader(str))
|
||||
{
|
||||
return FromJson(sr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// From Json String to LZ4MessagePack binary
|
||||
/// </summary>
|
||||
public static byte[] FromJson(TextReader reader)
|
||||
{
|
||||
var buffer = MessagePackSerializer.FromJsonUnsafe(reader); // offset is guranteed from 0
|
||||
return LZ4MessagePackSerializer.ToLZ4Binary(buffer);
|
||||
}
|
||||
|
||||
static int ToJsonCore(byte[] bytes, int offset, StringBuilder builder)
|
||||
{
|
||||
var readSize = 0;
|
||||
var type = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
switch (type)
|
||||
{
|
||||
case MessagePackType.Integer:
|
||||
var code = bytes[offset];
|
||||
if (MessagePackCode.MinNegativeFixInt <= code && code <= MessagePackCode.MaxNegativeFixInt) builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
|
||||
else if (MessagePackCode.MinFixInt <= code && code <= MessagePackCode.MaxFixInt) builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int8) builder.Append(MessagePackBinary.ReadSByte(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int16) builder.Append(MessagePackBinary.ReadInt16(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int32) builder.Append(MessagePackBinary.ReadInt32(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.Int64) builder.Append(MessagePackBinary.ReadInt64(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt8) builder.Append(MessagePackBinary.ReadByte(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt16) builder.Append(MessagePackBinary.ReadUInt16(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt32) builder.Append(MessagePackBinary.ReadUInt32(bytes, offset, out readSize));
|
||||
else if (code == MessagePackCode.UInt64) builder.Append(MessagePackBinary.ReadUInt64(bytes, offset, out readSize));
|
||||
break;
|
||||
case MessagePackType.Boolean:
|
||||
builder.Append(MessagePackBinary.ReadBoolean(bytes, offset, out readSize) ? "true" : "false");
|
||||
break;
|
||||
case MessagePackType.Float:
|
||||
var floatCode = bytes[offset];
|
||||
if (floatCode == MessagePackCode.Float32)
|
||||
{
|
||||
builder.Append(MessagePackBinary.ReadSingle(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append(MessagePackBinary.ReadDouble(bytes, offset, out readSize).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
break;
|
||||
case MessagePackType.String:
|
||||
WriteJsonString(MessagePackBinary.ReadString(bytes, offset, out readSize), builder);
|
||||
break;
|
||||
case MessagePackType.Binary:
|
||||
builder.Append("\"" + Convert.ToBase64String(MessagePackBinary.ReadBytes(bytes, offset, out readSize)) + "\"");
|
||||
break;
|
||||
case MessagePackType.Array:
|
||||
{
|
||||
var length = MessagePackBinary.ReadArrayHeaderRaw(bytes, offset, out readSize);
|
||||
var totalReadSize = readSize;
|
||||
offset += readSize;
|
||||
builder.Append("[");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
|
||||
if (i != length - 1)
|
||||
{
|
||||
builder.Append(",");
|
||||
}
|
||||
}
|
||||
builder.Append("]");
|
||||
|
||||
return totalReadSize;
|
||||
}
|
||||
case MessagePackType.Map:
|
||||
{
|
||||
var length = MessagePackBinary.ReadMapHeaderRaw(bytes, offset, out readSize);
|
||||
var totalReadSize = readSize;
|
||||
offset += readSize;
|
||||
builder.Append("{");
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
// write key
|
||||
{
|
||||
var keyType = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (keyType == MessagePackType.String || keyType == MessagePackType.Binary)
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("\"");
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
builder.Append("\"");
|
||||
}
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
}
|
||||
|
||||
builder.Append(":");
|
||||
|
||||
// write body
|
||||
{
|
||||
readSize = ToJsonCore(bytes, offset, builder);
|
||||
offset += readSize;
|
||||
totalReadSize += readSize;
|
||||
}
|
||||
|
||||
if (i != length - 1)
|
||||
{
|
||||
builder.Append(",");
|
||||
}
|
||||
}
|
||||
builder.Append("}");
|
||||
|
||||
return totalReadSize;
|
||||
}
|
||||
case MessagePackType.Extension:
|
||||
var extHeader = MessagePackBinary.ReadExtensionFormatHeader(bytes, offset, out readSize);
|
||||
if (extHeader.TypeCode == ReservedMessagePackExtensionTypeCode.DateTime)
|
||||
{
|
||||
var dt = MessagePackBinary.ReadDateTime(bytes, offset, out readSize);
|
||||
builder.Append("\"");
|
||||
builder.Append(dt.ToString("o", CultureInfo.InvariantCulture));
|
||||
builder.Append("\"");
|
||||
}
|
||||
#if NETSTANDARD
|
||||
else if (extHeader.TypeCode == TypelessFormatter.ExtensionTypeCode)
|
||||
{
|
||||
int startOffset = offset;
|
||||
// prepare type name token
|
||||
offset += 6;
|
||||
var typeNameToken = new StringBuilder();
|
||||
var typeNameReadSize = ToJsonCore(bytes, offset, typeNameToken);
|
||||
offset += typeNameReadSize;
|
||||
int startBuilderLength = builder.Length;
|
||||
if (extHeader.Length > typeNameReadSize)
|
||||
{
|
||||
// object map or array
|
||||
var typeInside = MessagePackBinary.GetMessagePackType(bytes, offset);
|
||||
if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
|
||||
builder.Append("{");
|
||||
offset += ToJsonCore(bytes, offset, builder);
|
||||
// insert type name token to start of object map or array
|
||||
if (typeInside != MessagePackType.Array)
|
||||
typeNameToken.Insert(0, "\"$type\":");
|
||||
if (typeInside != MessagePackType.Array && typeInside != MessagePackType.Map)
|
||||
builder.Append("}");
|
||||
if (builder.Length - startBuilderLength > 2)
|
||||
typeNameToken.Append(",");
|
||||
builder.Insert(startBuilderLength + 1, typeNameToken.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("{\"$type\":\"" + typeNameToken.ToString() + "}");
|
||||
}
|
||||
readSize = offset - startOffset;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
var ext = MessagePackBinary.ReadExtensionFormat(bytes, offset, out readSize);
|
||||
builder.Append("[");
|
||||
builder.Append(ext.TypeCode);
|
||||
builder.Append(",");
|
||||
builder.Append("\"");
|
||||
builder.Append(Convert.ToBase64String(ext.Data));
|
||||
builder.Append("\"");
|
||||
builder.Append("]");
|
||||
}
|
||||
break;
|
||||
case MessagePackType.Unknown:
|
||||
case MessagePackType.Nil:
|
||||
default:
|
||||
readSize = 1;
|
||||
builder.Append("null");
|
||||
break;
|
||||
}
|
||||
|
||||
return readSize;
|
||||
}
|
||||
|
||||
// escape string
|
||||
static void WriteJsonString(string value, StringBuilder builder)
|
||||
{
|
||||
builder.Append('\"');
|
||||
|
||||
var len = value.Length;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
var c = value[i];
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
builder.Append("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
builder.Append("\\\\");
|
||||
break;
|
||||
case '\b':
|
||||
builder.Append("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
builder.Append("\\f");
|
||||
break;
|
||||
case '\n':
|
||||
builder.Append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
builder.Append("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
builder.Append("\\t");
|
||||
break;
|
||||
default:
|
||||
builder.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
builder.Append('\"');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ccfe7519f2315b94b9cd47ab4c7742c7
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,277 @@
|
|||
#if NETSTANDARD
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
public static partial class LZ4MessagePackSerializer
|
||||
{
|
||||
public static class NonGeneric
|
||||
{
|
||||
static readonly Func<Type, CompiledMethods> CreateCompiledMethods;
|
||||
static readonly MessagePack.Internal.ThreadsafeTypeKeyHashTable<CompiledMethods> serializes = new MessagePack.Internal.ThreadsafeTypeKeyHashTable<CompiledMethods>(capacity: 64);
|
||||
|
||||
|
||||
static NonGeneric()
|
||||
{
|
||||
CreateCompiledMethods = t => new CompiledMethods(t);
|
||||
}
|
||||
|
||||
public static byte[] Serialize(Type type, object obj)
|
||||
{
|
||||
return GetOrAdd(type).serialize1.Invoke(obj);
|
||||
}
|
||||
|
||||
public static byte[] Serialize(Type type, object obj, IFormatterResolver resolver)
|
||||
{
|
||||
return GetOrAdd(type).serialize2.Invoke(obj, resolver);
|
||||
}
|
||||
|
||||
public static void Serialize(Type type, Stream stream, object obj)
|
||||
{
|
||||
GetOrAdd(type).serialize3.Invoke(stream, obj);
|
||||
}
|
||||
|
||||
public static void Serialize(Type type, Stream stream, object obj, IFormatterResolver resolver)
|
||||
{
|
||||
GetOrAdd(type).serialize4.Invoke(stream, obj, resolver);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, byte[] bytes)
|
||||
{
|
||||
return GetOrAdd(type).deserialize1.Invoke(bytes);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, byte[] bytes, IFormatterResolver resolver)
|
||||
{
|
||||
return GetOrAdd(type).deserialize2.Invoke(bytes, resolver);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, Stream stream)
|
||||
{
|
||||
return GetOrAdd(type).deserialize3.Invoke(stream);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, Stream stream, IFormatterResolver resolver)
|
||||
{
|
||||
return GetOrAdd(type).deserialize4.Invoke(stream, resolver);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, Stream stream, bool readStrict)
|
||||
{
|
||||
return GetOrAdd(type).deserialize5.Invoke(stream, readStrict);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
{
|
||||
return GetOrAdd(type).deserialize6.Invoke(stream, resolver, readStrict);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, ArraySegment<byte> bytes)
|
||||
{
|
||||
return GetOrAdd(type).deserialize7.Invoke(bytes);
|
||||
}
|
||||
|
||||
public static object Deserialize(Type type, ArraySegment<byte> bytes, IFormatterResolver resolver)
|
||||
{
|
||||
return GetOrAdd(type).deserialize8.Invoke(bytes, resolver);
|
||||
}
|
||||
|
||||
static CompiledMethods GetOrAdd(Type type)
|
||||
{
|
||||
return serializes.GetOrAdd(type, CreateCompiledMethods);
|
||||
}
|
||||
|
||||
class CompiledMethods
|
||||
{
|
||||
public readonly Func<object, byte[]> serialize1;
|
||||
public readonly Func<object, IFormatterResolver, byte[]> serialize2;
|
||||
public readonly Action<Stream, object> serialize3;
|
||||
public readonly Action<Stream, object, IFormatterResolver> serialize4;
|
||||
|
||||
public readonly Func<byte[], object> deserialize1;
|
||||
public readonly Func<byte[], IFormatterResolver, object> deserialize2;
|
||||
public readonly Func<Stream, object> deserialize3;
|
||||
public readonly Func<Stream, IFormatterResolver, object> deserialize4;
|
||||
public readonly Func<Stream, bool, object> deserialize5;
|
||||
public readonly Func<Stream, IFormatterResolver, bool, object> deserialize6;
|
||||
|
||||
public readonly Func<ArraySegment<byte>, object> deserialize7;
|
||||
public readonly Func<ArraySegment<byte>, IFormatterResolver, object> deserialize8;
|
||||
|
||||
public CompiledMethods(Type type)
|
||||
{
|
||||
var ti = type.GetTypeInfo();
|
||||
{
|
||||
// public static byte[] Serialize<T>(T obj)
|
||||
var serialize = GetMethod(type, new Type[] { null });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(object), "obj");
|
||||
var body = Expression.Call(serialize, ti.IsValueType
|
||||
? Expression.Unbox(param1, type)
|
||||
: Expression.Convert(param1, type));
|
||||
var lambda = Expression.Lambda<Func<object, byte[]>>(body, param1).Compile();
|
||||
|
||||
this.serialize1 = lambda;
|
||||
}
|
||||
{
|
||||
// public static byte[] Serialize<T>(T obj, IFormatterResolver resolver)
|
||||
var serialize = GetMethod(type, new Type[] { null, typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(object), "obj");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");
|
||||
|
||||
var body = Expression.Call(serialize, ti.IsValueType
|
||||
? Expression.Unbox(param1, type)
|
||||
: Expression.Convert(param1, type), param2);
|
||||
var lambda = Expression.Lambda<Func<object, IFormatterResolver, byte[]>>(body, param1, param2).Compile();
|
||||
|
||||
this.serialize2 = lambda;
|
||||
}
|
||||
{
|
||||
// public static void Serialize<T>(Stream stream, T obj)
|
||||
var serialize = GetMethod(type, new Type[] { typeof(Stream), null });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(object), "obj");
|
||||
|
||||
var body = Expression.Call(serialize, param1, ti.IsValueType
|
||||
? Expression.Unbox(param2, type)
|
||||
: Expression.Convert(param2, type));
|
||||
var lambda = Expression.Lambda<Action<Stream, object>>(body, param1, param2).Compile();
|
||||
|
||||
this.serialize3 = lambda;
|
||||
}
|
||||
{
|
||||
// public static void Serialize<T>(Stream stream, T obj, IFormatterResolver resolver)
|
||||
var serialize = GetMethod(type, new Type[] { typeof(Stream), null, typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(object), "obj");
|
||||
var param3 = Expression.Parameter(typeof(IFormatterResolver), "formatterResolver");
|
||||
|
||||
var body = Expression.Call(serialize, param1, ti.IsValueType
|
||||
? Expression.Unbox(param2, type)
|
||||
: Expression.Convert(param2, type), param3);
|
||||
var lambda = Expression.Lambda<Action<Stream, object, IFormatterResolver>>(body, param1, param2, param3).Compile();
|
||||
|
||||
this.serialize4 = lambda;
|
||||
}
|
||||
|
||||
{
|
||||
// public static T Deserialize<T>(byte[] bytes)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(byte[]) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<byte[], object>>(body, param1).Compile();
|
||||
|
||||
this.deserialize1 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(byte[] bytes, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(byte[]), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(byte[]), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<byte[], IFormatterResolver, object>>(body, param1, param2).Compile();
|
||||
|
||||
this.deserialize2 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<Stream, object>>(body, param1).Compile();
|
||||
|
||||
this.deserialize3 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<Stream, IFormatterResolver, object>>(body, param1, param2).Compile();
|
||||
|
||||
this.deserialize4 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream, bool readStrict)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream), typeof(bool) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(bool), "readStrict");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<Stream, bool, object>>(body, param1, param2).Compile();
|
||||
|
||||
this.deserialize5 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(Stream), typeof(IFormatterResolver), typeof(bool) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(Stream), "stream");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var param3 = Expression.Parameter(typeof(bool), "readStrict");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1, param2, param3), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<Stream, IFormatterResolver, bool, object>>(body, param1, param2, param3).Compile();
|
||||
|
||||
this.deserialize6 = lambda;
|
||||
}
|
||||
|
||||
{
|
||||
// public static T Deserialize<T>(ArraySegment<byte> bytes)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(ArraySegment<byte>) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(ArraySegment<byte>), "bytes");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<ArraySegment<byte>, object>>(body, param1).Compile();
|
||||
|
||||
this.deserialize7 = lambda;
|
||||
}
|
||||
{
|
||||
// public static T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver)
|
||||
var deserialize = GetMethod(type, new Type[] { typeof(ArraySegment<byte>), typeof(IFormatterResolver) });
|
||||
|
||||
var param1 = Expression.Parameter(typeof(ArraySegment<byte>), "bytes");
|
||||
var param2 = Expression.Parameter(typeof(IFormatterResolver), "resolver");
|
||||
var body = Expression.Convert(Expression.Call(deserialize, param1, param2), typeof(object));
|
||||
var lambda = Expression.Lambda<Func<ArraySegment<byte>, IFormatterResolver, object>>(body, param1, param2).Compile();
|
||||
|
||||
this.deserialize8 = lambda;
|
||||
}
|
||||
}
|
||||
|
||||
// null is generic type marker.
|
||||
static MethodInfo GetMethod(Type type, Type[] parameters)
|
||||
{
|
||||
return typeof(LZ4MessagePackSerializer).GetRuntimeMethods().Where(x =>
|
||||
{
|
||||
if (!(x.Name == "Serialize" || x.Name == "Deserialize")) return false;
|
||||
var ps = x.GetParameters();
|
||||
if (ps.Length != parameters.Length) return false;
|
||||
for (int i = 0; i < ps.Length; i++)
|
||||
{
|
||||
if (parameters[i] == null && ps[i].ParameterType.IsGenericParameter) continue;
|
||||
if (ps[i].ParameterType != parameters[i]) return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.Single()
|
||||
.MakeGenericMethod(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c2e691a6783e1584c96d2705a6533e54
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
361
Assets/Scripts/MessagePack/LZ4/LZ4MessagePackSerializer.cs
Normal file
361
Assets/Scripts/MessagePack/LZ4/LZ4MessagePackSerializer.cs
Normal file
|
|
@ -0,0 +1,361 @@
|
|||
using MessagePack.Internal;
|
||||
using System;
|
||||
using System.IO;
|
||||
using MessagePack.LZ4;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
/// <summary>
|
||||
/// LZ4 Compressed special serializer.
|
||||
/// </summary>
|
||||
public static partial class LZ4MessagePackSerializer
|
||||
{
|
||||
public const sbyte ExtensionTypeCode = 99;
|
||||
|
||||
public const int NotCompressionSize = 64;
|
||||
|
||||
/// <summary>
|
||||
/// Serialize to binary with default resolver.
|
||||
/// </summary>
|
||||
public static byte[] Serialize<T>(T obj)
|
||||
{
|
||||
return Serialize(obj, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize to binary with specified resolver.
|
||||
/// </summary>
|
||||
public static byte[] Serialize<T>(T obj, IFormatterResolver resolver)
|
||||
{
|
||||
if (resolver == null) resolver = MessagePackSerializer.DefaultResolver;
|
||||
var buffer = SerializeCore(obj, resolver);
|
||||
|
||||
return MessagePackBinary.FastCloneWithResize(buffer.Array, buffer.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize to stream.
|
||||
/// </summary>
|
||||
public static void Serialize<T>(Stream stream, T obj)
|
||||
{
|
||||
Serialize(stream, obj, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize to stream with specified resolver.
|
||||
/// </summary>
|
||||
public static void Serialize<T>(Stream stream, T obj, IFormatterResolver resolver)
|
||||
{
|
||||
if (resolver == null) resolver = MessagePackSerializer.DefaultResolver;
|
||||
var buffer = SerializeCore(obj, resolver);
|
||||
|
||||
stream.Write(buffer.Array, 0, buffer.Count);
|
||||
}
|
||||
|
||||
public static int SerializeToBlock<T>(ref byte[] bytes, int offset, T obj, IFormatterResolver resolver)
|
||||
{
|
||||
var serializedData = MessagePackSerializer.SerializeUnsafe(obj, resolver);
|
||||
|
||||
if (serializedData.Count < NotCompressionSize)
|
||||
{
|
||||
// can't write direct, shoganai...
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, serializedData.Count);
|
||||
Buffer.BlockCopy(serializedData.Array, serializedData.Offset, bytes, offset, serializedData.Count);
|
||||
return serializedData.Count;
|
||||
}
|
||||
else
|
||||
{
|
||||
var maxOutCount = LZ4Codec.MaximumOutputLength(serializedData.Count);
|
||||
|
||||
MessagePackBinary.EnsureCapacity(ref bytes, offset, 6 + 5 + maxOutCount); // (ext header size + fixed length size)
|
||||
|
||||
// acquire ext header position
|
||||
var extHeaderOffset = offset;
|
||||
offset += (6 + 5);
|
||||
|
||||
// write body
|
||||
var lz4Length = LZ4Codec.Encode(serializedData.Array, serializedData.Offset, serializedData.Count, bytes, offset, bytes.Length - offset);
|
||||
|
||||
// write extension header(always 6 bytes)
|
||||
extHeaderOffset += MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref bytes, extHeaderOffset, (sbyte)ExtensionTypeCode, lz4Length + 5);
|
||||
|
||||
// write length(always 5 bytes)
|
||||
MessagePackBinary.WriteInt32ForceInt32Block(ref bytes, extHeaderOffset, serializedData.Count);
|
||||
|
||||
return 6 + 5 + lz4Length;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] ToLZ4Binary(ArraySegment<byte> messagePackBinary)
|
||||
{
|
||||
var buffer = ToLZ4BinaryCore(messagePackBinary);
|
||||
return MessagePackBinary.FastCloneWithResize(buffer.Array, buffer.Count);
|
||||
}
|
||||
|
||||
static ArraySegment<byte> SerializeCore<T>(T obj, IFormatterResolver resolver)
|
||||
{
|
||||
var serializedData = MessagePackSerializer.SerializeUnsafe(obj, resolver);
|
||||
return ToLZ4BinaryCore(serializedData);
|
||||
}
|
||||
|
||||
static ArraySegment<byte> ToLZ4BinaryCore(ArraySegment<byte> serializedData)
|
||||
{
|
||||
if (serializedData.Count < NotCompressionSize)
|
||||
{
|
||||
return serializedData;
|
||||
}
|
||||
else
|
||||
{
|
||||
var offset = 0;
|
||||
var buffer = LZ4MemoryPool.GetBuffer();
|
||||
var maxOutCount = LZ4Codec.MaximumOutputLength(serializedData.Count);
|
||||
if (buffer.Length + 6 + 5 < maxOutCount) // (ext header size + fixed length size)
|
||||
{
|
||||
buffer = new byte[6 + 5 + maxOutCount];
|
||||
}
|
||||
|
||||
// acquire ext header position
|
||||
var extHeaderOffset = offset;
|
||||
offset += (6 + 5);
|
||||
|
||||
// write body
|
||||
var lz4Length = LZ4Codec.Encode(serializedData.Array, serializedData.Offset, serializedData.Count, buffer, offset, buffer.Length - offset);
|
||||
|
||||
// write extension header(always 6 bytes)
|
||||
extHeaderOffset += MessagePackBinary.WriteExtensionFormatHeaderForceExt32Block(ref buffer, extHeaderOffset, (sbyte)ExtensionTypeCode, lz4Length + 5);
|
||||
|
||||
// write length(always 5 bytes)
|
||||
MessagePackBinary.WriteInt32ForceInt32Block(ref buffer, extHeaderOffset, serializedData.Count);
|
||||
|
||||
return new ArraySegment<byte>(buffer, 0, 6 + 5 + lz4Length);
|
||||
}
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(byte[] bytes)
|
||||
{
|
||||
return Deserialize<T>(bytes, null);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(byte[] bytes, IFormatterResolver resolver)
|
||||
{
|
||||
return DeserializeCore<T>(new ArraySegment<byte>(bytes, 0, bytes.Length), resolver);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(ArraySegment<byte> bytes)
|
||||
{
|
||||
return DeserializeCore<T>(bytes, null);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(ArraySegment<byte> bytes, IFormatterResolver resolver)
|
||||
{
|
||||
return DeserializeCore<T>(bytes, resolver);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(Stream stream)
|
||||
{
|
||||
return Deserialize<T>(stream, null);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(Stream stream, IFormatterResolver resolver)
|
||||
{
|
||||
return Deserialize<T>(stream, resolver, false);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(Stream stream, bool readStrict)
|
||||
{
|
||||
return Deserialize<T>(stream, MessagePackSerializer.DefaultResolver, readStrict);
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(Stream stream, IFormatterResolver resolver, bool readStrict)
|
||||
{
|
||||
if (!readStrict)
|
||||
{
|
||||
var buffer = MessagePack.Internal.InternalMemoryPool.GetBuffer(); // use MessagePackSerializer.Pool!
|
||||
var len = FillFromStream(stream, ref buffer);
|
||||
return DeserializeCore<T>(new ArraySegment<byte>(buffer, 0, len), resolver);
|
||||
}
|
||||
else
|
||||
{
|
||||
int blockSize;
|
||||
var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out blockSize);
|
||||
return DeserializeCore<T>(new ArraySegment<byte>(bytes, 0, blockSize), resolver);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Decode(Stream stream, bool readStrict = false)
|
||||
{
|
||||
if (!readStrict)
|
||||
{
|
||||
var buffer = MessagePack.Internal.InternalMemoryPool.GetBuffer(); // use MessagePackSerializer.Pool!
|
||||
var len = FillFromStream(stream, ref buffer);
|
||||
return Decode(new ArraySegment<byte>(buffer, 0, len));
|
||||
}
|
||||
else
|
||||
{
|
||||
int blockSize;
|
||||
var bytes = MessagePackBinary.ReadMessageBlockFromStreamUnsafe(stream, false, out blockSize);
|
||||
return Decode(new ArraySegment<byte>(bytes, 0, blockSize));
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Decode(byte[] bytes)
|
||||
{
|
||||
return Decode(new ArraySegment<byte>(bytes, 0, bytes.Length));
|
||||
}
|
||||
|
||||
public static byte[] Decode(ArraySegment<byte> bytes)
|
||||
{
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes.Array, bytes.Offset) == MessagePackType.Extension)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes.Array, bytes.Offset, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
{
|
||||
// decode lz4
|
||||
var offset = bytes.Offset + readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes.Array, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = new byte[length]; // use new buffer.
|
||||
|
||||
// LZ4 Decode
|
||||
var len = bytes.Count + bytes.Offset - offset;
|
||||
LZ4Codec.Decode(bytes.Array, offset, len, buffer, 0, length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes.Offset == 0 && bytes.Array.Length == bytes.Count)
|
||||
{
|
||||
// return same reference
|
||||
return bytes.Array;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = new byte[bytes.Count];
|
||||
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get the war memory pool byte[]. The result can not share across thread and can not hold and can not call LZ4Deserialize before use it.
|
||||
/// </summary>
|
||||
public static byte[] DecodeUnsafe(byte[] bytes)
|
||||
{
|
||||
return DecodeUnsafe(new ArraySegment<byte>(bytes, 0, bytes.Length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the war memory pool byte[]. The result can not share across thread and can not hold and can not call LZ4Deserialize before use it.
|
||||
/// </summary>
|
||||
public static byte[] DecodeUnsafe(ArraySegment<byte> bytes)
|
||||
{
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes.Array, bytes.Offset) == MessagePackType.Extension)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes.Array, bytes.Offset, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
{
|
||||
// decode lz4
|
||||
var offset = bytes.Offset + readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes.Array, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = LZ4MemoryPool.GetBuffer(); // use LZ4 Pool(Unsafe)
|
||||
if (buffer.Length < length)
|
||||
{
|
||||
buffer = new byte[length];
|
||||
}
|
||||
|
||||
// LZ4 Decode
|
||||
var len = bytes.Count + bytes.Offset - offset;
|
||||
LZ4Codec.Decode(bytes.Array, offset, len, buffer, 0, length);
|
||||
|
||||
return buffer; // return pooled bytes.
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes.Offset == 0 && bytes.Array.Length == bytes.Count)
|
||||
{
|
||||
// return same reference
|
||||
return bytes.Array;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = new byte[bytes.Count];
|
||||
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
static T DeserializeCore<T>(ArraySegment<byte> bytes, IFormatterResolver resolver)
|
||||
{
|
||||
if (resolver == null) resolver = MessagePackSerializer.DefaultResolver;
|
||||
var formatter = resolver.GetFormatterWithVerify<T>();
|
||||
|
||||
int readSize;
|
||||
if (MessagePackBinary.GetMessagePackType(bytes.Array, bytes.Offset) == MessagePackType.Extension)
|
||||
{
|
||||
var header = MessagePackBinary.ReadExtensionFormatHeader(bytes.Array, bytes.Offset, out readSize);
|
||||
if (header.TypeCode == ExtensionTypeCode)
|
||||
{
|
||||
// decode lz4
|
||||
var offset = bytes.Offset + readSize;
|
||||
var length = MessagePackBinary.ReadInt32(bytes.Array, offset, out readSize);
|
||||
offset += readSize;
|
||||
|
||||
var buffer = LZ4MemoryPool.GetBuffer(); // use LZ4 Pool
|
||||
if (buffer.Length < length)
|
||||
{
|
||||
buffer = new byte[length];
|
||||
}
|
||||
|
||||
// LZ4 Decode
|
||||
var len = bytes.Count + bytes.Offset - offset;
|
||||
LZ4Codec.Decode(bytes.Array, offset, len, buffer, 0, length);
|
||||
|
||||
return formatter.Deserialize(buffer, 0, resolver, out readSize);
|
||||
}
|
||||
}
|
||||
|
||||
return formatter.Deserialize(bytes.Array, bytes.Offset, resolver, out readSize);
|
||||
}
|
||||
|
||||
static int FillFromStream(Stream input, ref byte[] buffer)
|
||||
{
|
||||
int length = 0;
|
||||
int read;
|
||||
while ((read = input.Read(buffer, length, buffer.Length - length)) > 0)
|
||||
{
|
||||
length += read;
|
||||
if (length == buffer.Length)
|
||||
{
|
||||
MessagePackBinary.FastResize(ref buffer, length * 2);
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace MessagePack.Internal
|
||||
{
|
||||
internal static class LZ4MemoryPool
|
||||
{
|
||||
[ThreadStatic]
|
||||
static byte[] lz4buffer = null;
|
||||
|
||||
public static byte[] GetBuffer()
|
||||
{
|
||||
if (lz4buffer == null)
|
||||
{
|
||||
lz4buffer = new byte[LZ4.LZ4Codec.MaximumOutputLength(65536)];
|
||||
}
|
||||
return lz4buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: cfb55b01b61fbfa4b9d769ae82715bcb
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
6004
Assets/Scripts/MessagePack/MessagePackBinary.cs
Normal file
6004
Assets/Scripts/MessagePack/MessagePackBinary.cs
Normal file
File diff suppressed because it is too large
Load diff
12
Assets/Scripts/MessagePack/MessagePackBinary.cs.meta
Normal file
12
Assets/Scripts/MessagePack/MessagePackBinary.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0dd783c6adb355a49aa376da38578ec5
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
199
Assets/Scripts/MessagePack/MessagePackCode.cs
Normal file
199
Assets/Scripts/MessagePack/MessagePackCode.cs
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MessagePack
|
||||
{
|
||||
/// <summary>
|
||||
/// https://github.com/msgpack/msgpack/blob/master/spec.md#serialization-type-to-format-conversion
|
||||
/// </summary>
|
||||
public enum MessagePackType : byte
|
||||
{
|
||||
Unknown = 0,
|
||||
|
||||
Integer = 1,
|
||||
Nil = 2,
|
||||
Boolean = 3,
|
||||
Float = 4,
|
||||
String = 5,
|
||||
Binary = 6,
|
||||
Array = 7,
|
||||
Map = 8,
|
||||
Extension = 9,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://github.com/msgpack/msgpack/blob/master/spec.md#overview
|
||||
/// </summary>
|
||||
public static class MessagePackCode
|
||||
{
|
||||
public const byte MinFixInt = 0x00; // 0
|
||||
public const byte MaxFixInt = 0x7f; // 127
|
||||
public const byte MinFixMap = 0x80; // 128
|
||||
public const byte MaxFixMap = 0x8f; // 143
|
||||
public const byte MinFixArray = 0x90; // 144
|
||||
public const byte MaxFixArray = 0x9f; // 159
|
||||
public const byte MinFixStr = 0xa0; // 160
|
||||
public const byte MaxFixStr = 0xbf; // 191
|
||||
public const byte Nil = 0xc0;
|
||||
public const byte NeverUsed = 0xc1;
|
||||
public const byte False = 0xc2;
|
||||
public const byte True = 0xc3;
|
||||
public const byte Bin8 = 0xc4;
|
||||
public const byte Bin16 = 0xc5;
|
||||
public const byte Bin32 = 0xc6;
|
||||
public const byte Ext8 = 0xc7;
|
||||
public const byte Ext16 = 0xc8;
|
||||
public const byte Ext32 = 0xc9;
|
||||
public const byte Float32 = 0xca;
|
||||
public const byte Float64 = 0xcb;
|
||||
public const byte UInt8 = 0xcc;
|
||||
public const byte UInt16 = 0xcd;
|
||||
public const byte UInt32 = 0xce;
|
||||
public const byte UInt64 = 0xcf;
|
||||
public const byte Int8 = 0xd0;
|
||||
public const byte Int16 = 0xd1;
|
||||
public const byte Int32 = 0xd2;
|
||||
public const byte Int64 = 0xd3;
|
||||
public const byte FixExt1 = 0xd4;
|
||||
public const byte FixExt2 = 0xd5;
|
||||
public const byte FixExt4 = 0xd6;
|
||||
public const byte FixExt8 = 0xd7;
|
||||
public const byte FixExt16 = 0xd8;
|
||||
public const byte Str8 = 0xd9;
|
||||
public const byte Str16 = 0xda;
|
||||
public const byte Str32 = 0xdb;
|
||||
public const byte Array16 = 0xdc;
|
||||
public const byte Array32 = 0xdd;
|
||||
public const byte Map16 = 0xde;
|
||||
public const byte Map32 = 0xdf;
|
||||
public const byte MinNegativeFixInt = 0xe0; // 224
|
||||
public const byte MaxNegativeFixInt = 0xff; // 255
|
||||
|
||||
static readonly MessagePackType[] typeLookupTable = new MessagePackType[256];
|
||||
static readonly string[] formatNameTable = new string[256];
|
||||
|
||||
static MessagePackCode()
|
||||
{
|
||||
// Init Lookup Table
|
||||
for (int i = MinFixInt; i <= MaxFixInt; i++)
|
||||
{
|
||||
typeLookupTable[i] = MessagePackType.Integer;
|
||||
formatNameTable[i] = "positive fixint";
|
||||
}
|
||||
for (int i = MinFixMap; i <= MaxFixMap; i++)
|
||||
{
|
||||
typeLookupTable[i] = MessagePackType.Map;
|
||||
formatNameTable[i] = "fixmap";
|
||||
}
|
||||
for (int i = MinFixArray; i <= MaxFixArray; i++)
|
||||
{
|
||||
typeLookupTable[i] = MessagePackType.Array;
|
||||
formatNameTable[i] = "fixarray";
|
||||
}
|
||||
for (int i = MinFixStr; i <= MaxFixStr; i++)
|
||||
{
|
||||
typeLookupTable[i] = MessagePackType.String;
|
||||
formatNameTable[i] = "fixstr";
|
||||
}
|
||||
|
||||
typeLookupTable[Nil] = MessagePackType.Nil;
|
||||
typeLookupTable[NeverUsed] = MessagePackType.Unknown;
|
||||
typeLookupTable[False] = MessagePackType.Boolean;
|
||||
typeLookupTable[True] = MessagePackType.Boolean;
|
||||
typeLookupTable[Bin8] = MessagePackType.Binary;
|
||||
typeLookupTable[Bin16] = MessagePackType.Binary;
|
||||
typeLookupTable[Bin32] = MessagePackType.Binary;
|
||||
typeLookupTable[Ext8] = MessagePackType.Extension;
|
||||
typeLookupTable[Ext16] = MessagePackType.Extension;
|
||||
typeLookupTable[Ext32] = MessagePackType.Extension;
|
||||
typeLookupTable[Float32] = MessagePackType.Float;
|
||||
typeLookupTable[Float64] = MessagePackType.Float;
|
||||
typeLookupTable[UInt8] = MessagePackType.Integer;
|
||||
typeLookupTable[UInt16] = MessagePackType.Integer;
|
||||
typeLookupTable[UInt32] = MessagePackType.Integer;
|
||||
typeLookupTable[UInt64] = MessagePackType.Integer;
|
||||
typeLookupTable[Int8] = MessagePackType.Integer;
|
||||
typeLookupTable[Int16] = MessagePackType.Integer;
|
||||
typeLookupTable[Int32] = MessagePackType.Integer;
|
||||
typeLookupTable[Int64] = MessagePackType.Integer;
|
||||
typeLookupTable[FixExt1] = MessagePackType.Extension;
|
||||
typeLookupTable[FixExt2] = MessagePackType.Extension;
|
||||
typeLookupTable[FixExt4] = MessagePackType.Extension;
|
||||
typeLookupTable[FixExt8] = MessagePackType.Extension;
|
||||
typeLookupTable[FixExt16] = MessagePackType.Extension;
|
||||
typeLookupTable[Str8] = MessagePackType.String;
|
||||
typeLookupTable[Str16] = MessagePackType.String;
|
||||
typeLookupTable[Str32] = MessagePackType.String;
|
||||
typeLookupTable[Array16] = MessagePackType.Array;
|
||||
typeLookupTable[Array32] = MessagePackType.Array;
|
||||
typeLookupTable[Map16] = MessagePackType.Map;
|
||||
typeLookupTable[Map32] = MessagePackType.Map;
|
||||
|
||||
formatNameTable[Nil] = "nil";
|
||||
formatNameTable[NeverUsed] = "(never used)";
|
||||
formatNameTable[False] = "false";
|
||||
formatNameTable[True] = "true";
|
||||
formatNameTable[Bin8] = "bin 8";
|
||||
formatNameTable[Bin16] = "bin 16";
|
||||
formatNameTable[Bin32] = "bin 32";
|
||||
formatNameTable[Ext8] = "ext 8";
|
||||
formatNameTable[Ext16] = "ext 16";
|
||||
formatNameTable[Ext32] = "ext 32";
|
||||
formatNameTable[Float32] = "float 32";
|
||||
formatNameTable[Float64] = "float 64";
|
||||
formatNameTable[UInt8] = "uint 8";
|
||||
formatNameTable[UInt16] = "uint 16";
|
||||
formatNameTable[UInt32] = "uint 32";
|
||||
formatNameTable[UInt64] = "uint 64";
|
||||
formatNameTable[Int8] = "int 8";
|
||||
formatNameTable[Int16] = "int 16";
|
||||
formatNameTable[Int32] = "int 32";
|
||||
formatNameTable[Int64] = "int 64";
|
||||
formatNameTable[FixExt1] = "fixext 1";
|
||||
formatNameTable[FixExt2] = "fixext 2";
|
||||
formatNameTable[FixExt4] = "fixext 4";
|
||||
formatNameTable[FixExt8] = "fixext 8";
|
||||
formatNameTable[FixExt16] = "fixext 16";
|
||||
formatNameTable[Str8] = "str 8";
|
||||
formatNameTable[Str16] = "str 16";
|
||||
formatNameTable[Str32] = "str 32";
|
||||
formatNameTable[Array16] = "array 16";
|
||||
formatNameTable[Array32] = "array 32";
|
||||
formatNameTable[Map16] = "map 16";
|
||||
formatNameTable[Map32] = "map 32";
|
||||
|
||||
for (int i = MinNegativeFixInt; i <= MaxNegativeFixInt; i++)
|
||||
{
|
||||
typeLookupTable[i] = MessagePackType.Integer;
|
||||
formatNameTable[i] = "negative fixint";
|
||||
}
|
||||
}
|
||||
|
||||
public static MessagePackType ToMessagePackType(byte code)
|
||||
{
|
||||
return typeLookupTable[code];
|
||||
}
|
||||
|
||||
public static string ToFormatName(byte code)
|
||||
{
|
||||
return formatNameTable[code];
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReservedMessagePackExtensionTypeCode
|
||||
{
|
||||
public const sbyte DateTime = -1;
|
||||
}
|
||||
|
||||
public static class MessagePackRange
|
||||
{
|
||||
public const int MinFixNegativeInt = -32;
|
||||
public const int MaxFixNegativeInt = -1;
|
||||
public const int MaxFixPositiveInt = 127;
|
||||
public const int MinFixStringLength = 0;
|
||||
public const int MaxFixStringLength = 31;
|
||||
public const int MaxFixMapCount = 15;
|
||||
public const int MaxFixArrayCount = 15;
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/MessagePack/MessagePackCode.cs.meta
Normal file
12
Assets/Scripts/MessagePack/MessagePackCode.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 11570467e3a856b459a0411607b655d2
|
||||
timeCreated: 1489131596
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue