using System.Diagnostics; using System.Reflection; namespace Ghost.Editor.Core.AssetHandle; public static partial class AssetDatabase { private static readonly Dictionary> _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() != null && m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(string)); foreach (var method in methods) { var attr = method.GetCustomAttribute()!; var del = (Action)Delegate.CreateDelegate(typeof(Action), 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 }); } } }