add '--script', '--version' command line arguments

This commit is contained in:
MihailRis 2024-12-18 00:48:29 +03:00
parent 48d94036fd
commit fa1646a9bd
7 changed files with 48 additions and 19 deletions

View File

@ -1,4 +1,4 @@
#include "TestMainloop.hpp"
#include "ServerMainloop.hpp"
#include "logic/scripting/scripting.hpp"
#include "logic/LevelController.hpp"
@ -6,22 +6,27 @@
#include "debug/Logger.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "util/platform.hpp"
#include "engine.hpp"
#include <chrono>
using namespace std::chrono;
static debug::Logger logger("mainloop");
inline constexpr int TPS = 20;
TestMainloop::TestMainloop(Engine& engine) : engine(engine) {
ServerMainloop::ServerMainloop(Engine& engine) : engine(engine) {
}
TestMainloop::~TestMainloop() = default;
ServerMainloop::~ServerMainloop() = default;
void TestMainloop::run() {
void ServerMainloop::run() {
const auto& coreParams = engine.getCoreParameters();
auto& time = engine.getTime();
if (coreParams.testFile.empty()) {
if (coreParams.scriptFile.empty()) {
logger.info() << "nothing to do";
return;
}
@ -29,21 +34,34 @@ void TestMainloop::run() {
setLevel(std::move(level));
});
logger.info() << "starting test " << coreParams.testFile;
auto process = scripting::start_coroutine(coreParams.testFile);
logger.info() << "starting test " << coreParams.scriptFile;
auto process = scripting::start_coroutine(coreParams.scriptFile);
double targetDelta = 1.0f / static_cast<float>(TPS);
double delta = targetDelta;
auto begin = steady_clock::now();
while (process->isActive()) {
time.step(1.0f / static_cast<float>(TPS));
time.step(delta);
process->update();
if (controller) {
float delta = time.getDelta();
controller->getLevel()->getWorld()->updateTimers(delta);
controller->update(glm::min(delta, 0.2f), false);
}
if (!coreParams.testMode) {
auto end = steady_clock::now();
platform::sleep(targetDelta * 1000 -
duration_cast<microseconds>(end - begin).count() / 1000);
end = steady_clock::now();
delta = duration_cast<microseconds>(end - begin).count() / 1e6;
begin = end;
}
}
logger.info() << "test finished";
}
void TestMainloop::setLevel(std::unique_ptr<Level> level) {
void ServerMainloop::setLevel(std::unique_ptr<Level> level) {
if (level == nullptr) {
controller->onWorldQuit();
engine.getPaths()->setCurrentWorldFolder(fs::path());

View File

@ -6,12 +6,12 @@ class Level;
class LevelController;
class Engine;
class TestMainloop {
class ServerMainloop {
Engine& engine;
std::unique_ptr<LevelController> controller;
public:
TestMainloop(Engine& engine);
~TestMainloop();
ServerMainloop(Engine& engine);
~ServerMainloop();
void run();

View File

@ -36,7 +36,7 @@
#include "window/Window.hpp"
#include "world/Level.hpp"
#include "Mainloop.hpp"
#include "TestMainloop.hpp"
#include "ServerMainloop.hpp"
#include <iostream>
#include <assert.h>
@ -173,7 +173,7 @@ void Engine::saveScreenshot() {
void Engine::run() {
if (params.headless) {
TestMainloop(*this).run();
ServerMainloop(*this).run();
} else {
Mainloop(*this).run();
}

View File

@ -48,9 +48,10 @@ public:
struct CoreParameters {
bool headless = false;
bool testMode = false;
std::filesystem::path resFolder {"res"};
std::filesystem::path userFolder {"."};
std::filesystem::path testFile;
std::filesystem::path scriptFile;
};
class Engine : public util::ObjectsKeeper {

View File

@ -121,7 +121,7 @@ void lua::initialize(const EnginePaths& paths, const CoreParameters& params) {
main_thread = create_state(
paths, params.headless ? StateType::TEST : StateType::BASE
);
lua::pushstring(main_thread, params.testFile.stem().u8string());
lua::pushstring(main_thread, params.scriptFile.stem().u8string());
lua::setglobal(main_thread, "__VC_TEST_NAME");
}

View File

@ -19,19 +19,30 @@ static bool perform_keyword(
auto token = reader.next();
params.userFolder = fs::u8path(token);
} else if (keyword == "--help" || keyword == "-h") {
std::cout << "VoxelEngine command-line arguments:\n";
std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n";
std::cout << "command-line arguments:\n";
std::cout << " --help - show help\n";
std::cout << " --version - print engine version\n";
std::cout << " --res <path> - set resources directory\n";
std::cout << " --dir <path> - set userfiles directory\n";
std::cout << " --headless - run in headless mode\n";
std::cout << " --test <path> - test script file\n";
std::cout << " --script <path> - main script file\n";
std::cout << std::endl;
return false;
} else if (keyword == "--version") {
std::cout << ENGINE_VERSION_STRING << std::endl;
return false;
} else if (keyword == "--headless") {
params.headless = true;
} else if (keyword == "--test") {
auto token = reader.next();
params.testFile = fs::u8path(token);
params.testMode = true;
params.scriptFile = fs::u8path(token);
} else if (keyword == "--script") {
auto token = reader.next();
params.testMode = false;
params.scriptFile = fs::u8path(token);
} else {
throw std::runtime_error("unknown argument " + keyword);
}

View File

@ -27,7 +27,6 @@ public:
std::unique_ptr<GlobalChunks> chunks;
std::unique_ptr<Inventories> inventories;
std::unique_ptr<PhysicsSolver> physics;
std::unique_ptr<LevelEvents> events;
std::unique_ptr<Entities> entities;