tolua#各个平台添加自定义扩展并编译tolua库

先放上tolua#的几个github地址
tolua#
https://github.com/topameng/tolua
Debugger
https://github.com/topameng/Debugger
tolua_runtime
https://github.com/topameng/tolua_runtime

Windows环境配置
NDK 版本:android-ndk-r10e 默认安装到 D:/android-ndk-r10ehttps://dl.google.com/android/repository/android-ndk-r10e-windows-x86_64.zip
Msys2配置说明https://github.com/topameng/tolua_runtime/wiki
配置好的Msys2下载https://pan.baidu.com/s/1c2JzvDQ

OSX环境配置
需要Xcode命令行工具

tolua_runtime build
pc: build_win32.sh build_win64.h (mingw + luajit2.1.0-beat3)
android: build_arm.sh build_x86.sh (mingw + luajit2.1.0-beat3)
mac: build_osx.sh (xcode + luac5.1.5 luajit can't run in unity5)
ios: build_ios.sh (xcode + luajit2.1.0-beat3)

添加自定义扩展库(以添加云风的Snapshot.c库 为例)
需要将snapshot.c文件稍微修改一下,符合tolua语法

/*
int
luaopen_snapshot(lua_State *L) {
    luaL_checkversion(L);
    lua_pushcfunction(L, snapshot);
    return 1;
} */

static const struct luaL_Reg snapshot_funcs[] = {
    { "snapshot", snapshot },
    { NULL, NULL }
};

LUALIB_API int luaopen_snapshot(lua_State *L) {
    luaL_checkversion(L);
    #if LUA_VERSION_NUM < 502
        luaL_register(L, "snapshot", snapshot_funcs);
    #else
        luaL_newlib(L, snapshot_funcs);
    #endif
    return 1;
}

将Snapshot.c放到tolua_runtime-master/目录下
pc:分别在build_win32.sh和build_win64.h中将 snapshot.c \添加到 luasocket/wsocket.c \下一行
android:tolua_runtime-master/android/jni/Android.mk中将 ../../snapshot.c \添加到../../luasocket/usocket.c \下一行
mac:将tolua_runtime-master/macnojit/中的xcode工程文件打开,将snapshot.c加入到工程中
mac:将tolua_runtime-master/iOS/中的xcode工程文件打开,将snapshot.c加入到工程中

修改完成后根据各个平台执行编译脚本。

在tolua中的LuaDLL.cs中 添加
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int luaopen_snapshot(IntPtr L);
然后通过下面代码开启snapshot库

LuaState lua = new LuaState();
lua.Start();
lua.OpenLibs(LuaDLL.luaopen_snapshot);

然后再lua中再使用

require("snapshot")

你可能感兴趣的:(tolua#各个平台添加自定义扩展并编译tolua库)