1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-16 18:05:22 +02:00

Consolidate API object code (#12728)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
Jude Melton-Houghton
2022-10-04 08:31:36 -04:00
committed by GitHub
parent b21fb18379
commit 7632af3c73
42 changed files with 463 additions and 1079 deletions

View File

@@ -29,13 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
/*
InvRef
*/
InvRef* InvRef::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 *(InvRef**)ud; // unbox pointer
}
Inventory* InvRef::getinv(lua_State *L, InvRef *ref)
{
@@ -71,7 +64,7 @@ int InvRef::gc_object(lua_State *L) {
int InvRef::l_is_empty(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
InventoryList *list = getlist(L, ref, listname);
if(list && list->getUsedSlots() > 0){
@@ -86,7 +79,7 @@ int InvRef::l_is_empty(lua_State *L)
int InvRef::l_get_size(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
InventoryList *list = getlist(L, ref, listname);
if(list){
@@ -101,7 +94,7 @@ int InvRef::l_get_size(lua_State *L)
int InvRef::l_get_width(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
InventoryList *list = getlist(L, ref, listname);
if(list){
@@ -116,7 +109,7 @@ int InvRef::l_get_width(lua_State *L)
int InvRef::l_set_size(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
int newsize = luaL_checknumber(L, 3);
@@ -156,7 +149,7 @@ int InvRef::l_set_size(lua_State *L)
int InvRef::l_set_width(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
int newwidth = luaL_checknumber(L, 3);
Inventory *inv = getinv(L, ref);
@@ -177,7 +170,7 @@ int InvRef::l_set_width(lua_State *L)
int InvRef::l_get_stack(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3) - 1;
InventoryList *list = getlist(L, ref, listname);
@@ -192,7 +185,7 @@ int InvRef::l_get_stack(lua_State *L)
int InvRef::l_set_stack(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
int i = luaL_checknumber(L, 3) - 1;
ItemStack newitem = read_item(L, 4, getServer(L)->idef());
@@ -211,7 +204,7 @@ int InvRef::l_set_stack(lua_State *L)
int InvRef::l_get_list(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv = getinv(L, ref);
if (!inv) {
@@ -231,7 +224,7 @@ int InvRef::l_get_list(lua_State *L)
int InvRef::l_set_list(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv = getinv(L, ref);
if(inv == NULL){
@@ -251,7 +244,7 @@ int InvRef::l_set_list(lua_State *L)
int InvRef::l_get_lists(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
Inventory *inv = getinv(L, ref);
if (!inv) {
return 0;
@@ -264,7 +257,7 @@ int InvRef::l_get_lists(lua_State *L)
int InvRef::l_set_lists(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
Inventory *inv = getinv(L, ref);
if (!inv) {
return 0;
@@ -292,7 +285,7 @@ int InvRef::l_set_lists(lua_State *L)
int InvRef::l_add_item(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
@@ -312,7 +305,7 @@ int InvRef::l_add_item(lua_State *L)
int InvRef::l_room_for_item(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
@@ -329,7 +322,7 @@ int InvRef::l_room_for_item(lua_State *L)
int InvRef::l_contains_item(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
@@ -349,7 +342,7 @@ int InvRef::l_contains_item(lua_State *L)
int InvRef::l_remove_item(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const char *listname = luaL_checkstring(L, 2);
ItemStack item = read_item(L, 3, getServer(L)->idef());
InventoryList *list = getlist(L, ref, listname);
@@ -368,7 +361,7 @@ int InvRef::l_remove_item(lua_State *L)
int InvRef::l_get_location(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
InvRef *ref = checkobject(L, 1);
InvRef *ref = checkObject<InvRef>(L, 1);
const InventoryLocation &loc = ref->m_loc;
switch(loc.type){
case InventoryLocation::PLAYER:
@@ -421,27 +414,11 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc)
void InvRef::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, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1); // drop metatable
luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
static const luaL_Reg metamethods[] = {
{"__gc", gc_object},
{0, 0}
};
registerClass(L, className, methods, metamethods);
// Cannot be created from Lua
//lua_register(L, className, create_object);