Skip to content

TileSetDefinition PreProcessor & added missing PreProcess invocations #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -127,10 +127,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 @@ -190,6 +186,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 @@ -218,6 +218,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 @@ -223,13 +223,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 @@ -306,15 +310,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 @@ -304,6 +304,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 @@ -494,7 +499,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