Skip to content

Commit b9e36c4

Browse files
authored
Merge pull request #5158 from ab9rf/dry-up-viewscreen
clean up moisture in `Screen.cpp`
2 parents 6b0e049 + fd74daa commit b9e36c4

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

library/include/modules/Screen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ namespace DFHack
380380
bool safe_call_lua(int (*pf)(lua_State *), int args, int rvs);
381381
static dfhack_lua_viewscreen *get_self(lua_State *L);
382382

383+
template<typename T> T* getSelected(const char* method_name);
384+
383385
static int do_destroy(lua_State *L);
384386
static int do_render(lua_State *L);
385387
static int do_notify(lua_State *L);

library/modules/Screen.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,60 +1116,48 @@ void dfhack_lua_viewscreen::onDismiss()
11161116
safe_call_lua(do_notify, 1, 0);
11171117
}
11181118

1119-
df::unit *dfhack_lua_viewscreen::getSelectedUnit()
1119+
template<typename T>
1120+
T* dfhack_lua_viewscreen::getSelected(const char* method_name)
11201121
{
11211122
Lua::StackUnwinder frame(Lua::Core::State);
1122-
lua_pushstring(Lua::Core::State, "onGetSelectedUnit");
1123+
lua_pushstring(Lua::Core::State, method_name);
11231124
safe_call_lua(do_notify, 1, 1);
1124-
return Lua::GetDFObject<df::unit>(Lua::Core::State, -1);
1125+
return Lua::GetDFObject<T>(Lua::Core::State, -1);
1126+
}
1127+
1128+
df::unit* dfhack_lua_viewscreen::getSelectedUnit()
1129+
{
1130+
return getSelected<df::unit>("onGetSelectedUnit");
11251131
}
11261132

11271133
df::item *dfhack_lua_viewscreen::getSelectedItem()
11281134
{
1129-
Lua::StackUnwinder frame(Lua::Core::State);
1130-
lua_pushstring(Lua::Core::State, "onGetSelectedItem");
1131-
safe_call_lua(do_notify, 1, 1);
1132-
return Lua::GetDFObject<df::item>(Lua::Core::State, -1);
1135+
return getSelected<df::item>("onGetSelectedItem");
11331136
}
11341137

11351138
df::job *dfhack_lua_viewscreen::getSelectedJob()
11361139
{
1137-
Lua::StackUnwinder frame(Lua::Core::State);
1138-
lua_pushstring(Lua::Core::State, "onGetSelectedJob");
1139-
safe_call_lua(do_notify, 1, 1);
1140-
return Lua::GetDFObject<df::job>(Lua::Core::State, -1);
1140+
return getSelected<df::job>("onGetSelectedJob");
11411141
}
11421142

11431143
df::building *dfhack_lua_viewscreen::getSelectedBuilding()
11441144
{
1145-
Lua::StackUnwinder frame(Lua::Core::State);
1146-
lua_pushstring(Lua::Core::State, "onGetSelectedBuilding");
1147-
safe_call_lua(do_notify, 1, 1);
1148-
return Lua::GetDFObject<df::building>(Lua::Core::State, -1);
1145+
return getSelected<df::building>("onGetSelectedBuilding");
11491146
}
11501147

11511148
df::building_stockpilest *dfhack_lua_viewscreen::getSelectedStockpile()
11521149
{
1153-
Lua::StackUnwinder frame(Lua::Core::State);
1154-
lua_pushstring(Lua::Core::State, "onGetSelectedStockpile");
1155-
safe_call_lua(do_notify, 1, 1);
1156-
return Lua::GetDFObject<df::building_stockpilest>(Lua::Core::State, -1);
1150+
return getSelected<df::building_stockpilest>("onGetSelectedStockpile");
11571151
}
11581152

11591153
df::building_civzonest *dfhack_lua_viewscreen::getSelectedCivZone()
11601154
{
1161-
Lua::StackUnwinder frame(Lua::Core::State);
1162-
lua_pushstring(Lua::Core::State, "onGetSelectedCivZone");
1163-
safe_call_lua(do_notify, 1, 1);
1164-
return Lua::GetDFObject<df::building_civzonest>(Lua::Core::State, -1);
1155+
return getSelected<df::building_civzonest>("onGetSelectedCivZone");
11651156
}
11661157

11671158
df::plant *dfhack_lua_viewscreen::getSelectedPlant()
11681159
{
1169-
Lua::StackUnwinder frame(Lua::Core::State);
1170-
lua_pushstring(Lua::Core::State, "onGetSelectedPlant");
1171-
safe_call_lua(do_notify, 1, 1);
1172-
return Lua::GetDFObject<df::plant>(Lua::Core::State, -1);
1160+
return getSelected<df::plant>("onGetSelectedPlant");
11731161
}
11741162

11751163
#include "DataStaticsFields.inc"

0 commit comments

Comments
 (0)