LuaInterface – Lua in .NET – Now on NuGet

I found myself looking at Lua today and was disappointed there was no NuGet package. I did find a project which acts as a gateway between the .NET CLR and Lua called LuaInterface. I created a quick project to see how easy it was to leverage Lua in a .NET project. I was shocked at how easy it actually was which made me even more disappointed to know there was no NuGet presence!

I packaged up the DLLs for Lua 5.1 and LuaInterface 2 with a couple little tidbits (for now) to modernize (a bit) the use of Lua and published the package to the public NuGet feed. Most examples I found on the web were old, weak on OO and did not make use of modern language features.

I created a custom Attribute – LuaAccessible – which acts as a marker for methods you wish to make available for use within the Lua environment. It contains no code or properties of any kind though someone could subclass it and the Register method below should still work.

I also created a short extension method – RegisterMarkedMethodsOf – to the Lua object which takes an object parameter to scan for methods marked with the custom Attribute and registers them with that Lua instance using the method name.

Grab the NuGet package and give it a shot. Three DLLs will be added to your .NET 3.5 or above project – the Lua library, LuaInterface module and the tidbits I mentioned. Embedding a script language into your next .NET Project just got crazy easy.

See below for a short code piece I used to test the library.

Crazy easy,
-Erik

using System;
using LuaInterface;

namespace LuaPlayground
{
   class Program
   {
       static void Main(string[] args)
       {
           Program p = new Program();
           p.Run();
       }

       public void Run()
       {
           using (Lua luaVM = new Lua())
           {
               luaVM.RegisterMarkedMethodsOf(this);

               luaVM.DoString("res = f()");
               luaVM.DoString(@"io.write(res,'\n')");

               Console.Read();
           }
       }

       [LuaAccessible]
       public int f()
       {
           Console.WriteLine("Hello there!");
           return 8;
       }
   }
}

Output:
Hello there!
8
Advertisements

2 responses to “LuaInterface – Lua in .NET – Now on NuGet

  1. Hi, thank you very much for this package. Sadly enough, it’s only for 32-Bit-Environments. Any chances of getting a 64-Bit-Upgrade?

    • Hi Oliver,
      I took a look at this recently but I was unable to package up a 64-bit version at the time because the key used to sign the 32-bit build wasn’t available in order to build the source for 64-bit. I’ll take a look and see if there is a full 64-bit signed version now available.

      Or maybe the strong name isn’t a priority?

      If you have any info, it’d be helpful. I just haven’t had enough time to really revisit this or maintain it.

      -Erik

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s