Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate json serializer #298

Merged
merged 9 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions Assets/VRM.Samples/Editor/Tests/VRMImportExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ public void ImportExportTest()
var vrm = VRMExporter.Export(context.Root);

// TODO: Check contents in JSON
/*var exportJson = */JsonParser.Parse(vrm.ToJson());
/*var exportJson = */
JsonParser.Parse(vrm.ToJson());

// TODO: Check contents in JSON
/*var newExportedJson = */JsonParser.Parse(JsonSchema.FromType<glTF>().Serialize(vrm));
/*var newExportedJson = */
JsonParser.Parse(JsonSchema.FromType<glTF>().Serialize(vrm));

/*
foreach (var kv in importJson.Diff(exportJson))
Expand Down Expand Up @@ -117,5 +119,35 @@ public void MeshCopyTest()
MeshTests.MeshEquals(src, dst);
}
}

[Test]
public void SerializerCompare()
{
// Aliciaを古いデシリアライザでロードする
var path = AliciaPath;
var context = new VRMImporterContext();
context.ParseGlb(File.ReadAllBytes(path));
var oldJson = context.GLTF.ToJson().ParseAsJson().ToString(" ");

// 生成シリアライザでJSON化する
var f = new JsonFormatter();
f.GenSerialize(context.GLTF);
var parsed = f.ToString().ParseAsJson();
var newJson = parsed.ToString(" ");

File.WriteAllText("old.json", oldJson);
File.WriteAllText("new.json", newJson);

// 比較
Assert.AreEqual(oldJson, newJson);

// 生成デシリアライザでロードする
var ff = new JsonFormatter();
var des = GltfDeserializer.Deserialize(parsed);
ff.Clear();
ff.GenSerialize(des);
var desJson = ff.ToString().ParseAsJson().ToString(" ");
Assert.AreEqual(oldJson, desJson);
}
}
}
9 changes: 9 additions & 0 deletions Assets/VRM/UniGLTF/Editor/Serialization.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions Assets/VRM/UniGLTF/Editor/Serialization/CollectionSerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.IO;

namespace UniGLTF
{
public class ArraySerialization : FunctionSerializationBase
{
IValueSerialization m_inner;

public ArraySerialization(Type t, IValueSerialization inner)
{
ValueType = t;
m_inner = inner;
}
public override void GenerateDeserializer(StreamWriter writer, string callName)
{
var itemCallName = callName + "_ARRAY";

writer.Write(@"
public static $0 $2(ListTreeNode<JsonValue> parsed)
{
var value = new $1[parsed.GetArrayCount()];
int i=0;
foreach(var x in parsed.ArrayItems())
{
value[i++] = $3;
}
return value;
}
"
.Replace("$0", UniJSON.JsonSchemaAttribute.GetTypeName(ValueType))
.Replace("$1", m_inner.ValueType.Name)
.Replace("$2", callName)
.Replace("$3", m_inner.GenerateDeserializerCall(itemCallName, "x"))
);

if (!m_inner.IsInline)
{
m_inner.GenerateDeserializer(writer, itemCallName);
}
}
}

public class ListSerialization : FunctionSerializationBase
{
IValueSerialization m_inner;

public ListSerialization(Type t, IValueSerialization inner)
{
ValueType = t;
m_inner = inner;
}
public override void GenerateDeserializer(StreamWriter writer, string callName)
{
var itemCallName = callName + "_LIST";
writer.Write(@"
public static $0 $2(ListTreeNode<JsonValue> parsed)
{
var value = new List<$1>();
foreach(var x in parsed.ArrayItems())
{
value.Add($3);
}
return value;
}"
.Replace("$0", UniJSON.JsonSchemaAttribute.GetTypeName(ValueType))
.Replace("$1", m_inner.ValueType.Name)
.Replace("$2", callName)
.Replace("$3", m_inner.GenerateDeserializerCall(itemCallName, "x"))
);

if (!m_inner.IsInline)
{
m_inner.GenerateDeserializer(writer, itemCallName);
}
}
}

public class StringKeyDictionarySerialization : FunctionSerializationBase
{
IValueSerialization m_inner;

public StringKeyDictionarySerialization(Type t, IValueSerialization inner)
{
ValueType = t;
m_inner = inner;
}
public override void GenerateDeserializer(StreamWriter writer, string callName)
{
var itemCallName = callName + "_DICT";
writer.Write(@"

public static $0 $2(ListTreeNode<JsonValue> parsed)
{
var value = new Dictionary<string, $1>();
foreach(var kv in parsed.ObjectItems())
{
value.Add(kv.Key.GetString(), $3);
}
return value;
}
"
.Replace("$0", UniJSON.JsonSchemaAttribute.GetTypeName(ValueType))
.Replace("$1", m_inner.ValueType.Name)
.Replace("$2", callName)
.Replace("$3", m_inner.GenerateDeserializerCall(itemCallName, "kv.Value"))
);

if (!m_inner.IsInline)
{
m_inner.GenerateDeserializer(writer, itemCallName);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions Assets/VRM/UniGLTF/Editor/Serialization/DeserializerGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.IO;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UniGLTF
{
public static class DeserializerGenerator
{
public const BindingFlags FIELD_FLAGS = BindingFlags.Instance | BindingFlags.Public;

static string OutPath
{
get
{
return Path.Combine(UnityEngine.Application.dataPath,
"VRM/UniGLTF/Scripts/IO/GltfDeserializer.g.cs");
}
}

/// <summary>
/// AOT向けにデシリアライザを生成する
/// </summary>
[MenuItem(VRM.VRMVersion.MENU + "/Generate Deserializer")]
static void GenerateSerializer()
{
var info = new ObjectSerialization(typeof(glTF), "gltf");
Debug.Log(info);

using (var s = File.Open(OutPath, FileMode.Create))
using (var w = new StreamWriter(s, Encoding.UTF8))
{
// header
w.Write(@"
using UniJSON;
using System;
using System.Collections.Generic;
using VRM;
using UnityEngine;

namespace UniGLTF {

public static class GltfDeserializer
{

");

info.GenerateDeserializer(w, "Deserialize");

// footer
w.Write(@"
} // GltfDeserializer
} // UniGLTF
");

Debug.LogFormat("write: {0}", OutPath);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading