Skip to content

Instantly share code, notes, and snippets.

@st0326s
Last active June 16, 2017 09:03
Show Gist options
  • Save st0326s/058b450321d4f719a083260026631bf4 to your computer and use it in GitHub Desktop.
Save st0326s/058b450321d4f719a083260026631bf4 to your computer and use it in GitHub Desktop.
MessagePack-CSharp Array型 メモ ref: http://qiita.com/satotin/items/8456e3f9c78be44ac961
using System.Collections.Generic;
using System.Text;
using MessagePack;
[MessagePackObject]
public class SubClass
{
[Key(0)]
public int acb;
[Key(1)]
public StringBuilder def;
}
[MessagePackObject]
public class TestClass
{
[Key(0)]
public int aaa;
[Key(1)]
public long bbb;
[Key(2)]
public float ccc;
[Key(3)]
public double ddd;
[Key(4)]
public StringBuilder eee = new StringBuilder();
[Key(5)]
public int[] fff = new int[] { 1, 3, 5, 7, 9 };
[Key(6)]
public StringBuilder[] ggg = new StringBuilder[5];
[Key(7)]
public List<int> hhh = new List<int>();
[Key(8)]
public List<StringBuilder> iii = new List<StringBuilder>();
[Key(9)]
public Dictionary<int, StringBuilder> jjj = new Dictionary<int, StringBuilder>();
[Key(10)]
public Dictionary<StringBuilder, StringBuilder> kkk = new Dictionary<StringBuilder, StringBuilder>();
[Key(11)]
public List<SubClass> lll = new List<SubClass>();
[Key(12)]
public Dictionary<StringBuilder, SubClass> mmm = new Dictionary<StringBuilder, SubClass>();
[Key(13)]
public List<List<SubClass>> nnn = new List<List<SubClass>>();
[Key(14)]
public Dictionary<StringBuilder, List<SubClass>> ooo = new Dictionary<StringBuilder, List<SubClass>>();
}
.\mpc.exe -i 指定プロジェクトのプロジェクトファイルのパス(*.csproj) -o 指定プロジェクトの任意の名前のクラス名(フルパス)※仮にMessagePackGenerated.csとする。
例:)
.\mpc.exe -i 自分PCドキュメントフォルダ\MessagePack_CSharp_Sample_Various_Type\MessagePack_CSharp_Sample_Various_Type.csproj -o 自分PCドキュメントフォルダ\MessagePack_CSharp_Sample_Various_\Assets\Scripts\GenerateArray.cs
#pragma warning disable 618
#pragma warning disable 612
#pragma warning disable 414
#pragma warning disable 168
namespace MessagePack.Resolvers
{
using System;
using MessagePack;
public class GeneratedResolver : global::MessagePack.IFormatterResolver
{
public static readonly global::MessagePack.IFormatterResolver Instance = new GeneratedResolver();
GeneratedResolver()
{
}
public global::MessagePack.Formatters.IMessagePackFormatter<T> GetFormatter<T>()
{
return FormatterCache<T>.formatter;
}
static class FormatterCache<T>
{
public static readonly global::MessagePack.Formatters.IMessagePackFormatter<T> formatter;
static FormatterCache()
{
var f = GeneratedResolverGetFormatterHelper.GetFormatter(typeof(T));
if (f != null)
{
formatter = (global::MessagePack.Formatters.IMessagePackFormatter<T>)f;
}
}
}
}
internal static class GeneratedResolverGetFormatterHelper
{
static readonly global::System.Collections.Generic.Dictionary<Type, int> lookup;
static GeneratedResolverGetFormatterHelper()
{
lookup = new global::System.Collections.Generic.Dictionary<Type, int>(11)
{
{typeof(global::System.Text.StringBuilder[]), 0 },
{typeof(global::System.Collections.Generic.List<int>), 1 },
{typeof(global::System.Collections.Generic.List<global::System.Text.StringBuilder>), 2 },
{typeof(global::System.Collections.Generic.Dictionary<int, global::System.Text.StringBuilder>), 3 },
{typeof(global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Text.StringBuilder>), 4 },
{typeof(global::System.Collections.Generic.List<global::SubClass>), 5 },
{typeof(global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::SubClass>), 6 },
{typeof(global::System.Collections.Generic.List<global::System.Collections.Generic.List<global::SubClass>>), 7 },
{typeof(global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Collections.Generic.List<global::SubClass>>), 8 },
{typeof(global::SubClass), 9 },
{typeof(global::TestClass), 10 },
};
}
internal static object GetFormatter(Type t)
{
int key;
if (!lookup.TryGetValue(t, out key)) return null;
switch (key)
{
case 0: return new global::MessagePack.Formatters.ArrayFormatter<global::System.Text.StringBuilder>();
case 1: return new global::MessagePack.Formatters.ListFormatter<int>();
case 2: return new global::MessagePack.Formatters.ListFormatter<global::System.Text.StringBuilder>();
case 3: return new global::MessagePack.Formatters.DictionaryFormatter<int, global::System.Text.StringBuilder>();
case 4: return new global::MessagePack.Formatters.DictionaryFormatter<global::System.Text.StringBuilder, global::System.Text.StringBuilder>();
case 5: return new global::MessagePack.Formatters.ListFormatter<global::SubClass>();
case 6: return new global::MessagePack.Formatters.DictionaryFormatter<global::System.Text.StringBuilder, global::SubClass>();
case 7: return new global::MessagePack.Formatters.ListFormatter<global::System.Collections.Generic.List<global::SubClass>>();
case 8: return new global::MessagePack.Formatters.DictionaryFormatter<global::System.Text.StringBuilder, global::System.Collections.Generic.List<global::SubClass>>();
case 9: return new MessagePack.Formatters.SubClassFormatter();
case 10: return new MessagePack.Formatters.TestClassFormatter();
default: return null;
}
}
}
}
#pragma warning disable 168
#pragma warning restore 414
#pragma warning restore 618
#pragma warning restore 612
#pragma warning disable 618
#pragma warning disable 612
#pragma warning disable 414
#pragma warning disable 168
namespace MessagePack.Formatters
{
using System;
using MessagePack;
public sealed class SubClassFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::SubClass>
{
public int Serialize(ref byte[] bytes, int offset, global::SubClass value, global::MessagePack.IFormatterResolver formatterResolver)
{
if (value == null)
{
return global::MessagePack.MessagePackBinary.WriteNil(ref bytes, offset);
}
var startOffset = offset;
offset += global::MessagePack.MessagePackBinary.WriteFixedArrayHeaderUnsafe(ref bytes, offset, 2);
offset += MessagePackBinary.WriteInt32(ref bytes, offset, value.acb);
offset += formatterResolver.GetFormatterWithVerify<global::System.Text.StringBuilder>().Serialize(ref bytes, offset, value.def, formatterResolver);
return offset - startOffset;
}
public global::SubClass Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
{
if (global::MessagePack.MessagePackBinary.IsNil(bytes, offset))
{
readSize = 1;
return null;
}
var startOffset = offset;
var length = global::MessagePack.MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
offset += readSize;
var __acb__ = default(int);
var __def__ = default(global::System.Text.StringBuilder);
for (int i = 0; i < length; i++)
{
var key = i;
switch (key)
{
case 0:
__acb__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
break;
case 1:
__def__ = formatterResolver.GetFormatterWithVerify<global::System.Text.StringBuilder>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
default:
readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
break;
}
offset += readSize;
}
readSize = offset - startOffset;
var ____result = new global::SubClass();
____result.acb = __acb__;
____result.def = __def__;
return ____result;
}
}
public sealed class TestClassFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::TestClass>
{
public int Serialize(ref byte[] bytes, int offset, global::TestClass value, global::MessagePack.IFormatterResolver formatterResolver)
{
if (value == null)
{
return global::MessagePack.MessagePackBinary.WriteNil(ref bytes, offset);
}
var startOffset = offset;
offset += global::MessagePack.MessagePackBinary.WriteFixedArrayHeaderUnsafe(ref bytes, offset, 15);
offset += MessagePackBinary.WriteInt32(ref bytes, offset, value.aaa);
offset += MessagePackBinary.WriteInt64(ref bytes, offset, value.bbb);
offset += MessagePackBinary.WriteSingle(ref bytes, offset, value.ccc);
offset += MessagePackBinary.WriteDouble(ref bytes, offset, value.ddd);
offset += formatterResolver.GetFormatterWithVerify<global::System.Text.StringBuilder>().Serialize(ref bytes, offset, value.eee, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<int[]>().Serialize(ref bytes, offset, value.fff, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Text.StringBuilder[]>().Serialize(ref bytes, offset, value.ggg, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<int>>().Serialize(ref bytes, offset, value.hhh, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::System.Text.StringBuilder>>().Serialize(ref bytes, offset, value.iii, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<int, global::System.Text.StringBuilder>>().Serialize(ref bytes, offset, value.jjj, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Text.StringBuilder>>().Serialize(ref bytes, offset, value.kkk, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::SubClass>>().Serialize(ref bytes, offset, value.lll, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::SubClass>>().Serialize(ref bytes, offset, value.mmm, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::System.Collections.Generic.List<global::SubClass>>>().Serialize(ref bytes, offset, value.nnn, formatterResolver);
offset += formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Collections.Generic.List<global::SubClass>>>().Serialize(ref bytes, offset, value.ooo, formatterResolver);
return offset - startOffset;
}
public global::TestClass Deserialize(byte[] bytes, int offset, global::MessagePack.IFormatterResolver formatterResolver, out int readSize)
{
if (global::MessagePack.MessagePackBinary.IsNil(bytes, offset))
{
readSize = 1;
return null;
}
var startOffset = offset;
var length = global::MessagePack.MessagePackBinary.ReadArrayHeader(bytes, offset, out readSize);
offset += readSize;
var __aaa__ = default(int);
var __bbb__ = default(long);
var __ccc__ = default(float);
var __ddd__ = default(double);
var __eee__ = default(global::System.Text.StringBuilder);
var __fff__ = default(int[]);
var __ggg__ = default(global::System.Text.StringBuilder[]);
var __hhh__ = default(global::System.Collections.Generic.List<int>);
var __iii__ = default(global::System.Collections.Generic.List<global::System.Text.StringBuilder>);
var __jjj__ = default(global::System.Collections.Generic.Dictionary<int, global::System.Text.StringBuilder>);
var __kkk__ = default(global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Text.StringBuilder>);
var __lll__ = default(global::System.Collections.Generic.List<global::SubClass>);
var __mmm__ = default(global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::SubClass>);
var __nnn__ = default(global::System.Collections.Generic.List<global::System.Collections.Generic.List<global::SubClass>>);
var __ooo__ = default(global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Collections.Generic.List<global::SubClass>>);
for (int i = 0; i < length; i++)
{
var key = i;
switch (key)
{
case 0:
__aaa__ = MessagePackBinary.ReadInt32(bytes, offset, out readSize);
break;
case 1:
__bbb__ = MessagePackBinary.ReadInt64(bytes, offset, out readSize);
break;
case 2:
__ccc__ = MessagePackBinary.ReadSingle(bytes, offset, out readSize);
break;
case 3:
__ddd__ = MessagePackBinary.ReadDouble(bytes, offset, out readSize);
break;
case 4:
__eee__ = formatterResolver.GetFormatterWithVerify<global::System.Text.StringBuilder>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 5:
__fff__ = formatterResolver.GetFormatterWithVerify<int[]>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 6:
__ggg__ = formatterResolver.GetFormatterWithVerify<global::System.Text.StringBuilder[]>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 7:
__hhh__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<int>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 8:
__iii__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::System.Text.StringBuilder>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 9:
__jjj__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<int, global::System.Text.StringBuilder>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 10:
__kkk__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Text.StringBuilder>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 11:
__lll__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::SubClass>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 12:
__mmm__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::SubClass>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 13:
__nnn__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.List<global::System.Collections.Generic.List<global::SubClass>>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
case 14:
__ooo__ = formatterResolver.GetFormatterWithVerify<global::System.Collections.Generic.Dictionary<global::System.Text.StringBuilder, global::System.Collections.Generic.List<global::SubClass>>>().Deserialize(bytes, offset, formatterResolver, out readSize);
break;
default:
readSize = global::MessagePack.MessagePackBinary.ReadNextBlock(bytes, offset);
break;
}
offset += readSize;
}
readSize = offset - startOffset;
var ____result = new global::TestClass();
____result.aaa = __aaa__;
____result.bbb = __bbb__;
____result.ccc = __ccc__;
____result.ddd = __ddd__;
____result.eee = __eee__;
____result.fff = __fff__;
____result.ggg = __ggg__;
____result.hhh = __hhh__;
____result.iii = __iii__;
____result.jjj = __jjj__;
____result.kkk = __kkk__;
____result.lll = __lll__;
____result.mmm = __mmm__;
____result.nnn = __nnn__;
____result.ooo = __ooo__;
return ____result;
}
}
}
#pragma warning disable 168
#pragma warning restore 414
#pragma warning restore 618
#pragma warning restore 612
using System.Text;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrayTest : MonoBehaviour {
// Use this for initialization
void Start () {
// カスタムリゾルバーを登録する。
// CompositeResolver is singleton helper for use custom resolver.
// Ofcourse you can also make custom resolver.
MessagePack.Resolvers.CompositeResolver.RegisterAndSetAsDefault
(
// use generated resolver first, and combine many other generated/custom resolvers
MessagePack.Resolvers.GeneratedResolver.Instance,
// finally, use builtin/primitive resolver(don't use StandardResolver, it includes dynamic generation)
MessagePack.Resolvers.BuiltinResolver.Instance,
MessagePack.Resolvers.PrimitiveObjectResolver.Instance
);
}
public void ArrayTypeSelialize()
{
Debug.Log("OnClickButton");
var multi = new TestClass();
//*******************************************************
Debug.Log("0");
multi.aaa = 123;
//*******************************************************
Debug.Log("1");
multi.bbb = 234;
//*******************************************************
Debug.Log("2");
multi.ccc = 345.6f;
//*******************************************************
Debug.Log("3");
multi.ddd = 456.789d;
//*******************************************************
Debug.Log("4");
multi.eee = multi.eee.Append("あいうえおかきくけこ");
//*******************************************************
Debug.Log("5");
multi.fff[0] = 999;
//*******************************************************
Debug.Log("6");
multi.ggg[1] = new System.Text.StringBuilder();
multi.ggg[1].Append("さしすせそ");
//*******************************************************
Debug.Log("7");
multi.hhh.Add(12);
multi.hhh.Add(34);
multi.hhh.Add(56);
//*******************************************************
Debug.Log("8");
var sb1 = new System.Text.StringBuilder();
sb1.Append("たちつてと");
multi.iii.Add(sb1);
var sb2 = new System.Text.StringBuilder();
sb2.Append("なにぬねの");
multi.iii.Add(sb2);
var sb3 = new System.Text.StringBuilder();
sb3.Append("はひふへほ");
multi.iii.Add(sb3);
//*******************************************************
Debug.Log("9");
var sb4 = new System.Text.StringBuilder();
sb4.Append("ああああ");
multi.jjj.Add(0, sb4);
var sb5 = new System.Text.StringBuilder();
sb5.Append("いいいい");
multi.jjj.Add(1, sb5);
var sb6 = new System.Text.StringBuilder();
sb6.Append("うううう");
multi.jjj.Add(2, sb6);
//*******************************************************
Debug.Log("10");
var sb7 = new System.Text.StringBuilder();
var sb8 = new System.Text.StringBuilder();
var sb9 = new System.Text.StringBuilder();
var sb10 = new System.Text.StringBuilder();
var sb11 = new System.Text.StringBuilder();
var sb12 = new System.Text.StringBuilder();
sb7.Append("あ");
sb8.Append("い");
sb9.Append("う");
sb10.Append("え");
sb11.Append("お");
sb12.Append("か");
multi.kkk.Add(sb7, sb8);
multi.kkk.Add(sb9, sb10);
multi.kkk.Add(sb11, sb12);
//*******************************************************
Debug.Log("11");
var parts1 = new SubClass();
parts1.acb = 123;
parts1.def = new System.Text.StringBuilder();
parts1.def.Append("あいうえお");
multi.lll.Add(parts1);
var parts2 = new SubClass();
parts2.acb = 234;
parts2.def = new System.Text.StringBuilder();
parts2.def.Append("かきくけこ");
multi.lll.Add(parts2);
//*******************************************************
Debug.Log("12");
var parts3 = new SubClass();
parts3.acb = 345;
parts3.def = new System.Text.StringBuilder();
parts3.def.Append("さしすせそ");
var sb13 = new System.Text.StringBuilder();
sb13.Append("aaa");
multi.mmm.Add(sb13, parts3);
var parts4 = new SubClass();
parts4.acb = 456;
parts4.def = new System.Text.StringBuilder();
parts4.def.Append("たちつてと");
var sb14 = new System.Text.StringBuilder();
sb14.Append("bbb");
multi.mmm.Add(sb14, parts4);
//*******************************************************
Debug.Log("13");
List<SubClass> listTest1 = new List<SubClass>();
var parts5 = new SubClass();
parts5.acb = 456;
parts5.def = new System.Text.StringBuilder();
parts5.def.Append("たちつてと");
listTest1.Add(parts5);
var parts6 = new SubClass();
parts6.acb = 567;
parts6.def = new System.Text.StringBuilder();
parts6.def.Append("なにぬねの");
listTest1.Add(parts6);
multi.nnn.Add(listTest1);
List<SubClass> listTest2 = new List<SubClass>();
var parts7 = new SubClass();
parts7.acb = 456;
parts7.def = new System.Text.StringBuilder();
parts7.def.Append("たちつてと");
listTest2.Add(parts7);
var parts8 = new SubClass();
parts8.acb = 567;
parts8.def = new System.Text.StringBuilder();
parts8.def.Append("なにぬねの");
listTest2.Add(parts8);
multi.nnn.Add(listTest2);
//*******************************************************
Debug.Log("14");
List<SubClass> listTest3 = new List<SubClass>();
var parts9 = new SubClass();
parts9.acb = 456;
parts9.def = new System.Text.StringBuilder();
parts9.def.Append("たちつてと");
listTest3.Add(parts9);
var parts10 = new SubClass();
parts10.acb = 567;
parts10.def = new System.Text.StringBuilder();
parts10.def.Append("なにぬねの");
listTest3.Add(parts10);
var sb15 = new System.Text.StringBuilder();
sb15.Append("aaa");
multi.ooo.Add(sb15, listTest3);
List<SubClass> listTest4 = new List<SubClass>();
var parts11 = new SubClass();
parts11.acb = 456;
parts11.def = new System.Text.StringBuilder();
parts11.def.Append("たちつてと");
listTest4.Add(parts11);
var parts12 = new SubClass();
parts12.acb = 567;
parts12.def = new System.Text.StringBuilder();
parts12.def.Append("なにぬねの");
listTest3.Add(parts12);
var sb16 = new System.Text.StringBuilder();
sb16.Append("bbb");
multi.ooo.Add(sb16, listTest4);
Debug.Log("000000000000000000000000");
MessagePack.MessagePackSerializer.Serialize<TestClass>(multi, MessagePack.Resolvers.CompositeResolver.Instance);
Debug.Log("111111111111111111111111");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment