178 lines
3.9 KiB
C++
178 lines
3.9 KiB
C++
#include "lua_wraper.h"
|
||
|
||
void LuaDelegate::Stop()
|
||
{
|
||
|
||
}
|
||
|
||
int LuaDelegate::DoString(std::string scr)
|
||
{
|
||
if (mVM != nullptr){
|
||
int ret = luaL_dostring(mVM,scr.c_str());
|
||
if (ret > 0){
|
||
printf("lua error");
|
||
PrintError(mVM);
|
||
return -1;
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int LuaDelegate::UpdateScript(std::string scr)
|
||
{
|
||
if(nullptr != mVM){
|
||
lua_close(mVM);
|
||
}
|
||
free(mVM);
|
||
mVM = luaL_newstate(); //打开lua
|
||
if(nullptr != mVM){
|
||
printf("shit is nullptr");
|
||
}
|
||
for (auto x : mFunc) {
|
||
lua_register(mVM, x.first.c_str(), x.second);
|
||
}
|
||
if (mVM != nullptr){
|
||
int ret = luaL_dostring(mVM,scr.c_str());
|
||
if (ret > 0){
|
||
printf("lua error");
|
||
PrintError(mVM);
|
||
return -1;
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
void LuaDelegate::PrintError(lua_State *L)
|
||
{
|
||
std::cout<<"\nFATAL ERROR:%s\n\n"<< lua_tostring(L, -1);
|
||
}
|
||
|
||
void LuaDelegate::OnSerialData(std::string data){
|
||
int i = lua_getglobal(mVM,"OnDataReady");
|
||
if(i == 0)
|
||
return;
|
||
lua_pushstring(mVM,data.data());
|
||
lua_call(mVM,1,0);
|
||
}
|
||
|
||
/// <summary>
|
||
// 这部分代码可以写成一个统一的c++ 调用lua的接口,核心在于
|
||
// lua_getglobal这个函数调用,其他的参数可以通过回调函数传入,不排除RTTI等技术使用
|
||
// </summary>
|
||
// <param name="addr"></param>
|
||
// <param name="data"></param>
|
||
// <param name="port"></param>
|
||
void LuaDelegate::OnNetworkData(char *addr, char *data, uint32_t port)
|
||
{
|
||
std::cout<<"call lua network callback";
|
||
int ret = lua_getglobal(mVM,"OnNetworkData");
|
||
if(ret == 0)
|
||
return;
|
||
std::cout<<ret;
|
||
lua_pushstring(mVM,addr);
|
||
lua_pushstring(mVM,data);
|
||
lua_pushnumber(mVM,port);
|
||
lua_call(mVM,3,0);
|
||
}
|
||
|
||
void LuaDelegate::OnNewTcpClient(std::string ip, uint32_t port, uint32_t sockptr)
|
||
{
|
||
std::cout<<"call lua network new client callback";
|
||
int ret = lua_getglobal(mVM,"OnNewClient");
|
||
if(ret == 0)
|
||
return;
|
||
std::cout<<ret;
|
||
lua_pushstring(mVM,ip.c_str());
|
||
lua_pushinteger(mVM,port);
|
||
lua_pushinteger(mVM,sockptr);
|
||
lua_call(mVM,3,0);
|
||
}
|
||
|
||
void LuaDelegate::OnClientLeave(std::string ip, uint32_t port, uint32_t sockptr)
|
||
{
|
||
std::cout<<"call lua network leave callback";
|
||
int ret = lua_getglobal(mVM,"OnClientLeave");
|
||
if(ret == 0)
|
||
return;
|
||
std::cout<<ret;
|
||
lua_pushstring(mVM,ip.c_str());
|
||
lua_pushnumber(mVM,port);
|
||
lua_pushnumber(mVM,sockptr);
|
||
lua_call(mVM,3,0);
|
||
}
|
||
|
||
|
||
void LuaDelegate::DumpStack()
|
||
{
|
||
static int count = 0;
|
||
printf("begin dump lua stack:%d\n", count);
|
||
int top = lua_gettop(mVM);
|
||
for (int i = top; i > 0; --i)
|
||
{
|
||
int t = lua_type(mVM, i);
|
||
switch (t)
|
||
{
|
||
case LUA_TSTRING:
|
||
std::cout<<("%s\n", lua_tostring(mVM, i));
|
||
break;
|
||
case LUA_TBOOLEAN:
|
||
std::cout<<(lua_toboolean(mVM, i) ? "true\n" : "false\n");
|
||
break;
|
||
case LUA_TNUMBER:
|
||
std::cout<<("%g\n", lua_tonumber(mVM, i));
|
||
break;
|
||
default:
|
||
std::cout<<("%s\n", lua_typename(mVM, t));
|
||
break;
|
||
}
|
||
}
|
||
++count;
|
||
}
|
||
|
||
|
||
LuaDelegate::~LuaDelegate()
|
||
{
|
||
if(nullptr != mVM){
|
||
lua_close(mVM);
|
||
}
|
||
}
|
||
|
||
LuaDelegate::LuaDelegate():
|
||
mVM(nullptr)
|
||
{
|
||
mVM = luaL_newstate(); //打开lua
|
||
if(nullptr != mVM){
|
||
printf("shit is nullptr");
|
||
}
|
||
luaL_openlibs(mVM); //打开标准库
|
||
|
||
}
|
||
|
||
LuaDelegate::LuaDelegate(std::map<std::string, lua_CFunction> maps) :
|
||
mVM(nullptr)
|
||
{
|
||
mVM = luaL_newstate(); //打开lua
|
||
if (nullptr != mVM) {
|
||
printf("shit is nullptr");
|
||
}
|
||
luaL_openlibs(mVM); //打开标准库
|
||
|
||
for (auto x : maps) {
|
||
lua_register(mVM, x.first.c_str(), x.second);
|
||
}
|
||
mFunc = maps;
|
||
}
|
||
|
||
int LuaDelegate::DoFile(std::string path)
|
||
{
|
||
if(mVM != nullptr){
|
||
int ret = luaL_dofile(mVM, path.c_str());
|
||
if (ret > 0){
|
||
printf("lua error");
|
||
return -1;
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|