Skip to content

Commit

Permalink
use constructor syntax and find_if-ify
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Dec 20, 2024
1 parent d08863a commit 723de97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
27 changes: 12 additions & 15 deletions library/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,35 +469,32 @@ bool Plugin::reload(color_ostream &out)

command_result Plugin::invoke(color_ostream &out, const std::string & command, std::vector <std::string> & parameters)
{
Core & c = Core::getInstance();
command_result cr = CR_NOT_IMPLEMENTED;
access->lock_add();
if (state == PS_LOADED) {
for (auto & cmd : commands) {
if (cmd.name != command)
continue;

if (auto cmdIt = std::ranges::find_if(commands, [&](const PluginCommand &cmd) { return cmd.name == command; });
commands.end() != cmdIt)
{
// running interactive things from some other source than the console would break it
if (!out.is_console() && cmd.interactive)
if (!out.is_console() && cmdIt->interactive)
cr = CR_NEEDS_CONSOLE;
else if (cmd.guard) {
CoreSuspender suspend(&c);
if (!cmd.guard(c.getTopViewscreen())) {
else if (cmdIt->guard) {
CoreSuspender suspend;
if (!cmdIt->guard(Core::getInstance().getTopViewscreen())) {
out.printerr("Could not invoke %s: unsuitable UI state.\n", command.c_str());
cr = CR_WRONG_USAGE;
}
else {
cr = cmd.function(out, parameters);
cr = cmdIt->function(out, parameters);
}
}
else if (cmd.unlocked) {
cr = cmd.function(out, parameters);
else if (cmdIt->unlocked) {
cr = cmdIt->function(out, parameters);
}
else {
CoreSuspender suspend(&c);
cr = cmd.function(out, parameters);
CoreSuspender suspend;
cr = cmdIt->function(out, parameters);
}
break;
}
}
access->lock_sub();
Expand Down
12 changes: 6 additions & 6 deletions library/include/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ namespace DFHack
bool unlocked_ = false,
const char * usage_ = ""
)
: name(_name), description(_description), function(function_),
interactive(interactive_), unlocked(unlocked_), guard(NULL),
usage(usage_)
: name{_name}, description{_description}, function{function_},
interactive{interactive_}, unlocked{unlocked_}, guard{NULL},
usage{usage_}
{
fix_usage();
}
Expand All @@ -114,9 +114,9 @@ namespace DFHack
command_function function_,
command_hotkey_guard guard_,
const char * usage_ = "")
: name(_name), description(_description), function(function_),
interactive(false), unlocked(false), guard(guard_),
usage(usage_)
: name{_name}, description{_description}, function{function_},
interactive{false}, unlocked{false}, guard{guard_},
usage{usage_}
{
fix_usage();
}
Expand Down

0 comments on commit 723de97

Please sign in to comment.