Server Plugins

From Legends of Aria Admin and Modding Wiki
Revision as of 21:43, 15 August 2018 by Yorlik (talk | contribs) (Synchronous Requests)
Jump to: navigation, search

Introduction

It is possible to write plugins in C# and use these from Lua.

These plugins must implement a certain Interface to be usable.

In the moment there is no extensive written documentation, but there exists a tutorial plugin which is well commented and which can be used as a starting point.

CAVEAT: 
Server plugins are officially not supported and you can seriously shoot yourself in the foot with them. 
You need to know what you are doing to really use them.

Compiled plugins need to be placed into

\build\base\plugins

API Overview

Synchronous Requests

Server Plugins can take requests from Lua and answer them directly, like any other synchronous function. Your Lua script will wait until the call returns. The engine function for that is

Lua Code:
SendRequestToPlugin("TutorialPlugin", "GimmeFive")

Where "Tutorialpligin" is the name of the plugin as returned by the function string GetPluginName();

in the IPlugin interface you implemented and "GimmeFive" is just a piece of data.

You can add any number or type of arguments to the request after specifying the plugin name.

These arguments find their way into the IPlugin function:

object[] DirectRequestFromLua(object[] _requestData);

Your arguments end up as elements of the _requestData array. In our example the string "GimmeFive" ends up in _requestData[0]

var gimme = new object[5];
gimme[0] = 1; // These are all elements in a LuaArray once they arrive at the Lua Side
gimme[1] = 4;
gimme[2] = 9;
gimme[3] = 16;
gimme[4] = 25;
return gimme; // This is what's being returned to Lua. 
// Returned always is an array with a list of data members, where index 0 in C# translates to index 1 in Lua, since LuaTables begin with element 1.
// So - in the example Lua will receive a table: {1, 4, 9, 16, 25} as result of the function call
// Synchronous calls with "SendRequestToPlugin" always return a table if there are multiple return values.
// Return value MUST be an array of type object which gets converted into a LuaTable.
// If the returned array has only one member, a single value is returned, which is NOT wrapped into a Lua table !!!
var result = new object[1];
result0] = "I am the result";
return result;
// in this example the result returned to Lua would be the string "I am the result" and NOT a table like {"I am the result"}

Asynchronous Requests

TBD

Returning Lua Tables

If you use LuaTableProxy types to return Lua tables your plugin also must be linked against Shardsserver.exe, which means you probably have to update it a lot.

Helper Functions

TBD

IPlugin

Every plugin must implement this interface. Check out the Tutorial Plugin to see how it is done.

using System.Collections.Concurrent;
namespace Gameplay.Plugin
{
   public class LuaRequest
   {
       public string EventId;
       public object[] Data;
   }

   public class LuaResponse
   {
       public string EventId;
       public ulong DestinationObjectId;
       public object[] Data;
   }

   public interface IPlugin
   {
       void Init(ConcurrentQueue<LuaResponse> _responseQueue);
       void Start();
       bool IsReadyForServerStartup();
       void Shutdown();
       bool IsShutDown();
       void MessageFromLua(LuaRequest _request);
       object[] DirectRequestFromLua(object[] _requestData);
       string GetPluginName();
   }
}

Related Engine Functions

These are the engine functions which can be used to interact with your plugin from Lua. There is example Lua code in the Tutorial plugin you can use.

SendMessageToPlugin(...)
Description: Sends a message to a C# gameplay plugin.
Params:
   (string) - Plug-in name to send to.
   (string) - EventId name.
   (...) One or more object arguments to be passed to the plug-in.
Returns:
   None
----------------------------------
SendRequestToPlugin(...)
Description: Sends a direct request to a C# gameplay plugin.
Params:
   (string) - Plug-in name to send to.
   (...) One or more object arguments to be passed to the plug-in.
Returns:
   A single value, or an array of values.
----------------------------------
IsPluginLoaded(...)
Description: Ask if a c# gameplay plug-in has been loaded or not
Params:
   (string) - Plug-in name to send to.
Returns:
   true or false
----------------------------------

Tutorial Plugin Download Link

https://www.legendsofaria.com/downloads/u3d2bdea/LoA_TutorialPlugin.zip