202 lines
5.8 KiB
C++
202 lines
5.8 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: Commands.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "Commands.hpp"
|
|
|
|
namespace Aurora::Console::Commands
|
|
{
|
|
struct Command;
|
|
|
|
struct CommandDispatch
|
|
{
|
|
Parse::ParsedObject arguments;
|
|
AuSPtr<ICommandSubscriber> callback;
|
|
|
|
CommandDispatch(const Parse::ParsedObject &arguments, const AuSPtr<ICommandSubscriber> &callback) : arguments(arguments), callback(callback) {}
|
|
};
|
|
|
|
static AuHashMap<AuString, Command> gCommands;
|
|
static AuList<Hooks::LineHook_cb> gLineCallbacks;
|
|
static AuList<CommandDispatch> gPendingCommands;
|
|
static AuMutex gMutex;
|
|
static AuMutex gPendingCommandsMutex;
|
|
static AuOptionalEx<AuWorkerPId_t> gCommandDispatcher;
|
|
|
|
struct Command
|
|
{
|
|
AuString tag;
|
|
Parse::ParseObject commandStructure;
|
|
AuSPtr<ICommandSubscriber> callback;
|
|
|
|
Command(AuString tag, Parse::ParseObject commandStructure, const AuSPtr<ICommandSubscriber> &callback) : tag(tag), commandStructure(commandStructure), callback(callback) {}
|
|
Command(AuString tag, Parse::ParseObject commandStructure, AuSPtr<ICommandSubscriber> &&callback) : tag(tag), commandStructure(commandStructure), callback(AuMove(callback)) {}
|
|
};
|
|
|
|
enum class EDispatchType
|
|
{
|
|
eNow,
|
|
eSys,
|
|
eAsync
|
|
};
|
|
|
|
static bool Dispatch(const AuString &string, EDispatchType type, AuOptionalEx<AuWorkerPId_t> workerId)
|
|
{
|
|
Parse::ParseResult res;
|
|
AuSPtr<ICommandSubscriber> callback;
|
|
|
|
{
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
|
|
{
|
|
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())
|
|
{
|
|
AuLogWarn("Command {} does not exist", tag);
|
|
return false;
|
|
}
|
|
|
|
auto const &cmdEntry = cmdItr->second;
|
|
|
|
offset = 0;
|
|
Parse::ParseState consumable(AuIO::Character::ProviderFromStringShared(cmdParse, offset));
|
|
auto status = Parse::Parse(consumable, cmdEntry.commandStructure, res);
|
|
|
|
if (!status)
|
|
{
|
|
AuLogWarn("Couldn't parse command {}", string);
|
|
return false;
|
|
}
|
|
|
|
if (type == EDispatchType::eSys)
|
|
{
|
|
return AuTryInsert(gPendingCommands, CommandDispatch(res.result, cmdEntry.callback));
|
|
}
|
|
else
|
|
{
|
|
callback = cmdEntry.callback;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (type == EDispatchType::eNow)
|
|
{
|
|
callback->OnCommand(res.result);
|
|
}
|
|
else if (workerId)
|
|
{
|
|
Async::DispatchOn(workerId.value(), [=]() -> void
|
|
{
|
|
callback->OnCommand(res.result);
|
|
});
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
AUKN_SYM void RemoveCommand(const AuString &tag)
|
|
{
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
AuTryRemove(gCommands, tag);
|
|
}
|
|
|
|
AUKN_SYM void AddCommand(const AuString &tag, const Parse::ParseObject &commandStructure, const AuSPtr<ICommandSubscriber> &callback)
|
|
{
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
SysAssert(callback);
|
|
gCommands.insert(AuMakePair(tag, Command(tag, commandStructure, callback)));
|
|
}
|
|
|
|
AUKN_SYM bool DispatchCommand(const AuString &string)
|
|
{
|
|
return Dispatch(string, EDispatchType::eSys, gCommandDispatcher);
|
|
}
|
|
|
|
AUKN_SYM bool DispatchCommandThisThread(const AuString &string)
|
|
{
|
|
return Dispatch(string, EDispatchType::eNow, {});
|
|
}
|
|
|
|
AUKN_SYM bool DispatchCommandToAsyncRunner(const AuString &string, Async::WorkerPId_t id)
|
|
{
|
|
return Dispatch(string, EDispatchType::eAsync, id);
|
|
}
|
|
|
|
void UpdateDispatcher(AuOptionalEx<AuWorkerPId_t> target)
|
|
{
|
|
AU_LOCK_GUARD(gMutex);
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
|
|
// process commands before async app termination
|
|
if ((!target.has_value()))
|
|
{
|
|
auto commands = AuExchange(gPendingCommands, {});
|
|
for (const auto &command : commands)
|
|
{
|
|
command.callback->OnCommand(command.arguments);
|
|
}
|
|
}
|
|
|
|
gCommandDispatcher = target;
|
|
}
|
|
|
|
static void DispatchCommandsFromThis(const AuList<CommandDispatch> &commands)
|
|
{
|
|
for (const auto &command : commands)
|
|
{
|
|
command.callback->OnCommand(command.arguments);
|
|
}
|
|
}
|
|
|
|
void RunCommandFunction(const AuFunction<void()> &func)
|
|
{
|
|
if (!gCommandDispatcher)
|
|
{
|
|
func();
|
|
}
|
|
else
|
|
{
|
|
NewWorkItem(gCommandDispatcher.value(), AuMakeShared<Async::BasicWorkStdFunc>(func))->Dispatch()->BlockUntilComplete();
|
|
}
|
|
}
|
|
|
|
void PumpCommands()
|
|
{
|
|
AU_LOCK_GUARD(gMutex);
|
|
AuList<CommandDispatch> commands;
|
|
|
|
{
|
|
AU_LOCK_GUARD(gPendingCommandsMutex);
|
|
commands = AuExchange(gPendingCommands, {});
|
|
|
|
if (commands.empty())
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
RunCommandFunction([&commands]()
|
|
{
|
|
DispatchCommandsFromThis(commands);
|
|
});
|
|
}
|
|
} |