Trouble implementing Lua plugins system in C++

Started by
2 comments, last by dag10 14 years, 1 month ago
Hey there I'm trying to make a custom server for a certain type of game (the servers are really simple), and I wanted to make it based on a Lua plugins and gamemodes system, like how Garrys Mod servers function. Each plugin is one lua file. So far, I have a Plugin class (one per plugin), that each has its own instance of the Lua interpreter to isolate them. I have successfully made them load their lua files and execute, so that's not the issue. What is the issue is registering C++ functions to each plugin. For instance, I made a function to replace the normal print() function (this function does the same thing, but puts [PluginName]: in front of its outputted text. Here it is:

int Plugin::Print(lua_State *L) {
	int numArgs = lua_gettop(L);

	printf(pluginName);

	for (int i=1; i <= numArgs; i++) {
		printf(lua_tostring(L, i));
	}

	printf("\n");

	return 0;
}

pluginName is a const char* private member of Plugin. And Lua doesn't like non-static functions (And this can't be static because it has to access members of "this"). So my problem is what to do about this. I hope by this point you see exactly what I'm trying to accomplish. I've tried toLua++ and luaBind, but they both seemed very complex, and I was unable to build either of them. Other libraries don't seem to do what I want. What should I do? In case it helps you see my situation, here is the rest of the Plugin class. Here's Plugin.h:

#ifndef _PLUGIN_H
#define _PLUGIN_H
/*	============================================
		The Plugin class represents an indivitual
		plugin, which is one lua file. Each plugin
		has its own instance of Lua.
	============================================ */

#pragma comment(lib, "lua/lua51.lib")

extern "C" {
	#include "lua/lua.h"
	#include "lua/lualib.h"
	#include "lua/lauxlib.h"
}

class Plugin {
public:
	Plugin();
	~Plugin();

	void Load(const char *pluginFileName, const char *name);

private:
	lua_State *Lua;
	const char *pluginName;

	void RegisterLua();
	
	/* Lua Functions */
	static void Plugin::Print(const char *str);
	static int Plugin::Print_Glue(lua_State *L);
	/* ! Lua Functions */
};

#endif

And here's Plugin.cpp:

#include "Plugin.h"
#include "Lua_Functions.h"

Plugin::Plugin() {
}

Plugin::~Plugin() {
	lua_close(Lua);
}

void Plugin::Load(const char* pluginFileName, const char *name) {
	Lua = lua_open();

	if(Lua == NULL) {
		return;
	}

	luaL_openlibs(Lua);

	pluginName = name;

	RegisterLua();

	luaL_dofile(Lua, pluginFileName);
}

Thank you for your time. -Drew
Advertisement
Using a binding library will likely be significantly easier, and is probably worth the effort to figure out why they're not compiling. However, if you decide to go with the base Lua API there's several ways to bind state to a function.

As usual, the Programming in Lua book has a good section on this.
Storing State in C Functions

In your case, I'd recommend using an upvalue with a pointer (light user data) to your class's instance.
As I see it the problem most binding libraries will have is that the number of string arguments are not known. Luabind, Swig, toLua++ et al allow overloading of functions and will try to call the correct one given the type and number of arguments, that would require you to overload for n parameters.

You could use a number of methods to solve this some using a binding lib.

Provide a property get method for pluginName and print the arguments from the Lua side.
Pass a table of strings to the function as an argument then traverse the table printing the args. Using a binding.
Use upvalues as suggested yet push pluginName as the arg and then call stand alone LUA_CFUNCTION which retrieves the upvalue and does the printf like your current function.
toLua++ still looks nice, but I can't get it to compile.

I installed SCons, but when I typed "scons all" in the directory, it said "unknown command: gcc". So, I installed MingW and tried it again. Now it has many syntax errors:

C:\Users\Drew\Downloads\tolua++-1.0.93>scons allscons: Reading SConscript files ...scons: warning: The Options class is deprecated; use the Variables class instead.File "C:\Users\Drew\Downloads\tolua++-1.0.93\SConstruct", line 19, in <module>('********* tolua is ', 'bin\\tolua++_bootstrap.exe')scons: warning: The env.Copy() method is deprecated; use the env.Clone() methodinstead.File "C:\Users\Drew\Downloads\tolua++-1.0.93\src\tests\SCsub", line 2, in <module>scons: done reading SConscript files.scons: Building targets ...gcc -o src\bin\tolua.o -c -O2 -ansi -Wall -Iinclude src\bin\tolua.cIn file included from src\bin\tolua.c:15:include/tolua++.h:46:17: lua.h: No such file or directoryinclude/tolua++.h:47:21: lauxlib.h: No such file or directoryIn file included from src\bin\tolua.c:15:include/tolua++.h:59: error: syntax error before '*' tokeninclude/tolua++.h:60: error: syntax error before '*' tokeninclude/tolua++.h:61: error: syntax error before '*' tokeninclude/tolua++.h:62: error: syntax error before '*' tokeninclude/tolua++.h:63: error: syntax error before '*' tokeninclude/tolua++.h:64: error: syntax error before '*' tokeninclude/tolua++.h:65: error: syntax error before '*' tokeninclude/tolua++.h:66: error: syntax error before '*' tokeninclude/tolua++.h:67: error: syntax error before '*' tokeninclude/tolua++.h:68: error: syntax error before '*' tokeninclude/tolua++.h:69: error: syntax error before '*' tokeninclude/tolua++.h:70: error: syntax error before '*' tokeninclude/tolua++.h:72: error: syntax error before '*' tokeninclude/tolua++.h:74: error: syntax error before '*' tokeninclude/tolua++.h:76: error: syntax error before '*' tokeninclude/tolua++.h:78: error: syntax error before '*' tokeninclude/tolua++.h:80: error: syntax error before '*' tokeninclude/tolua++.h:82: error: syntax error before '*' tokeninclude/tolua++.h:84: error: syntax error before '*' tokeninclude/tolua++.h:86: error: syntax error before '*' tokeninclude/tolua++.h:88: error: syntax error before '*' tokeninclude/tolua++.h:89: error: syntax error before '*' tokeninclude/tolua++.h:90: error: syntax error before '*' tokeninclude/tolua++.h:92: error: syntax error before '*' tokeninclude/tolua++.h:93: error: syntax error before '*' tokeninclude/tolua++.h:94: error: syntax error before '*' tokeninclude/tolua++.h:95: error: syntax error before '*' tokeninclude/tolua++.h:96: error: syntax error before '*' tokeninclude/tolua++.h:97: error: syntax error before '*' tokeninclude/tolua++.h:98: error: syntax error before '*' tokeninclude/tolua++.h:99: error: syntax error before '*' tokeninclude/tolua++.h:100: error: syntax error before '*' tokeninclude/tolua++.h:101: error: syntax error before '*' tokeninclude/tolua++.h:106: error: syntax error before '*' tokeninclude/tolua++.h:107: error: syntax error before '*' tokeninclude/tolua++.h:108: error: syntax error before '*' tokeninclude/tolua++.h:109: error: syntax error before '*' tokeninclude/tolua++.h:110: error: syntax error before '*' tokeninclude/tolua++.h:111: error: syntax error before '*' tokeninclude/tolua++.h:112: error: syntax error before '*' tokeninclude/tolua++.h:113: error: syntax error before '*' tokeninclude/tolua++.h:114: error: syntax error before '*' tokeninclude/tolua++.h:115: error: syntax error before '*' tokeninclude/tolua++.h:116: error: syntax error before '*' tokeninclude/tolua++.h:117: error: syntax error before '*' tokeninclude/tolua++.h:118: error: syntax error before '*' tokeninclude/tolua++.h:119: error: syntax error before '*' tokeninclude/tolua++.h:121: error: syntax error before "tolua_tonumber"include/tolua++.h:121: error: syntax error before '*' tokeninclude/tolua++.h:121: warning: type defaults to `int' in declaration of `tolua_tonumber'include/tolua++.h:121: warning: data definition has no type or storage classinclude/tolua++.h:122: error: syntax error before '*' tokeninclude/tolua++.h:123: error: syntax error before '*' tokeninclude/tolua++.h:124: error: syntax error before '*' tokeninclude/tolua++.h:125: error: syntax error before '*' tokeninclude/tolua++.h:126: error: syntax error before '*' tokeninclude/tolua++.h:127: error: syntax error before "tolua_tofieldnumber"include/tolua++.h:127: error: syntax error before '*' tokeninclude/tolua++.h:127: warning: type defaults to `int' in declaration of `tolua_tofieldnumber'include/tolua++.h:127: warning: data definition has no type or storage classinclude/tolua++.h:128: error: syntax error before '*' tokeninclude/tolua++.h:129: error: syntax error before '*' tokeninclude/tolua++.h:130: error: syntax error before '*' tokeninclude/tolua++.h:131: error: syntax error before '*' tokeninclude/tolua++.h:132: error: syntax error before '*' tokeninclude/tolua++.h:134: error: syntax error before '*' tokeninclude/tolua++.h:136: error: syntax error before '*' tokeninclude/tolua++.h:156: error: syntax error before '*' tokensrc\bin\tolua.c:17:17: lua.h: No such file or directorysrc\bin\tolua.c:18:20: lualib.h: No such file or directorysrc\bin\tolua.c:19:21: lauxlib.h: No such file or directorysrc\bin\tolua.c:60: error: syntax error before '*' tokensrc\bin\tolua.c: In function `setfield':src\bin\tolua.c:62: warning: implicit declaration of function `lua_pushstring'src\bin\tolua.c:62: error: `L' undeclared (first use in this function)src\bin\tolua.c:62: error: (Each undeclared identifier is reported only oncesrc\bin\tolua.c:62: error: for each function it appears in.)src\bin\tolua.c:62: error: `f' undeclared (first use in this function)src\bin\tolua.c:63: error: `v' undeclared (first use in this function)src\bin\tolua.c:64: warning: implicit declaration of function `lua_settable'src\bin\tolua.c:64: error: `table' undeclared (first use in this function)src\bin\tolua.c: At top level:src\bin\tolua.c:67: error: syntax error before '*' tokensrc\bin\tolua.c: In function `add_extra':src\bin\tolua.c:69: warning: implicit declaration of function `lua_getglobal'src\bin\tolua.c:69: error: `L' undeclared (first use in this function)src\bin\tolua.c:70: warning: implicit declaration of function `luaL_getn'src\bin\tolua.c:71: error: `value' undeclared (first use in this function)src\bin\tolua.c:72: warning: implicit declaration of function `lua_rawseti'src\bin\tolua.c:73: warning: implicit declaration of function `lua_pop'src\bin\tolua.c: In function `main':src\bin\tolua.c:89: error: `lua_State' undeclared (first use in this function)src\bin\tolua.c:89: error: `L' undeclared (first use in this function)src\bin\tolua.c:89: warning: implicit declaration of function `lua_open'src\bin\tolua.c:90: warning: implicit declaration of function `luaopen_base'src\bin\tolua.c:91: warning: implicit declaration of function `luaopen_io'src\bin\tolua.c:92: warning: implicit declaration of function `luaopen_string'src\bin\tolua.c:93: warning: implicit declaration of function `luaopen_table'src\bin\tolua.c:94: warning: implicit declaration of function `luaopen_math'src\bin\tolua.c:95: warning: implicit declaration of function `luaopen_debug'src\bin\tolua.c:98: warning: implicit declaration of function `lua_setglobal'src\bin\tolua.c:99: error: `LUA_VERSION' undeclared (first use in this function)src\bin\tolua.c:109: warning: implicit declaration of function `lua_newtable'src\bin\tolua.c:112: warning: implicit declaration of function `lua_pushvalue'src\bin\tolua.c:114: warning: implicit declaration of function `lua_gettop'src\bin\tolua.c:151: error: syntax error before '*' tokenscons: *** [src\bin\tolua.o] Error 1scons: building terminated because of errors.


Trying to compile the "simple" toLua++ has been hell, let alone trying any other Lua binding library.

This topic is closed to new replies.

Advertisement