Click or drag to resize

Server Basics

A Brief Overview

Before we jump into the art of DarkRift we should explore how DarkRift operates.

DarkRift uses plugins to implement all server side logic, you write plugins as you would develop a standard .NET library and then you just drop the generated DLL into DarkRift’s ‘Plugins’ directory. DarkRift offers a large API for managing clients and sending messages etc. and provides events that inform your plugins when a client connects, sends a message etc.

Architecture

Everything related to the server code is in the namespace DarkRift.Server, everything to do with the client in DarkRift.Client and everything used in both is just in DarkRift.

Setting up the Plugin

Create a new Class Library C# project in Visual Studio named something like “AgarPlugin” and add references to DarkRift.Server.dll and DarkRift.dll.

Rename the default class to AgarPlayerManager and rename the file similarly. Add using directives to the top of the file so that we have access to both DarkRift.Server and DarkRift namespaces.

All plugins in DarkRift must inherit from DarkRift.Server.Plugin so that DarkRift can correctly discover and instantiate them and so it can easily read details about your plugin. It is also your entry point into DarkRift’s API so that you make changes on the correct server instances.

Go ahead and make AgarPlayerManager inherit from Plugin, you’ll notice Visual Studio throws an error saying that there is no constructor so add it. You’ll then notice it says that it does not implement a number of abstract properties, once again add them. If you’re following along, you should have a class like this:

public class AgarPlayerManager : Plugin
{
    public AgarPlayerManager(PluginLoadData pluginLoadData) : base(pluginLoadData)
    {
    }

    public override bool ThreadSafe
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public override Version Version
    {
        get
        {
            throw new NotImplementedException();
        }
    }
}

Hopefully the autogenerated properties should be fairly self-explanatory. When implementing them remember:

  • Version should follow Semantic Versioning (unless you really hate best practices). It is used to trigger upgrades to plugins (we’ll cover that later) so you should give it a default value now.

  • ThreadSafe indicates whether your program can handle multithreaded events to boost performance. Since I won’t cover any multithreading in this set it to false, if you know about multithreading then it might be a nice exercise at the end to reimplement the plugin with multithreading enabled!

After some formatting, we should have something like:

C#
public class AgarPlayerManager : Plugin
{
    public override bool ThreadSafe => false;

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

    public AgarPlayerManager(PluginLoadData pluginLoadData) : base(pluginLoadData)
    {
    }
}

If you want to try it you can build the library and place the DLL in DarkRift’s plugins folder, when you start DarkRift you should see your plugin get loaded in!