Nougat Documentation


V - Library Plug-ins






5.1 What is a Library Plug-in?


Library plug-ins are Dynamic Link Library (DLL) files that are containing organized functions and are dynamically loaded on Nougat runtime. These libraries are organized containing a function that has something to do with a specific specialization (e.g.: networking, I/O, arrays, etc.)

A Nougat programmer can customize and create a library plug-in using C# (will be discussed on next sections). Nougat was created without an intention of making its library (or API) collection, however, it has one, the IO library (see section 5.4).



5.2 Creating New Library Plug-in


Library plug-ins can be created in C#. Here is an example of library plug-in written in C#, a function named "greet()" from class named "Plugin" that prints the good old "Hello, world!" greetings.



using Nougat;
using System;

namespace Nougat {
    public class Plugin {
        public static object greet() {
            Console.WriteLine("Hello, world!");
        }
    }
}


As shown above, every function to be exported within the plug-in must be of static object type (even the parameters) and the namespace could be anything you want. After compiling and producing a DLL file out from the code above, move the DLL file into the bin/plug-ins folder in the Nougat installation.

After moving the DLL file, you can now use the plug-in you've created.



fun main() {
    Plugin.greet();
}


5.3 Returning Arrays and Maps



In a nutshell, the equivalent type of Nougat's arrays in C# is just an object[], while maps are System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<object, object>>.



using Nougat;
using System.Collections.Generic;

namespace ExamplePlugin {
    public class Plugin {
        public static object[] greetings() {
            return new string[] { "Hello!", "Howdy!", "Hola!" };
        }

        public static List<KeyValuePair<object, object>> numbers() {
            List<KeyValuePair<object, object>> nmbrs = new List<KeyValuePair<object, object>>();
            nmbrs.Add(new KeyValuePair<object, object>((double) 1, "One"));
            nmbrs.Add(new KeyValuePair<object, object>((double) 2, "Two"));
            nmbrs.Add(new KeyValuePair<object, object>((double) 3, "Three"));

            return nmbrs;
        }
    }
}


Additionally, it is important to cast all the number-like types into double types, for it is the default type of numbers in Nougat.

If no errors encountered and was compiled successfully, the plug-in can now be used as shown in the example below.



fun main() {
    iter(greeting of Plugin.greetings())
        render greeting + "\n";
            
    var dict = Plugin.numbers();
    render "\nNumber 2 is \"" + dict[2] + "\"";
}


5.4 Using a Proxy Bridge For Callbacks


Callbacks in programming are commonly functions that can be passed as an argument to another code and expected to be called back as an argument at a certain situation.

In order to create a callback argument within a function in a plug-in, the Nougat-Bridge.dll (located at the bin folder in the Nougat installation) must be referenced to the plug-in. However, it is important to use the namespace of the referenced plug-in, which is the Nougat.

Now, the object parameter that would be treated as callback must be converted into type Nougat.FunctionCallback, it contains a method .Call(System.Collections.Generic.List<object>) of void type. This function will make a callback with the list values as parameters.



using Nougat;

namespace ExamplePlugin {
    public class Plugin {
        public static object callbackExample(object Callback) {
            (Callback as FunctionCallback).Call(new List<object>() {
                "Hello from proxy test."
            });

            return null;
        }
    }
}


After successful compilation, the plug-in (the DLL output file) must be moved to the bin\plug-ins folder.



fun main() {
    Plugin.callbackExample(fun(evt) {
        render "The message is \"" + evt + "\"";
    });
}