Skip to content

Commit

Permalink
changed landscape for how import logging is done. It's now properly r…
Browse files Browse the repository at this point in the history
…e-logs on repeat imports.
  • Loading branch information
Cammin committed Aug 16, 2023
1 parent 0e1e333 commit 58b8d2b
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace LDtkUnity.Editor
{
internal sealed class LDtkProjectImporterFactory
internal sealed class LDtkBuilderProjectFactory
{
private readonly LDtkProjectImporter _importer;

public LDtkProjectImporterFactory(LDtkProjectImporter importer)
public LDtkBuilderProjectFactory(LDtkProjectImporter importer)
{
_importer = importer;
}
Expand All @@ -17,7 +17,7 @@ public void Import(LdtkJson json)

if (projectGameObject == null)
{
LDtkDebug.LogError("LDtk: Project GameObject null, not building correctly", _importer.ImportContext);
_importer.Logger.LogError("LDtk: Project GameObject null, not building correctly");
return;
}

Expand Down
11 changes: 7 additions & 4 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal abstract class LDtkJsonImporter : ScriptedImporter
public const string REIMPORT_ON_DEPENDENCY_CHANGE = nameof(_reimportOnDependencyChange);
[SerializeField] private bool _reimportOnDependencyChange = true;

public LDtkDebugInstance Logger;

public bool ReimportOnDependencyChange => _reimportOnDependencyChange;
public AssetImportContext ImportContext { get; private set; }
public string AssetName => Path.GetFileNameWithoutExtension(assetPath);
Expand All @@ -24,6 +26,7 @@ internal abstract class LDtkJsonImporter : ScriptedImporter
public sealed override void OnImportAsset(AssetImportContext ctx)
{
ImportContext = ctx;
Logger = new LDtkDebugInstance(ctx);

if (LDtkPrefs.VerboseLogging)
{
Expand Down Expand Up @@ -91,14 +94,14 @@ protected T ReadAssetText()

public TJson FromJson<TJson>()
{
return FromJson<TJson>(assetPath, ImportContext);
return FromJson<TJson>(assetPath, Logger);
}

public static TJson FromJson<TJson>(string path, AssetImportContext ctx = null)
public static TJson FromJson<TJson>(string path, LDtkDebugInstance debug = null)
{
if (!File.Exists(path))
{
LDtkDebug.LogError($"Could not find the json file to deserialize at \"{path}\"");
LDtkDebug.LogError($"Could not find the json file to deserialize at \"{path}\"", debug);
return default;
}

Expand All @@ -116,7 +119,7 @@ public static TJson FromJson<TJson>(string path, AssetImportContext ctx = null)
}
catch (Exception e)
{
LDtkDebug.LogError($"Failure to deserialize json: {e}", ctx);
LDtkDebug.LogError($"Failure to deserialize json: {e}", debug);
}
Profiler.EndSample();

Expand Down
8 changes: 4 additions & 4 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkLevelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private bool DeserializeAndAssign()
if (_projectImporter == null)
{
string levelName = _levelJson != null ? _levelJson.Identifier : "<Null>";
LDtkDebug.LogError($"Tried to build level {levelName}, but the project importer was not found");
Logger.LogError($"Tried to build level {levelName}, but the project importer was not found");
return false;
}

Expand All @@ -124,7 +124,7 @@ private bool DeserializeAndAssign()

if (_projectJson == null)
{
LDtkDebug.LogError("Tried to build level, but the project json data was null");
Logger.LogError("Tried to build level, but the project json data was null");
return false;
}

Expand All @@ -134,7 +134,7 @@ private bool DeserializeAndAssign()

if (_levelFile == null)
{
LDtkDebug.LogError("Tried to build level, but the level json ScriptableObject was null");
Logger.LogError("Tried to build level, but the level json ScriptableObject was null");
return false;
}

Expand All @@ -144,7 +144,7 @@ private bool DeserializeAndAssign()
}
catch (Exception e)
{
LDtkDebug.LogError(e.ToString());
Logger.LogError(e.ToString());
return false;
}

Expand Down
60 changes: 32 additions & 28 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected override void Import()
Profiler.BeginSample("CheckOutdatedJsonVersion");
string version = "";
LDtkJsonDigger.GetJsonVersion(assetPath, ref version);
if (!CheckOutdatedJsonVersion(version, AssetName, ImportContext))
if (!CheckOutdatedJsonVersion(version, AssetName, Logger))
{
Profiler.EndSample();
BufferEditorCache();
Expand All @@ -129,7 +129,7 @@ protected override void Import()

if (!TryGetJson(out LdtkJson json))
{
LDtkDebug.LogError("Json deserialization error. Not importing.", ImportContext);
Logger.LogError("Json deserialization error. Not importing.");
BufferEditorCache();
return;
}
Expand Down Expand Up @@ -184,7 +184,7 @@ private static void CheckDefaultEditorBehaviour()
}
}

public static bool CheckOutdatedJsonVersion(string jsonVersion, string assetName, AssetImportContext projectCtx = null)
public static bool CheckOutdatedJsonVersion(string jsonVersion, string assetName, LDtkDebugInstance projectCtx = null)
{
jsonVersion = Regex.Replace(jsonVersion, "[^0-9.]", "");
if (!Version.TryParse(jsonVersion, out Version version))
Expand All @@ -211,7 +211,7 @@ private bool TryGetJson(out LdtkJson json)
return true;
}

LDtkDebug.LogError("LDtk: Json import error", ImportContext);
Logger.LogError("LDtk: Json import error");
return false;
}

Expand Down Expand Up @@ -242,7 +242,7 @@ private void BufferEditorCache()

private void MainBuild(LdtkJson json)
{
LDtkProjectImporterFactory factory = new LDtkProjectImporterFactory(this);
LDtkBuilderProjectFactory factory = new LDtkBuilderProjectFactory(this);
factory.Import(json);
}

Expand Down Expand Up @@ -281,15 +281,15 @@ private void CreateBackgroundArtifacts(LdtkJson json)
{
if (input == null)
{
LDtkDebug.LogError("LDtk: Tried getting an asset from the build data but the array was null. Is the project asset properly saved?", ImportContext);
Logger.LogError("LDtk: Tried getting an asset from the build data but the array was null. Is the project asset properly saved?");
return default;
}

foreach (LDtkAsset<T> asset in input)
{
if (ReferenceEquals(asset, null))
{
LDtkDebug.LogError($"LDtk: A field in the build data is null.", ImportContext);
Logger.LogError($"LDtk: A field in the build data is null.");
continue;
}

Expand Down Expand Up @@ -329,14 +329,12 @@ private LDtkArtifactAssetsTileset LoadTilesetArtifacts(TilesetDefinition def)
LDtkTilesetImporter importer = LoadAndCacheTilesetImporter(def);
if (importer == null)
{
LDtkDebug.LogError($"Tried loading the tileset file, but didn't exist? At \"{assetPath}\"", ImportContext);
return null;
}

LDtkArtifactAssetsTileset artifacts = importer.LoadArtifacts(ImportContext);
if (artifacts == null)
{
LDtkDebug.LogError($"Loading artifacts didn't work for getting tileset sprite artifacts. Was the tileset file properly imported? At \"{assetPath}\"", ImportContext);
return null;
}

Expand All @@ -347,53 +345,59 @@ public Sprite GetBackgroundArtifact(Level level)
{
if (_backgroundArtifacts == null)
{
LDtkDebug.LogError("Project importer's artifact assets was null, this needs to be cached", ImportContext);
Logger.LogError("Project importer's artifact assets was null, this needs to be cached");
return null;
}

string assetName = level.Identifier;
Sprite asset = _backgroundArtifacts.GetBackgroundSlow(assetName);
if (asset != null)
{
{
return asset;
}

LDtkDebug.LogError($"Tried retrieving a background from the importer's artifacts, but was null: \"{assetName}\"", ImportContext);
Logger.LogError($"Tried retrieving a background from the importer's artifacts, but was null: \"{assetName}\"");
return asset;
}

public LDtkTilesetImporter LoadAndCacheTilesetImporter(TilesetDefinition def)
{
if (!_importersForDefs.TryGetValue(def, out LDtkTilesetImporter importer))
if (_importersForDefs.TryGetValue(def, out LDtkTilesetImporter importer))
{
string path = TilesetImporterPath(def);
importer = (LDtkTilesetImporter)GetAtPath(path);
_importersForDefs.Add(def, importer);
Debug.Log($"Cache Tileset importer {path}");
return importer;
}

string path = TilesetImporterPath(assetPath, def.Identifier);

if (!File.Exists(path))
{
Logger.LogError($"Failed to find the required tileset file at \"{path}\". Ensure that LDtk exported a tileset file through a custom command. If the command wasn't configured yet, check the project inspector for more info.");
return null;
}

importer = (LDtkTilesetImporter)GetAtPath(path);
if (importer == null)
{
string path = TilesetImporterPath(def);
_importersForDefs[def] = (LDtkTilesetImporter)GetAtPath(path);
Debug.Log($"Cache Tileset importer {path}");
Logger.LogError($"Failed to find the required tileset file importer at \"{path}\", but the file exists. The tileset file may have failed to import?");
return null;
}

return _importersForDefs[def];

_importersForDefs.Add(def, importer);
return importer;
}

public string TilesetImporterPath(TilesetDefinition def)
public static string TilesetImporterPath(string projectPath, string tilesetDefIdentifier)
{
string directoryName = Path.GetDirectoryName(assetPath);
string projectName = Path.GetFileNameWithoutExtension(assetPath);
string directoryName = Path.GetDirectoryName(projectPath);
string projectName = Path.GetFileNameWithoutExtension(projectPath);

if (directoryName == null)
{
LDtkDebug.LogError($"Issue formulating a tileset definition path; Path was invalid for: {assetPath}", ImportContext);
LDtkDebug.LogError($"Issue formulating a tileset definition path; Path was invalid for: \"{projectPath}\"");
return null;
}

return Path.Combine(directoryName, projectName, def.Identifier) + '.' + LDtkImporterConsts.TILESET_EXT;
return Path.Combine(directoryName, projectName, tilesetDefIdentifier) + '.' + LDtkImporterConsts.TILESET_EXT;
}

private void OnResetPPU()
Expand Down Expand Up @@ -438,7 +442,7 @@ public void TryCacheArtifactsAsset()
_backgroundArtifacts = AssetDatabase.LoadAssetAtPath<LDtkArtifactAssets>(assetPath);
if (_backgroundArtifacts == null)
{
LDtkDebug.LogError($"Artifacts was null during the import, this should never happen. Does the sub asset not exist for \"{assetPath}\"?", ImportContext);
Logger.LogError($"Artifacts was null during the import, this should never happen. Does the sub asset not exist for \"{assetPath}\"?");
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkTilesetImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ protected override void Import()
Texture2D outputTexture = output.texture;
if (output.sprites.IsNullOrEmpty() && outputTexture == null)
{
LDtkDebug.LogWarning("No Sprites or Texture are generated. Possibly because all assets in file are hidden or failed to generate texture.", ImportContext, this);
Logger.LogWarning("No Sprites or Texture are generated. Possibly because all assets in file are hidden or failed to generate texture.");
return;
}
if (!string.IsNullOrEmpty(output.importInspectorWarnings))
{
LDtkDebug.LogWarning(output.importInspectorWarnings, ImportContext);
Logger.LogWarning(output.importInspectorWarnings);
}
if (output.importWarnings != null)
{
foreach (var warning in output.importWarnings)
{
LDtkDebug.LogWarning(warning, ImportContext);
Logger.LogWarning(warning);
}
}
if (output.thumbNail == null)
{
LDtkDebug.LogWarning("Thumbnail generation fail", ImportContext);
Logger.LogWarning("Thumbnail generation fail");
}

outputTexture.name = AssetName;
Expand Down Expand Up @@ -337,7 +337,7 @@ private bool DeserializeAndAssign()
}
catch (Exception e)
{
LDtkDebug.LogError(e.ToString(), ImportContext);
Logger.LogError(e.ToString());
return false;
}

Expand All @@ -347,7 +347,7 @@ private bool DeserializeAndAssign()

if (_srcTextureImporter == null)
{
LDtkDebug.LogError($"Tried to build tileset {AssetName}, but the texture importer was not found. Is this tileset asset in a folder relative to the LDtk project file? Ensure that it's relativity is maintained if the project was moved also.", ImportContext);
Logger.LogError($"Tried to build tileset {AssetName}, but the texture importer was not found. Is this tileset asset in a folder relative to the LDtk project file? Ensure that it's relativity is maintained if the project was moved also.");
return false;
}

Expand All @@ -357,7 +357,7 @@ private bool DeserializeAndAssign()

if (_tilesetFile == null)
{
LDtkDebug.LogError("Tried to build tileset, but the tileset json ScriptableObject was null", ImportContext);
Logger.LogError("Tried to build tileset, but the tileset json ScriptableObject was null");
return false;
}

Expand Down Expand Up @@ -503,12 +503,14 @@ public LDtkArtifactAssetsTileset LoadArtifacts(AssetImportContext projectCtx)
{
_cachedArtifacts = AssetDatabase.LoadAssetAtPath<LDtkArtifactAssetsTileset>(assetPath);
}

//It's possible that the artifact assets don't exist, either because the texture importer failed to import, or the artifact assets weren't produced due to being an aseprite file or otherwise
if (_cachedArtifacts == null)
{
LDtkDebug.LogError($"Cached artifacts didnt load! For \"{assetPath}\"", projectCtx);

Logger.LogError($"Loading artifacts didn't work for getting tileset sprite artifacts. Was the tileset file properly imported? At \"{assetPath}\"");
return null;
}

return _cachedArtifacts;
}

Expand Down
Loading

0 comments on commit 58b8d2b

Please sign in to comment.