Example #1
0
        public static void DiscoverPlugins()
        {
            foreach (var file in Directory.GetFiles(PluginsDirectory, $"{CurrentProcessId}-*.tmp", SearchOption.TopDirectoryOnly).ToList())
            {
                try
                {
                    var assembly = Assembly.Load(File.ReadAllBytes(file));
                    File.Delete(file);

                    var plugins = assembly.GetTypes().Where(p => typeof(DetouriumPlugin).IsAssignableFrom(p) && p.IsClass)
                                  .Select(plugin => (DetouriumPlugin)Activator.CreateInstance(plugin));

                    if (!(plugins.Any()))
                    {
                        ConsoleLogger.LogWarning(Globalization <DetouriumPlugin> .PluginInterfaceNotFound());
                    }

                    foreach (var plugin in plugins)
                    {
                        try
                        {
                            if (!plugin.Configuration.PluginEnabled)
                            {
                                ConsoleLogger.LogWarning(Globalization <DetouriumPlugin> .PluginDisabledMessage(plugin));
                                continue;
                            }

                            var duplicate = PluginsLoaded.Find(p => p.PluginName == plugin.PluginName);

                            if (duplicate != null)
                            {
                                if (!duplicate.Configuration.PluginEnabled)
                                {
                                    ConsoleLogger.LogWarning(Globalization <DetouriumPlugin> .PluginDisabledMessage(duplicate));
                                    continue;
                                }

                                if (duplicate.Configuration.EnforceVersionPriority && plugin.PluginVersion < duplicate.PluginVersion)
                                {
                                    ConsoleLogger.LogWarning(Globalization <DetouriumPlugin> .PluginVersionPriorityMessage(plugin, duplicate));

                                    continue;
                                }

                                ConsoleLogger.LogDebug(Globalization <DetouriumPlugin> .PluginUnloadedMessage(duplicate));

                                PluginsLoaded.Remove(duplicate);
                            }

                            ConsoleLogger.LogDebug(Globalization <DetouriumPlugin> .PluginLoadedMessage(plugin));

                            if (PluginsLoaded.Count == 0 && !plugin.Configuration.DisplayConsole)
                            {
                                NativeMethods.FreeConsole();
                            }

                            PluginsLoaded.Add(plugin);
                            plugin.OnInstalled();
                        }
                        catch (Exception exception)
                        {
                            ConsoleLogger.LogError($"[Runtime] An unhandled exception occurred while loading plugin(s).", exception);
                        }
                    }
                }
                catch (ReflectionTypeLoadException exception)
                {
                    ConsoleLogger.LogError($"[Runtime] An unhandled reflection exception occurred while loading plugin(s).", exception);
                }
                catch (Exception exception)
                {
                    ConsoleLogger.LogError($"[Runtime] An unhandled exception occurred while loading plugin(s).", exception);
                }
            }
        }