Factory for creating plugins of various types.
Exemplo n.º 1
0
        /// <summary>
        ///     Loads the writers found by the plugin factory.
        /// </summary>
        /// <param name="settings">The settings to load writers from.</param>
        /// <param name="pluginFactory">The server's plugin factory.</param>
        internal void LoadWriters(ServerSpawnData.LoggingSettings settings, PluginFactory pluginFactory)
        {
            if (logWriters != null)
            {
                throw new InvalidOperationException("Cannot load writers if writers are already present. This suggests that writers have already been loaded into the server.\n\nThis is likely an internal DR issue, please consider creating an issue here: https://github.com/DarkRiftNetworking/DarkRift/issues");
            }

            List <LogWriter> traceWriters   = new List <LogWriter>();
            List <LogWriter> infoWriters    = new List <LogWriter>();
            List <LogWriter> warningWriters = new List <LogWriter>();
            List <LogWriter> errorWriters   = new List <LogWriter>();
            List <LogWriter> fatalWriters   = new List <LogWriter>();

            logWriters = new LogWriter[settings.LogWriters.Count];

            for (int i = 0; i < settings.LogWriters.Count; i++)
            {
                ServerSpawnData.LoggingSettings.LogWriterSettings s = settings.LogWriters[i];

                //Create a load data object and backup
                LogWriterLoadData loadData       = new LogWriterLoadData(s.Name, server, s.Settings, GetLoggerFor(nameof(s.Name)));
                PluginLoadData    backupLoadData = new PluginLoadData(s.Name, server, s.Settings, GetLoggerFor(nameof(s.Name)),
#if PRO
                                                                      null,
#endif
                                                                      null);

                LogWriter writer = pluginFactory.Create <LogWriter>(s.Type, loadData, backupLoadData);

                logWriters[i] = writer;

                if (s.LogLevels.Contains(LogType.Trace))
                {
                    traceWriters.Add(writer);
                }
                if (s.LogLevels.Contains(LogType.Info))
                {
                    infoWriters.Add(writer);
                }
                if (s.LogLevels.Contains(LogType.Warning))
                {
                    warningWriters.Add(writer);
                }
                if (s.LogLevels.Contains(LogType.Error))
                {
                    errorWriters.Add(writer);
                }
                if (s.LogLevels.Contains(LogType.Fatal))
                {
                    fatalWriters.Add(writer);
                }
            }

            writerMatrix = new LogWriter[][]
            {
                traceWriters.ToArray(),
                    infoWriters.ToArray(),
                    warningWriters.ToArray(),
                    errorWriters.ToArray(),
                    fatalWriters.ToArray()
            };
        }