diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomTsxImporterAttribute.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomTsxImporterAttribute.cs new file mode 100644 index 00000000..932d3f89 --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomTsxImporterAttribute.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace SuperTiled2Unity.Editor +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class AutoCustomTsxImporterAttribute : Attribute + { + private static List m_CachedImporters; + + public AutoCustomTsxImporterAttribute() + { + Order = 0; + } + + public AutoCustomTsxImporterAttribute(int order) + { + Order = order; + } + + public int Order { get; private set; } + + public static List GetOrderedAutoImportersTypes() + { + if (m_CachedImporters != null) + { + return m_CachedImporters; + } + + var importers = from t in AppDomain.CurrentDomain.GetAllDerivedTypes() + where !t.IsAbstract + from attr in GetCustomAttributes(t, typeof(AutoCustomTsxImporterAttribute)) + let auto = attr as AutoCustomTsxImporterAttribute + orderby auto.Order + select t; + + m_CachedImporters = importers.ToList(); + return m_CachedImporters; + } + } +} diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomTsxImporterAttribute.cs.meta b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomTsxImporterAttribute.cs.meta new file mode 100644 index 00000000..7249957d --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomTsxImporterAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3db579a4676a43159fe4b27be21c2f58 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomWorldImporterAttribute.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomWorldImporterAttribute.cs new file mode 100644 index 00000000..1ddb79a9 --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomWorldImporterAttribute.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace SuperTiled2Unity.Editor +{ + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class AutoCustomWorldImporterAttribute : Attribute + { + private static List m_CachedImporters; + + public AutoCustomWorldImporterAttribute() + { + Order = 0; + } + + public AutoCustomWorldImporterAttribute(int order) + { + Order = order; + } + + public int Order { get; private set; } + + public static List GetOrderedAutoImportersTypes() + { + if (m_CachedImporters != null) + { + return m_CachedImporters; + } + + var importers = from t in AppDomain.CurrentDomain.GetAllDerivedTypes() + where !t.IsAbstract + from attr in GetCustomAttributes(t, typeof(AutoCustomWorldImporterAttribute)) + let auto = attr as AutoCustomWorldImporterAttribute + orderby auto.Order + select t; + + m_CachedImporters = importers.ToList(); + return m_CachedImporters; + } + } +} diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomWorldImporterAttribute.cs.meta b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomWorldImporterAttribute.cs.meta new file mode 100644 index 00000000..9314983e --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Attributes/AutoCustomWorldImporterAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 10f6fd41fda444f5a02139c8e7d21431 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomTsxImporter.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomTsxImporter.cs new file mode 100644 index 00000000..2e8b5277 --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomTsxImporter.cs @@ -0,0 +1,14 @@ +namespace SuperTiled2Unity.Editor +{ + public class TsxAssetImportedArgs + { + public TsxAssetImporter AssetImporter { get; set; } + public SuperTileset ImportedTileset { get; set; } + }; + + public abstract class CustomTsxImporter + { + // Invoked when a Tsx asset import is completed (the prefab and all other objects associated with the asset have been constructed) + public abstract void TsxAssetImported(TsxAssetImportedArgs args); + } +} diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomTsxImporter.cs.meta b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomTsxImporter.cs.meta new file mode 100644 index 00000000..97cfb4aa --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomTsxImporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 19774f8762cc4fe3a326a394e4ebe1f7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomWorldImporter.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomWorldImporter.cs new file mode 100644 index 00000000..f4b21617 --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomWorldImporter.cs @@ -0,0 +1,14 @@ +namespace SuperTiled2Unity.Editor +{ + public class WorldAssetImportedArgs + { + public WorldAssetImporter AssetImporter { get; set; } + public SuperWorld ImportedSuperWorld { get; set; } + }; + + public abstract class CustomWorldImporter + { + // Invoked when a World asset import is completed (the prefab and all other objects associated with the asset have been constructed) + public abstract void WorldAssetImported(WorldAssetImportedArgs args); + } +} diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomWorldImporter.cs.meta b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomWorldImporter.cs.meta new file mode 100644 index 00000000..1db65116 --- /dev/null +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/CustomImporters/CustomWorldImporter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: abcdac119c3b4bbca93978b789263d9b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/TsxAssetImporter.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/TsxAssetImporter.cs index e9bec06f..e18be58c 100644 --- a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/TsxAssetImporter.cs +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/TsxAssetImporter.cs @@ -1,4 +1,5 @@ -using System.Xml.Linq; +using System; +using System.Xml.Linq; using UnityEditor.AssetImporters; using UnityEngine; using UnityEngine.Assertions; @@ -23,6 +24,7 @@ protected override void InternalOnImportAsset() AddSuperAsset(); ImportTsxFile(); + DoCustomImporting(); } private void ImportTsxFile() @@ -54,5 +56,46 @@ private void CreateTileset(XElement xTileset) loader.ColliderType = m_ColliderType; loader.LoadFromXml(xTileset); } + + private void DoCustomImporting() + { + foreach (var type in AutoCustomTsxImporterAttribute.GetOrderedAutoImportersTypes()) + { + RunCustomImporterType(type); + } + } + + private void RunCustomImporterType(Type type) + { + CustomTsxImporter customImporter; + try + { + customImporter = Activator.CreateInstance(type) as CustomTsxImporter; + } + catch (Exception e) + { + ReportGenericError($"Error creating custom importer class. Message = '{e.Message}'\n{e.StackTrace}"); + return; + } + + try + { + var args = new TsxAssetImportedArgs(); + args.AssetImporter = this; + args.ImportedTileset = Tileset; + + customImporter.TsxAssetImported(args); + } + catch (CustomImporterException cie) + { + ReportGenericError($"Custom Importer error: \n Importer: {customImporter.GetType().Name}\n Message: {cie.Message}"); + Debug.LogErrorFormat("Custom Importer ({0}) exception: {1}", customImporter.GetType().Name, cie.Message); + } + catch (Exception e) + { + ReportGenericError($"Custom importer '{customImporter.GetType().Name}' threw an exception. Message = '{e.Message}', Stack:\n{e.StackTrace}"); + Debug.LogErrorFormat("Custom importer general exception: {0}", e.Message); + } + } } } diff --git a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/WorldAssetImporter.cs b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/WorldAssetImporter.cs index 7010fc6d..aae8499b 100644 --- a/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/WorldAssetImporter.cs +++ b/SuperTiled2Unity/Packages/com.seanba.super-tiled2unity/Editor/Importers/WorldAssetImporter.cs @@ -11,6 +11,8 @@ namespace SuperTiled2Unity.Editor [ScriptedImporter(ImporterConstants.WorldVersion, ImporterConstants.WorldExtension, ImporterConstants.WorldImportOrder)] public class WorldAssetImporter : TiledAssetImporter { + private SuperWorld m_WorldComponent; + private enum WorldType { world, @@ -61,6 +63,7 @@ protected override void InternalOnImportAsset() SuperImportContext.SetMainObject(goWorld); var superWorld = goWorld.AddComponent(); + m_WorldComponent = superWorld; try { @@ -73,6 +76,8 @@ protected override void InternalOnImportAsset() // Were any import errors captured along the way? superWorld.m_ImportErrors = ImportErrors; + + DoCustomImporting(); } private void ParseJsonAsset(GameObject goWorld) @@ -148,5 +153,46 @@ private void ParsePatterns(GameObject goWorld, JsonWorld jsonWorld) } } } + + private void DoCustomImporting() + { + foreach (var type in AutoCustomWorldImporterAttribute.GetOrderedAutoImportersTypes()) + { + RunCustomImporterType(type); + } + } + + private void RunCustomImporterType(Type type) + { + CustomWorldImporter customImporter; + try + { + customImporter = Activator.CreateInstance(type) as CustomWorldImporter; + } + catch (Exception e) + { + ReportGenericError($"Error creating custom importer class. Message = '{e.Message}'\n{e.StackTrace}"); + return; + } + + try + { + var args = new WorldAssetImportedArgs(); + args.AssetImporter = this; + args.ImportedSuperWorld = m_WorldComponent; + + customImporter.WorldAssetImported(args); + } + catch (CustomImporterException cie) + { + ReportGenericError($"Custom Importer error: \n Importer: {customImporter.GetType().Name}\n Message: {cie.Message}"); + Debug.LogErrorFormat("Custom Importer ({0}) exception: {1}", customImporter.GetType().Name, cie.Message); + } + catch (Exception e) + { + ReportGenericError($"Custom importer '{customImporter.GetType().Name}' threw an exception. Message = '{e.Message}', Stack:\n{e.StackTrace}"); + Debug.LogErrorFormat("Custom importer general exception: {0}", e.Message); + } + } } }