Skip to content

Commit 18211f5

Browse files
committed
Lua: Add dev.items.get() command
1 parent 20d3f2e commit 18211f5

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

Source/lua/metadoc.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ inline std::string LuaUserdataMemberTypeKey(std::string_view key)
3535

3636
namespace lua_metadoc_internal {
3737
template <typename U>
38-
inline void SetUsertypeSignatureAndDocstring(sol::usertype<U> &table, std::string_view key, const char *signature, const char *doc, LuaUserdataMemberType memberType)
38+
void SetUsertypeSignatureAndDocstring(sol::usertype<U> &table, std::string_view key, const char *signature, const char *doc, LuaUserdataMemberType memberType)
3939
{
4040
table.set(LuaSignatureKey(key), sol::var(signature));
4141
table.set(LuaDocstringKey(key), sol::var(doc));
4242
table.set(LuaUserdataMemberTypeKey(key), sol::var(static_cast<uint8_t>(memberType)));
4343
}
44+
45+
inline void SetSignatureAndDocstring(sol::table &table, std::string_view key, const char *signature, const char *doc)
46+
{
47+
table.set(LuaSignatureKey(key), signature);
48+
table.set(LuaDocstringKey(key), doc);
49+
}
4450
} // namespace lua_metadoc_internal
4551

4652
template <typename U, typename T>
@@ -75,20 +81,18 @@ template <typename T>
7581
void LuaSetDoc(sol::table &table, std::string_view key, const char *signature, const char *doc, T &&value)
7682
{
7783
table.set(key, std::forward<T>(value));
78-
table.set(LuaSignatureKey(key), signature);
79-
table.set(LuaDocstringKey(key), doc);
84+
lua_metadoc_internal::SetSignatureAndDocstring(table, key, signature, doc);
8085
}
8186

8287
template <typename T>
8388
void LuaSetDocFn(sol::table &table, std::string_view key, const char *signature, const char *doc, T &&value)
8489
{
8590
table.set_function(key, std::forward<T>(value));
86-
table.set(LuaSignatureKey(key), signature);
87-
table.set(LuaDocstringKey(key), doc);
91+
lua_metadoc_internal::SetSignatureAndDocstring(table, key, signature, doc);
8892
}
8993

9094
template <typename T>
91-
void LuaSetDocFn(sol::table &table, std::string_view key, std::string_view signature, T &&value)
95+
void LuaSetDocFn(sol::table &table, std::string_view key, const char *signature, T &&value)
9296
{
9397
table.set_function(key, std::forward<T>(value));
9498
table.set(LuaSignatureKey(key), signature);

Source/lua/modules/dev/items.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,24 @@ namespace devilution {
2020

2121
namespace {
2222

23-
std::string DebugCmdItemInfo()
23+
const Item *DebugCmdGetItem()
2424
{
25-
Player &myPlayer = *MyPlayer;
26-
Item *pItem = nullptr;
27-
if (!myPlayer.HoldItem.isEmpty()) {
28-
pItem = &myPlayer.HoldItem;
29-
} else if (pcursinvitem != -1) {
30-
if (pcursinvitem <= INVITEM_INV_LAST)
31-
pItem = &myPlayer.InvList[pcursinvitem - INVITEM_INV_FIRST];
32-
else
33-
pItem = &myPlayer.SpdList[pcursinvitem - INVITEM_BELT_FIRST];
34-
} else if (pcursitem != -1) {
35-
pItem = &Items[pcursitem];
25+
const Player &myPlayer = *MyPlayer;
26+
if (!myPlayer.HoldItem.isEmpty()) return &myPlayer.HoldItem;
27+
if (pcursinvitem != -1) {
28+
if (pcursinvitem < INVITEM_INV_FIRST) return &myPlayer.InvBody[pcursinvitem];
29+
if (pcursinvitem <= INVITEM_INV_LAST) return &myPlayer.InvList[pcursinvitem - INVITEM_INV_FIRST];
30+
return &myPlayer.SpdList[pcursinvitem - INVITEM_BELT_FIRST];
3631
}
32+
if (pcursitem != -1) return &Items[pcursitem];
33+
return nullptr;
34+
}
35+
36+
std::string DebugCmdItemInfo()
37+
{
38+
const Item *pItem = DebugCmdGetItem();
3739
if (pItem != nullptr) {
40+
const Player &myPlayer = *MyPlayer;
3841
std::string_view netPackValidation { "N/A" };
3942
if (gbIsMultiplayer) {
4043
ItemNetPack itemPack;
@@ -194,6 +197,7 @@ std::string DebugSpawnUniqueItem(std::string itemName)
194197
sol::table LuaDevItemsModule(sol::state_view &lua)
195198
{
196199
sol::table table = lua.create_table();
200+
LuaSetDocFn(table, "get", "() -> Item", "Get the currently selected item.", &DebugCmdGetItem);
197201
LuaSetDocFn(table, "info", "()", "Show info of currently selected item.", &DebugCmdItemInfo);
198202
LuaSetDocFn(table, "spawn", "(name: string)", "Attempt to generate an item.", &DebugSpawnItem);
199203
LuaSetDocFn(table, "spawnUnique", "(name: string)", "Attempt to generate a unique item.", &DebugSpawnUniqueItem);

0 commit comments

Comments
 (0)