From d5934e97d95fcce29fac8e9dccd3ff40b0c7257e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 7 Sep 2021 17:17:05 +0900 Subject: [PATCH] WIP --- Assets/VRM10/Runtime/IO/Vrm10Data.cs | 13 ++- Assets/VRM10/Runtime/Migration/Vrm10Meta.cs | 109 ++++++++++++++++++ .../VRM10/Runtime/Migration/Vrm10Meta.cs.meta | 11 ++ Assets/VRM10/Tests/MigrationTests.cs | 7 ++ 4 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 Assets/VRM10/Runtime/Migration/Vrm10Meta.cs create mode 100644 Assets/VRM10/Runtime/Migration/Vrm10Meta.cs.meta diff --git a/Assets/VRM10/Runtime/IO/Vrm10Data.cs b/Assets/VRM10/Runtime/IO/Vrm10Data.cs index 6364d9d22..405e06374 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Data.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Data.cs @@ -17,15 +17,17 @@ namespace UniVRM10 { public GltfData Data { get; } public UniGLTF.Extensions.VRMC_vrm.VRMC_vrm VrmExtension { get; } + public readonly Migration.Vrm0Meta OldMeta; public readonly Vrm10FileType FileType; public readonly String Message; - public Vrm10Data(GltfData data, VRMC_vrm vrm, Vrm10FileType fileType, string message) + public Vrm10Data(GltfData data, VRMC_vrm vrm, Vrm10FileType fileType, string message, Migration.Vrm0Meta oldMeta = null) { Data = data; VrmExtension = vrm; FileType = fileType; Message = message; + OldMeta = oldMeta; } public static bool TryParseOrMigrate(string path, bool doMigrate, out Vrm10Data result) @@ -56,6 +58,7 @@ namespace UniVRM10 // try migrateion byte[] migrated = default; + Migration.Vrm0Meta oldMeta = default; try { var glb = UniGLTF.Glb.Parse(bytes); @@ -92,6 +95,8 @@ namespace UniVRM10 result = new Vrm10Data(default, default, Vrm10FileType.Vrm0, "vrm0: cannot migrate"); return false; } + + oldMeta = Migration.Vrm0Meta.FromJsonBytes(json); } catch (Exception ex) { @@ -104,7 +109,11 @@ namespace UniVRM10 if (UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(data.GLTF.extensions, out VRMC_vrm vrm)) { // success - result = new Vrm10Data(data, vrm, Vrm10FileType.Vrm0, "vrm0: migrated"); + if (oldMeta == null) + { + throw new NullReferenceException("oldMeta"); + } + result = new Vrm10Data(data, vrm, Vrm10FileType.Vrm0, "vrm0: migrated", oldMeta); return true; } diff --git a/Assets/VRM10/Runtime/Migration/Vrm10Meta.cs b/Assets/VRM10/Runtime/Migration/Vrm10Meta.cs new file mode 100644 index 000000000..5ee907af6 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Vrm10Meta.cs @@ -0,0 +1,109 @@ +using System; +using UniJSON; + +namespace UniVRM10.Migration +{ + public enum AllowedUser + { + OnlyAuthor, + ExplicitlyLicensedPerson, + Everyone, + } + + public enum LicenseType + { + Redistribution_Prohibited, + CC0, + CC_BY, + CC_BY_NC, + CC_BY_SA, + CC_BY_NC_SA, + CC_BY_ND, + CC_BY_NC_ND, + Other + } + + public enum UssageLicense + { + Disallow, + Allow, + } + + /// + /// VRM0.X version meta. This class has meta before migrate. + /// + public class Vrm0Meta + { + public string title; + public string version; + public string author; + public string contactInformation; + public string reference; + public int texture = -1; + public AllowedUser allowedUser = AllowedUser.OnlyAuthor; + public bool violentUsage = false; + public bool sexualUsage = false; + public bool commercialUsage = false; + public string otherPermissionUrl; + public LicenseType licenseType = LicenseType.Redistribution_Prohibited; + public string otherLicenseUrl; + + public static Vrm0Meta FromJsonBytes(UniJSON.JsonNode glTF) + { + var oldMeta = new Vrm0Meta(); + var extensions = glTF["extensions"]; + var vrm = extensions["VRM"]; + var meta = vrm["meta"]; + foreach (var kv in meta.ObjectItems()) + { + var key = kv.Key.GetString(); + switch (key) + { + case "title": + oldMeta.title = kv.Value.GetString(); + break; + case "version": + oldMeta.version = kv.Value.GetString(); + break; + case "author": + oldMeta.author = kv.Value.GetString(); + break; + case "contactInformation": + oldMeta.contactInformation = kv.Value.GetString(); + break; + case "reference": + oldMeta.reference = kv.Value.GetString(); + break; + case "texture": + oldMeta.texture = kv.Value.GetInt32(); + break; + case "allowedUserName": + oldMeta.allowedUser = (AllowedUser)Enum.Parse(typeof(AllowedUser), kv.Value.GetString(), true); + break; + case "violentUssageName": + oldMeta.violentUsage = kv.Value.GetString() == "Allow"; + break; + case "sexualUssageName": + oldMeta.sexualUsage = kv.Value.GetString() == "Allow"; + break; + case "commercialUssageName": + oldMeta.commercialUsage = kv.Value.GetString() == "Allow"; + break; + case "otherPermissionUrl": + oldMeta.otherPermissionUrl = kv.Value.GetString(); + break; + case "licenseName": + oldMeta.licenseType = (LicenseType)Enum.Parse(typeof(LicenseType), kv.Value.GetString(), true); + break; + case "otherLicenseUrl": + oldMeta.otherLicenseUrl = kv.Value.GetString(); + break; + default: + UnityEngine.Debug.Log($"{key}"); + break; + } + } + return oldMeta; + } + } +} diff --git a/Assets/VRM10/Runtime/Migration/Vrm10Meta.cs.meta b/Assets/VRM10/Runtime/Migration/Vrm10Meta.cs.meta new file mode 100644 index 000000000..efd52afc5 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Vrm10Meta.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 57e782f0f79ef4d4abcd4f1c5438e9e7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Tests/MigrationTests.cs b/Assets/VRM10/Tests/MigrationTests.cs index b0006eea3..5f1fc4d14 100644 --- a/Assets/VRM10/Tests/MigrationTests.cs +++ b/Assets/VRM10/Tests/MigrationTests.cs @@ -254,5 +254,12 @@ namespace UniVRM10 Assert.AreEqual(VALUE.y, springBone.Colliders[colliderIndex].Shape.Sphere.Offset[1]); Assert.AreEqual(VALUE.z, springBone.Colliders[colliderIndex].Shape.Sphere.Offset[2]); } + + [Test] + public void MigrateMeta() + { + Assert.True(Vrm10Data.TryParseOrMigrate(AliciaPath, true, out Vrm10Data vrm)); + var a = 0; + } } }