| | 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 System; |
| | 23 | | using System.IO; |
| | 24 | | using System.Linq; |
| | 25 | | using System.Reflection; |
| | 26 | | using UnityEditor; |
| | 27 | | using UnityObject = UnityEngine.Object; |
| | 28 | |
|
| | 29 | | namespace Willykc.Templ.Editor.Entry |
| | 30 | | { |
| | 31 | | using Abstraction; |
| | 32 | |
|
| | 33 | | internal sealed class TemplEntryFacade : ITemplEntryFacade |
| | 34 | | { |
| | 35 | | private const int ValidInputFieldCount = 1; |
| | 36 | |
|
| 55 | 37 | | private readonly object lockHandle = new object(); |
| | 38 | | private readonly ISettingsProvider settingsProvider; |
| | 39 | | private readonly IAssetDatabase assetDatabase; |
| | 40 | | private readonly IEditorUtility editorUtility; |
| | 41 | | private readonly ITemplEntryCore entryCore; |
| | 42 | |
|
| 55 | 43 | | internal TemplEntryFacade( |
| | 44 | | ISettingsProvider settingsProvider, |
| | 45 | | IAssetDatabase assetDatabase, |
| | 46 | | IEditorUtility editorUtility, |
| | 47 | | ITemplEntryCore entryCore) |
| 55 | 48 | | { |
| 55 | 49 | | this.settingsProvider = settingsProvider; |
| 55 | 50 | | this.assetDatabase = assetDatabase; |
| 55 | 51 | | this.editorUtility = editorUtility; |
| 55 | 52 | | this.entryCore = entryCore; |
| 55 | 53 | | } |
| | 54 | |
|
| | 55 | | TemplEntry[] ITemplEntryFacade.GetEntries() |
| 2 | 56 | | { |
| 2 | 57 | | if (!settingsProvider.SettingsExist()) |
| 1 | 58 | | { |
| 1 | 59 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 60 | | } |
| | 61 | |
|
| 1 | 62 | | lock (lockHandle) |
| 1 | 63 | | { |
| 1 | 64 | | return settingsProvider.GetSettings().Entries.ToArray(); |
| | 65 | | } |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | string ITemplEntryFacade.AddEntry<T>( |
| | 69 | | UnityObject inputAsset, |
| | 70 | | ScribanAsset template, |
| | 71 | | string outputAssetPath) |
| 19 | 72 | | { |
| 19 | 73 | | if (!settingsProvider.SettingsExist()) |
| 1 | 74 | | { |
| 1 | 75 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 76 | | } |
| | 77 | |
|
| 18 | 78 | | inputAsset = inputAsset |
| | 79 | | ? inputAsset |
| | 80 | | : throw new ArgumentNullException(nameof(inputAsset)); |
| 17 | 81 | | template = template |
| | 82 | | ? template |
| | 83 | | : throw new ArgumentNullException(nameof(template)); |
| 16 | 84 | | outputAssetPath = !string.IsNullOrWhiteSpace(outputAssetPath) |
| | 85 | | ? outputAssetPath |
| | 86 | | : throw new ArgumentException( |
| | 87 | | $"{nameof(outputAssetPath)} must not be null or empty"); |
| | 88 | |
|
| 14 | 89 | | var settings = settingsProvider.GetSettings(); |
| | 90 | |
|
| 14 | 91 | | if (inputAsset == settings) |
| 1 | 92 | | { |
| 1 | 93 | | throw new ArgumentException( |
| | 94 | | $"{nameof(inputAsset)} can not be {nameof(TemplSettings)}", nameof(inputAsset)); |
| | 95 | | } |
| | 96 | |
|
| 13 | 97 | | if (inputAsset.GetType() == typeof(ScribanAsset)) |
| 1 | 98 | | { |
| 1 | 99 | | throw new ArgumentException( |
| | 100 | | $"{nameof(inputAsset)} can not be a {nameof(ScribanAsset)}", |
| | 101 | | nameof(inputAsset)); |
| | 102 | | } |
| | 103 | |
|
| 12 | 104 | | if (template.HasErrors) |
| 1 | 105 | | { |
| 1 | 106 | | throw new ArgumentException($"{nameof(template)} has syntax errors", |
| | 107 | | nameof(template)); |
| | 108 | | } |
| | 109 | |
|
| 11 | 110 | | outputAssetPath = outputAssetPath.SanitizePath(); |
| 11 | 111 | | string filename = null; |
| | 112 | |
|
| | 113 | | try |
| 11 | 114 | | { |
| 11 | 115 | | filename = Path.GetFileName(outputAssetPath); |
| 11 | 116 | | } |
| 0 | 117 | | catch (Exception exception) |
| 0 | 118 | | { |
| 0 | 119 | | throw new ArgumentException($"'{outputAssetPath}' is not a valid path", |
| | 120 | | nameof(outputAssetPath), exception); |
| | 121 | | } |
| | 122 | |
|
| 11 | 123 | | if(!filename.IsValidFileName()) |
| 2 | 124 | | { |
| 2 | 125 | | throw new ArgumentException($"'{filename}' is not a valid file name", |
| | 126 | | nameof(outputAssetPath)); |
| | 127 | | } |
| | 128 | |
|
| 9 | 129 | | var directoryPath = Path.GetDirectoryName(outputAssetPath); |
| 9 | 130 | | var directory = assetDatabase.LoadAssetAtPath<DefaultAsset>(directoryPath); |
| | 131 | |
|
| 9 | 132 | | if (!directory || !assetDatabase.IsValidFolder(directoryPath)) |
| 1 | 133 | | { |
| 1 | 134 | | throw new DirectoryNotFoundException( |
| | 135 | | $"Directory does not exist in the asset database: '{directoryPath}'"); |
| | 136 | | } |
| | 137 | |
|
| 8 | 138 | | var entryType = typeof(T); |
| | 139 | |
|
| 8 | 140 | | if (!IsValidEntryType(entryType)) |
| 1 | 141 | | { |
| 1 | 142 | | throw new InvalidOperationException( |
| | 143 | | $"'{entryType.Name}' is not a valid entry type"); |
| | 144 | | } |
| | 145 | |
|
| 7 | 146 | | if (settings.Entries.Any(e => e.OutputAssetPath.PathsEquivalent(outputAssetPath))) |
| 2 | 147 | | { |
| 2 | 148 | | throw new InvalidOperationException("Existing entry already uses " + |
| | 149 | | $"'{outputAssetPath}' as output asset path"); |
| | 150 | | } |
| | 151 | |
|
| 5 | 152 | | var newEntry = Activator.CreateInstance(entryType) as TemplEntry; |
| | 153 | |
|
| | 154 | | try |
| 5 | 155 | | { |
| 5 | 156 | | newEntry.InputAsset = inputAsset; |
| 4 | 157 | | } |
| 1 | 158 | | catch (Exception exception) |
| 1 | 159 | | { |
| 1 | 160 | | throw new ArgumentException( |
| | 161 | | $"Could not assign {nameof(inputAsset)} to entry. " + |
| | 162 | | "Type must match entry input field", |
| | 163 | | nameof(inputAsset), exception); |
| | 164 | | } |
| | 165 | |
|
| 4 | 166 | | newEntry.Template = template; |
| 4 | 167 | | newEntry.Directory = directory; |
| 4 | 168 | | newEntry.Filename = filename; |
| | 169 | |
|
| 4 | 170 | | lock (lockHandle) |
| 4 | 171 | | { |
| 4 | 172 | | settingsProvider.GetSettings().Entries.Add(newEntry); |
| | 173 | |
|
| 4 | 174 | | editorUtility.SetDirty(settings); |
| 4 | 175 | | assetDatabase.SaveAssets(); |
| 4 | 176 | | } |
| | 177 | |
|
| 4 | 178 | | return newEntry.Id; |
| 4 | 179 | | } |
| | 180 | |
|
| | 181 | | void ITemplEntryFacade.UpdateEntry( |
| | 182 | | string id, |
| | 183 | | UnityObject inputAsset, |
| | 184 | | ScribanAsset template, |
| | 185 | | string outputAssetPath) |
| 16 | 186 | | { |
| 16 | 187 | | if (!settingsProvider.SettingsExist()) |
| 1 | 188 | | { |
| 1 | 189 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 190 | | } |
| | 191 | |
|
| 15 | 192 | | id = id ?? throw new ArgumentNullException(nameof(id)); |
| | 193 | |
|
| 14 | 194 | | var settings = settingsProvider.GetSettings(); |
| | 195 | |
|
| 14 | 196 | | if (inputAsset && inputAsset == settings) |
| 1 | 197 | | { |
| 1 | 198 | | throw new ArgumentException( |
| | 199 | | $"{nameof(inputAsset)} can not be {nameof(TemplSettings)}", nameof(inputAsset)); |
| | 200 | | } |
| | 201 | |
|
| 13 | 202 | | if (inputAsset && inputAsset.GetType() == typeof(ScribanAsset)) |
| 1 | 203 | | { |
| 1 | 204 | | throw new ArgumentException( |
| | 205 | | $"{nameof(inputAsset)} can not be a {nameof(ScribanAsset)}", |
| | 206 | | nameof(inputAsset)); |
| | 207 | | } |
| | 208 | |
|
| 12 | 209 | | if (template && template.HasErrors) |
| 1 | 210 | | { |
| 1 | 211 | | throw new ArgumentException($"{nameof(template)} has syntax errors", |
| | 212 | | nameof(template)); |
| | 213 | | } |
| | 214 | |
|
| 11 | 215 | | outputAssetPath = outputAssetPath != null |
| | 216 | | ? outputAssetPath.SanitizePath() |
| | 217 | | : outputAssetPath; |
| | 218 | |
|
| 11 | 219 | | string filename = null; |
| | 220 | |
|
| | 221 | | try |
| 11 | 222 | | { |
| 11 | 223 | | filename = outputAssetPath != null |
| | 224 | | ? Path.GetFileName(outputAssetPath) |
| | 225 | | : filename; |
| 11 | 226 | | } |
| 0 | 227 | | catch (Exception exception) |
| 0 | 228 | | { |
| 0 | 229 | | throw new ArgumentException($"'{outputAssetPath}' is not a valid path", |
| | 230 | | nameof(outputAssetPath), exception); |
| | 231 | | } |
| | 232 | |
|
| 11 | 233 | | if (outputAssetPath != null && !filename.IsValidFileName()) |
| 2 | 234 | | { |
| 2 | 235 | | throw new ArgumentException($"'{filename}' is not a valid file name", |
| | 236 | | nameof(outputAssetPath)); |
| | 237 | | } |
| | 238 | |
|
| 9 | 239 | | var directoryPath = outputAssetPath != null |
| | 240 | | ? Path.GetDirectoryName(outputAssetPath) |
| | 241 | | : string.Empty; |
| 9 | 242 | | var directory = assetDatabase.LoadAssetAtPath<DefaultAsset>(directoryPath); |
| | 243 | |
|
| 9 | 244 | | if (outputAssetPath != null && |
| | 245 | | (!directory || !assetDatabase.IsValidFolder(directoryPath))) |
| 1 | 246 | | { |
| 1 | 247 | | throw new DirectoryNotFoundException( |
| | 248 | | $"Directory does not exist in the asset database: '{directoryPath}'"); |
| | 249 | | } |
| | 250 | |
|
| 17 | 251 | | var entry = settings.Entries.FirstOrDefault(e => e.Id == id); |
| | 252 | |
|
| 8 | 253 | | if (entry == null) |
| 1 | 254 | | { |
| 1 | 255 | | throw new InvalidOperationException($"No entry could be found with id '{id}'"); |
| | 256 | | } |
| | 257 | |
|
| 7 | 258 | | var pathConflicts = settings.Entries |
| 14 | 259 | | .Any(e => e != entry && e.OutputAssetPath.PathsEquivalent(outputAssetPath)); |
| | 260 | |
|
| 6 | 261 | | if (pathConflicts) |
| 2 | 262 | | { |
| 2 | 263 | | throw new InvalidOperationException("Existing entry already uses " + |
| | 264 | | $"'{outputAssetPath}' as output asset path"); |
| | 265 | | } |
| | 266 | |
|
| 4 | 267 | | lock (lockHandle) |
| 4 | 268 | | { |
| | 269 | | try |
| 4 | 270 | | { |
| 4 | 271 | | entry.InputAsset = inputAsset ? inputAsset : entry.InputAsset; |
| 4 | 272 | | } |
| 0 | 273 | | catch (Exception exception) |
| 0 | 274 | | { |
| 0 | 275 | | throw new ArgumentException( |
| | 276 | | $"Could not assign {nameof(inputAsset)} to entry. " + |
| | 277 | | "Type must match entry input field", |
| | 278 | | nameof(inputAsset), exception); |
| | 279 | | } |
| | 280 | |
|
| 4 | 281 | | entry.Template = template ? template : entry.Template; |
| 4 | 282 | | entry.Directory = outputAssetPath != null ? directory : entry.Directory; |
| 4 | 283 | | entry.Filename = outputAssetPath != null ? filename : entry.Filename; |
| | 284 | |
|
| 4 | 285 | | editorUtility.SetDirty(settings); |
| 4 | 286 | | assetDatabase.SaveAssets(); |
| 4 | 287 | | } |
| 4 | 288 | | } |
| | 289 | |
|
| | 290 | | void ITemplEntryFacade.RemoveEntry(string id) |
| 6 | 291 | | { |
| 6 | 292 | | if (!settingsProvider.SettingsExist()) |
| 1 | 293 | | { |
| 1 | 294 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 295 | | } |
| | 296 | |
|
| 5 | 297 | | id = id ?? throw new ArgumentNullException(nameof(id)); |
| | 298 | |
|
| 4 | 299 | | var settings = settingsProvider.GetSettings(); |
| | 300 | |
|
| 9 | 301 | | var entry = settings.Entries.FirstOrDefault(e => e.Id == id); |
| | 302 | |
|
| 4 | 303 | | if (entry == null) |
| 1 | 304 | | { |
| 1 | 305 | | throw new InvalidOperationException($"No entry could be found with id '{id}'"); |
| | 306 | | } |
| | 307 | |
|
| 3 | 308 | | lock (lockHandle) |
| 3 | 309 | | { |
| 3 | 310 | | settings.Entries.Remove(entry); |
| | 311 | |
|
| 3 | 312 | | editorUtility.SetDirty(settings); |
| 3 | 313 | | assetDatabase.SaveAssets(); |
| 3 | 314 | | } |
| 3 | 315 | | } |
| | 316 | |
|
| | 317 | | bool ITemplEntryFacade.EntryExist(string outputAssetPath) |
| 5 | 318 | | { |
| 5 | 319 | | if (!settingsProvider.SettingsExist()) |
| 1 | 320 | | { |
| 1 | 321 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 322 | | } |
| | 323 | |
|
| 4 | 324 | | outputAssetPath = outputAssetPath |
| | 325 | | ?? throw new ArgumentNullException(nameof(outputAssetPath)); |
| | 326 | |
|
| 3 | 327 | | var settings = settingsProvider.GetSettings(); |
| | 328 | |
|
| 3 | 329 | | lock (lockHandle) |
| 3 | 330 | | { |
| 3 | 331 | | return settings.Entries |
| 4 | 332 | | .Any(e => e.OutputAssetPath.PathsEquivalent(outputAssetPath)); |
| | 333 | | } |
| 3 | 334 | | } |
| | 335 | |
|
| | 336 | | void ITemplEntryFacade.ForceRenderEntry(string id) |
| 5 | 337 | | { |
| 5 | 338 | | if (!settingsProvider.SettingsExist()) |
| 1 | 339 | | { |
| 1 | 340 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 341 | | } |
| | 342 | |
|
| 4 | 343 | | id = id ?? throw new ArgumentNullException(nameof(id)); |
| | 344 | |
|
| 3 | 345 | | var settings = settingsProvider.GetSettings(); |
| | 346 | |
|
| 7 | 347 | | var entry = settings.Entries.FirstOrDefault(e => e.Id == id); |
| | 348 | |
|
| 3 | 349 | | if (entry == null) |
| 1 | 350 | | { |
| 1 | 351 | | throw new InvalidOperationException($"No entry could be found with id '{id}'"); |
| | 352 | | } |
| | 353 | |
|
| 2 | 354 | | if (!entry.IsValid) |
| 1 | 355 | | { |
| 1 | 356 | | throw new InvalidOperationException($"Can not render invalid entry with id '{id}'"); |
| | 357 | | } |
| | 358 | |
|
| 1 | 359 | | lock (lockHandle) |
| 1 | 360 | | { |
| 1 | 361 | | entryCore.RenderEntry(id); |
| 1 | 362 | | } |
| 1 | 363 | | } |
| | 364 | |
|
| | 365 | | void ITemplEntryFacade.ForceRenderAllValidEntries() |
| 2 | 366 | | { |
| 2 | 367 | | if (!settingsProvider.SettingsExist()) |
| 1 | 368 | | { |
| 1 | 369 | | throw new InvalidOperationException($"{nameof(TemplSettings)} not found"); |
| | 370 | | } |
| | 371 | |
|
| 1 | 372 | | lock (lockHandle) |
| 1 | 373 | | { |
| 1 | 374 | | entryCore.RenderAllValidEntries(); |
| 1 | 375 | | } |
| 1 | 376 | | } |
| | 377 | |
|
| | 378 | | internal static bool IsValidEntryType(Type type) => |
| 8 | 379 | | type.IsSubclassOf(typeof(TemplEntry)) && !type.IsAbstract && |
| | 380 | | type.IsDefined(typeof(TemplEntryInfoAttribute), false) && |
| | 381 | | type.GetFields().Count(IsValidInputField) == ValidInputFieldCount; |
| | 382 | |
|
| | 383 | | private static bool IsValidInputField(FieldInfo field) => |
| 7 | 384 | | field.IsDefined(typeof(TemplInputAttribute), false) && |
| | 385 | | field.FieldType.IsSubclassOf(typeof(UnityObject)); |
| | 386 | | } |
| | 387 | | } |