49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System.Collections.Immutable;
|
|
using NLog;
|
|
|
|
namespace CringePlugins.Loader;
|
|
|
|
public class PluginsLifetime
|
|
{
|
|
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly ImmutableArray<PluginInstance> _plugins;
|
|
|
|
public PluginsLifetime()
|
|
{
|
|
var dir = Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CringeLauncher", "plugins"));
|
|
|
|
var plugins = ImmutableArray<PluginInstance>.Empty.ToBuilder();
|
|
|
|
foreach (var directory in dir.EnumerateDirectories())
|
|
{
|
|
var files = directory.GetFiles("*.dll");
|
|
|
|
if (files.Length != 1) continue;
|
|
|
|
try
|
|
{
|
|
plugins.Add(new PluginInstance(files[0].FullName));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e, "Failed to load plugin {PluginPath}", files[0].FullName);
|
|
}
|
|
}
|
|
|
|
_plugins = plugins.ToImmutable();
|
|
|
|
foreach (var instance in _plugins)
|
|
{
|
|
try
|
|
{
|
|
instance.Instantiate();
|
|
instance.RegisterLifetime();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e, "Failed to instantiate plugin {Plugin}", instance.Metadata);
|
|
}
|
|
}
|
|
}
|
|
} |