2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: Commands.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "Commands.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::Console::Commands
|
|
|
|
{
|
|
|
|
struct Command;
|
2023-08-22 19:17:57 +00:00
|
|
|
|
|
|
|
struct CommandDispatch
|
|
|
|
{
|
|
|
|
Parse::ParsedObject arguments;
|
|
|
|
AuSPtr<ICommandSubscriber> callback;
|
|
|
|
|
|
|
|
CommandDispatch(const Parse::ParsedObject &arguments, const AuSPtr<ICommandSubscriber> &callback) : arguments(arguments), callback(callback) {}
|
|
|
|
};
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-11-05 17:34:23 +00:00
|
|
|
static AuHashMap<AuString, Command> gCommands;
|
|
|
|
static AuList<Hooks::LineHook_cb> gLineCallbacks;
|
|
|
|
static AuList<CommandDispatch> gPendingCommands;
|
2023-09-15 17:08:02 +00:00
|
|
|
static AuMutex gMutex;
|
|
|
|
static AuMutex gPendingCommandsMutex;
|
|
|
|
static AuOptionalEx<AuWorkerPId_t> gCommandDispatcher;
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
struct Command
|
|
|
|
{
|
|
|
|
AuString tag;
|
|
|
|
Parse::ParseObject commandStructure;
|
2021-10-08 19:51:34 +00:00
|
|
|
AuSPtr<ICommandSubscriber> callback;
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
Command(AuString tag, Parse::ParseObject commandStructure, const AuSPtr<ICommandSubscriber> &callback) : tag(tag), commandStructure(commandStructure), callback(callback) {}
|
2022-01-19 18:18:13 +00:00
|
|
|
Command(AuString tag, Parse::ParseObject commandStructure, AuSPtr<ICommandSubscriber> &&callback) : tag(tag), commandStructure(commandStructure), callback(AuMove(callback)) {}
|
2021-06-27 21:25:29 +00:00
|
|
|
};
|
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
enum class EDispatchType
|
|
|
|
{
|
|
|
|
eNow,
|
|
|
|
eSys,
|
|
|
|
eAsync
|
|
|
|
};
|
|
|
|
|
2023-09-15 17:08:02 +00:00
|
|
|
static bool Dispatch(const AuString &string, EDispatchType type, AuOptionalEx<AuWorkerPId_t> workerId)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
Parse::ParseResult res;
|
2021-10-08 19:51:34 +00:00
|
|
|
AuSPtr<ICommandSubscriber> callback;
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
{
|
2021-10-08 19:51:34 +00:00
|
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
{
|
|
|
|
AuString tag;
|
|
|
|
AuString cmdParse;
|
|
|
|
AuMach offset;
|
|
|
|
|
|
|
|
auto commandSplit = string.find(" ");
|
|
|
|
if (commandSplit != AuString::npos)
|
|
|
|
{
|
|
|
|
tag = string.substr(0, commandSplit);
|
|
|
|
cmdParse = string.substr(commandSplit + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tag = string;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cmdItr = gCommands.find(tag);
|
|
|
|
if (cmdItr == gCommands.end())
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Command {} does not exist", tag);
|
2021-10-08 19:51:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
auto const &cmdEntry = cmdItr->second;
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
offset = 0;
|
2022-03-13 16:48:05 +00:00
|
|
|
Parse::ParseState consumable(AuIO::Character::ProviderFromStringShared(cmdParse, offset));
|
2021-10-08 19:51:34 +00:00
|
|
|
auto status = Parse::Parse(consumable, cmdEntry.commandStructure, res);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
if (!status)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Couldn't parse command {}", string);
|
2021-10-08 19:51:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == EDispatchType::eSys)
|
|
|
|
{
|
2022-06-22 13:42:17 +00:00
|
|
|
return AuTryInsert(gPendingCommands, CommandDispatch(res.result, cmdEntry.callback));
|
2021-10-08 19:51:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback = cmdEntry.callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == EDispatchType::eNow)
|
|
|
|
{
|
2021-10-23 19:21:16 +00:00
|
|
|
callback->OnCommand(res.result);
|
2021-10-08 19:51:34 +00:00
|
|
|
}
|
2023-09-15 17:08:02 +00:00
|
|
|
else if (workerId)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-09-15 17:21:47 +00:00
|
|
|
Async::DispatchOn(workerId.value(), [=]() -> void
|
2023-08-10 02:34:44 +00:00
|
|
|
{
|
|
|
|
callback->OnCommand(res.result);
|
|
|
|
});
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-15 06:05:22 +00:00
|
|
|
AUKN_SYM void RemoveCommand(const AuString &tag)
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
|
|
AuTryRemove(gCommands, tag);
|
|
|
|
}
|
|
|
|
|
2021-10-08 19:51:34 +00:00
|
|
|
AUKN_SYM void AddCommand(const AuString &tag, const Parse::ParseObject &commandStructure, const AuSPtr<ICommandSubscriber> &callback)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
2022-06-22 13:42:17 +00:00
|
|
|
SysAssert(callback);
|
2021-09-06 10:58:08 +00:00
|
|
|
gCommands.insert(AuMakePair(tag, Command(tag, commandStructure, callback)));
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
AUKN_SYM bool DispatchCommand(const AuString &string)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-06-22 13:42:17 +00:00
|
|
|
return Dispatch(string, EDispatchType::eSys, gCommandDispatcher);
|
2021-10-08 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool DispatchCommandThisThread(const AuString &string)
|
|
|
|
{
|
|
|
|
return Dispatch(string, EDispatchType::eNow, {});
|
|
|
|
}
|
2021-11-05 17:34:23 +00:00
|
|
|
|
|
|
|
AUKN_SYM bool DispatchCommandToAsyncRunner(const AuString &string, Async::WorkerPId_t id)
|
2021-10-08 19:51:34 +00:00
|
|
|
{
|
2021-11-05 17:34:23 +00:00
|
|
|
return Dispatch(string, EDispatchType::eAsync, id);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 17:08:02 +00:00
|
|
|
void UpdateDispatcher(AuOptionalEx<AuWorkerPId_t> target)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
AU_LOCK_GUARD(gMutex);
|
|
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2021-09-30 09:19:04 +00:00
|
|
|
// process commands before async app termination
|
|
|
|
if ((!target.has_value()))
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
2022-01-19 17:08:13 +00:00
|
|
|
auto commands = AuExchange(gPendingCommands, {});
|
2021-07-15 16:16:23 +00:00
|
|
|
for (const auto &command : commands)
|
|
|
|
{
|
2021-10-23 19:21:16 +00:00
|
|
|
command.callback->OnCommand(command.arguments);
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:08:02 +00:00
|
|
|
gCommandDispatcher = target;
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2021-07-15 16:16:23 +00:00
|
|
|
static void DispatchCommandsFromThis(const AuList<CommandDispatch> &commands)
|
|
|
|
{
|
2021-06-27 21:25:29 +00:00
|
|
|
for (const auto &command : commands)
|
|
|
|
{
|
2021-10-23 19:21:16 +00:00
|
|
|
command.callback->OnCommand(command.arguments);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2022-05-10 13:22:37 +00:00
|
|
|
void RunCommandFunction(const AuFunction<void()> &func)
|
|
|
|
{
|
|
|
|
if (!gCommandDispatcher)
|
|
|
|
{
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-15 17:08:02 +00:00
|
|
|
NewWorkItem(gCommandDispatcher.value(), AuMakeShared<Async::BasicWorkStdFunc>(func))->Dispatch()->BlockUntilComplete();
|
2022-05-10 13:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:16:23 +00:00
|
|
|
void PumpCommands()
|
|
|
|
{
|
2022-02-19 09:05:58 +00:00
|
|
|
AU_LOCK_GUARD(gMutex);
|
|
|
|
AuList<CommandDispatch> commands;
|
2021-07-15 16:16:23 +00:00
|
|
|
|
2022-02-19 09:05:58 +00:00
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
|
|
commands = AuExchange(gPendingCommands, {});
|
2022-05-10 13:22:37 +00:00
|
|
|
|
|
|
|
if (commands.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-02-19 09:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 13:22:37 +00:00
|
|
|
RunCommandFunction([&commands]()
|
2021-07-15 16:16:23 +00:00
|
|
|
{
|
|
|
|
DispatchCommandsFromThis(commands);
|
2022-05-10 13:22:37 +00:00
|
|
|
});
|
2021-07-15 16:16:23 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|