DarkRift2 Plugin scheduler

This short article will explain you how to schedule plugins initialization easely, even with the free version of Darkrift2. This article assumes that you have basics of networking with DarkRift2.

Prepare your plugins.

The most important thing is :

« Don’t put code in the plugin constructor, but use an Init() function instead »

By doing this, we will let DarkRift instanciate our plugins, and after that, we will initialize them with a plugin dedicated to schedule other plugins

Here is an example of a simple plugin for the purpose (FirstPlugin.cs) :

using System;
using DarkRift.Server;

namespace MateriaGame.DarkRift.Plugins
{
    public class FirstPlugin : Plugin
    {
        #region Plugin implementation
        public override bool ThreadSafe => false;

        public override Version Version => new Version(1, 0, 0);

        public FirstPlugin(PluginLoadData pluginLoadData) : base(pluginLoadData)
        {
            //Do nothing here
            //Eventually, store pluginLoadData data if needed
        }
        #endregion

        #region Implementation

        public void Init()
        {
            Console.WriteLine("FirstPlugin plugin initialization");
        }

        #endregion
    }
}

I have made 5 plugins with the same code to test our scheduler. (Of course, i changed their names :

Create the Scheduler

The scheduler task is very simpe :

  • Wait for DarkRift to load all plugins
  • Initialize manually all plugins with the desired sorting

For that, we’ll need to call an async task wich enable the constructor to finish it’s own execution. We need to do this because DarkRift2 Plugin Manager doesn’t allow us to find plugins during Plugin instantiation step (Internal to DarkRift)

Here is the Scheduler.cs code :

using DarkRift.Server;
using System;
using System.Threading.Tasks;

namespace MateriaGame.DarkRift.Plugins
{
    /// <summary>
    /// Plugin wich schedule initialization of darkrift plugins
    /// </summary>
    class Scheduler : Plugin
    {
        #region Plugin implementation

        public override bool ThreadSafe => false;
        public override Version Version => new Version(1, 0, 0);

        public Scheduler(PluginLoadData pluginLoadData) : base(pluginLoadData)
        {
            //Initialize some data
            //...
            Console.WriteLine("Initialize some stuff here that is needed in plugins");

            //Initialize plugins async - enable continue execution
            InitPluginsAsync(PluginManager);

            Console.WriteLine("Scheduler instanciated successfully");
        }

        /// <summary>
        /// Initialize plugins
        /// </summary>
        /// <param name="pPluginManager"></param>
        /// <returns></returns>
        private static async Task InitPluginsAsync(IPluginManager pPluginManager)
        {
            //Free execution
            await Task.Yield();

            bool arePluginsLoaded = false;
            while (!arePluginsLoaded)
            {
                try
                {
                    pPluginManager.GetPluginByType<Scheduler>();
                    arePluginsLoaded = true;
                }
                catch (Exception)
                {
                    //Console.WriteLine("Darkrift hasn't finished to load plugins");
                }
            }

            //Initialize plugins
            Console.WriteLine("Scheduler initialization start");
            ((FirstPlugin)pPluginManager.GetPluginByType<FirstPlugin>()).Init();
            ((SecondPlugin)pPluginManager.GetPluginByType<SecondPlugin>()).Init();
            ((ThirdPlugin)pPluginManager.GetPluginByType<ThirdPlugin>()).Init();
            ((FourthPlugin)pPluginManager.GetPluginByType<FourthPlugin>()).Init();
            ((FifthPlugin)pPluginManager.GetPluginByType<FifthPlugin>()).Init();
            Console.WriteLine("Scheduler initialization finished");
        }

        #endregion
    }
}

You can now build your plugins and add them into the Plugins directory of DarkRift. Let’s start the server and here is the result you should have if you’ve followed the tutorial correctly :

You can find the solution in my official github right here.

Don’t forget to follow me on twitter , i’ll really appreciate the help ! I just want to provide a lots of articles like that, but need your support.

Thanks again,
David.

Posted in Dark Rift 2 and tagged .

Leave a Reply

Your email address will not be published. Required fields are marked *