Encapsulate Lua for C #

Source: Internet
Author: User

To understand how LUA can be used in our GDEX, I decided to study how to better encapsulate a lua-based APP framework in WPF.

 

Today, we first made a simple encapsulation of Lua for C.

Anyone who has used Lua in C # knows that when using C # To implement a function and bind it to LUA, The RegisterFunction method of the Lua class is required.

 

It is very useful when there are few functions, but if you need to bind hundreds of functions in C #, It is troublesome to add a function, at least two places need to be modified each time: function implementation, function binding (RegisterFunction ). In addition, if the bound name in lua is different from that in C #, it is more troublesome. You also need to maintain a function ing.

 

Today, I flipped through google and pulled out the previous article "Using Lua with C #" by a foreigner in GameDev.net. After reading this article, I did a good job. (Another day, I want to translate this article). However, his sample code is too long. Most of the Code is generating function introduction and function help documentation, which is ignored directly. Take the core of it and encapsulate it on your own. It feels good to use it.

 

The basic idea is to use the Attribute of C # to mark the function and implement automatic binding.

 

The core code is as follows (LuaFramework. cs ):

 

[C-sharp] using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;
Using System. Reflection;
Using LuaInterface;
 
Namespace WPFLuaFramework
{
/// <Summary>
/// Lua function description feature class
/// </Summary>
Public class LuaFunction: Attribute
{
Private String FunctionName;
 
Public LuaFunction (String strFuncName)
{
FunctionName = strFuncName;
}
 
Public String getFuncName ()
{
Return FunctionName;
}
}
 
/// <Summary>
/// Lua Engine
/// </Summary>
Class LuaFramework
{
Private Lua pLuaVM = new Lua (); // lua Virtual Machine
 
/// <Summary>
/// Register the lua Function
/// </Summary>
/// <Param name = "pLuaAPIClass"> lua function class </param>
Public void BindLuaApiClass (Object pLuaAPIClass)
{
Foreach (MethodInfo mInfo in pLuaAPIClass. GetType (). GetMethods ())
{
Foreach (Attribute attr in Attribute. GetCustomAttributes (mInfo ))
{
String LuaFunctionName = (attr as LuaFunction). getFuncName ();
PLuaVM. RegisterFunction (LuaFunctionName, pLuaAPIClass, mInfo );
}
}
}
 
/// <Summary>
/// Execute the lua script file
/// </Summary>
/// <Param name = "luaFileName"> script file name </param>
Public void ExecuteFile (string luaFileName)
{
Try
{
PLuaVM. DoFile (luaFileName );
}
Catch (Exception e)
{
MessageBox. Show (e. ToString ());
}
}
 
/// <Summary>
/// Execute the lua script
/// </Summary>
/// <Param name = "luaCommand"> lua command </param>
Public void ExecuteString (string luaCommand)
{
Try
{
PLuaVM. DoString (luaCommand );
}
Catch (Exception e)
{
MessageBox. Show (e. ToString ());
}
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;
Using System. Reflection;
Using LuaInterface;

Namespace WPFLuaFramework
{
/// <Summary>
/// Lua function description feature class
/// </Summary>
Public class LuaFunction: Attribute
{
Private String FunctionName;

Public LuaFunction (String strFuncName)
{
FunctionName = strFuncName;
}

Public String getFuncName ()
{
Return FunctionName;
}
}

/// <Summary>
/// Lua Engine
/// </Summary>
Class LuaFramework
{
Private Lua pLuaVM = new Lua (); // lua Virtual Machine

/// <Summary>
/// Register the lua Function
/// </Summary>
/// <Param name = "pLuaAPIClass"> lua function class </param>
Public void BindLuaApiClass (Object pLuaAPIClass)
{
Foreach (MethodInfo mInfo in pLuaAPIClass. GetType (). GetMethods ())
{
Foreach (Attribute attr in Attribute. GetCustomAttributes (mInfo ))
{
String LuaFunctionName = (attr as LuaFunction). getFuncName ();
PLuaVM. RegisterFunction (LuaFunctionName, pLuaAPIClass, mInfo );
}
}
}

/// <Summary>
/// Execute the lua script file www.2cto.com
/// </Summary>
/// <Param name = "luaFileName"> script file name </param>
Public void ExecuteFile (string luaFileName)
{
Try
{
PLuaVM. DoFile (luaFileName );
}
Catch (Exception e)
{
MessageBox. Show (e. ToString ());
}
}

/// <Summary>
/// Execute the lua script
/// </Summary>
/// <Param name = "luaCommand"> lua command </param>
Public void ExecuteString (string luaCommand)
{
Try
{
PLuaVM. DoString (luaCommand );
}
Catch (Exception e)
{
MessageBox. Show (e. ToString ());
}
}
}
}
 

 

My lua api class is as follows, used to implement the functions of C # for lua (LuaAPI. cs)

 

[C-sharp] using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;
 
Namespace WPFLuaFramework
{
Class LuaAPI
{
[LuaFunction ("lua1")]
Public void a1 ()
{
MessageBox. Show ("a1 called ");
}
 
[LuaFunction ("lua2")]
Public int a2 ()
{
MessageBox. Show ("a2 called ");
Return 0;
}
 
[LuaFunction ("lua3")]
Public void a3 (string s)
{
MessageBox. Show ("a3 called ");
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Windows;

Namespace WPFLuaFramework
{
Class LuaAPI
{
[LuaFunction ("lua1")]
Public void a1 ()
{
MessageBox. Show ("a1 called ");
}

[LuaFunction ("lua2")]
Public int a2 ()
{
MessageBox. Show ("a2 called ");
Return 0;
}

[LuaFunction ("lua3")]
Public void a3 (string s)
{
MessageBox. Show ("a3 called ");
}
}
}
 

 

Finally, check whether the call code is very simple.

 

[C-sharp] LuaFramework test = new LuaFramework ();
Test. BindLuaApiClass (new LuaAPI ());
Test. ExecuteFile ("test. lua ");
Test. ExecuteString ("lua1 ()");
LuaFramework test = new LuaFramework ();
Test. BindLuaApiClass (new LuaAPI ());
Test. ExecuteFile ("test. lua ");
Test. ExecuteString ("lua1 ()");

 

The LUA code is as follows:

 

Lua1 ();
Lua2 ();
Lua3 ("test ");

 

From the growth Diary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.