public static void GenerateTo(JsonSchemaSource root, DirectoryInfo dir, bool clearFolder) { // clear or create folder if (dir.Exists) { if (dir.EnumerateFileSystemInfos().Any()) { if (!clearFolder) { Console.WriteLine($"{dir} is not empty."); return; } // clear ClearFolder(dir); } } else { Console.WriteLine($"create: {dir}"); dir.Create(); } foreach (var s in root.Traverse()) { // title を掃除 s.title = CleanupTitle(s.title); } { var dst = Path.Combine(dir.FullName, "Format.g.cs"); Console.WriteLine(dst); using (var w = new StringWriter()) { FormatWriter.Write(w, root, GetStem(root.FilePath.Name)); File.WriteAllText(dst, w.ToString().Replace("\r\n", "\n")); } } { var dst = Path.Combine(dir.FullName, "Deserializer.g.cs"); Console.WriteLine(dst); using (var w = new StringWriter()) { DeserializerWriter.Write(w, root, GetStem(root.FilePath.Name)); File.WriteAllText(dst, w.ToString().Replace("\r\n", "\n")); } } { var dst = Path.Combine(dir.FullName, "Serializer.g.cs"); Console.WriteLine(dst); using (var w = new StringWriter()) { SerializerWriter.Write(w, root, GetStem(root.FilePath.Name)); File.WriteAllText(dst, w.ToString().Replace("\r\n", "\n")); } } }
public static void Write(TextWriter w, JsonSchemaSource root, string rootName) { w.Write($@"// This file is generated from JsonSchema. Don't modify this source code. using System; using System.Collections.Generic; namespace UniGLTF.Extensions.{rootName} {{ "); new FormatWriter(w, root.title).Traverse(root, rootName); // close namespace w.WriteLine("}"); }
static void Main(string[] args) { if (args.Length < 1) { // USAGE return; } var root = new FileInfo(args[0]); var parser = new JsonSchemaParser(root.Directory); var source = parser.Load(root.Name, ""); // extra source.AddJsonPath(".meshes[].primitives[].extras.targetNames", new JsonSchemaSource { type = JsonSchemaType.Array, items = new JsonSchemaSource { JsonPath = ".meshes[].primitives[].extras.targetNames[]", type = JsonSchemaType.String, }, }); for (int i = 2; i < args.Length; i += 2) { var jsonPath = args[i]; var extensionSchemaPath = new FileInfo(args[i + 1]); var extensionParser = new JsonSchemaParser(root.Directory, extensionSchemaPath.Directory); var extensionSchema = extensionParser.Load(extensionSchemaPath, jsonPath); var(parent, child) = JsonSchemaSource.SplitParent(jsonPath); { var parentSchema = source.Get(parent); parentSchema.AddProperty(child, extensionSchema); } } source.Dump(); if (args.Length < 2) { return; } ProtoGenerator.Generator.GenerateTo(source, new DirectoryInfo(args[1]), clearFolder: true); }
public static void GenerateTo(JsonSchemaSource root, DirectoryInfo dir, bool clearFolder) { // clear or create folder if (dir.Exists) { if (dir.EnumerateFileSystemInfos().Any()) { if (!clearFolder) { Console.WriteLine($"{dir} is not empty."); return; } // clear ClearFolder(dir); } } else { Console.WriteLine($"create: {dir}"); dir.Create(); } var d = new ProtoNodeDistributer(); d.Distribute(root); // 各ファイルに出力する foreach (var node in d.Roots) { Console.WriteLine($"## file: {node.Path} ##"); node.Dump(); node.WriteTo(Path.Combine(dir.FullName, node.Path)); } // write google/protobuf/wrappers.proto { Directory.CreateDirectory(Path.Combine(dir.FullName, "google/protobuf")); File.WriteAllText(Path.Combine(dir.FullName, "google/protobuf/wrappers.proto"), Wrappers.Source); } }
void Traverse(JsonSchemaSource source, string rootName = default) { foreach (var child in source.Children()) { Traverse(child); } switch (source.type) { case JsonSchemaType.Object: { var schema = source.Create(true); if (schema is ObjectJsonSchema obj) { if (!string.IsNullOrEmpty(rootName)) { obj.Title = rootName; } WriteObject(obj, rootName); } else if (schema is ExtensionJsonSchema ext) { // WriteObject(ext); } else if (schema is DictionaryJsonSchema dict) { WriteObject((ObjectJsonSchema)dict.AdditionalProperties, rootName); } else { throw new Exception(); } } break; case JsonSchemaType.EnumString: WriteEnumString(source.Create(true) as EnumStringJsonSchema); break; } }
public static void GenerateTo(JsonSchemaSource root, DirectoryInfo formatDir, DirectoryInfo serializerDir) { CleanDirectory(formatDir); CleanDirectory(serializerDir); foreach (var s in root.Traverse()) { // title を掃除 s.title = CleanupTitle(s.title); } { var dst = Path.Combine(formatDir.FullName, "Format.g.cs"); Console.WriteLine(dst); using (var w = new StringWriter()) { FormatWriter.Write(w, root, GetStem(root.FilePath.Name)); WriteAllTextForce(dst, w.ToString().Replace("\r\n", "\n")); } } { var dst = Path.Combine(serializerDir.FullName, "Deserializer.g.cs"); Console.WriteLine(dst); using (var w = new StringWriter()) { DeserializerWriter.Write(w, root, GetStem(root.FilePath.Name)); WriteAllTextForce(dst, w.ToString().Replace("\r\n", "\n")); } } { var dst = Path.Combine(serializerDir.FullName, "Serializer.g.cs"); Console.WriteLine(dst); using (var w = new StringWriter()) { SerializerWriter.Write(w, root, GetStem(root.FilePath.Name)); WriteAllTextForce(dst, w.ToString().Replace("\r\n", "\n")); } } }
static void Main(string[] args) { if (args.Length < 1) { // USAGE return; } var root = new FileInfo(args[0]); var parser = new JsonSchemaParser(root.Directory); var jsonSchema = parser.Load(root.Name, ""); // add extensions jsonSchema.AddJsonPath(".materials[].extensions.KHR_materials_unlit", new JsonSchemaSource { type = JsonSchemaType.Object, }); jsonSchema.AddJsonPath(".meshes[].primitives[].extras.targetNames", new JsonSchemaSource { type = JsonSchemaType.Array, items = new JsonSchemaSource { JsonPath = ".meshes[].primitives[].extras.targetNames[]", type = JsonSchemaType.String, }, }); for (int i = 2; i < args.Length; i += 2) { var jsonPath = args[i]; var extensionSchemaPath = new FileInfo(args[i + 1]); var extensionParser = new JsonSchemaParser(root.Directory, extensionSchemaPath.Directory); var extensionSchema = extensionParser.Load(extensionSchemaPath, jsonPath); var(parent, child) = JsonSchemaSource.SplitParent(jsonPath); jsonSchema.Get(parent).AddProperty(child, extensionSchema); } foreach (var source in jsonSchema.Traverse()) { var schema = source.Create(); // index 項を列挙する if (IndexTargets.Map.TryGetValue(schema.JsonPath, out string target)) { if (schema is IntegerJsonSchema index) { Console.WriteLine($"{schema.JsonPath} => {target}"); index.IndexTargetJsonPath = target; } else { throw new NotImplementedException(); } } else { // Console.WriteLine(schema.JsonPath); } if (schema.JsonPath == ".extensions.VRM.blendShapeMaster.blendShapeGroups[].binds[]") { // 特別処理 schema.HardCode = @" // 対象の morph が存在していることを確認する var meshProp = json.GetProperty(""mesh""); var indexProp = json.GetProperty(""index""); if(meshProp.ValueKind == JsonValueKind.Number && indexProp.ValueKind == JsonValueKind.Number) { var mesh = meshProp.GetInt32(); if(mesh<0) { m_context.AddMessage(MessageTypes.MinimumException, mesh, ""${json_path}"", ""mesh""); } else{ var jsonPath = string.Format("".meshes[{0}].primitives[*].targets"" , mesh); if(m_context.TryGetArrayLength(jsonPath, out int length)) { var index = indexProp.GetInt32(); if(index<0) { m_context.AddMessage(MessageTypes.MinimumException, index, ""${json_path}"", ""index""); } else if(index >= length) { m_context.AddMessage(MessageTypes.ArrayExceedLength, index, ""${json_path}"", ""index""); } else{ // OK } } else { m_context.AddMessage(MessageTypes.ArrayNotExists, mesh, ""${json_path}"", ""mesh""); } } } else{ m_context.AddMessage(MessageTypes.InvalidType, json, ""${json_path}"", null); } "; } } if (args.Length < 2) { return; } var config = new ValidatorGenerator.Configuration { Prefix = "gltf", Suffix = "__Validator", }; ValidatorGenerator.GenerateTo(jsonSchema, new DirectoryInfo(args[1]), config, true); }
public static void Fix(JsonSchemaSource obj) { if (obj.title == "Mesh Primitive") { // repeated map<string, int32> targets = 7; var items = new JsonSchemaSource { JsonPath = "", type = JsonSchemaType.Object, title = "target", }; items.AddProperty("POSITION", new JsonSchemaSource { JsonPath = "", type = JsonSchemaType.Integer, title = "POSITION", }); items.AddProperty("NORMAL", new JsonSchemaSource { JsonPath = "", type = JsonSchemaType.Integer, title = "TARGET", }); items.AddProperty("TANGENT", new JsonSchemaSource { JsonPath = "", type = JsonSchemaType.Integer, title = "TANGENT", }); obj.GetProperty("targets", true); obj.AddProperty("targets", new JsonSchemaSource { JsonPath = "", type = JsonSchemaType.Array, items = items, title = "targets", description = "An array of Morph Targets, each Morph Target is a dictionary mapping attributes (only `POSITION`, `NORMAL`, and `TANGENT` supported) to their deviations in the Morph Target.", }); return; } if (obj.title == "Image") { var mimeType = obj.GetProperty("mimeType", true); obj.AddProperty("mimeType", new JsonSchemaSource { JsonPath = mimeType.JsonPath, type = JsonSchemaType.String, title = mimeType.title, description = mimeType.description, }); return; } if (obj.title == "Camera") { var type = obj.GetProperty("type", true); obj.AddProperty("type", new JsonSchemaSource { JsonPath = type.JsonPath, type = JsonSchemaType.String, title = type.title, description = type.description, }); return; } }
public static void Write(TextWriter w, JsonSchemaSource root, string rootName) { w.Write(Begin.Replace("$0", rootName)); root.Create(true, rootName).GenerateSerializer(new TraverseContext(w), "Serialize"); w.Write(End); }