mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
Merge pull request #783 from MihailRis/stdin-commands
executing commands from stdin
This commit is contained in:
commit
4478afa1ef
5 changed files with 52 additions and 0 deletions
36
src/devtools/stdin_reader.cpp
Normal file
36
src/devtools/stdin_reader.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include "stdin_reader.hpp"
|
||||
|
||||
#include "logic/CommandsInterpreter.hpp"
|
||||
#include "coders/json.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
static debug::Logger logger("stdin-reader");
|
||||
|
||||
static std::thread reader_thread;
|
||||
|
||||
void start_stdin_reader(cmd::CommandsInterpreter& interpreter) {
|
||||
reader_thread = std::thread([&interpreter]() {
|
||||
logger.info() << "reader thread started";
|
||||
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
auto result = interpreter.execute(line);
|
||||
if (result.isString()) {
|
||||
logger.info() << result.asString();
|
||||
} else {
|
||||
logger.info() << json::stringify(result, true);
|
||||
}
|
||||
} catch (const std::exception& err) {
|
||||
logger.error() << err.what();
|
||||
}
|
||||
}
|
||||
});
|
||||
reader_thread.detach();
|
||||
}
|
||||
7
src/devtools/stdin_reader.hpp
Normal file
7
src/devtools/stdin_reader.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
namespace cmd {
|
||||
class CommandsInterpreter;
|
||||
}
|
||||
|
||||
void start_stdin_reader(cmd::CommandsInterpreter& interpreter);
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
struct CoreParameters {
|
||||
bool headless = false;
|
||||
bool testMode = false;
|
||||
bool stdinCommands = false;
|
||||
std::filesystem::path resFolder = "res";
|
||||
std::filesystem::path userFolder = ".";
|
||||
std::filesystem::path scriptFile;
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
#include "devtools/DebuggingServer.hpp"
|
||||
#include "devtools/Editor.hpp"
|
||||
#include "devtools/Project.hpp"
|
||||
#include "devtools/stdin_reader.hpp"
|
||||
#include "EnginePaths.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "frontend/menu.hpp"
|
||||
|
|
@ -191,6 +192,9 @@ void Engine::initialize(CoreParameters coreParameters) {
|
|||
if (!params.headless) {
|
||||
project->loadProjectClientScript();
|
||||
}
|
||||
if (params.stdinCommands) {
|
||||
start_stdin_reader(*cmd);
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::loadSettings() {
|
||||
|
|
|
|||
|
|
@ -62,6 +62,10 @@ static bool perform_keyword(
|
|||
params.headless = true;
|
||||
return true;
|
||||
}, "", "run in headless mode."),
|
||||
ArgC("--stdin-cmd", [¶ms]() -> bool {
|
||||
params.stdinCommands = true;
|
||||
return true;
|
||||
}, "", "run commands from stdin."),
|
||||
ArgC("--tps", [¶ms, &reader]() -> bool {
|
||||
params.tps = reader.nextInt();
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue