VRMVersionMenu.cs
4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.IO;
using UniGLTF;
using UniJSON;
using UnityEditor;
using UnityEngine;
namespace VRM
{
public static class VRMVersionMenu
{
const string path = "Assets/VRM/UniVRM/Scripts/Format/VRMVersion.cs";
const string template = @"
namespace VRM
{{
public static partial class VRMVersion
{{
public const int MAJOR = {0};
public const int MINOR = {1};
public const int PATCH = {2};
public const string PRE_ID = ""{3}"";
public const string VERSION = ""{0}.{1}.{2}{4}"";
}}
}}
";
#if VRM_DEVELOP
[MenuItem(VRMVersion.MENU + "/Increment")]
#endif
static void IncrementVersion()
{
var source = string.Format(
template,
VRMVersion.MAJOR,
VRMVersion.MINOR + 1,
VRMVersion.PATCH,
VRMVersion.PRE_ID,
VRMVersion.PRE_ID != "" ? string.Format("-{0}", VRMVersion.PRE_ID) : ""
);
File.WriteAllText(path, source);
AssetDatabase.Refresh();
}
#if VRM_DEVELOP
[MenuItem(VRMVersion.MENU + "/Decrement")]
#endif
static void DecrementVersion()
{
var source = string.Format(
template,
VRMVersion.MAJOR,
VRMVersion.MINOR - 1,
VRMVersion.PATCH,
VRMVersion.PRE_ID,
VRMVersion.PRE_ID != "" ? string.Format("-{0}", VRMVersion.PRE_ID) : ""
);
File.WriteAllText(path, source);
AssetDatabase.Refresh();
}
static string GetTitle(ListTreeNode<JsonValue> node)
{
try
{
var titleNode = node["title"];
if (titleNode.IsString())
{
return titleNode.GetString();
}
}
catch(Exception)
{
}
return "";
}
static void TraverseItem(ListTreeNode<JsonValue> node, JsonFormatter f, UnityPath dir)
{
var title = GetTitle(node);
if (string.IsNullOrEmpty(title))
{
Traverse(node, f, dir);
}
else
{
// ref
f.BeginMap();
f.Key("$ref");
var fileName = string.Format("{0}.schema.json", title);
f.Value(fileName);
f.EndMap();
// new formatter
{
var subFormatter = new JsonFormatter(4);
subFormatter.BeginMap();
foreach (var _kv in node.ObjectItems())
{
subFormatter.Key(_kv.Key.GetUtf8String());
Traverse(_kv.Value, subFormatter, dir);
}
subFormatter.EndMap();
var subJson = subFormatter.ToString();
var path = dir.Child(fileName);
File.WriteAllText(path.FullPath, subJson);
}
}
}
static void Traverse(ListTreeNode<JsonValue> node, JsonFormatter f, UnityPath dir)
{
if (node.IsArray())
{
f.BeginList();
foreach (var x in node.ArrayItems())
{
TraverseItem(x, f, dir);
}
f.EndList();
}
else if (node.IsMap())
{
f.BeginMap();
foreach (var kv in node.ObjectItems())
{
f.Key(kv.Key.GetUtf8String());
TraverseItem(kv.Value, f, dir);
}
f.EndMap();
}
else
{
f.Value(node);
}
}
static UnityPath SplitAndWriteJson(ListTreeNode<JsonValue> parsed, UnityPath dir)
{
var f = new JsonFormatter(4);
Traverse(parsed, f, dir);
var json = f.ToString();
var path = dir.Child("vrm.schema.json");
Debug.LogFormat("write JsonSchema: {0}", path.FullPath);
File.WriteAllText(path.FullPath, json);
return path;
}
#if VRM_DEVELOP
[MenuItem(VRMVersion.MENU + "/Export JsonSchema")]
#endif
static void ExportJsonSchema()
{
var schema = JsonSchema.FromType<glTF_VRM_extensions>();
var f = new JsonFormatter(2);
schema.ToJson(f);
var json = f.ToString();
var dir = UnityPath.FromFullpath(Application.dataPath + "/VRM/specification/0.0/schema");
dir.EnsureFolder();
var path = SplitAndWriteJson(JsonParser.Parse(json), dir);
Selection.activeObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path.Value);
}
}
}