commands results and variables

This commit is contained in:
MihailRis 2024-05-13 19:23:46 +03:00
parent 0a45987811
commit 5b2b89c778
7 changed files with 39 additions and 8 deletions

View File

@ -1,3 +1,5 @@
<container size='400' color='#00000080' size-func="unpack(gui.get_viewport())">
<textbox id='prompt' margin='0' gravity='bottom-left' size-func="gui.get_viewport()[1],40"></textbox>
<textbox id='log' margin='0' multiline='true' size-func="gui.get_viewport()[1],gui.get_viewport()[2]-40">
</textbox>
<textbox id='prompt' consumer='submit' margin='0' gravity='bottom-left' size-func="gui.get_viewport()[1],40"></textbox>
</container>

View File

@ -1,3 +1,15 @@
function submit(text)
local x,y,z = player.get_pos(0)
console.set('pos.x', x)
console.set('pos.y', y)
console.set('pos.z', z)
local status, result = pcall(function() return console.execute(text) end)
if result ~= nil then
document.log.text = document.log.text..tostring(result)..'\n'
end
document.prompt.text = ""
end
function on_open()
document.prompt.focused = true
end

View File

@ -229,7 +229,10 @@ public:
case ArgType::integer:
return typeCheck<integer_t>(arg, value, "integer");
case ArgType::string:
return typeCheck<std::string>(arg, value, "string");
if (!std::holds_alternative<std::string>(value)) {
return !arg->optional;
}
break;
}
return true;
}
@ -316,6 +319,11 @@ public:
if (hasNext() && peekNoJump() != ' ') {
value = parseValue();
if (auto string = std::get_if<std::string>(&value)) {
if ((*string)[0] == '$') {
value = (*interpreter)[string->substr(1)];
}
}
// keyword argument
if (!relative && hasNext() && peek() == '=') {
@ -328,10 +336,9 @@ public:
Argument* arg = nullptr;
do {
if (arg) {
std::cout << "skipped arg " << arg->name << std::endl;
if (auto string = std::get_if<std::string>(&arg->def)) {
if ((*string)[0] == '$') {
args->put((*interpreter)[*string]);
args->put((*interpreter)[string->substr(1)]);
} else {
args->put(arg->def);
}

View File

@ -93,6 +93,8 @@ namespace cmd {
public:
CommandsInterpreter() : repository(std::make_unique<CommandsRepository>()) {}
CommandsInterpreter(const CommandsInterpreter&) = delete;
CommandsInterpreter(std::unique_ptr<CommandsRepository> repository)
: repository(std::move(repository)){}

View File

@ -154,8 +154,8 @@ void lua::LuaState::loadbuffer(int env, const std::string& src, const std::strin
}
}
int lua::LuaState::call(int argc) {
if (lua_pcall(L, argc, LUA_MULTRET, 0)) {
int lua::LuaState::call(int argc, int nresults) {
if (lua_pcall(L, argc, nresults, 0)) {
throw lua::luaerror(lua_tostring(L, -1));
}
return 1;
@ -416,7 +416,7 @@ scripting::common_func lua::LuaState::createLambda() {
for (const auto& arg : args) {
pushvalue(arg);
}
if (call(args.size())) {
if (call(args.size(), 1)) {
auto result = tovalue(-1);
pop(1);
return result;

View File

@ -59,7 +59,7 @@ namespace lua {
const char* tostring(int idx);
bool isstring(int idx);
bool isfunction(int idx);
int call(int argc);
int call(int argc, int nresults=-1);
int callNoThrow(int argc);
int execute(int env, const std::string& src, const std::string& file="<string>");
int eval(int env, const std::string& src, const std::string& file="<eval>");

View File

@ -36,8 +36,16 @@ static int l_execute(lua_State* L) {
return 1;
}
static int l_set(lua_State* L) {
auto name = lua_tostring(L, 1);
auto value = state->tovalue(2);
(*engine->getCommandsInterpreter())[name] = value;
return 0;
}
const luaL_Reg consolelib [] = {
{"add_command", lua_wrap_errors<l_add_command>},
{"execute", lua_wrap_errors<l_execute>},
{"set", lua_wrap_errors<l_set>},
{NULL, NULL}
};