Skip to content

Commit

Permalink
init Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Jan 20, 2021
1 parent 505417a commit 00b3c26
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/Format/glbTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public static GlbChunkType ToChunkType(string src)
}

public static Glb Parse(Byte[] bytes)
{
return Parse(new ArraySegment<byte>(bytes));
}

public static Glb Parse(ArraySegment<Byte> bytes)
{
if (TryParse(bytes, out Glb glb, out Exception ex))
{
Expand Down
8 changes: 8 additions & 0 deletions Assets/VRM10/Runtime/Migration.meta

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

60 changes: 60 additions & 0 deletions Assets/VRM10/Runtime/Migration/Migration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using UniGLTF.Extensions.VRMC_vrm;
using UniJSON;

namespace UniVRM10
{
/// <summary>
/// Convert vrm0 binary to vrm1 binary. Json processing
/// </summary>
public static class Migration
{
static bool TryGet(this UniGLTF.glTFExtensionImport extensions, string key, out ListTreeNode<JsonValue> value)
{
foreach (var kv in extensions.ObjectItems())
{
if (kv.Key.GetString() == key)
{
value = kv.Value;
return true;
}
}

value = default;
return false;
}

public static byte[] Migrate(byte[] src)
{
var glb = UniGLTF.Glb.Parse(src);
var json = glb.Json.Bytes.ParseAsJson();

var gltf = UniGLTF.GltfDeserializer.Deserialize(json);
if (!(gltf.extensions is UniGLTF.glTFExtensionImport import))
{
throw new Exception("not extensions");
}
if (!import.TryGet("VRM", out ListTreeNode<JsonValue> vrm))
{
throw new Exception("no vrm");
}

{
var vrm1 = new VRMC_vrm();
var f = new JsonFormatter();
GltfSerializer.Serialize(f, vrm1);
gltf.extensions = new UniGLTF.glTFExtensionExport().Add(VRMC_vrm.ExtensionName, f.GetStoreBytes());
}

ArraySegment<byte> vrm1Json = default;
{
var f = new JsonFormatter();
UniGLTF.GltfSerializer.Serialize(f, gltf);
vrm1Json = f.GetStoreBytes();
}

return UniGLTF.Glb.Create(vrm1Json, glb.Binary.Bytes).ToBytes();
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM10/Runtime/Migration/Migration.cs.meta

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

47 changes: 47 additions & 0 deletions Assets/VRM10/Tests/MigrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.IO;
using NUnit.Framework;
using UnityEngine;
using UniJSON;

namespace UniVRM10
{
public class MigrationTests
{
static string AliciaPath
{
get
{
return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm")
.Replace("\\", "/");
}
}

UniGLTF.Extensions.VRMC_vrm.VRMC_vrm GetVRM(UniGLTF.glTFExtension extensions)
{
if (extensions is UniGLTF.glTFExtensionImport import)
{
foreach (var kv in import.ObjectItems())
{
if (kv.Key.GetUtf8String() == UniGLTF.Extensions.VRMC_vrm.VRMC_vrm.ExtensionNameUtf8)
{
return UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.Deserialize(kv.Value);
}
}
}

return default;
}

[Test]
public void Migrate0to1()
{
var vrm0 = File.ReadAllBytes(AliciaPath);
var vrm1 = Migration.Migrate(vrm0);
var glb = UniGLTF.Glb.Parse(vrm1);
var json = glb.Json.Bytes.ParseAsJson();
var gltf = UniGLTF.GltfDeserializer.Deserialize(json);
var vrm = GetVRM(gltf.extensions);
Assert.NotNull(vrm);
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM10/Tests/MigrationTests.cs.meta

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

0 comments on commit 00b3c26

Please sign in to comment.