]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_item.cpp
Reject registering node with empty name
[dragonfireclient.git] / src / script / lua_api / l_item.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "lua_api/l_item.h"
21 #include "lua_api/l_itemstackmeta.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
25 #include "common/c_packer.h"
26 #include "itemdef.h"
27 #include "nodedef.h"
28 #include "server.h"
29 #include "inventory.h"
30 #include "log.h"
31
32
33 // garbage collector
34 int LuaItemStack::gc_object(lua_State *L)
35 {
36         LuaItemStack *o = *(LuaItemStack **)(lua_touserdata(L, 1));
37         delete o;
38         return 0;
39 }
40
41 // __tostring metamethod
42 int LuaItemStack::mt_tostring(lua_State *L)
43 {
44         LuaItemStack *o = checkobject(L, 1);
45         std::string itemstring = o->m_stack.getItemString(false);
46         lua_pushfstring(L, "ItemStack(\"%s\")", itemstring.c_str());
47         return 1;
48 }
49
50 // is_empty(self) -> true/false
51 int LuaItemStack::l_is_empty(lua_State *L)
52 {
53         NO_MAP_LOCK_REQUIRED;
54         LuaItemStack *o = checkobject(L, 1);
55         ItemStack &item = o->m_stack;
56         lua_pushboolean(L, item.empty());
57         return 1;
58 }
59
60 // get_name(self) -> string
61 int LuaItemStack::l_get_name(lua_State *L)
62 {
63         NO_MAP_LOCK_REQUIRED;
64         LuaItemStack *o = checkobject(L, 1);
65         ItemStack &item = o->m_stack;
66         lua_pushstring(L, item.name.c_str());
67         return 1;
68 }
69
70 // set_name(self, name)
71 int LuaItemStack::l_set_name(lua_State *L)
72 {
73         NO_MAP_LOCK_REQUIRED;
74         LuaItemStack *o = checkobject(L, 1);
75         ItemStack &item = o->m_stack;
76
77         bool status = true;
78         item.name = luaL_checkstring(L, 2);
79         if (item.name.empty() || item.empty()) {
80                 item.clear();
81                 status = false;
82         }
83
84         lua_pushboolean(L, status);
85         return 1;
86 }
87
88 // get_count(self) -> number
89 int LuaItemStack::l_get_count(lua_State *L)
90 {
91         NO_MAP_LOCK_REQUIRED;
92         LuaItemStack *o = checkobject(L, 1);
93         ItemStack &item = o->m_stack;
94         lua_pushinteger(L, item.count);
95         return 1;
96 }
97
98 // set_count(self, number)
99 int LuaItemStack::l_set_count(lua_State *L)
100 {
101         NO_MAP_LOCK_REQUIRED;
102         LuaItemStack *o = checkobject(L, 1);
103         ItemStack &item = o->m_stack;
104
105         bool status;
106         lua_Integer count = luaL_checkinteger(L, 2);
107         if (count > 0 && count <= 65535) {
108                 item.count = count;
109                 status = true;
110         } else {
111                 item.clear();
112                 status = false;
113         }
114
115         lua_pushboolean(L, status);
116         return 1;
117 }
118
119 // get_wear(self) -> number
120 int LuaItemStack::l_get_wear(lua_State *L)
121 {
122         NO_MAP_LOCK_REQUIRED;
123         LuaItemStack *o = checkobject(L, 1);
124         ItemStack &item = o->m_stack;
125         lua_pushinteger(L, item.wear);
126         return 1;
127 }
128
129 // set_wear(self, number)
130 int LuaItemStack::l_set_wear(lua_State *L)
131 {
132         NO_MAP_LOCK_REQUIRED;
133         LuaItemStack *o = checkobject(L, 1);
134         ItemStack &item = o->m_stack;
135
136         bool status;
137         lua_Integer wear = luaL_checkinteger(L, 2);
138         if (wear <= 65535) {
139                 item.wear = wear;
140                 status = true;
141         } else {
142                 item.clear();
143                 status = false;
144         }
145
146         lua_pushboolean(L, status);
147         return 1;
148 }
149
150 // get_meta(self) -> string
151 int LuaItemStack::l_get_meta(lua_State *L)
152 {
153         NO_MAP_LOCK_REQUIRED;
154         LuaItemStack *o = checkobject(L, 1);
155         ItemStackMetaRef::create(L, &o->m_stack);
156         return 1;
157 }
158
159 // DEPRECATED
160 // get_metadata(self) -> string
161 int LuaItemStack::l_get_metadata(lua_State *L)
162 {
163         NO_MAP_LOCK_REQUIRED;
164         LuaItemStack *o = checkobject(L, 1);
165         ItemStack &item = o->m_stack;
166         const std::string &value = item.metadata.getString("");
167         lua_pushlstring(L, value.c_str(), value.size());
168         return 1;
169 }
170
171 // DEPRECATED
172 // set_metadata(self, string)
173 int LuaItemStack::l_set_metadata(lua_State *L)
174 {
175         NO_MAP_LOCK_REQUIRED;
176         LuaItemStack *o = checkobject(L, 1);
177         ItemStack &item = o->m_stack;
178
179         size_t len = 0;
180         const char *ptr = luaL_checklstring(L, 2, &len);
181         item.metadata.setString("", std::string(ptr, len));
182
183         lua_pushboolean(L, true);
184         return 1;
185 }
186
187 // get_description(self)
188 int LuaItemStack::l_get_description(lua_State *L)
189 {
190         NO_MAP_LOCK_REQUIRED;
191         LuaItemStack *o = checkobject(L, 1);
192         std::string desc = o->m_stack.getDescription(getGameDef(L)->idef());
193         lua_pushstring(L, desc.c_str());
194         return 1;
195 }
196
197 // get_short_description(self)
198 int LuaItemStack::l_get_short_description(lua_State *L)
199 {
200         NO_MAP_LOCK_REQUIRED;
201         LuaItemStack *o = checkobject(L, 1);
202         std::string desc = o->m_stack.getShortDescription(getGameDef(L)->idef());
203         lua_pushstring(L, desc.c_str());
204         return 1;
205 }
206
207 // clear(self) -> true
208 int LuaItemStack::l_clear(lua_State *L)
209 {
210         NO_MAP_LOCK_REQUIRED;
211         LuaItemStack *o = checkobject(L, 1);
212         o->m_stack.clear();
213         lua_pushboolean(L, true);
214         return 1;
215 }
216
217 // replace(self, itemstack or itemstring or table or nil) -> true
218 int LuaItemStack::l_replace(lua_State *L)
219 {
220         NO_MAP_LOCK_REQUIRED;
221         LuaItemStack *o = checkobject(L, 1);
222         o->m_stack = read_item(L, 2, getGameDef(L)->idef());
223         lua_pushboolean(L, true);
224         return 1;
225 }
226
227 // to_string(self) -> string
228 int LuaItemStack::l_to_string(lua_State *L)
229 {
230         NO_MAP_LOCK_REQUIRED;
231         LuaItemStack *o = checkobject(L, 1);
232         std::string itemstring = o->m_stack.getItemString();
233         lua_pushstring(L, itemstring.c_str());
234         return 1;
235 }
236
237 // to_table(self) -> table or nil
238 int LuaItemStack::l_to_table(lua_State *L)
239 {
240         NO_MAP_LOCK_REQUIRED;
241         LuaItemStack *o = checkobject(L, 1);
242         const ItemStack &item = o->m_stack;
243         if(item.empty())
244         {
245                 lua_pushnil(L);
246         }
247         else
248         {
249                 lua_newtable(L);
250                 lua_pushstring(L, item.name.c_str());
251                 lua_setfield(L, -2, "name");
252                 lua_pushinteger(L, item.count);
253                 lua_setfield(L, -2, "count");
254                 lua_pushinteger(L, item.wear);
255                 lua_setfield(L, -2, "wear");
256
257                 const std::string &metadata_str = item.metadata.getString("");
258                 lua_pushlstring(L, metadata_str.c_str(), metadata_str.size());
259                 lua_setfield(L, -2, "metadata");
260
261                 lua_newtable(L);
262                 const StringMap &fields = item.metadata.getStrings();
263                 for (const auto &field : fields) {
264                         const std::string &name = field.first;
265                         if (name.empty())
266                                 continue;
267                         const std::string &value = field.second;
268                         lua_pushlstring(L, name.c_str(), name.size());
269                         lua_pushlstring(L, value.c_str(), value.size());
270                         lua_settable(L, -3);
271                 }
272                 lua_setfield(L, -2, "meta");
273         }
274         return 1;
275 }
276
277 // get_stack_max(self) -> number
278 int LuaItemStack::l_get_stack_max(lua_State *L)
279 {
280         NO_MAP_LOCK_REQUIRED;
281         LuaItemStack *o = checkobject(L, 1);
282         ItemStack &item = o->m_stack;
283         lua_pushinteger(L, item.getStackMax(getGameDef(L)->idef()));
284         return 1;
285 }
286
287 // get_free_space(self) -> number
288 int LuaItemStack::l_get_free_space(lua_State *L)
289 {
290         NO_MAP_LOCK_REQUIRED;
291         LuaItemStack *o = checkobject(L, 1);
292         ItemStack &item = o->m_stack;
293         lua_pushinteger(L, item.freeSpace(getGameDef(L)->idef()));
294         return 1;
295 }
296
297 // is_known(self) -> true/false
298 // Checks if the item is defined.
299 int LuaItemStack::l_is_known(lua_State *L)
300 {
301         NO_MAP_LOCK_REQUIRED;
302         LuaItemStack *o = checkobject(L, 1);
303         ItemStack &item = o->m_stack;
304         bool is_known = item.isKnown(getGameDef(L)->idef());
305         lua_pushboolean(L, is_known);
306         return 1;
307 }
308
309 // get_definition(self) -> table
310 // Returns the item definition table from registered_items,
311 // or a fallback one (name="unknown")
312 int LuaItemStack::l_get_definition(lua_State *L)
313 {
314         NO_MAP_LOCK_REQUIRED;
315         LuaItemStack *o = checkobject(L, 1);
316         ItemStack &item = o->m_stack;
317
318         // Get registered_items[name]
319         lua_getglobal(L, "core");
320         lua_getfield(L, -1, "registered_items");
321         luaL_checktype(L, -1, LUA_TTABLE);
322         lua_getfield(L, -1, item.name.c_str());
323         if(lua_isnil(L, -1))
324         {
325                 lua_pop(L, 1);
326                 lua_getfield(L, -1, "unknown");
327         }
328         return 1;
329 }
330
331 // get_tool_capabilities(self) -> table
332 // Returns the effective tool digging properties.
333 // Returns those of the hand ("") if this item has none associated.
334 int LuaItemStack::l_get_tool_capabilities(lua_State *L)
335 {
336         NO_MAP_LOCK_REQUIRED;
337         LuaItemStack *o = checkobject(L, 1);
338         ItemStack &item = o->m_stack;
339         const ToolCapabilities &prop =
340                 item.getToolCapabilities(getGameDef(L)->idef());
341         push_tool_capabilities(L, prop);
342         return 1;
343 }
344
345 // add_wear(self, amount) -> true/false
346 // The range for "amount" is [0,65535]. Wear is only added if the item
347 // is a tool. Adding wear might destroy the item.
348 // Returns true if the item is (or was) a tool.
349 int LuaItemStack::l_add_wear(lua_State *L)
350 {
351         NO_MAP_LOCK_REQUIRED;
352         LuaItemStack *o = checkobject(L, 1);
353         ItemStack &item = o->m_stack;
354         int amount = lua_tointeger(L, 2);
355         bool result = item.addWear(amount, getGameDef(L)->idef());
356         lua_pushboolean(L, result);
357         return 1;
358 }
359
360 // add_item(self, itemstack or itemstring or table or nil) -> itemstack
361 // Returns leftover item stack
362 int LuaItemStack::l_add_item(lua_State *L)
363 {
364         NO_MAP_LOCK_REQUIRED;
365         LuaItemStack *o = checkobject(L, 1);
366         ItemStack &item = o->m_stack;
367         ItemStack newitem = read_item(L, -1, getGameDef(L)->idef());
368         ItemStack leftover = item.addItem(newitem, getGameDef(L)->idef());
369         create(L, leftover);
370         return 1;
371 }
372
373 // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
374 // First return value is true iff the new item fits fully into the stack
375 // Second return value is the would-be-left-over item stack
376 int LuaItemStack::l_item_fits(lua_State *L)
377 {
378         NO_MAP_LOCK_REQUIRED;
379         LuaItemStack *o = checkobject(L, 1);
380         ItemStack &item = o->m_stack;
381         ItemStack newitem = read_item(L, 2, getGameDef(L)->idef());
382         ItemStack restitem;
383         bool fits = item.itemFits(newitem, &restitem, getGameDef(L)->idef());
384         lua_pushboolean(L, fits);  // first return value
385         create(L, restitem);       // second return value
386         return 2;
387 }
388
389 // take_item(self, takecount=1) -> itemstack
390 int LuaItemStack::l_take_item(lua_State *L)
391 {
392         NO_MAP_LOCK_REQUIRED;
393         LuaItemStack *o = checkobject(L, 1);
394         ItemStack &item = o->m_stack;
395         u32 takecount = 1;
396         if(!lua_isnone(L, 2))
397                 takecount = luaL_checkinteger(L, 2);
398         ItemStack taken = item.takeItem(takecount);
399         create(L, taken);
400         return 1;
401 }
402
403 // peek_item(self, peekcount=1) -> itemstack
404 int LuaItemStack::l_peek_item(lua_State *L)
405 {
406         NO_MAP_LOCK_REQUIRED;
407         LuaItemStack *o = checkobject(L, 1);
408         ItemStack &item = o->m_stack;
409         u32 peekcount = 1;
410         if(!lua_isnone(L, 2))
411                 peekcount = lua_tointeger(L, 2);
412         ItemStack peekaboo = item.peekItem(peekcount);
413         create(L, peekaboo);
414         return 1;
415 }
416
417 LuaItemStack::LuaItemStack(const ItemStack &item):
418         m_stack(item)
419 {
420 }
421
422 const ItemStack& LuaItemStack::getItem() const
423 {
424         return m_stack;
425 }
426 ItemStack& LuaItemStack::getItem()
427 {
428         return m_stack;
429 }
430
431 // LuaItemStack(itemstack or itemstring or table or nil)
432 // Creates an LuaItemStack and leaves it on top of stack
433 int LuaItemStack::create_object(lua_State *L)
434 {
435         NO_MAP_LOCK_REQUIRED;
436         ItemStack item;
437         if (!lua_isnone(L, 1))
438                 item = read_item(L, 1, getGameDef(L)->idef());
439         LuaItemStack *o = new LuaItemStack(item);
440         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
441         luaL_getmetatable(L, className);
442         lua_setmetatable(L, -2);
443         return 1;
444 }
445
446 // Not callable from Lua
447 int LuaItemStack::create(lua_State *L, const ItemStack &item)
448 {
449         NO_MAP_LOCK_REQUIRED;
450         LuaItemStack *o = new LuaItemStack(item);
451         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
452         luaL_getmetatable(L, className);
453         lua_setmetatable(L, -2);
454         return 1;
455 }
456
457 LuaItemStack *LuaItemStack::checkobject(lua_State *L, int narg)
458 {
459         return *(LuaItemStack **)luaL_checkudata(L, narg, className);
460 }
461
462 void *LuaItemStack::packIn(lua_State *L, int idx)
463 {
464         LuaItemStack *o = checkobject(L, idx);
465         return new ItemStack(o->getItem());
466 }
467
468 void LuaItemStack::packOut(lua_State *L, void *ptr)
469 {
470         ItemStack *stack = reinterpret_cast<ItemStack*>(ptr);
471         if (L)
472                 create(L, *stack);
473         delete stack;
474 }
475
476 void LuaItemStack::Register(lua_State *L)
477 {
478         lua_newtable(L);
479         int methodtable = lua_gettop(L);
480         luaL_newmetatable(L, className);
481         int metatable = lua_gettop(L);
482
483         // hide metatable from Lua getmetatable()
484         lua_pushliteral(L, "__metatable");
485         lua_pushvalue(L, methodtable);
486         lua_settable(L, metatable);
487
488         lua_pushliteral(L, "__index");
489         lua_pushvalue(L, methodtable);
490         lua_settable(L, metatable);
491
492         lua_pushliteral(L, "__gc");
493         lua_pushcfunction(L, gc_object);
494         lua_settable(L, metatable);
495
496         lua_pushliteral(L, "__tostring");
497         lua_pushcfunction(L, mt_tostring);
498         lua_settable(L, metatable);
499
500         lua_pop(L, 1);  // drop metatable
501
502         luaL_register(L, nullptr, methods);  // fill methodtable
503         lua_pop(L, 1);  // drop methodtable
504
505         // Can be created from Lua (ItemStack(itemstack or itemstring or table or nil))
506         lua_register(L, className, create_object);
507
508         script_register_packer(L, className, packIn, packOut);
509 }
510
511 const char LuaItemStack::className[] = "ItemStack";
512 const luaL_Reg LuaItemStack::methods[] = {
513         luamethod(LuaItemStack, is_empty),
514         luamethod(LuaItemStack, get_name),
515         luamethod(LuaItemStack, set_name),
516         luamethod(LuaItemStack, get_count),
517         luamethod(LuaItemStack, set_count),
518         luamethod(LuaItemStack, get_wear),
519         luamethod(LuaItemStack, set_wear),
520         luamethod(LuaItemStack, get_meta),
521         luamethod(LuaItemStack, get_metadata),
522         luamethod(LuaItemStack, set_metadata),
523         luamethod(LuaItemStack, get_description),
524         luamethod(LuaItemStack, get_short_description),
525         luamethod(LuaItemStack, clear),
526         luamethod(LuaItemStack, replace),
527         luamethod(LuaItemStack, to_string),
528         luamethod(LuaItemStack, to_table),
529         luamethod(LuaItemStack, get_stack_max),
530         luamethod(LuaItemStack, get_free_space),
531         luamethod(LuaItemStack, is_known),
532         luamethod(LuaItemStack, get_definition),
533         luamethod(LuaItemStack, get_tool_capabilities),
534         luamethod(LuaItemStack, add_wear),
535         luamethod(LuaItemStack, add_item),
536         luamethod(LuaItemStack, item_fits),
537         luamethod(LuaItemStack, take_item),
538         luamethod(LuaItemStack, peek_item),
539         {0,0}
540 };
541
542 /*
543         ItemDefinition
544 */
545
546 // register_item_raw({lots of stuff})
547 int ModApiItemMod::l_register_item_raw(lua_State *L)
548 {
549         NO_MAP_LOCK_REQUIRED;
550         luaL_checktype(L, 1, LUA_TTABLE);
551         int table = 1;
552
553         // Get the writable item and node definition managers from the server
554         IWritableItemDefManager *idef =
555                         getServer(L)->getWritableItemDefManager();
556         NodeDefManager *ndef =
557                         getServer(L)->getWritableNodeDefManager();
558
559         // Check if name is defined
560         std::string name;
561         lua_getfield(L, table, "name");
562         if(lua_isstring(L, -1)){
563                 name = readParam<std::string>(L, -1);
564         } else {
565                 throw LuaError("register_item_raw: name is not defined or not a string");
566         }
567
568         // Check if on_use is defined
569
570         ItemDefinition def;
571         // Set a distinctive default value to check if this is set
572         def.node_placement_prediction = "__default";
573
574         // Read the item definition
575         read_item_definition(L, table, def, def);
576
577         // Default to having client-side placement prediction for nodes
578         // ("" in item definition sets it off)
579         if(def.node_placement_prediction == "__default"){
580                 if(def.type == ITEM_NODE)
581                         def.node_placement_prediction = name;
582                 else
583                         def.node_placement_prediction = "";
584         }
585
586         // Register item definition
587         idef->registerItem(def);
588
589         // Read the node definition (content features) and register it
590         if (def.type == ITEM_NODE) {
591                 ContentFeatures f;
592                 read_content_features(L, f, table);
593                 // when a mod reregisters ignore, only texture changes and such should
594                 // be done
595                 if (f.name == "ignore")
596                         return 0;
597                 // This would break everything
598                 if (f.name.empty())
599                         throw LuaError("Cannot register node with empty name");
600
601                 content_t id = ndef->set(f.name, f);
602
603                 if (id > MAX_REGISTERED_CONTENT) {
604                         throw LuaError("Number of registerable nodes ("
605                                         + itos(MAX_REGISTERED_CONTENT+1)
606                                         + ") exceeded (" + name + ")");
607                 }
608         }
609
610         return 0; /* number of results */
611 }
612
613 // unregister_item(name)
614 int ModApiItemMod::l_unregister_item_raw(lua_State *L)
615 {
616         NO_MAP_LOCK_REQUIRED;
617         std::string name = luaL_checkstring(L, 1);
618
619         IWritableItemDefManager *idef =
620                         getServer(L)->getWritableItemDefManager();
621
622         // Unregister the node
623         if (idef->get(name).type == ITEM_NODE) {
624                 NodeDefManager *ndef =
625                         getServer(L)->getWritableNodeDefManager();
626                 ndef->removeNode(name);
627         }
628
629         idef->unregisterItem(name);
630
631         return 0; /* number of results */
632 }
633
634 // register_alias_raw(name, convert_to_name)
635 int ModApiItemMod::l_register_alias_raw(lua_State *L)
636 {
637         NO_MAP_LOCK_REQUIRED;
638         std::string name = luaL_checkstring(L, 1);
639         std::string convert_to = luaL_checkstring(L, 2);
640
641         // Get the writable item definition manager from the server
642         IWritableItemDefManager *idef =
643                         getServer(L)->getWritableItemDefManager();
644
645         idef->registerAlias(name, convert_to);
646
647         return 0; /* number of results */
648 }
649
650 // get_content_id(name)
651 int ModApiItemMod::l_get_content_id(lua_State *L)
652 {
653         NO_MAP_LOCK_REQUIRED;
654         std::string name = luaL_checkstring(L, 1);
655
656         const IItemDefManager *idef = getGameDef(L)->idef();
657         const NodeDefManager *ndef = getGameDef(L)->ndef();
658
659         // If this is called at mod load time, NodeDefManager isn't aware of
660         // aliases yet, so we need to handle them manually
661         std::string alias_name = idef->getAlias(name);
662
663         content_t content_id;
664         if (alias_name != name) {
665                 if (!ndef->getId(alias_name, content_id))
666                         throw LuaError("Unknown node: " + alias_name +
667                                         " (from alias " + name + ")");
668         } else if (!ndef->getId(name, content_id)) {
669                 throw LuaError("Unknown node: " + name);
670         }
671
672         lua_pushinteger(L, content_id);
673         return 1; /* number of results */
674 }
675
676 // get_name_from_content_id(name)
677 int ModApiItemMod::l_get_name_from_content_id(lua_State *L)
678 {
679         NO_MAP_LOCK_REQUIRED;
680         content_t c = luaL_checkint(L, 1);
681
682         const NodeDefManager *ndef = getGameDef(L)->ndef();
683         const char *name = ndef->get(c).name.c_str();
684
685         lua_pushstring(L, name);
686         return 1; /* number of results */
687 }
688
689 void ModApiItemMod::Initialize(lua_State *L, int top)
690 {
691         API_FCT(register_item_raw);
692         API_FCT(unregister_item_raw);
693         API_FCT(register_alias_raw);
694         API_FCT(get_content_id);
695         API_FCT(get_name_from_content_id);
696 }
697
698 void ModApiItemMod::InitializeAsync(lua_State *L, int top)
699 {
700         // all read-only functions
701         API_FCT(get_content_id);
702         API_FCT(get_name_from_content_id);
703 }