| | 1 | | /* |
| | 2 | | * Copyright (c) 2024 Willy Alberto Kuster |
| | 3 | | * |
| | 4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| | 5 | | * of this software and associated documentation files (the "Software"), to deal |
| | 6 | | * in the Software without restriction, including without limitation the rights |
| | 7 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| | 8 | | * copies of the Software, and to permit persons to whom the Software is |
| | 9 | | * furnished to do so, subject to the following conditions: |
| | 10 | | * |
| | 11 | | * The above copyright notice and this permission notice shall be included in |
| | 12 | | * all copies or substantial portions of the Software. |
| | 13 | | * |
| | 14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| | 15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| | 16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| | 17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| | 18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| | 19 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| | 20 | | * THE SOFTWARE. |
| | 21 | | */ |
| | 22 | | using SharpYaml.Serialization; |
| | 23 | | using System; |
| | 24 | | using System.Collections.Generic; |
| | 25 | | using System.IO; |
| | 26 | | using System.Linq; |
| | 27 | | using UnityEditor; |
| | 28 | |
|
| | 29 | | namespace Willykc.Templ.Editor.Scaffold |
| | 30 | | { |
| | 31 | | internal static class TemplScaffoldYamlSerializer |
| | 32 | | { |
| | 33 | | private static readonly Serializer YamlSerializer; |
| | 34 | |
|
| | 35 | | static TemplScaffoldYamlSerializer() |
| 1 | 36 | | { |
| 1 | 37 | | var settings = new SerializerSettings() |
| | 38 | | { |
| | 39 | | EmitAlias = false, |
| | 40 | | ComparerForKeySorting = null |
| | 41 | | }; |
| | 42 | |
|
| 1 | 43 | | YamlSerializer = new Serializer(settings); |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | internal static string SerializeTree(TemplScaffoldRoot root, bool useGuids = false) |
| 2 | 47 | | { |
| 2 | 48 | | root = root ?? throw new ArgumentNullException(nameof(root)); |
| | 49 | |
|
| 2 | 50 | | var dtoTree = BuildDtoTree(root, useGuids); |
| | 51 | |
|
| 2 | 52 | | return YamlSerializer.Serialize(dtoTree); |
| 2 | 53 | | } |
| | 54 | |
|
| | 55 | | internal static TemplScaffoldRoot DeserializeTree(string fromText) |
| 14 | 56 | | { |
| 14 | 57 | | fromText = !string.IsNullOrWhiteSpace(fromText) |
| | 58 | | ? fromText |
| | 59 | | : throw new ArgumentException($"{nameof(fromText)} must not be null or empty"); |
| | 60 | |
|
| 14 | 61 | | var dtoTree = YamlSerializer.Deserialize(fromText); |
| 13 | 62 | | var children = ReadDtoTree(dtoTree); |
| 9 | 63 | | var root = new TemplScaffoldRoot() { name = nameof(TemplScaffold.Root) }; |
| 9 | 64 | | root.AddChildrenRange(children); |
| | 65 | |
|
| 9 | 66 | | return root; |
| 9 | 67 | | } |
| | 68 | |
|
| | 69 | | private static List<object> BuildDtoTree(TemplScaffoldNode node, bool useGuids) |
| 4 | 70 | | { |
| 4 | 71 | | var childrenList = new List<object>(); |
| | 72 | |
|
| 20 | 73 | | foreach (var child in node.Children) |
| 4 | 74 | | { |
| 4 | 75 | | var childDto = new Dictionary<string, object>(); |
| | 76 | |
|
| 4 | 77 | | var value = child is TemplScaffoldFile file |
| | 78 | | ? GetTemplateReference(file, useGuids) |
| | 79 | | : BuildDtoTree(child, useGuids); |
| | 80 | |
|
| 4 | 81 | | childDto.Add(child.name, value); |
| | 82 | |
|
| 4 | 83 | | childrenList.Add(childDto); |
| 4 | 84 | | } |
| | 85 | |
|
| 4 | 86 | | return childrenList; |
| 4 | 87 | | } |
| | 88 | |
|
| | 89 | | private static object GetTemplateReference(TemplScaffoldFile file, bool useGuids) |
| 2 | 90 | | { |
| 2 | 91 | | var path = AssetDatabase.GetAssetPath(file.Template); |
| 2 | 92 | | return useGuids |
| | 93 | | ? AssetDatabase.AssetPathToGUID(path) |
| | 94 | | : path; |
| 2 | 95 | | } |
| | 96 | |
|
| | 97 | | private static List<TemplScaffoldNode> ReadDtoTree(object dtoTree) |
| 38 | 98 | | { |
| 38 | 99 | | var childrenList = new List<TemplScaffoldNode>(); |
| | 100 | |
|
| 38 | 101 | | if (!(dtoTree is IEnumerable<object> nodeEnumerable)) |
| 1 | 102 | | { |
| 1 | 103 | | throw new InvalidOperationException( |
| | 104 | | "Serialized Scaffold tree nodes must always be collections"); |
| | 105 | | } |
| | 106 | |
|
| 206 | 107 | | foreach (var child in nodeEnumerable) |
| 50 | 108 | | { |
| 50 | 109 | | if (!(child is IDictionary<object, object> childDictionary)) |
| 1 | 110 | | { |
| 1 | 111 | | throw new InvalidOperationException( |
| | 112 | | "Serialized Scaffold child nodes must always be dictionaries"); |
| | 113 | | } |
| | 114 | |
|
| 49 | 115 | | if (childDictionary.Count == 0) |
| 0 | 116 | | { |
| 0 | 117 | | continue; |
| | 118 | | } |
| | 119 | |
|
| 49 | 120 | | var childNode = GetNode(childDictionary); |
| 45 | 121 | | childrenList.Add(childNode); |
| 45 | 122 | | } |
| | 123 | |
|
| 32 | 124 | | return childrenList; |
| 32 | 125 | | } |
| | 126 | |
|
| | 127 | | private static TemplScaffoldNode GetNode(IDictionary<object, object> childDictionary) |
| 49 | 128 | | { |
| 49 | 129 | | var firstPair = childDictionary.First(); |
| | 130 | |
|
| 49 | 131 | | var name = firstPair.Key.ToString(); |
| | 132 | |
|
| 49 | 133 | | var nodeInputs = childDictionary |
| | 134 | | .Skip(1) |
| 32 | 135 | | .ToDictionary(p => p.Key.ToString(), p => p.Value); |
| | 136 | |
|
| 49 | 137 | | var value = firstPair.Value; |
| 49 | 138 | | return value is string reference |
| | 139 | | ? GetFileNode(name, reference, nodeInputs) |
| | 140 | | : GetDirectoryNode(name, value); |
| 45 | 141 | | } |
| | 142 | |
|
| | 143 | | private static TemplScaffoldNode GetDirectoryNode(string name, object value) |
| 25 | 144 | | { |
| 25 | 145 | | var directoryNode = new TemplScaffoldDirectory() { name = name }; |
| 25 | 146 | | var directoryChildren = ReadDtoTree(value); |
| 23 | 147 | | directoryNode.AddChildrenRange(directoryChildren); |
| 23 | 148 | | return directoryNode; |
| 23 | 149 | | } |
| | 150 | |
|
| | 151 | | private static TemplScaffoldNode GetFileNode( |
| | 152 | | string name, |
| | 153 | | string reference, |
| | 154 | | IDictionary<string, object> nodeInputs) |
| 24 | 155 | | { |
| 24 | 156 | | var path = GUID.TryParse(reference, out _) |
| | 157 | | ? AssetDatabase.GUIDToAssetPath(reference) |
| | 158 | | : reference; |
| | 159 | |
|
| 24 | 160 | | if (string.IsNullOrWhiteSpace(path)) |
| 1 | 161 | | { |
| 1 | 162 | | throw new FileNotFoundException( |
| | 163 | | $"Could not find template asset with guid '{reference}'"); |
| | 164 | | } |
| | 165 | |
|
| 23 | 166 | | if (!File.Exists(path)) |
| 1 | 167 | | { |
| 1 | 168 | | throw new FileNotFoundException($"Could not find template asset at {path}"); |
| | 169 | | } |
| | 170 | |
|
| 22 | 171 | | var template = AssetDatabase.LoadAssetAtPath<ScribanAsset>(path); |
| 22 | 172 | | return new TemplScaffoldFile(template) { name = name, NodeInputs = nodeInputs }; |
| 22 | 173 | | } |
| | 174 | | } |
| | 175 | | } |