Skip to content

Commit

Permalink
made the error/warning import ui available to unity versions older th…
Browse files Browse the repository at this point in the history
…an 2022.2. saves data in the library folder
  • Loading branch information
Cammin committed Aug 17, 2023
1 parent 1a50fa4 commit 393f83c
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

#if UNITY_2020_2_OR_NEWER
Expand All @@ -16,6 +18,9 @@ internal abstract class LDtkImporterEditor : ScriptedImporterEditor
protected override bool useAssetDrawPreview => false;
//protected override bool ShouldHideOpenButton() => false;

protected LDtkJsonImporter Importer;
protected ImportLogEntries Entries;

protected LDtkSectionDependencies SectionDependencies;
private SerializedProperty _reimportOnDependencyChangedProp;
private readonly GUIContent _reimportOnDependencyChanged = new GUIContent
Expand All @@ -32,6 +37,13 @@ public override void OnEnable()
SectionDependencies = new LDtkSectionDependencies(this, serializedObject);
_reimportOnDependencyChangedProp = serializedObject.FindProperty(LDtkJsonImporter.REIMPORT_ON_DEPENDENCY_CHANGE);
SectionDependencies.Init();

Importer = target as LDtkJsonImporter;

#if !UNITY_2022_2_OR_NEWER
Entries = new ImportLogEntries(Importer.assetPath);
Entries.ReadTheEntries();
#endif
}

protected override void Apply()
Expand Down Expand Up @@ -88,5 +100,56 @@ public void DrawDependenciesProperty()
{
EditorGUILayout.PropertyField(_reimportOnDependencyChangedProp, _reimportOnDependencyChanged);
}

public void DrawLogEntries()
{
if (Entries == null)
{
return;
}

List<ImportLogEntry> entries = Entries._entries;

if (entries.IsNullOrEmpty())
{
return;
}

ImportLogEntry[] errors = entries.Where(p => p._flag == ImportLogFlags.Error).ToArray();
ImportLogEntry[] warnings = entries.Where(p => p._flag == ImportLogFlags.Warning).ToArray();

MessageType msgType = errors.IsNullOrEmpty() ? MessageType.Warning : MessageType.Error;

string msg;
if (!errors.IsNullOrEmpty() && !warnings.IsNullOrEmpty())
{
msg = $"Last import generated {errors.Length} errors / {warnings.Length} warnings.";
}
else if (!errors.IsNullOrEmpty())
{
msg = $"Last import generated {errors.Length} errors";
}
else
{
msg = $"Last import generated {warnings.Length} warnings";
}

Rect rect = EditorGUILayout.GetControlRect(false, 40f);
rect.xMin -= 15;

EditorGUI.HelpBox(rect, msg, msgType);

rect.x += rect.width - 97;
rect.y += 12;
rect.size = new Vector2(93, 18);

if (GUI.Button(rect, "Print to console", EditorStyles.miniButtonRight))
{
foreach (ImportLogEntry entry in entries)
{
entry.PrintToConsole(target);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public override void OnInspectorGUI()

try
{
DrawLogEntries();
TryDrawProjectReferenceButton();
DrawDependenciesProperty();
SectionDependencies.Draw();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ private void GUIUpdate()
{
return;
}

DrawLogEntries();

TryReconstructCache();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public override void OnInspectorGUI()
return;
}

DrawLogEntries();

try
{
TryDrawProjectReferenceButton();
Expand Down
17 changes: 15 additions & 2 deletions Assets/LDtkUnity/Editor/ScriptedImporter/LDtkJsonImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,21 @@ public sealed override void OnImportAsset(AssetImportContext ctx)
{
LDtkDebug.Log($"OnImportAsset {GetType().Name} {assetPath}");
}

MainImport();

try
{
MainImport();
}
catch (Exception e)
{
Logger.LogError(e.ToString());
}
finally
{
#if !UNITY_2022_2_OR_NEWER
Logger._entries.WriteTheEntries();
#endif
}

//serialize dependencies to display them in the inspector for easier dependency tracking.
Profiler.BeginSample("SerializeStringDependencies");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ public Sprite GetBackgroundArtifact(Level level)
{
return asset;
}

Logger.LogError($"Tried retrieving a background from the importer's artifacts, but was null: \"{assetName}\"");
return asset;
}
Expand Down
5 changes: 5 additions & 0 deletions Assets/LDtkUnity/Runtime/Tools/LDtkDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static void Assert(bool condition, string msg = "Assertion failed", Objec

private static bool ShouldBlock(string msg)
{
#if UNITY_EDITOR
if (!_dueToResetThisFrame)
{
EditorApplication.delayCall += Flush;
Expand All @@ -71,6 +72,10 @@ void Flush()

Messages[msg]++;
return false;
#else
//in builds, we always want to log everything
return false;
#endif
}


Expand Down
127 changes: 123 additions & 4 deletions Assets/LDtkUnity/Runtime/Tools/LDtkDebugInstance.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
Expand All @@ -14,26 +19,42 @@ internal sealed class LDtkDebugInstance
{
private readonly HashSet<string> _importMessages = new HashSet<string>();
private readonly AssetImportContext _ctx;
public readonly ImportLogEntries _entries;

public LDtkDebugInstance(AssetImportContext ctx)
{
_ctx = ctx;
_entries = new ImportLogEntries(_ctx.assetPath);
}

public void LogError(string msg, Object obj = null)
{
if (!ShouldBlockImport(msg))
if (ShouldBlockImport(msg))
{
_ctx.LogImportError(LDtkDebug.Format(msg) + '\n' + StackTraceUtility.ExtractStackTrace(), obj);
return;
}

msg = LDtkDebug.Format(msg) + '\n' + StackTraceUtility.ExtractStackTrace();
_ctx.LogImportError(msg, obj);

#if !UNITY_2022_2_OR_NEWER
_entries.Log(msg, ImportLogFlags.Error);
#endif
}

public void LogWarning(string msg, Object obj = null)
{
if (!ShouldBlockImport(msg))
if (ShouldBlockImport(msg))
{
_ctx.LogImportWarning(LDtkDebug.Format(msg) + '\n' + StackTraceUtility.ExtractStackTrace(), obj);
return;
}

msg = LDtkDebug.Format(msg) + '\n' + StackTraceUtility.ExtractStackTrace();
_ctx.LogImportWarning(msg, obj);

#if !UNITY_2022_2_OR_NEWER
_entries.Log(msg, ImportLogFlags.Warning);
#endif
}

private bool ShouldBlockImport(string msg)
Expand All @@ -47,5 +68,103 @@ private bool ShouldBlockImport(string msg)
return false;
}
}

[Serializable]
internal sealed class ImportLogEntries
{
public List<ImportLogEntry> _entries = new List<ImportLogEntry>();
[NonSerialized] public string AssetPath;

public ImportLogEntries(string assetPath)
{
AssetPath = assetPath;
}

public void Log(string msg, ImportLogFlags flag)
{
_entries.Add(new ImportLogEntry()
{
_message = msg,
_flag = flag
});
}

[UsedImplicitly]
public void WriteTheEntries()
{
string dir = Dir();
string path = FilePath();

Directory.CreateDirectory(dir);
if (_entries.IsNullOrEmpty())
{
File.WriteAllText(path, string.Empty);
return;
}

string json = JsonUtility.ToJson(this);
File.WriteAllText(path, json);
}

[UsedImplicitly]
public void ReadTheEntries()
{
string dir = Dir();
string path = FilePath();

if (!Directory.Exists(dir))
{
return;
}
if (!File.Exists(path))
{
return;
}

string json = File.ReadAllText(path);
if (json.IsNullOrEmpty())
{
return;
}
_entries = JsonUtility.FromJson<ImportLogEntries>(json)._entries;
}

private string Dir()
{
return Path.GetFullPath(Path.Combine(Application.dataPath, "..", "Library", "LDtkImportLogs"));
}
private string FilePath()
{
string guid = AssetDatabase.AssetPathToGUID(AssetPath);
return Path.Combine(Dir(), $"{Path.GetFileNameWithoutExtension(AssetPath)}_{guid}.txt");
}
}
[Serializable]
public struct ImportLogEntry
{
public string _message;
public ImportLogFlags _flag;

public void PrintToConsole(Object ctx = null)
{
switch (_flag)
{
case ImportLogFlags.Warning:
Debug.LogWarning(_message, ctx);
break;
case ImportLogFlags.Error:
Debug.LogError(_message, ctx);
break;
default:
break;
}
}
}

public enum ImportLogFlags
{
Warning = 0,
Error = 1,
}
}
#endif
Loading

0 comments on commit 393f83c

Please sign in to comment.