2025-03-22 20:12:14 +00:00
|
|
|
using AIStudio.Settings;
|
|
|
|
|
|
|
|
namespace AIStudio.Tools.PluginSystem;
|
|
|
|
|
2025-03-29 17:40:17 +00:00
|
|
|
public static partial class PluginFactory
|
2025-03-22 20:12:14 +00:00
|
|
|
{
|
2025-04-12 19:13:33 +00:00
|
|
|
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(nameof(PluginFactory));
|
|
|
|
private static readonly SettingsManager SETTINGS_MANAGER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
|
2025-06-01 19:14:21 +00:00
|
|
|
private static readonly SettingsLocker SETTINGS_LOCKER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsLocker>();
|
2025-04-23 12:07:22 +00:00
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
private static bool IS_INITIALIZED;
|
|
|
|
private static string DATA_DIR = string.Empty;
|
|
|
|
private static string PLUGINS_ROOT = string.Empty;
|
|
|
|
private static string INTERNAL_PLUGINS_ROOT = string.Empty;
|
2025-06-01 19:14:21 +00:00
|
|
|
private static string CONFIGURATION_PLUGINS_ROOT = string.Empty;
|
2025-06-09 12:06:54 +00:00
|
|
|
private static string HOT_RELOAD_LOCK_FILE = string.Empty;
|
2025-04-12 19:13:33 +00:00
|
|
|
private static FileSystemWatcher HOT_RELOAD_WATCHER = null!;
|
|
|
|
private static ILanguagePlugin BASE_LANGUAGE_PLUGIN = NoPluginLanguage.INSTANCE;
|
|
|
|
|
|
|
|
public static ILanguagePlugin BaseLanguage => BASE_LANGUAGE_PLUGIN;
|
2025-04-07 17:36:24 +00:00
|
|
|
|
2025-04-12 19:13:33 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Set up the plugin factory. We will read the data directory from the settings manager.
|
|
|
|
/// Afterward, we will create the plugins directory and the internal plugin directory.
|
|
|
|
/// </summary>
|
2025-04-24 07:50:03 +00:00
|
|
|
public static bool Setup()
|
2025-04-07 17:36:24 +00:00
|
|
|
{
|
2025-04-23 12:07:22 +00:00
|
|
|
if(IS_INITIALIZED)
|
2025-04-24 07:50:03 +00:00
|
|
|
return false;
|
2025-04-23 12:07:22 +00:00
|
|
|
|
2025-06-02 18:08:25 +00:00
|
|
|
LOG.LogInformation("Initializing plugin factory...");
|
2025-04-12 19:13:33 +00:00
|
|
|
DATA_DIR = SettingsManager.DataDirectory!;
|
|
|
|
PLUGINS_ROOT = Path.Join(DATA_DIR, "plugins");
|
2025-06-09 12:06:54 +00:00
|
|
|
HOT_RELOAD_LOCK_FILE = Path.Join(PLUGINS_ROOT, ".lock");
|
2025-04-12 19:13:33 +00:00
|
|
|
INTERNAL_PLUGINS_ROOT = Path.Join(PLUGINS_ROOT, ".internal");
|
2025-06-01 19:14:21 +00:00
|
|
|
CONFIGURATION_PLUGINS_ROOT = Path.Join(PLUGINS_ROOT, ".config");
|
2025-04-12 19:13:33 +00:00
|
|
|
|
2025-04-07 17:36:24 +00:00
|
|
|
if (!Directory.Exists(PLUGINS_ROOT))
|
|
|
|
Directory.CreateDirectory(PLUGINS_ROOT);
|
|
|
|
|
|
|
|
HOT_RELOAD_WATCHER = new(PLUGINS_ROOT);
|
2025-04-12 19:13:33 +00:00
|
|
|
IS_INITIALIZED = true;
|
2025-06-02 18:08:25 +00:00
|
|
|
LOG.LogInformation("Plugin factory initialized successfully.");
|
2025-04-24 07:50:03 +00:00
|
|
|
return true;
|
2025-04-07 17:36:24 +00:00
|
|
|
}
|
2025-06-09 12:06:54 +00:00
|
|
|
|
|
|
|
private static async Task LockHotReloadAsync()
|
|
|
|
{
|
|
|
|
if (!IS_INITIALIZED)
|
|
|
|
{
|
|
|
|
LOG.LogError("PluginFactory is not initialized.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (File.Exists(HOT_RELOAD_LOCK_FILE))
|
|
|
|
{
|
|
|
|
LOG.LogWarning("Hot reload lock file already exists.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await File.WriteAllTextAsync(HOT_RELOAD_LOCK_FILE, DateTime.UtcNow.ToString("o"));
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LOG.LogError(e, "An error occurred while trying to lock hot reloading.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void UnlockHotReload()
|
|
|
|
{
|
|
|
|
if (!IS_INITIALIZED)
|
|
|
|
{
|
|
|
|
LOG.LogError("PluginFactory is not initialized.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if(File.Exists(HOT_RELOAD_LOCK_FILE))
|
|
|
|
File.Delete(HOT_RELOAD_LOCK_FILE);
|
|
|
|
else
|
|
|
|
LOG.LogWarning("Hot reload lock file does not exist. Nothing to unlock.");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LOG.LogError(e, "An error occurred while trying to unlock hot reloading.");
|
|
|
|
}
|
|
|
|
}
|
2025-03-29 17:40:17 +00:00
|
|
|
|
2025-03-30 18:34:30 +00:00
|
|
|
public static void Dispose()
|
|
|
|
{
|
2025-04-12 19:13:33 +00:00
|
|
|
if(!IS_INITIALIZED)
|
|
|
|
return;
|
|
|
|
|
2025-03-30 18:34:30 +00:00
|
|
|
HOT_RELOAD_WATCHER.Dispose();
|
|
|
|
}
|
2025-03-22 20:12:14 +00:00
|
|
|
}
|