Skip to content
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
Original file line number Diff line number Diff line change
@@ -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<Type> m_CachedImporters;

public AutoCustomTsxImporterAttribute()
{
Order = 0;
}

public AutoCustomTsxImporterAttribute(int order)
{
Order = order;
}

public int Order { get; private set; }

public static List<Type> GetOrderedAutoImportersTypes()
{
if (m_CachedImporters != null)
{
return m_CachedImporters;
}

var importers = from t in AppDomain.CurrentDomain.GetAllDerivedTypes<CustomTsxImporter>()
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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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<Type> m_CachedImporters;

public AutoCustomWorldImporterAttribute()
{
Order = 0;
}

public AutoCustomWorldImporterAttribute(int order)
{
Order = order;
}

public int Order { get; private set; }

public static List<Type> GetOrderedAutoImportersTypes()
{
if (m_CachedImporters != null)
{
return m_CachedImporters;
}

var importers = from t in AppDomain.CurrentDomain.GetAllDerivedTypes<CustomWorldImporter>()
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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Xml.Linq;
using System;
using System.Xml.Linq;
using UnityEditor.AssetImporters;
using UnityEngine;
using UnityEngine.Assertions;
Expand All @@ -23,6 +24,7 @@ protected override void InternalOnImportAsset()

AddSuperAsset<SuperAssetTileset>();
ImportTsxFile();
DoCustomImporting();
}

private void ImportTsxFile()
Expand Down Expand Up @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -61,6 +63,7 @@ protected override void InternalOnImportAsset()
SuperImportContext.SetMainObject(goWorld);

var superWorld = goWorld.AddComponent<SuperWorld>();
m_WorldComponent = superWorld;

try
{
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
}