コード例 #1
0
        /// <summary>
        /// Loads the enabled plugins.
        /// </summary>
        public static void Refresh()
        {
            if (IsLocked)
            {
                throw new NotSupportedException();
            }

            var files = Directory.GetFiles(SMPath.Plugins, "*.dll");

            foreach (var file in files)
            {
                var name = Path.GetFileNameWithoutExtension(file);
                loadQueue.Enqueue(NameToKey(name));
            }

            while (loadQueue.Count > 0)
            {
                Load(loadQueue.Dequeue(), true);
            }

            if (!ConVarManager.ConfigsLoaded)
            {
                ConVarManager.ExecuteConfigs();
                foreach (var k in Plugins.Keys.ToArray())
                {
                    if (Plugins.TryGetValue(k, out var plugin) && plugin.LoadStatus == PluginContainer.Status.Loaded)
                    {
                        try
                        {
                            plugin.Plugin.OnConfigsExecuted();
                        }
                        catch (HaltPluginException)
                        {
                        }
                        catch (Exception e)
                        {
                            plugin.SetFailState(e);
                        }
                    }
                }
            }

            AdminManager.ReloadAdmins();
        }
コード例 #2
0
        /// <summary>
        /// Loads a plugin.
        /// </summary>
        /// <param name="key">The dictionary key of the plugin.</param>
        /// <param name="refreshing">A value indicating whether the plugin list is being refreshed.</param>
        private static void Load(string key, bool refreshing)
        {
            if (IsLocked)
            {
                throw new NotSupportedException();
            }

            if (Plugins.ContainsKey(key) && Plugins[key].LoadStatus == PluginContainer.Status.Loaded)
            {
                return;
            }

            var file = $"{SMPath.Plugins}{key}.dll";

            if (!File.Exists(file))
            {
                return;
            }

            try
            {
                var dll       = Assembly.Load(File.ReadAllBytes(file));
                var type      = dll.GetType($"SevenMod.Plugin.{key}.{key}", true, true);
                var container = new PluginContainer(Path.GetFileName(file));
                if (type.IsSubclassOf(PluginParentType))
                {
                    try
                    {
                        var plugin = Activator.CreateInstance(type) as PluginAbstract;
                        plugin.Container = container;
                        plugin.OnLoadPlugin();
                        if (API.IsGameAwake)
                        {
                            plugin.OnGameAwake();
                        }

                        if (API.IsGameStartDone)
                        {
                            plugin.OnGameStartDone();
                        }

                        if (ConVarManager.ConfigsLoaded)
                        {
                            ConVarManager.ExecuteConfigs(plugin);
                            plugin.OnConfigsExecuted();
                        }

                        if (!refreshing)
                        {
                            AdminManager.ReloadAdmins();
                        }

                        container.Plugin     = plugin;
                        container.PluginInfo = plugin.Info;
                        container.LoadStatus = PluginContainer.Status.Loaded;
                    }
                    catch (HaltPluginException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        container.LoadStatus = PluginContainer.Status.Error;
                        container.Error      = e.Message;
                        throw;
                    }

                    Plugins[key] = container;
                }
                else
                {
                    throw new Exception($"{type.Name} does not inherit from {PluginParentType.Name}");
                }
            }
            catch (Exception e)
            {
                SMLog.Error($"Failed loading plugin {key}.dll: {e.Message}");
                SMLog.Error(e);
            }
        }