2017-02-08 00:15:55 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lua_api/l_storage.h"
|
|
|
|
#include "l_internal.h"
|
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
int ModApiStorage::l_get_mod_storage(lua_State *L)
|
|
|
|
{
|
2022-07-23 22:27:07 +02:00
|
|
|
// Note that this is wrapped in Lua, see builtin/common/mod_storage.lua
|
|
|
|
std::string mod_name = readParam<std::string>(L, 1);
|
2017-02-08 00:15:55 +01:00
|
|
|
|
2017-03-16 07:53:39 +01:00
|
|
|
if (IGameDef *gamedef = getGameDef(L)) {
|
2022-09-26 23:03:43 +02:00
|
|
|
StorageRef::create(L, mod_name, gamedef->getModStorageDatabase());
|
2017-02-08 00:15:55 +01:00
|
|
|
} else {
|
|
|
|
assert(false); // this should not happen
|
2022-09-26 23:03:43 +02:00
|
|
|
lua_pushnil(L);
|
2017-02-08 00:15:55 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModApiStorage::Initialize(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
API_FCT(get_mod_storage);
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
void StorageRef::create(lua_State *L, const std::string &mod_name, ModMetadataDatabase *db)
|
2018-06-30 17:11:04 +02:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
StorageRef *o = new StorageRef(mod_name, db);
|
2017-02-08 00:15:55 +01:00
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
|
|
|
luaL_getmetatable(L, className);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int StorageRef::gc_object(lua_State *L)
|
|
|
|
{
|
|
|
|
StorageRef *o = *(StorageRef **)(lua_touserdata(L, 1));
|
|
|
|
delete o;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageRef::Register(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int methodtable = lua_gettop(L);
|
|
|
|
luaL_newmetatable(L, className);
|
|
|
|
int metatable = lua_gettop(L);
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__metatable");
|
|
|
|
lua_pushvalue(L, methodtable);
|
|
|
|
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
|
|
|
|
|
|
|
|
lua_pushliteral(L, "metadata_class");
|
|
|
|
lua_pushlstring(L, className, strlen(className));
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__index");
|
|
|
|
lua_pushvalue(L, methodtable);
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
|
|
lua_pushliteral(L, "__gc");
|
|
|
|
lua_pushcfunction(L, gc_object);
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
2017-03-10 18:25:58 +01:00
|
|
|
lua_pushliteral(L, "__eq");
|
|
|
|
lua_pushcfunction(L, l_equals);
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
2017-02-08 00:15:55 +01:00
|
|
|
lua_pop(L, 1); // drop metatable
|
|
|
|
|
2021-05-25 20:20:16 +02:00
|
|
|
luaL_register(L, nullptr, methods); // fill methodtable
|
2017-02-08 00:15:55 +01:00
|
|
|
lua_pop(L, 1); // drop methodtable
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageRef* StorageRef::checkobject(lua_State *L, int narg)
|
|
|
|
{
|
|
|
|
luaL_checktype(L, narg, LUA_TUSERDATA);
|
|
|
|
void *ud = luaL_checkudata(L, narg, className);
|
|
|
|
if (!ud) luaL_typerror(L, narg, className);
|
|
|
|
return *(StorageRef**)ud; // unbox pointer
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
IMetadata* StorageRef::getmeta(bool auto_create)
|
2017-02-08 00:15:55 +01:00
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
return &m_object;
|
2017-02-08 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StorageRef::clearMeta()
|
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
m_object.clear();
|
2017-02-08 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char StorageRef::className[] = "StorageRef";
|
2017-04-08 09:28:37 +02:00
|
|
|
const luaL_Reg StorageRef::methods[] = {
|
2018-04-30 18:42:51 +02:00
|
|
|
luamethod(MetaDataRef, contains),
|
|
|
|
luamethod(MetaDataRef, get),
|
2017-02-08 00:15:55 +01:00
|
|
|
luamethod(MetaDataRef, get_string),
|
|
|
|
luamethod(MetaDataRef, set_string),
|
|
|
|
luamethod(MetaDataRef, get_int),
|
|
|
|
luamethod(MetaDataRef, set_int),
|
|
|
|
luamethod(MetaDataRef, get_float),
|
|
|
|
luamethod(MetaDataRef, set_float),
|
|
|
|
luamethod(MetaDataRef, to_table),
|
|
|
|
luamethod(MetaDataRef, from_table),
|
2017-03-10 18:25:58 +01:00
|
|
|
luamethod(MetaDataRef, equals),
|
2017-02-08 00:15:55 +01:00
|
|
|
{0,0}
|
|
|
|
};
|