mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-07 20:11:50 +00:00
Merge pull request #785 from MihailRis/stdin-commands
fix stdin commands
This commit is contained in:
commit
41c414db19
5 changed files with 49 additions and 45 deletions
40
src/devtools/stdin_cmd_reader.cpp
Normal file
40
src/devtools/stdin_cmd_reader.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "stdin_cmd_reader.hpp"
|
||||
|
||||
#include "engine/Engine.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 cmd::start_stdin_cmd_reader(Engine& engine) {
|
||||
reader_thread = std::thread([&engine]() {
|
||||
auto& interpreter = engine.getCmd();
|
||||
logger.info() << "reader thread started";
|
||||
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
engine.postRunnable([line, &interpreter] () {
|
||||
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_cmd_reader.hpp
Normal file
7
src/devtools/stdin_cmd_reader.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
class Engine;
|
||||
|
||||
namespace cmd {
|
||||
void start_stdin_cmd_reader(Engine& engine);
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
#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();
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
namespace cmd {
|
||||
class CommandsInterpreter;
|
||||
}
|
||||
|
||||
void start_stdin_reader(cmd::CommandsInterpreter& interpreter);
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
#include "devtools/DebuggingServer.hpp"
|
||||
#include "devtools/Editor.hpp"
|
||||
#include "devtools/Project.hpp"
|
||||
#include "devtools/stdin_reader.hpp"
|
||||
#include "devtools/stdin_cmd_reader.hpp"
|
||||
#include "EnginePaths.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "frontend/menu.hpp"
|
||||
|
|
@ -193,7 +193,7 @@ void Engine::initialize(CoreParameters coreParameters) {
|
|||
project->loadProjectClientScript();
|
||||
}
|
||||
if (params.stdinCommands) {
|
||||
start_stdin_reader(*cmd);
|
||||
cmd::start_stdin_cmd_reader(*this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue