forked from Misaki/GhostEngine
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
namespace Ghost.Editor.Core.AssetHandle;
|
|
|
|
public static partial class AssetDatabase
|
|
{
|
|
private static readonly Dictionary<string, Action<string>> _assetOpenHandlers = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
private static void InitializeAssetHandle()
|
|
{
|
|
var methods = AppDomain.CurrentDomain.GetAssemblies()
|
|
.SelectMany(a => a.GetTypes())
|
|
.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
|
|
.Where(m => m.GetCustomAttribute<AssetOpenHandlerAttribute>() != null &&
|
|
m.GetParameters().Length == 1 &&
|
|
m.GetParameters()[0].ParameterType == typeof(string));
|
|
|
|
foreach (var method in methods)
|
|
{
|
|
var attr = method.GetCustomAttribute<AssetOpenHandlerAttribute>()!;
|
|
var del = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), method);
|
|
foreach (var ext in attr.Extensions)
|
|
{
|
|
if (_assetOpenHandlers.ContainsKey(ext))
|
|
{
|
|
throw new InvalidOperationException($"Duplicate handler for extension '{ext}'");
|
|
}
|
|
|
|
_assetOpenHandlers[ext] = del;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void OpenAsset(string path)
|
|
{
|
|
var extension = Path.GetExtension(path);
|
|
if (_assetOpenHandlers.TryGetValue(extension, out var handler))
|
|
{
|
|
handler(path);
|
|
}
|
|
else
|
|
{
|
|
Process.Start(new ProcessStartInfo(path)
|
|
{
|
|
UseShellExecute = true
|
|
});
|
|
}
|
|
}
|
|
} |