Ejemplo n.º 1
0
void CustomSystem::Init()
{
	lua_State *L = csLua = lua_open();
	luaL_openlibs(L);
	OOLUA::setup_user_lua_state(L);

	PiLuaClasses::RegisterClasses(L);
	LuaConstants::Register(L);

	OOLUA::register_class<CustomSystem>(L);
	OOLUA::register_class<CustomSBody>(L);

	lua_register(L, "load_lua", pi_load_lua);

	lua_pushstring(L, PIONEER_DATA_DIR);
	lua_setglobal(L, "CurrentDirectory");

	lua_pushcfunction(L, pi_lua_panic);
	if (luaL_loadfile(L, (std::string(PIONEER_DATA_DIR) + "/pisystems.lua").c_str())) {
		pi_lua_panic(L);
	} else {
		lua_pcall(L, 0, LUA_MULTRET, -2);
	}

	lua_close(L);
}
Ejemplo n.º 2
0
static void pi_lua_dofile(lua_State *l, const FileSystem::FileData &code)
{
	assert(l);
	LUA_DEBUG_START(l);
	// XXX make this a proper protected call (after working out the implications -- *sigh*)
	lua_pushcfunction(l, &pi_lua_panic);
	if (luaL_loadbuffer(l, code.GetData(), code.GetSize(), code.GetInfo().GetPath().c_str())) {
		pi_lua_panic(l);
	} else {
		int ret = lua_pcall(l, 0, 0, -2);
		if (ret) {
			const char *emsg = lua_tostring(l, -1);
			if (emsg) { fprintf(stderr, "lua error: %s\n", emsg); }
			switch (ret) {
				case LUA_ERRRUN:
					fprintf(stderr, "Lua runtime error in pi_lua_dofile('%s')\n",
							code.GetInfo().GetAbsolutePath().c_str());
					break;
				case LUA_ERRMEM:
					fprintf(stderr, "Memory allocation error in Lua pi_lua_dofile('%s')\n",
							code.GetInfo().GetAbsolutePath().c_str());
					break;
				case LUA_ERRERR:
					fprintf(stderr, "Error running error handler in pi_lua_dofile('%s')\n",
							code.GetInfo().GetAbsolutePath().c_str());
					break;
				default: abort();
			}
			lua_pop(l, 1);
		}
	}
	lua_pop(l, 1);
	LUA_DEBUG_END(l, 0);
}
Ejemplo n.º 3
0
void pi_lua_dofile_recursive(lua_State *l, const std::string &basepath)
{
	DIR *dir;
	struct dirent *entry;
	std::string path;
	struct stat info;
	// putting directory contents into sorted order so order of
	// model definition is consistent
	std::set<std::string> entries;

	LUA_DEBUG_START(l);

	// XXX CurrentDirectory stuff has to go
	lua_getglobal(l, "CurrentDirectory");
	std::string save_dir = luaL_checkstring(l, -1);
	lua_pop(l, 1);

	lua_pushstring(l, basepath.c_str());
	lua_setglobal(l, "CurrentDirectory");

	if ((dir = opendir(basepath.c_str())) == NULL) {
		fprintf(stderr, "opendir: couldn't open directory '%s': %s\n", basepath.c_str(), strerror(errno));
		return;
	}

	while ((entry = readdir(dir)) != NULL) {
		if (entry->d_name[0] != '.') {
			entries.insert(entry->d_name);
		}
	}
	closedir(dir);

	for (std::set<std::string>::iterator i = entries.begin(); i!=entries.end(); ++i) {
		const std::string &name = *i;
		path = basepath + "/" + name;

		if (stat(path.c_str(), &info) != 0) {
			fprintf(stderr, "stat: couldn't get info for '%s': %s\n", path.c_str(), strerror(errno));
			continue;
		}

		if (S_ISDIR(info.st_mode)) {
			pi_lua_dofile_recursive(l, path.c_str());
			continue;
		}

		if ( name.size() >= 4 && name.find(".lua") == name.size() - 4 ) {
			// XXX panic stuff can be removed once the global lua is used everywhere
			lua_pushcfunction(l, pi_lua_panic);
			if (luaL_loadfile(l, path.c_str())) {
				pi_lua_panic(l);
			} else {
				lua_pcall(l, 0, 0, -2);
			}
			lua_pop(l, 1);
		}
	}

	lua_pushstring(l, save_dir.c_str());
	lua_setglobal(l, "CurrentDirectory");

	LUA_DEBUG_END(l, 0);
}