Skip to content

Commit

Permalink
feat: added TileSetDefinition PreProcessor & added missing PreProcess…
Browse files Browse the repository at this point in the history
… invocations
  • Loading branch information
leohilbert committed May 23, 2024
1 parent eb90ce4 commit e756fc9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
4 changes: 4 additions & 0 deletions Assets/LDtkUnity/Editor/CustomEditor/LDtkJsonEditorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ private void Reconstruct(LDtkProjectImporter importer, byte[] newHash)
{
LDtkProfiler.EndWriting();
}

var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessProject(preAction, fromJson, importer.AssetName, importer.assetPath);
preAction.Process();

GlobalCache[_assetPath] = new LDtkJsonEditorCacheInstance()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ private static void Reset()
_preprocessors = null;
_postprocessors = null;
}

public static void AddPreProcessProject(LDtkAssetProcessorActionCache cache, LdtkJson projectJson, string projectName) =>
AddPreprocessActions(cache, LDtkPreprocessor.METHOD_PROJECT, new object[]{projectJson, projectName});

public static void AddPreProcessLevel(LDtkAssetProcessorActionCache cache, Level levelJson, LdtkJson projectJson, string projectName) =>
AddPreprocessActions(cache, LDtkPreprocessor.METHOD_LEVEL, new object[]{levelJson, projectJson, projectName});

public static void AddPreProcessProject(LDtkAssetProcessorActionCache cache, LdtkJson projectJson, string projectName, string assetPath) =>
AddPreprocessActions(cache, LDtkPreprocessor.METHOD_PROJECT, new object[] { projectJson, projectName, assetPath });

public static void AddPreProcessLevel(LDtkAssetProcessorActionCache cache, Level levelJson, LdtkJson projectJson, string projectName, string assetPath) =>
AddPreprocessActions(cache, LDtkPreprocessor.METHOD_LEVEL, new object[] { levelJson, projectJson, projectName, assetPath });

public static void AddPreProcessTilesetDef(LDtkAssetProcessorActionCache cache, LDtkTilesetDefinitionWrapper tilesetDefinition, string assetPath) =>
AddPreprocessActions(cache, LDtkPreprocessor.METHOD_TILESETDEF, new object[] { tilesetDefinition, assetPath });

public static void AddPostProcessProject(LDtkAssetProcessorActionCache cache, LDtkJsonImporter importer, GameObject projectObj) =>
AddPostprocessAction(cache, importer, LDtkPostprocessor.METHOD_PROJECT, new object[]{projectObj});
Expand Down
24 changes: 21 additions & 3 deletions Assets/LDtkUnity/Editor/Postprocessing/LDtkPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public abstract class LDtkPreprocessor
{
internal const string METHOD_PROJECT = nameof(OnPreprocessProject);
internal const string METHOD_LEVEL = nameof(OnPreprocessLevel);

internal const string METHOD_TILESETDEF = nameof(OnPreprocessTilesetDefinition);

/// <summary>
/// Use to perform operations before the project hierarchy is created.<br/>
/// This is only called for project files, not separate level files.
Expand All @@ -19,7 +20,10 @@ public abstract class LDtkPreprocessor
/// <param name="projectName">
/// Name of the project file.
/// </param>
protected virtual void OnPreprocessProject(LdtkJson projectJson, string projectName) { }
/// <param name="assetPath">
/// Path to the project file
/// </param>
protected virtual void OnPreprocessProject(LdtkJson projectJson, string projectName, string assetPath) { }

/// <summary>
/// Use to perform operations before the level hierarchy is created.<br/>
Expand All @@ -34,8 +38,22 @@ protected virtual void OnPreprocessProject(LdtkJson projectJson, string projectN
/// <param name="projectName">
/// Name of the project file.
/// </param>
protected virtual void OnPreprocessLevel(Level level, LdtkJson projectJson, string projectName) { }
/// <param name="assetPath">
/// Path to the level file
/// </param>
protected virtual void OnPreprocessLevel(Level level, LdtkJson projectJson, string projectName, string assetPath) { }

/// <summary>
/// Use to perform operations before the tileset definition is created.<br/>
/// </summary>
/// <param name="tilesetDefinition">
/// The tileset definition json.
/// </param>
/// <param name="assetPath">
/// Path to the tileset definition file
/// </param>
protected virtual void OnPreprocessTilesetDefinition(LDtkTilesetDefinitionWrapper tilesetDefinition, string assetPath) { }

/// <summary>
/// Override the order in which preprocessors are processed.
/// All levels will only process after all projects. This is due to the way that Unity's import pipeline is structured.
Expand Down
12 changes: 8 additions & 4 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkLevelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ private bool InitializeDefinitionObjects()

private void BuildLevel()
{
var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessLevel(preAction, _levelJson, _projectJson, AssetName);
preAction.Process();

LDtkAssetProcessorActionCache assetProcess = new LDtkAssetProcessorActionCache();


Expand Down Expand Up @@ -196,6 +192,10 @@ private bool DeserializeAndAssign()
Logger.LogError(e.ToString());
return false;
}

var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessLevel(preAction, _levelJson, _projectJson, AssetName, assetPath);
preAction.Process();

return true;
}
Expand Down Expand Up @@ -224,6 +224,10 @@ private static LdtkJson GetProjectJsonData(LDtkProjectImporter importer)
return null;
}

var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessProject(preAction, json, importer.AssetName, importer.assetPath);
preAction.Process();

if (Jsons.IsNullOrEmpty())
{
//Debug.Log("Added delayCall, this should only be called once per mass-reimport instance");
Expand Down
21 changes: 8 additions & 13 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,17 @@ private static void CheckDefaultEditorBehaviour()
private bool TryGetJson(out LdtkJson json)
{
json = FromJson<LdtkJson>();
if (json != null)
if (json == null)
{
return true;
Logger.LogError("LDtk: Json import error");
return false;
}

Logger.LogError("LDtk: Json import error");
return false;
var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessProject(preAction, json, AssetName, assetPath);
preAction.Process();

return true;
}

private void CreateJsonAsset()
Expand Down Expand Up @@ -253,15 +257,6 @@ private void BufferEditorCache()

private void MainBuild(LdtkJson json)
{
LDtkProfiler.BeginSample("SetupPreprocessors");
var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessProject(preAction, json, AssetName);
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("RunPreprocessors");
preAction.Process();
LDtkProfiler.EndSample();

LDtkProfiler.BeginSample("ImportProject");
LDtkBuilderProjectFactory factory = new LDtkBuilderProjectFactory(this);
factory.Import(json);
Expand Down
13 changes: 12 additions & 1 deletion Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ private bool DeserializeAndAssign()
try
{
_definition = FromJson<LDtkTilesetDefinitionWrapper>();

var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessTilesetDef(preAction, _definition, assetPath);
preAction.Process();

_json = _definition.Def;
}
catch (Exception e)
Expand Down Expand Up @@ -740,7 +745,13 @@ private static string PathToTexture(string assetPath, TilesetDefinition def = nu
{
if (def == null)
{
def = FromJson<LDtkTilesetDefinitionWrapper>(assetPath).Def;
LDtkTilesetDefinitionWrapper wrapper = FromJson<LDtkTilesetDefinitionWrapper>(assetPath);

var preAction = new LDtkAssetProcessorActionCache();
LDtkAssetProcessorInvoker.AddPreProcessTilesetDef(preAction, wrapper, assetPath);
preAction.Process();

def = wrapper.Def;
}

if (def.IsEmbedAtlas)
Expand Down

0 comments on commit e756fc9

Please sign in to comment.