• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua简单包装

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#ifndef _LUA_WRAPPER_
#define _LUA_WRAPPER_

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <vector>
#include <string>
#include <assert.h>


static size_t Lua_GetSize(lua_State* L, int pos)
{
    if (lua_istable(L, pos))
    {
        return lua_rawlen(L, pos);
    }
    return 0;
}

static void Lua_Remove(lua_State* L, int n)
{
    if (n > 0)
    {
        lua_pop(L, n);
    }
    else if (n < 0)
    {
        int top = lua_gettop(L);
        lua_pop(L, top);
    }
}

static int Lua_CreateRef(lua_State* L)
{
    if (lua_isfunction(L, -1))
    {
        return luaL_ref(L, LUA_REGISTRYINDEX);
    }
    return 0;
}

static void Lua_DeleteRef(lua_State* L,int ref)
{
    lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
    luaL_unref(L, LUA_REGISTRYINDEX, ref);
}

/*****************************************************************/

static void Lua_Pack(lua_State * L, int value)
{
    lua_pushinteger(L, value);
}
static void Lua_Pack(lua_State* L, size_t value)
{
    lua_pushinteger(L, value);
}
static void Lua_Pack(lua_State* L, float value)
{
    lua_pushnumber(L, value);
}
static void Lua_Pack(lua_State* L, double value)
{
    lua_pushnumber(L, value);
}
static void Lua_Pack(lua_State* L, const char* value)
{
    lua_pushstring(L, value);
}
static void Lua_Pack(lua_State* L, const std::string& value)
{
    lua_pushlstring(L, value.c_str(), value.length());
}

template<class T>
static void Lua_Pack(lua_State* L, const std::vector<T>& value)
{
    lua_newtable(L);
    for (size_t i = 0; i < value.size(); ++i)
    {
        Lua_Pack(L, value[i]);
        lua_rawseti(L,-2,i+1);
    }
}

template<typename T1, typename T2, typename... Args>
static void Lua_Pack(lua_State* L, const T1& value1, const T2& value2, Args&... args)
{
    Lua_Pack(L, value1);
    Lua_Pack(L, value2, args...);
}

/*******************************************************/
    
static int Lua_Unpack(lua_State* L, int& value)
{
    if (lua_isnumber(L, -1))
    {
        value = (int)lua_tointeger(L, -1);
        lua_pop(L, 1);
        return 1;
    }
    return 0;
}

static int Lua_Unpack(lua_State* L, size_t& value)
{
    if (lua_isnumber(L, -1))
    {
        value = (size_t)lua_tointeger(L, -1);
        lua_pop(L, 1);
        return 1;
    }
    return 0;
}

static int Lua_Unpack(lua_State* L, float& value)
{
    if (lua_isnumber(L, -1))
    {
        value = (float)lua_tonumber(L, -1);
        lua_pop(L, 1);
        return 1;
    }
    return 0;
}
static int Lua_Unpack(lua_State* L, double& value)
{
    if (lua_isnumber(L, -1))
    {
        value = lua_tonumber(L, -1);
        lua_pop(L, 1);
        return 1;
    }
    return 0;
}

static int Lua_Unpack(lua_State* L, const char* value)
{
    if (lua_isstring(L, -1))
    {
        value = lua_tostring(L, -1);
        lua_pop(L,1);
        return 1;
    }
    return 0;
}

static int Lua_Unpack(lua_State* L, std::string& value)
{
    if (lua_isstring(L, -1))
    {
        const char* str = lua_tostring(L, -1);
        value.append(str);
        lua_pop(L, 1);
        return 1;
    }
    return 0;
}

template<class T>
static void Lua_Unpack(lua_State* L, std::vector<T>& value)
{
    T t;
    if (!lua_istable(L, -1))
        return 0;
    size_t len = Lua_GetSize(L, -1);
    for (size_t i = 1; i <= len; ++i)
    {
        lua_rawgeti(L, -1, i);
        if (Lua_Unpack(L, t))
            value.push_back(t);
        else
            lua_pop(L, 1);
    }
    lua_pop(L, 1);
    return 1;
}

template<typename T1, typename T2, typename... Args>
static void Lua_Unpack(lua_State* L, T1& value1, T2& value2, Args&... args)
{
    Lua_Unpack(L, value2, args...);
    Lua_Unpack(L, value1);
}
/*******************************************/

static bool Lua_LoadFile(lua_State* L, const char* filename)
{
    if (luaL_dofile(L, filename))
    {
        const char* err = lua_tostring(L, -1);
        printf("loadl file %s failed , err: %s\n",filename,err);
        lua_pop(L, 1);
        return false;
    }
    return true;
}

static void Lua_Split(const char* str,  char c, std::vector<std::string>& result)
{
    const char* pre = str;
    while (*str != 0)
    {
        while (*str && *str != c) ++str;
        result.push_back(std::string(pre,str));
        pre = *str ? ++str : str;
    }
}

/*****************************************************************/

static int Lua_GetField(lua_State* L, int index, const char* key) 
{
    if (lua_istable(L, index))
    {
        lua_pushstring(L, key);
        lua_rawget(L, index);
        return 1;
    }
    return 0;
}

static int Lua_GetField(lua_State* L, int index, int i)
{
    if (lua_istable(L, index))
    {
        lua_rawgeti(L, index, i);
        return 1;
    }
    return 0;
}

template<typename T1,typename T2,typename... Args>
static int Lua_GetField(lua_State* L, int index, T1& t1, T2& t2, Args& ... args)
{
    int ret = Lua_GetField(L, index, t1) + Lua_GetField(L, index, t2, args...);
    return ret;
}

/**********************************************************/
static int Lua_TopField(lua_State* L, const char* key)
{
    if (lua_istable(L, -1))
    {
        lua_pushstring(L, key);
        lua_rawget(L, -1);
        return 1;
    }
    return 0;
}

static int Lua_TopField(lua_State* L, int i)
{
    if (lua_istable(L, -1))
    {
        lua_rawgeti(L, -1, i);
        return 1;
    }
    return 0;
}

template<typename T1,typename T2,typename... Args>
static int Lua_TopField(lua_State* L, T1& t1, T2& t2, Args... args)
{
    int ret = Lua_TopField(L, t1) + Lua_TopField(L, t2, args...);
    return ret;
}

static bool Lua_CallFunc(lua_State* L, int rt)
{
    if (!lua_isfunction(L, -1))
        return false;
    if (lua_pcall(L, 0, rt, 0) != 0)
    {
        const char* err = lua_tostring(L, -1);
        printf("call lua func failed!!!, err:%s\n", err);
        lua_pop(L, 1);
        return false;
    }
    return true;
}

template<typename T, typename... Args>
static bool Lua_CallFunc(lua_State* L,int rt, T& t, Args&... args)
{
    const int len = sizeof...(args) + 1;
    if (!lua_isfunction(L, -len))
        return false;
    
    Lua_Pack(L, t, args...) ;
    if (lua_pcall(L, len, rt, 0) != 0 )
    {
        const char* err = lua_tostring(L, -1);
        printf("call lua func failed!!!, err:%s\n",err);
        lua_pop(L, 1);
        return false;
    }
    return true;
}


#endif

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cocos lua 加密与解密 混淆 (版本号cocos3.4)发布时间:2022-07-22
下一篇:
血族手游Lua脚本及资源文件解密发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap