From 6fd8a44d744eab035103ad3d0f22b0f3502bf8bc Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 16 Aug 2023 01:41:17 -0700 Subject: [PATCH] registering a dependency on tileset files --- .../Editor/ScriptedImporter/LDtkJsonDigger.cs | 33 +++++++++++++++++++ .../LDtkProjectDependencyFactory.cs | 22 +++++++++---- .../Tests/EditMode/Tests/TestJsonDigging.cs | 13 ++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonDigger.cs b/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonDigger.cs index 84118bef7..fd08fa2ae 100644 --- a/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonDigger.cs +++ b/Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonDigger.cs @@ -15,6 +15,8 @@ internal static class LDtkJsonDigger public static bool GetTilesetTextureRelPaths(string projectPath, ref HashSet result) => DigIntoJson(projectPath, GetTilesetRelPathsReader, ref result); + public static bool GetTilesetDefNames(string projectPath, ref HashSet result) => + DigIntoJson(projectPath, GetTilesetDefNamesReader, ref result); public static bool GetUsedEntities(string path, ref HashSet result) => DigIntoJson(path, GetUsedEntitiesReader, ref result); public static bool GetUsedIntGridValues(string path, ref HashSet result) => @@ -168,6 +170,37 @@ private static bool GetTilesetRelPathsReader(ref JsonReader reader, ref HashSet< } return false; } + private static bool GetTilesetDefNamesReader(ref JsonReader reader, ref HashSet textures) + { + while (reader.Read()) + { + if (!reader.ReadIsPropertyName("tilesets")) + { + continue; + } + + while (reader.CanRead()) + { + int depth = 0; + while (reader.IsInArray(ref depth)) + { + if (reader.GetCurrentJsonToken() != JsonToken.String) + { + reader.ReadNext(); + continue; + } + + if (reader.ReadString() == "identifier" && reader.ReadIsNameSeparator()) + { + string identifier = reader.ReadString(); + textures.Add(identifier); + } + } + return true; + } + } + return false; + } private static bool GetUsedEntitiesReader(ref JsonReader reader, ref HashSet entities) { diff --git a/Assets/LDtkUnity/Editor/Utility/Dependencies/LDtkProjectDependencyFactory.cs b/Assets/LDtkUnity/Editor/Utility/Dependencies/LDtkProjectDependencyFactory.cs index 7ca45c08b..e8931711f 100644 --- a/Assets/LDtkUnity/Editor/Utility/Dependencies/LDtkProjectDependencyFactory.cs +++ b/Assets/LDtkUnity/Editor/Utility/Dependencies/LDtkProjectDependencyFactory.cs @@ -16,23 +16,33 @@ public static string[] GatherProjectDependencies(string projectPath) return Array.Empty(); } - bool isExternalLevels = false; - if (!LDtkJsonDigger.GetIsExternalLevels(projectPath, ref isExternalLevels)) + string[] projectLines = LDtkDependencyUtil.LoadMetaLinesAtPath(projectPath); + if (LDtkDependencyUtil.ShouldDependOnNothing(projectLines)) { - LDtkDebug.LogError("Issue getting external levels"); return Array.Empty(); } - string[] projectLines = LDtkDependencyUtil.LoadMetaLinesAtPath(projectPath); - if (LDtkDependencyUtil.ShouldDependOnNothing(projectLines)) + bool isExternalLevels = false; + if (!LDtkJsonDigger.GetIsExternalLevels(projectPath, ref isExternalLevels)) { + LDtkDebug.LogError("Issue getting external levels"); return Array.Empty(); } HashSet paths = new HashSet(); //todo Create & depend on the tileset definition files! - + + + HashSet tilesetDefNames = new HashSet(); + LDtkJsonDigger.GetTilesetDefNames(projectPath, ref tilesetDefNames); + + foreach (string defName in tilesetDefNames) + { + string tilesetPath = LDtkProjectImporter.TilesetImporterPath(projectPath, defName); + paths.Add(tilesetPath); + } + /*HashSet texturePaths = new HashSet(); if (LDtkJsonDigger.GetTilesetTextureRelPaths(projectPath, ref texturePaths)) { diff --git a/Assets/Tests/EditMode/Tests/TestJsonDigging.cs b/Assets/Tests/EditMode/Tests/TestJsonDigging.cs index e0ddffe01..d717354a6 100644 --- a/Assets/Tests/EditMode/Tests/TestJsonDigging.cs +++ b/Assets/Tests/EditMode/Tests/TestJsonDigging.cs @@ -229,5 +229,18 @@ public static void GetTilesetRelPaths(string path) Assert.IsTrue(success, "not successful"); Debug.Log($"GetTilesetRelPaths was {result.Count}: {string.Join(", ", result)}"); } + [Test] + [TestCaseSource(nameof(Projects))] + public static void GetTilesetDefNames(string path) + { + HashSet result = new HashSet(); + + LDtkProfiler.BeginSample($"{nameof(TestJsonDigging)}/{nameof(GetTilesetDefNames)}/{path}"); + bool success = LDtkJsonDigger.GetTilesetDefNames(path, ref result); + LDtkProfiler.EndSample(); + + Assert.IsTrue(success, "not successful"); + Debug.Log($"GetTilesetDefNames was {result.Count}: {string.Join(", ", result)}"); + } } } \ No newline at end of file