2006-12-10
This commit is contained in:
commit
78c27f03c8
153 files changed
+24678
No files matched your search
@@ -0,0 +1,10 @@
|
||||
#ifndef LUA_CPP_H
|
||||
#define LUA_CPP_H
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "lua_camera.h"
|
||||
#define method(name) {#name, Camera::l_##name}
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
|
||||
using namespace OpenGL;
|
||||
|
||||
int Camera::l_setSpeed(lua_State *L) {
|
||||
float tmp = float(luaL_checknumber(L, 1));
|
||||
CameraHolder::Instance().setSpeed(tmp);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_setRotating(lua_State *L) {
|
||||
bool b = lua_toboolean(L, 1);
|
||||
CameraHolder::Instance().setRotating(b);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_getEye(lua_State *L) {
|
||||
Vector3D & e = CameraHolder::Instance().getEye();
|
||||
lua_pushnumber(L, e.x);
|
||||
lua_pushnumber(L, e.y);
|
||||
lua_pushnumber(L, e.z);
|
||||
return 3;
|
||||
}
|
||||
int Camera::l_setEye(lua_State *L) {
|
||||
Vector3D & e = CameraHolder::Instance().getEye();
|
||||
e.x = luaL_checknumber(L, 1);
|
||||
e.y = luaL_checknumber(L, 2);
|
||||
e.z = luaL_checknumber(L, 3);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_getCenter(lua_State *L) {
|
||||
Vector3D & e = CameraHolder::Instance().getCenter();
|
||||
lua_pushnumber(L, e.x);
|
||||
lua_pushnumber(L, e.y);
|
||||
lua_pushnumber(L, e.z);
|
||||
return 3;
|
||||
}
|
||||
int Camera::l_setCenter(lua_State *L) {
|
||||
Vector3D & e = CameraHolder::Instance().getCenter();
|
||||
e.x = luaL_checknumber(L, 1);
|
||||
e.y = luaL_checknumber(L, 2);
|
||||
e.z = luaL_checknumber(L, 3);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_getUp(lua_State *L) {
|
||||
Vector3D & e = CameraHolder::Instance().getUp();
|
||||
lua_pushnumber(L, e.x);
|
||||
lua_pushnumber(L, e.y);
|
||||
lua_pushnumber(L, e.z);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_setUp(lua_State *L) {
|
||||
Vector3D & e = CameraHolder::Instance().getUp();
|
||||
e.x = luaL_checknumber(L, 1);
|
||||
e.y = luaL_checknumber(L, 2);
|
||||
e.z = luaL_checknumber(L, 3);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_setGravityOn(lua_State *L) {
|
||||
bool v = lua_toboolean(L, 1);
|
||||
CameraHolder::Instance().setCamGravity(v);
|
||||
return 0;
|
||||
}
|
||||
int Camera::l_interpolateToPosition(lua_State *L) {
|
||||
float x, y, z;
|
||||
x = float(luaL_checknumber(L, 1));
|
||||
y = float(luaL_checknumber(L, 2));
|
||||
z = float(luaL_checknumber(L, 3));
|
||||
Uint32 msecInterval = Uint32(luaL_checkinteger(L, 4));
|
||||
CameraHolder::Instance().interpolate(Vector3D(x, y, z), 1, msecInterval);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
const luaL_reg Camera::methods[] = {
|
||||
method(setSpeed),
|
||||
method(setRotating),
|
||||
method(setEye),
|
||||
method(getEye),
|
||||
method(setCenter),
|
||||
method(getCenter),
|
||||
method(getUp),
|
||||
method(setUp),
|
||||
method(setGravityOn),
|
||||
method(interpolateToPosition),
|
||||
{NULL, NULL}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef LUA_OGTA_CAMERA_H
|
||||
#define LUA_OGTA_CAMERA_H
|
||||
|
||||
#include "gl_camera.h"
|
||||
#include "lua.hpp"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
static int l_setSpeed(lua_State *L);
|
||||
static int l_setRotating(lua_State *L);
|
||||
static int l_setGravityOn(lua_State *L);
|
||||
static int l_interpolateToPosition(lua_State *L);
|
||||
static int l_getEye(lua_State *L);
|
||||
static int l_setEye(lua_State *L);
|
||||
static int l_getCenter(lua_State *L);
|
||||
static int l_setCenter(lua_State *L);
|
||||
static int l_getUp(lua_State *L);
|
||||
static int l_setUp(lua_State *L);
|
||||
|
||||
/*static int mute(lua_State *L);
|
||||
static int unmute(lua_State *L);*/
|
||||
|
||||
|
||||
static const luaL_reg methods[];
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "lua_cityview.h"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
|
||||
int CityView::setCamPosition(lua_State *L) {
|
||||
float x = float(luaL_checknumber(L, 1));
|
||||
float y = float(luaL_checknumber(L, 2));
|
||||
float z = float(luaL_checknumber(L, 3));
|
||||
setPosition(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CityView::getCamPosition(lua_State *L) {
|
||||
lua_pushnumber(L, camPos[0]);
|
||||
lua_pushnumber(L, camPos[1]);
|
||||
lua_pushnumber(L, camPos[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
/*
|
||||
int CityView::setCamVector(lua_State *L) {
|
||||
float x = float(luaL_checknumber(L, 1));
|
||||
float y = float(luaL_checknumber(L, 2));
|
||||
float z = float(luaL_checknumber(L, 3));
|
||||
OpenGTA::CityView::setCamVector(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CityView::getCamVector(lua_State *L) {
|
||||
lua_pushnumber(L, camVec[0]);
|
||||
lua_pushnumber(L, camVec[1]);
|
||||
lua_pushnumber(L, camVec[2]);
|
||||
return 3;
|
||||
}
|
||||
*/
|
||||
|
||||
int CityView::setTopDownView(lua_State *L) {
|
||||
setViewMode(lua_toboolean(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CityView::setDrawHeadingArrows(lua_State *L) {
|
||||
OpenGTA::CityView::setDrawHeadingArrows(lua_toboolean(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CityView::setVisibleRange(lua_State *L) {
|
||||
OpenGTA::CityView::setVisibleRange(luaL_checkinteger(L, 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CityView::getVisibleRange(lua_State *L) {
|
||||
lua_pushnumber(L, visibleRange);
|
||||
return 1;
|
||||
}
|
||||
const char CityView::className[] = "CityView";
|
||||
#define method(name) {#name, &CityView::name}
|
||||
Lunar<CityView>::RegType CityView::methods[] = {
|
||||
method(setCamPosition),
|
||||
method(getCamPosition),
|
||||
// method(setCamVector),
|
||||
// method(getCamVector),
|
||||
method(setTopDownView),
|
||||
method(setVisibleRange),
|
||||
method(getVisibleRange),
|
||||
method(setDrawHeadingArrows),
|
||||
{0, 0}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef OPENGTA_SCRIPT_CV_H
|
||||
#define OPENGTA_SCRIPT_CV_H
|
||||
|
||||
#include "gl_cityview.h"
|
||||
#include "lunar.h"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
class CityView : public OpenGTA::CityView {
|
||||
public:
|
||||
int setCamPosition(lua_State *L);
|
||||
int getCamPosition(lua_State *L);
|
||||
|
||||
// int setCamVector(lua_State *L);
|
||||
// int getCamVector(lua_State *L);
|
||||
int setTopDownView(lua_State *L);
|
||||
|
||||
int setZoom(lua_State *L);
|
||||
int getZoom(lua_State *L);
|
||||
|
||||
int setVisibleRange(lua_State *L);
|
||||
int getVisibleRange(lua_State *L);
|
||||
int setDrawHeadingArrows(lua_State *L);
|
||||
|
||||
// --
|
||||
|
||||
static const char className[];
|
||||
static Lunar<CityView>::RegType methods[];
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "lua_screen.h"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
using namespace OpenGL;
|
||||
int Screen::getFullscreen(lua_State *L) {
|
||||
bool b = ScreenHolder::Instance().getFullscreen();
|
||||
lua_pushboolean(L, b);
|
||||
return 1;
|
||||
}
|
||||
int Screen::setFullscreen(lua_State *L) {
|
||||
bool b = ScreenHolder::Instance().getFullscreen();
|
||||
bool v = lua_toboolean(L, 1);
|
||||
if (b != v)
|
||||
ScreenHolder::Instance().toggleFullscreen();
|
||||
return 0;
|
||||
}
|
||||
int Screen::makeScreenShot(lua_State *L) {
|
||||
ScreenHolder::Instance().makeScreenshot(luaL_checkstring(L, 1));
|
||||
return 0;
|
||||
}
|
||||
#define method(name) {#name, Screen::name}
|
||||
const luaL_reg Screen::methods[] = {
|
||||
method(setFullscreen),
|
||||
method(getFullscreen),
|
||||
method(makeScreenShot),
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef LUA_OGTA_SCREEN_H
|
||||
#define LUA_OGTA_SCREEN_H
|
||||
|
||||
#include "gl_screen.h"
|
||||
#include "lua.hpp"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
|
||||
class Screen {
|
||||
public:
|
||||
static int getFullscreen(lua_State *L);
|
||||
static int setFullscreen(lua_State *L);
|
||||
static int makeScreenShot(lua_State *L);
|
||||
|
||||
static const luaL_reg methods[];
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <cassert>
|
||||
#include "lua_stackguard.h"
|
||||
#include "log.h"
|
||||
|
||||
namespace Util {
|
||||
LuaStackguard::LuaStackguard(const char* f, int l, lua_State *L) {
|
||||
assert(L);
|
||||
m_state = L;
|
||||
i_file = f;
|
||||
i_line = l;
|
||||
m_top = lua_gettop(m_state);
|
||||
}
|
||||
|
||||
LuaStackguard::~LuaStackguard() {
|
||||
int now_top = lua_gettop(m_state);
|
||||
if (now_top > m_top) {
|
||||
Util::Log::warn(i_file, i_line) << "Stack-balance: " << now_top << " > " << m_top << std::endl;
|
||||
lua_settop(m_state, m_top);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef LUA_STACK_GUARD_H
|
||||
#define LUA_STACK_GUARD_H
|
||||
|
||||
#include "lua.hpp"
|
||||
|
||||
namespace Util {
|
||||
|
||||
class LuaStackguard {
|
||||
public:
|
||||
LuaStackguard(const char* f, int l, lua_State *L);
|
||||
~LuaStackguard();
|
||||
private:
|
||||
int m_top;
|
||||
lua_State* m_state;
|
||||
// line number and filename where this instance was created
|
||||
int i_line;
|
||||
const char* i_file;
|
||||
};
|
||||
|
||||
#define LGUARD(L) LuaStackguard guard(__FILE__, __LINE__, L)
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,126 @@
|
||||
#include <string>
|
||||
#include "lua_vm.h"
|
||||
#include "lunar.h"
|
||||
#include "lua_cityview.h"
|
||||
#include "lua_stackguard.h"
|
||||
#include "lua_camera.h"
|
||||
#include "lua_screen.h"
|
||||
#include "lua_spritecache.h"
|
||||
#include "m_exceptions.h"
|
||||
|
||||
using namespace Util;
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
LuaVM::LuaVM() : L(NULL) {
|
||||
L = lua_open();
|
||||
if (L == NULL)
|
||||
throw E_SCRIPTERROR("Failed to create Lua state!");
|
||||
|
||||
luaopen_base(L);
|
||||
luaopen_math(L);
|
||||
_registered = false;
|
||||
lua_settop(L, 0);
|
||||
prepare();
|
||||
}
|
||||
|
||||
LuaVM::~LuaVM() {
|
||||
if (L != NULL)
|
||||
lua_close(L);
|
||||
L = NULL;
|
||||
}
|
||||
|
||||
void LuaVM::prepare() {
|
||||
LGUARD(L);
|
||||
if (!_registered) {
|
||||
Lunar<CityView>::Register2(L);
|
||||
luaL_openlib(L, "camera", Camera::methods, 0);
|
||||
luaL_openlib(L, "screen", Screen::methods, 0);
|
||||
luaL_openlib(L, "spritecache", SpriteCache::methods, 0);
|
||||
}
|
||||
_registered = true;
|
||||
}
|
||||
|
||||
void LuaVM::setCityView(OpenGTA::CityView & cv) {
|
||||
LGUARD(L);
|
||||
CityView *scv = static_cast<CityView*>(&cv);
|
||||
lua_gettable(L, LUA_GLOBALSINDEX);
|
||||
int scv_ref = Lunar<CityView>::push(L, scv, false);
|
||||
lua_pushliteral(L, "city_view");
|
||||
lua_pushvalue(L, scv_ref);
|
||||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
}
|
||||
|
||||
void LuaVM::runString(const char* _str) {
|
||||
LGUARD(L);
|
||||
if (!_str)
|
||||
return;
|
||||
if (luaL_loadbuffer(L, _str, strlen(_str), "cmd") ||
|
||||
lua_pcall(L, 0, 0, 0))
|
||||
throw E_SCRIPTERROR("Error running string: " + std::string(lua_tostring(L, -1)));
|
||||
}
|
||||
|
||||
void LuaVM::runFile(const char* filename) {
|
||||
LGUARD(L);
|
||||
if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0))
|
||||
throw E_SCRIPTERROR("Error running file: " + std::string(lua_tostring(L, -1)));
|
||||
}
|
||||
|
||||
void LuaVM::callSimpleFunction(const char* func_name) {
|
||||
LGUARD(L);
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_type(L, -1) == LUA_TFUNCTION) {
|
||||
if (lua_pcall(L, 0, 0, 0) != 0)
|
||||
throw E_SCRIPTERROR(("Exception calling function: ") + std::string(lua_tostring(L, -1)));
|
||||
}
|
||||
else
|
||||
throw E_SCRIPTERROR("No such function: " + std::string(func_name));
|
||||
}
|
||||
|
||||
int LuaVM::getGlobalInt(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getglobal(L, key);
|
||||
if (!lua_isnumber(L, -1))
|
||||
throw E_SCRIPTERROR("Expected int value for key: " + std::string(key));
|
||||
int v = int(lua_tonumber(L, -1));
|
||||
return v;
|
||||
}
|
||||
|
||||
float LuaVM::getGlobalFloat(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getglobal(L, key);
|
||||
if (!lua_isnumber(L, -1))
|
||||
throw E_SCRIPTERROR("Expected float value for key: " + std::string(key));
|
||||
float v = float(lua_tonumber(L, -1));
|
||||
return v;
|
||||
}
|
||||
|
||||
const char* LuaVM::getGlobalString(const char* key) {
|
||||
LGUARD(L);
|
||||
lua_getglobal(L, key);
|
||||
if (!lua_isstring(L, -1))
|
||||
throw E_SCRIPTERROR("Expected string value for key: " + std::string(key));
|
||||
const char* v = lua_tostring(L, -1);
|
||||
return v;
|
||||
}
|
||||
|
||||
void LuaVM::setGlobalInt(const char* key, int v) {
|
||||
LGUARD(L);
|
||||
lua_pushnumber(L, v);
|
||||
lua_setglobal(L, key);
|
||||
}
|
||||
|
||||
void LuaVM::setGlobalFloat(const char* key, float v) {
|
||||
LGUARD(L);
|
||||
lua_pushnumber(L, v);
|
||||
lua_setglobal(L, key);
|
||||
}
|
||||
|
||||
void LuaVM::setGlobalString(const char* key, const char* v) {
|
||||
LGUARD(L);
|
||||
lua_pushstring(L, v);
|
||||
lua_setglobal(L, key);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifndef OPENGTA_SCRIPT_VM_H
|
||||
#define OPENGTA_SCRIPT_VM_H
|
||||
|
||||
#include "lua.hpp"
|
||||
#include "Singleton.h"
|
||||
#include "gl_cityview.h"
|
||||
|
||||
namespace OpenGTA {
|
||||
namespace Script {
|
||||
class LuaVM {
|
||||
public:
|
||||
LuaVM();
|
||||
~LuaVM();
|
||||
void runString(const char*);
|
||||
void runFile(const char*);
|
||||
void callSimpleFunction(const char*);
|
||||
void prepare();
|
||||
void setCityView(OpenGTA::CityView &);
|
||||
int getGlobalInt(const char*);
|
||||
float getGlobalFloat(const char*);
|
||||
const char* getGlobalString(const char*);
|
||||
void setGlobalInt(const char*, int);
|
||||
void setGlobalFloat(const char*, float);
|
||||
void setGlobalString(const char*, const char*);
|
||||
protected:
|
||||
lua_State *L;
|
||||
private:
|
||||
bool _registered;
|
||||
};
|
||||
typedef Loki::SingletonHolder<LuaVM, Loki::CreateUsingNew, Loki::DefaultLifetime,
|
||||
Loki::SingleThreaded> LuaVMHolder;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,259 @@
|
||||
#ifndef LUNAR_H
|
||||
#define LUNAR_H
|
||||
|
||||
/** See: http://lua-users.org/wiki/CppBindingWithLunar .
|
||||
*
|
||||
* # include-guards
|
||||
* # fixed old-style cast
|
||||
* # Register2 function
|
||||
*
|
||||
*/
|
||||
|
||||
#include "lua.hpp"
|
||||
|
||||
template <typename T> class Lunar {
|
||||
typedef struct { T *pT; } userdataType;
|
||||
public:
|
||||
typedef int (T::*mfp)(lua_State *L);
|
||||
typedef struct { const char *name; mfp mfunc; } RegType;
|
||||
|
||||
static void Register(lua_State *L) {
|
||||
lua_newtable(L);
|
||||
int methods = lua_gettop(L);
|
||||
|
||||
luaL_newmetatable(L, T::className);
|
||||
int metatable = lua_gettop(L);
|
||||
|
||||
// store method table in globals so that
|
||||
// scripts can add functions written in Lua.
|
||||
lua_pushvalue(L, methods);
|
||||
set(L, LUA_GLOBALSINDEX, T::className);
|
||||
|
||||
// hide metatable from Lua getmetatable()
|
||||
lua_pushvalue(L, methods);
|
||||
set(L, metatable, "__metatable");
|
||||
|
||||
lua_pushvalue(L, methods);
|
||||
set(L, metatable, "__index");
|
||||
|
||||
lua_pushcfunction(L, tostring_T);
|
||||
set(L, metatable, "__tostring");
|
||||
|
||||
lua_pushcfunction(L, gc_T);
|
||||
set(L, metatable, "__gc");
|
||||
|
||||
lua_newtable(L); // mt for method table
|
||||
lua_pushcfunction(L, new_T);
|
||||
lua_pushvalue(L, -1); // dup new_T function
|
||||
set(L, methods, "new"); // add new_T to method table
|
||||
set(L, -3, "__call"); // mt.__call = new_T
|
||||
lua_setmetatable(L, methods);
|
||||
|
||||
// fill method table with methods from class T
|
||||
for (RegType *l = T::methods; l->name; l++) {
|
||||
lua_pushstring(L, l->name);
|
||||
lua_pushlightuserdata(L, static_cast<void*>(l));
|
||||
lua_pushcclosure(L, thunk, 1);
|
||||
lua_settable(L, methods);
|
||||
}
|
||||
|
||||
lua_pop(L, 2); // drop metatable and method table
|
||||
}
|
||||
|
||||
static void Register2(lua_State *L) {
|
||||
lua_newtable(L);
|
||||
int methods = lua_gettop(L);
|
||||
|
||||
luaL_newmetatable(L, T::className);
|
||||
int metatable = lua_gettop(L);
|
||||
|
||||
// store method table in globals so that
|
||||
// scripts can add functions written in Lua.
|
||||
lua_pushvalue(L, methods);
|
||||
set(L, LUA_GLOBALSINDEX, T::className);
|
||||
|
||||
// hide metatable from Lua getmetatable()
|
||||
lua_pushvalue(L, methods);
|
||||
set(L, metatable, "__metatable");
|
||||
|
||||
lua_pushvalue(L, methods);
|
||||
set(L, metatable, "__index");
|
||||
|
||||
lua_pushcfunction(L, tostring_T);
|
||||
set(L, metatable, "__tostring");
|
||||
|
||||
lua_pushcfunction(L, gc_T);
|
||||
set(L, metatable, "__gc");
|
||||
|
||||
lua_newtable(L); // mt for method table
|
||||
lua_setmetatable(L, methods);
|
||||
|
||||
// fill method table with methods from class T
|
||||
for (RegType *l = T::methods; l->name; l++) {
|
||||
lua_pushstring(L, l->name);
|
||||
lua_pushlightuserdata(L, static_cast<void*>(l));
|
||||
lua_pushcclosure(L, thunk, 1);
|
||||
lua_settable(L, methods);
|
||||
}
|
||||
|
||||
lua_pop(L, 2); // drop metatable and method table
|
||||
}
|
||||
|
||||
|
||||
// call named lua method from userdata method table
|
||||
static int call(lua_State *L, const char *method,
|
||||
int nargs=0, int nresults=LUA_MULTRET, int errfunc=0)
|
||||
{
|
||||
int base = lua_gettop(L) - nargs; // userdata index
|
||||
if (!luaL_checkudata(L, base, T::className)) {
|
||||
lua_settop(L, base-1); // drop userdata and args
|
||||
lua_pushfstring(L, "not a valid %s userdata", T::className);
|
||||
return -1;
|
||||
}
|
||||
|
||||
lua_pushstring(L, method); // method name
|
||||
lua_gettable(L, base); // get method from userdata
|
||||
if (lua_isnil(L, -1)) { // no method?
|
||||
lua_settop(L, base-1); // drop userdata and args
|
||||
lua_pushfstring(L, "%s missing method '%s'", T::className, method);
|
||||
return -1;
|
||||
}
|
||||
lua_insert(L, base); // put method under userdata, args
|
||||
|
||||
int status = lua_pcall(L, 1+nargs, nresults, errfunc); // call method
|
||||
if (status) {
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL) msg = "(error with no message)";
|
||||
lua_pushfstring(L, "%s:%s status = %d\n%s",
|
||||
T::className, method, status, msg);
|
||||
lua_remove(L, base); // remove old message
|
||||
return -1;
|
||||
}
|
||||
return lua_gettop(L) - base + 1; // number of results
|
||||
}
|
||||
|
||||
// push onto the Lua stack a userdata containing a pointer to T object
|
||||
static int push(lua_State *L, T *obj, bool gc=false) {
|
||||
if (!obj) { lua_pushnil(L); return 0; }
|
||||
luaL_getmetatable(L, T::className); // lookup metatable in Lua registry
|
||||
if (lua_isnil(L, -1)) luaL_error(L, "%s missing metatable", T::className);
|
||||
int mt = lua_gettop(L);
|
||||
subtable(L, mt, "userdata", "v");
|
||||
userdataType *ud =
|
||||
static_cast<userdataType*>(pushuserdata(L, obj, sizeof(userdataType)));
|
||||
if (ud) {
|
||||
ud->pT = obj; // store pointer to object in userdata
|
||||
lua_pushvalue(L, mt);
|
||||
lua_setmetatable(L, -2);
|
||||
if (gc == false) {
|
||||
lua_checkstack(L, 3);
|
||||
subtable(L, mt, "do not trash", "k");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_pushboolean(L, 1);
|
||||
lua_settable(L, -3);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
lua_replace(L, mt);
|
||||
lua_settop(L, mt);
|
||||
return mt; // index of userdata containing pointer to T object
|
||||
}
|
||||
|
||||
// get userdata from Lua stack and return pointer to T object
|
||||
static T *check(lua_State *L, int narg) {
|
||||
userdataType *ud =
|
||||
static_cast<userdataType*>(luaL_checkudata(L, narg, T::className));
|
||||
if(!ud) luaL_typerror(L, narg, T::className);
|
||||
return ud->pT; // pointer to T object
|
||||
}
|
||||
|
||||
private:
|
||||
Lunar(); // hide default constructor
|
||||
|
||||
// member function dispatcher
|
||||
static int thunk(lua_State *L) {
|
||||
// stack has userdata, followed by method args
|
||||
T *obj = check(L, 1); // get 'self', or if you prefer, 'this'
|
||||
lua_remove(L, 1); // remove self so member function args start at index 1
|
||||
// get member function from upvalue
|
||||
RegType *l = static_cast<RegType*>(lua_touserdata(L, lua_upvalueindex(1)));
|
||||
return (obj->*(l->mfunc))(L); // call member function
|
||||
}
|
||||
|
||||
// create a new T object and
|
||||
// push onto the Lua stack a userdata containing a pointer to T object
|
||||
static int new_T(lua_State *L) {
|
||||
lua_remove(L, 1); // use classname:new(), instead of classname.new()
|
||||
T *obj = new T(L); // call constructor for T objects
|
||||
//push(L, obj, false); // gc_T will delete this object
|
||||
push(L, obj, true); // gc_T will delete this object
|
||||
return 1; // userdata containing pointer to T object
|
||||
}
|
||||
|
||||
// garbage collection metamethod
|
||||
static int gc_T(lua_State *L) {
|
||||
if (luaL_getmetafield(L, 1, "do not trash")) {
|
||||
lua_pushvalue(L, 1); // dup userdata
|
||||
lua_gettable(L, -2);
|
||||
if (!lua_isnil(L, -1)) return 0; // do not delete object
|
||||
}
|
||||
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
|
||||
T *obj = ud->pT;
|
||||
if (obj) delete obj; // call destructor for T objects
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tostring_T (lua_State *L) {
|
||||
char buff[32];
|
||||
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
|
||||
T *obj = ud->pT;
|
||||
sprintf(buff, "%p", obj);
|
||||
lua_pushfstring(L, "%s (%s)", T::className, buff);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void set(lua_State *L, int table_index, const char *key) {
|
||||
lua_pushstring(L, key);
|
||||
lua_insert(L, -2); // swap value and key
|
||||
lua_settable(L, table_index);
|
||||
}
|
||||
|
||||
static void weaktable(lua_State *L, const char *mode) {
|
||||
lua_newtable(L);
|
||||
lua_pushvalue(L, -1); // table is its own metatable
|
||||
lua_setmetatable(L, -2);
|
||||
lua_pushliteral(L, "__mode");
|
||||
lua_pushstring(L, mode);
|
||||
lua_settable(L, -3); // metatable.__mode = mode
|
||||
}
|
||||
|
||||
static void subtable(lua_State *L, int tindex, const char *name, const char *mode) {
|
||||
lua_pushstring(L, name);
|
||||
lua_gettable(L, tindex);
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
lua_checkstack(L, 3);
|
||||
weaktable(L, mode);
|
||||
lua_pushstring(L, name);
|
||||
lua_pushvalue(L, -2);
|
||||
lua_settable(L, tindex);
|
||||
}
|
||||
}
|
||||
|
||||
static void *pushuserdata(lua_State *L, void *key, size_t sz) {
|
||||
void *ud = 0;
|
||||
lua_pushlightuserdata(L, key);
|
||||
lua_gettable(L, -2); // lookup[key]
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1); // drop nil
|
||||
lua_checkstack(L, 3);
|
||||
ud = lua_newuserdata(L, sz); // create new userdata
|
||||
lua_pushlightuserdata(L, key);
|
||||
lua_pushvalue(L, -2); // dup userdata
|
||||
lua_settable(L, -4); // lookup[key] = userdata
|
||||
}
|
||||
return ud;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user