Browse Source

Lua: Add dev.items.get() command

pull/8079/head
Gleb Mazovetskiy 8 months ago committed by Anders Jenbo
parent
commit
7ff65351cb
  1. 16
      Source/lua/metadoc.hpp
  2. 28
      Source/lua/modules/dev/items.cpp

16
Source/lua/metadoc.hpp

@ -35,12 +35,18 @@ inline std::string LuaUserdataMemberTypeKey(std::string_view key)
namespace lua_metadoc_internal {
template <typename U>
inline void SetUsertypeSignatureAndDocstring(sol::usertype<U> &table, std::string_view key, const char *signature, const char *doc, LuaUserdataMemberType memberType)
void SetUsertypeSignatureAndDocstring(sol::usertype<U> &table, std::string_view key, const char *signature, const char *doc, LuaUserdataMemberType memberType)
{
table.set(LuaSignatureKey(key), sol::var(signature));
table.set(LuaDocstringKey(key), sol::var(doc));
table.set(LuaUserdataMemberTypeKey(key), sol::var(static_cast<uint8_t>(memberType)));
}
inline void SetSignatureAndDocstring(sol::table &table, std::string_view key, const char *signature, const char *doc)
{
table.set(LuaSignatureKey(key), signature);
table.set(LuaDocstringKey(key), doc);
}
} // namespace lua_metadoc_internal
template <typename U, typename T>
@ -75,20 +81,18 @@ template <typename T>
void LuaSetDoc(sol::table &table, std::string_view key, const char *signature, const char *doc, T &&value)
{
table.set(key, std::forward<T>(value));
table.set(LuaSignatureKey(key), signature);
table.set(LuaDocstringKey(key), doc);
lua_metadoc_internal::SetSignatureAndDocstring(table, key, signature, doc);
}
template <typename T>
void LuaSetDocFn(sol::table &table, std::string_view key, const char *signature, const char *doc, T &&value)
{
table.set_function(key, std::forward<T>(value));
table.set(LuaSignatureKey(key), signature);
table.set(LuaDocstringKey(key), doc);
lua_metadoc_internal::SetSignatureAndDocstring(table, key, signature, doc);
}
template <typename T>
void LuaSetDocFn(sol::table &table, std::string_view key, std::string_view signature, T &&value)
void LuaSetDocFn(sol::table &table, std::string_view key, const char *signature, T &&value)
{
table.set_function(key, std::forward<T>(value));
table.set(LuaSignatureKey(key), signature);

28
Source/lua/modules/dev/items.cpp

@ -20,21 +20,24 @@ namespace devilution {
namespace {
std::string DebugCmdItemInfo()
const Item *DebugCmdGetItem()
{
Player &myPlayer = *MyPlayer;
Item *pItem = nullptr;
if (!myPlayer.HoldItem.isEmpty()) {
pItem = &myPlayer.HoldItem;
} else if (pcursinvitem != -1) {
if (pcursinvitem <= INVITEM_INV_LAST)
pItem = &myPlayer.InvList[pcursinvitem - INVITEM_INV_FIRST];
else
pItem = &myPlayer.SpdList[pcursinvitem - INVITEM_BELT_FIRST];
} else if (pcursitem != -1) {
pItem = &Items[pcursitem];
const Player &myPlayer = *MyPlayer;
if (!myPlayer.HoldItem.isEmpty()) return &myPlayer.HoldItem;
if (pcursinvitem != -1) {
if (pcursinvitem < INVITEM_INV_FIRST) return &myPlayer.InvBody[pcursinvitem];
if (pcursinvitem <= INVITEM_INV_LAST) return &myPlayer.InvList[pcursinvitem - INVITEM_INV_FIRST];
return &myPlayer.SpdList[pcursinvitem - INVITEM_BELT_FIRST];
}
if (pcursitem != -1) return &Items[pcursitem];
return nullptr;
}
std::string DebugCmdItemInfo()
{
const Item *pItem = DebugCmdGetItem();
if (pItem != nullptr) {
const Player &myPlayer = *MyPlayer;
std::string_view netPackValidation { "N/A" };
if (gbIsMultiplayer) {
ItemNetPack itemPack;
@ -194,6 +197,7 @@ std::string DebugSpawnUniqueItem(std::string itemName)
sol::table LuaDevItemsModule(sol::state_view &lua)
{
sol::table table = lua.create_table();
LuaSetDocFn(table, "get", "() -> Item", "Get the currently selected item.", &DebugCmdGetItem);
LuaSetDocFn(table, "info", "()", "Show info of currently selected item.", &DebugCmdItemInfo);
LuaSetDocFn(table, "spawn", "(name: string)", "Attempt to generate an item.", &DebugSpawnItem);
LuaSetDocFn(table, "spawnUnique", "(name: string)", "Attempt to generate a unique item.", &DebugSpawnUniqueItem);

Loading…
Cancel
Save