Skip to content

Instantly share code, notes, and snippets.

@tsubaki

tsubaki/Test.cs Secret

Created February 17, 2014 11:46
Show Gist options
  • Save tsubaki/31a9d351cdd7310b040e to your computer and use it in GitHub Desktop.
Save tsubaki/31a9d351cdd7310b040e to your computer and use it in GitHub Desktop.
msgpackcli 0.5 テストコード
using UnityEngine;
using System.Collections;
using MsgPack.Serialization;
using MsgPack;
public class Test : MonoBehaviour
{
#if UNITY_EDITOR
[ContextMenu("create")]
void Create ()
{
SerializerCodeGenerationConfiguration config = new SerializerCodeGenerationConfiguration ();
config.Namespace = "MsgPack";
config.OutputDirectory = "Assets/Msgpack/Generate";
var filePaths = SerializerGenerator.GenerateCode (config, new System.Type[]{typeof(Sub)});
foreach (var path in filePaths) {
Debug.Log (path);
}
UnityEditor.AssetDatabase.Refresh ();
}
#endif
void Start ()
{
Sub sub = new Sub ()
{
count = 100,
message = "test",
};
SerializationContext contex = new SerializationContext (PackerCompatibilityOptions.ProhibitExtendedTypeObjects);
var hoge = new Test_SubSerializer (contex);
var data = hoge.PackSingleObject (sub);
var sub2 = hoge.UnpackSingleObject (data);
Debug.Log (sub2.message);
Debug.Log (sub2.count);
}
public class Sub
{
public int count = 30;
public string message = "hogehoge";
}
}
// ------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Mono Runtime Version: 2.0.50727.1433
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
// ------------------------------------------------------------------------------
namespace MsgPack {
[System.CodeDom.Compiler.GeneratedCodeAttribute("MsgPack.Serialization.CodeDomSerializers.CodeDomSerializerBuilder", "0.5.0.0")]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public class Test_SubSerializer : MsgPack.Serialization.MessagePackSerializer<Test.Sub> {
private MsgPack.Serialization.MessagePackSerializer<int> _serializer0;
private MsgPack.Serialization.MessagePackSerializer<string> _serializer1;
public Test_SubSerializer(MsgPack.Serialization.SerializationContext context) :
base(Test_SubSerializer.__Conditional((context != null), context, MsgPack.Serialization.SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions) {
MsgPack.Serialization.SerializationContext safeContext = Test_SubSerializer.__Conditional((context != null), context, MsgPack.Serialization.SerializationContext.Default);
this._serializer0 = safeContext.GetSerializer <int>();
this._serializer1 = safeContext.GetSerializer <string>();
}
protected override void PackToCore(MsgPack.Packer packer, Test.Sub objectTree) {
packer.PackArrayHeader(2);
this._serializer0.PackTo(packer, objectTree.count);
this._serializer1.PackTo(packer, objectTree.message);
}
protected override Test.Sub UnpackFromCore(MsgPack.Unpacker unpacker) {
Test.Sub result = default(Test.Sub);
result = new Test.Sub();
if (unpacker.IsArrayHeader) {
int unpacked = default(int);
int itemsCount = default(int);
itemsCount = MsgPack.Serialization.UnpackHelpers.GetItemsCount(unpacker);
System.Nullable<int> nullable = default(System.Nullable<int>);
if ((unpacked < itemsCount)) {
nullable = MsgPack.Serialization.UnpackHelpers.UnpackNullableInt32Value(unpacker, typeof(Test.Sub), "System.Int32 count");
}
if (nullable.HasValue) {
result.count = nullable.Value;
}
unpacked = (unpacked + 1);
string nullable0 = default(string);
if ((unpacked < itemsCount)) {
nullable0 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(Test.Sub), "System.String message");
}
if (((nullable0 == null) == false)) {
result.message = nullable0;
}
unpacked = (unpacked + 1);
}
else {
int itemsCount0 = default(int);
itemsCount0 = MsgPack.Serialization.UnpackHelpers.GetItemsCount(unpacker);
for (int i = 0; (i < itemsCount0); i = (i + 1)) {
string key = default(string);
string nullable1 = default(string);
nullable1 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(Test.Sub), "MemberName");
if (((nullable1 == null) == false)) {
key = nullable1;
}
else {
throw MsgPack.Serialization.SerializationExceptions.NewNullIsProhibited("MemberName");
}
if ((key == "message")) {
string nullable3 = default(string);
nullable3 = MsgPack.Serialization.UnpackHelpers.UnpackStringValue(unpacker, typeof(Test.Sub), "System.String message");
if (((nullable3 == null) == false)) {
result.message = nullable3;
}
}
else {
if ((key == "count")) {
System.Nullable<int> nullable2 = default(System.Nullable<int>);
nullable2 = MsgPack.Serialization.UnpackHelpers.UnpackNullableInt32Value(unpacker, typeof(Test.Sub), "System.Int32 count");
if (nullable2.HasValue) {
result.count = nullable2.Value;
}
}
}
}
}
return result;
}
private static T __Conditional<T>(bool condition, T whenTrue, T whenFalse)
{
if (condition) {
return whenTrue;
}
else {
return whenFalse;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment