Add define parameters and support aliases and definitions in action files
This commit is contained in:
1 parent
fb8ef636bd
commit
0a6193f68e
12 files changed
+291
-63
No files matched your search
+20
-42
@@ -1,5 +1,4 @@
|
||||
#include "ConsoleCommand.h"
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
@@ -7,63 +6,41 @@ namespace Bilicraft
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr std::size_t maxLength = 65536;
|
||||
constexpr std::string_view whitespace = " \t\v\f";
|
||||
|
||||
std::string_view Trim(std::string_view text)
|
||||
{
|
||||
const auto first = text.find_first_not_of(" \t\v\f");
|
||||
const auto first = text.find_first_not_of(whitespace);
|
||||
if (first == std::string_view::npos) return {};
|
||||
return text.substr(first, text.find_last_not_of(" \t\v\f") - first + 1);
|
||||
return text.substr(first, text.find_last_not_of(whitespace) - first + 1);
|
||||
}
|
||||
std::pair<std::string_view, std::string_view> SplitFirst(std::string_view text)
|
||||
{
|
||||
const auto split = text.find_first_of(" \t\v\f");
|
||||
const auto split = text.find_first_of(whitespace);
|
||||
return {text.substr(0, split), split == text.npos ? std::string_view{} : Trim(text.substr(split))};
|
||||
}
|
||||
}
|
||||
ConsoleCommand ConsoleCommandParser::Parse(std::string_view line)
|
||||
{
|
||||
constexpr std::size_t maxLength = 65536;
|
||||
if (line.size() > maxLength || line.find_first_of("\r\n") != line.npos || line.find('\0') != line.npos)
|
||||
throw std::runtime_error("<console>:1: expected a single line of at most 64 KiB");
|
||||
line = Trim(line.substr(0, line.find('#')));
|
||||
if (line.empty()) return {};
|
||||
std::string display;
|
||||
std::string expanded;
|
||||
try
|
||||
{
|
||||
expanded = bindings.Expand(line, maxLength, &display);
|
||||
}
|
||||
catch (const std::runtime_error &error)
|
||||
{
|
||||
throw std::runtime_error(std::string("<console>:1: ") + error.what());
|
||||
}
|
||||
if (!display.empty()) return {ConsoleKind::Display, std::move(display), {}};
|
||||
if (expanded.empty()) return {};
|
||||
line = expanded;
|
||||
auto [verb, rest] = SplitFirst(line);
|
||||
std::set<std::string_view> expanded; // Views refer to stable map keys.
|
||||
std::string buffer;
|
||||
for (auto it = aliases.find(verb); it != aliases.end(); it = aliases.find(verb))
|
||||
{
|
||||
if (!expanded.insert(it->first).second)
|
||||
throw std::runtime_error("<console>:1: alias cycle at " + it->first);
|
||||
std::string next = it->second;
|
||||
if (!rest.empty()) next.append(" ").append(rest);
|
||||
if (next.size() > maxLength) throw std::runtime_error("<console>:1: expanded command exceeds 64 KiB");
|
||||
buffer = std::move(next);
|
||||
line = buffer;
|
||||
const auto parts = SplitFirst(line);
|
||||
verb = parts.first;
|
||||
rest = parts.second;
|
||||
if (verb == it->first) break; // Allow a shortcut such as "alias drop drop all".
|
||||
}
|
||||
if (verb == "alias")
|
||||
{
|
||||
const auto [name, body] = SplitFirst(rest);
|
||||
if (name.empty())
|
||||
{
|
||||
std::string output;
|
||||
for (const auto &[key, value] : aliases)
|
||||
output += "alias " + key + " " + value + "\n";
|
||||
return {ConsoleKind::Display, output.empty() ? "No aliases defined.\n" : std::move(output), {}};
|
||||
}
|
||||
if (body.empty())
|
||||
{
|
||||
const auto it = aliases.find(name);
|
||||
if (it == aliases.end()) throw std::runtime_error("<console>:1: unknown alias: " + std::string(name));
|
||||
return {ConsoleKind::Display, "alias " + it->first + " " + it->second + "\n", {}};
|
||||
}
|
||||
if (name == "alias") throw std::runtime_error("<console>:1: alias is a reserved name");
|
||||
aliases.insert_or_assign(std::string(name), std::string(body));
|
||||
return {};
|
||||
}
|
||||
|
||||
if (verb == "run")
|
||||
{
|
||||
if (rest.size() >= 2 && ((rest.front() == '"' && rest.back() == '"') ||
|
||||
@@ -84,6 +61,7 @@ ConsoleCommand ConsoleCommandParser::Parse(std::string_view line)
|
||||
std::string ConsoleHelp()
|
||||
{
|
||||
return "Console: alias [name [command args...]] | run <path.act> | tasks | help | quit (or exit)\n"
|
||||
"define [name [value...]]: store session parameters; use $name as a whole argument.\n"
|
||||
"Game commands: goto, rclick, lclick, grclick, glclick, sw, swb, drop, sendcmd, sendmsg\n"
|
||||
"rclick/lclick: add entry [t|b|s|n|w|e] for item frames; omit face to require a unique frame.\n"
|
||||
"A game command waits until the running action finishes or reaches wait.\n"
|
||||
|
||||
Reference in new issue
Block a user