下载
1 2 3 4 5
| # xlua官网 https://github.com/Tencent/xLua/tree/master/build
# lua-protobuf https://github.com/starwing/lua-protobuf
|
pb集成到xlua库
参考xlua增加删除第三方lua库.doc
- 下载
lua-protobuf
- 文件复制
pb.h
复制到xLua-master\build\lua-pb\include
pb.c
复制到xLua-master\build\lua-pb\source
protoc.lua
复制到项目的lua代码目录
luaunit.lua
复制到项目的lua代码目录
serpent.lua
复制到项目的lua代码目录
- 修改
xLua-master\build\CMakeLists.txt
在 MARK_AS_ADVANCED(XLUA_PROJECT_DIR) -大概34行后面添加
1 2 3 4 5 6 7 8 9 10 11
| #begin lua-pb set (PB_SRC lua-pb/source/pb.c) set_property( SOURCE ${PB_SRC} APPEND PROPERTY COMPILE_DEFINITIONS LUA_LIB ) list(APPEND THIRDPART_INC lua-pb/include) set (THIRDPART_SRC ${THIRDPART_SRC} ${PB_SRC}) #end lua-pb
|
编译
win64
- 进入
xLua-master\build
目录
- 找到
make_win64_luajit.bat
- 修改
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
为自己的路径
- 要是没有这个bat,可以使用vs安装包里面找到vc并安装。
- 执行
xLua-master\build\make_win64_luajit.bat
- 拷贝
xLua-master\build\build_lj64\Release\xlua.dll
到项目或替换之前的库
项目代码处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; using XLua;
namespace LuaDLL { public delegate int lua_CSFunction(IntPtr L);
public partial class Lua { const string LUADLL = "xlua";
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_rapidjson(System.IntPtr L);
[MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))] public static int LoadRapidJson(System.IntPtr L) { return luaopen_rapidjson(L); }
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_pb(System.IntPtr L);
[MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))] public static int LoadPb(System.IntPtr L) { return luaopen_pb(L); }
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_pb_io(System.IntPtr L);
[MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))] public static int LoadPb_io(System.IntPtr L) { return luaopen_pb_io(L); }
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_pb_conv(System.IntPtr L);
[MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))] public static int LoadPb_conv(System.IntPtr L) { return luaopen_pb_conv(L); }
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_pb_buffer(System.IntPtr L);
[MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))] public static int LoadPb_buffer(System.IntPtr L) { return luaopen_pb_buffer(L); }
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_pb_slice(System.IntPtr L);
[MonoPInvokeCallback(typeof(LuaDLL.lua_CSFunction))] public static int LoadPb_slice(System.IntPtr L) { return luaopen_pb_slice(L); } } }
|
找到项目的luaEnv实例管理的地方,添加大致代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
private void Init() { m_luaEnv = new LuaEnv(); m_luaEnv.AddLoader(CustomLoader); m_luaEnv.AddBuildin("rapidjson", LuaDLL.Lua.LoadRapidJson); m_luaEnv.AddBuildin("pb", LuaDLL.Lua.LoadPb); m_luaEnv.AddBuildin("pb.io", LuaDLL.Lua.LoadPb_io); m_luaEnv.AddBuildin("pb.buffer", LuaDLL.Lua.LoadPb_buffer); m_luaEnv.AddBuildin("pb.conv", LuaDLL.Lua.LoadPb_conv); m_luaEnv.AddBuildin("pb.slice", LuaDLL.Lua.LoadPb_slice); }
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| local pb = require "pb" local protoc = require "protoc"
assert(protoc:load [[ syntax = "proto3";
message Phone { string name = 1; int64 phonenumber = 2; } message Person { string name = 1; int32 age = 2; string address = 3; repeated Phone contacts = 4; } ]])
local data = { name = "ilse", age = 18, contacts = { { name = "alice", phonenumber = 12312341234 }, { name = "bob", phonenumber = 45645674567 } } }
print("==============")
local bytes = assert(pb.encode("Person", data)) print(pb.tohex(bytes))
local data2 = assert(pb.decode("Person", bytes)) print(require "serpent".block(data2))
print("==============")
|