feat: client-side '--script' and '--test' basic support

This commit is contained in:
MihailRis 2026-06-01 01:14:30 +03:00
parent 0ffefddee1
commit b2ed4b472f
8 changed files with 112 additions and 57 deletions

View file

@ -0,0 +1,59 @@
#include "AppScriptsControl.hpp"
#include "debug/Logger.hpp"
#include "engine/CoreParameters.hpp"
#include "io/io.hpp"
#include "Project.hpp"
#include "logic/scripting/scripting.hpp"
static debug::Logger logger("app-scripts");
AppScriptsControl::AppScriptsControl(
const CoreParameters& params, const Project& project
)
: project(project) {
io::path scriptFile =
params.scriptFile.empty()
? "project:start.lua"
: std::string("script:") + params.scriptFile.filename().u8string();
if (io::exists(scriptFile)) {
logger.info() << "starting script: " << params.scriptFile.u8string();
scriptCoroutine = scripting::start_app_script(scriptFile);
} else {
logger.warning() << "script does not exists: " << params.scriptFile.u8string();
}
if (!params.headless) {
loadProjectClientScript();
}
}
void AppScriptsControl::loadProjectClientScript() {
io::path scriptFile = "project:project_client.lua";
if (io::exists(scriptFile)) {
logger.info() << "starting project client script: " << scriptFile.string();
clientScript = scripting::load_client_project_script(scriptFile);
} else {
logger.warning() << "project client script does not exists: " << scriptFile.string();
}
}
void AppScriptsControl::onScreenChange(const std::string& name, bool show) {
if (clientScript) {
clientScript->onScreenChange(name, show);
}
}
void AppScriptsControl::tick() {
if (scriptCoroutine && scriptCoroutine->isActive()) {
scriptCoroutine->update();
}
}
void AppScriptsControl::terminate(std::string_view reason) {
if (scriptCoroutine->isActive()) {
scriptCoroutine->terminate();
logger.info() << "script has been terminated due to " << reason;
}
}

View file

@ -0,0 +1,28 @@
#pragma once
#include "interfaces/Process.hpp"
#include <memory>
#include <string>
namespace scripting {
class IClientProjectScript;
}
struct CoreParameters;
struct Project;
class AppScriptsControl {
public:
AppScriptsControl(const CoreParameters& params, const Project& project);
void tick();
void loadProjectClientScript();
void terminate(std::string_view reason);
void onScreenChange(const std::string& name, bool show);
private:
const Project& project;
std::unique_ptr<scripting::IClientProjectScript> clientScript;
std::unique_ptr<Process> scriptCoroutine;
};

View file

@ -2,6 +2,7 @@
#include "data/dv_util.hpp"
#include "debug/Logger.hpp"
#include "engine/CoreParameters.hpp"
#include "io/io.hpp"
#include "io/path.hpp"
#include "logic/scripting/scripting.hpp"
@ -40,26 +41,6 @@ void Project::deserialize(const dv::value& src) {
}
}
void Project::loadProjectClientScript() {
io::path scriptFile = "project:project_client.lua";
if (io::exists(scriptFile)) {
logger.info() << "starting project client script";
clientScript = scripting::load_client_project_script(scriptFile);
} else {
logger.warning() << "project client script does not exists";
}
}
void Project::loadProjectStartScript() {
io::path scriptFile = "project:start.lua";
if (io::exists(scriptFile)) {
logger.info() << "starting project start script";
setupCoroutine = scripting::start_app_script(scriptFile);
} else {
logger.warning() << "project start script does not exists";
}
}
bool Permissions::has(const std::string& name) const {
return permissions.find(name) != permissions.end();
}

View file

@ -1,17 +1,12 @@
#pragma once
#include "interfaces/Serializable.hpp"
#include <set>
#include <string>
#include <vector>
#include <memory>
#include "interfaces/Process.hpp"
#include "interfaces/Serializable.hpp"
namespace scripting {
class IClientProjectScript;
}
struct Permissions {
static inline std::string DEBUGGING = "debugging";
static inline std::string NETWORK = "network";
@ -27,15 +22,10 @@ struct Project : Serializable {
std::string name;
std::string title;
std::vector<std::string> basePacks;
std::unique_ptr<scripting::IClientProjectScript> clientScript;
std::unique_ptr<Process> setupCoroutine;
Permissions permissions;
~Project();
dv::value serialize() const override;
void deserialize(const dv::value& src) override;
void loadProjectClientScript();
void loadProjectStartScript();
};

View file

@ -13,6 +13,7 @@
#include "content/ContentControl.hpp"
#include "core_defs.hpp"
#include "debug/Logger.hpp"
#include "devtools/AppScriptsControl.hpp"
#include "devtools/DebuggingServer.hpp"
#include "devtools/Editor.hpp"
#include "devtools/Project.hpp"
@ -191,10 +192,8 @@ void Engine::initialize(CoreParameters coreParameters) {
audio::set_input_device(name == "auto" ? "" : name);
}));
project->loadProjectStartScript();
if (!params.headless) {
project->loadProjectClientScript();
}
appScripts = std::make_unique<AppScriptsControl>(params, *project);
if (params.stdinCommands) {
cmd::start_stdin_cmd_reader(*this);
}
@ -263,9 +262,7 @@ void Engine::detachDebugger() {
}
void Engine::applicationTick() {
if (project->setupCoroutine && project->setupCoroutine->isActive()) {
project->setupCoroutine->update();
}
appScripts->tick();
}
void Engine::updateFrontend() {
@ -353,6 +350,7 @@ void Engine::close() {
network.reset();
clearKeepedObjects();
project.reset();
appScripts.reset();
scripting::close();
logger.info() << "scripting finished";
if (!params.headless) {
@ -387,8 +385,8 @@ void Engine::loadProject() {
}
void Engine::setScreen(std::shared_ptr<Screen> screen) {
if (project->clientScript && this->screen) {
project->clientScript->onScreenChange(this->screen->getName(), false);
if (this->screen) {
appScripts->onScreenChange(this->screen->getName(), false);
}
// reset audio channels (stop all sources)
audio::reset_channel(audio::get_channel_index("regular"));
@ -397,8 +395,8 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
if (this->screen) {
this->screen->onOpen();
}
if (project->clientScript && this->screen) {
project->clientScript->onScreenChange(this->screen->getName(), true);
if (this->screen) {
appScripts->onScreenChange(this->screen->getName(), true);
window->setShouldRefresh();
}
}

View file

@ -12,6 +12,7 @@
class Assets;
class AssetsLoader;
class AssetsManagement;
class AppScriptsControl;
class ContentControl;
class EngineController;
class EnginePaths;
@ -55,6 +56,7 @@ class Engine : public util::ObjectsKeeper {
std::unique_ptr<Project> project;
std::unique_ptr<SettingsHandler> settingsHandler;
std::unique_ptr<AssetsManagement> assets;
std::unique_ptr<AppScriptsControl> appScripts;
std::shared_ptr<Screen> screen;
std::unique_ptr<ContentControl> content;
std::unique_ptr<EngineController> controller;

View file

@ -9,6 +9,8 @@
#include "world/Level.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/ui/elements/Container.hpp"
#include "logic/scripting/scripting.hpp"
#include "io/path.hpp"
static debug::Logger logger("mainloop");
@ -16,9 +18,11 @@ Mainloop::Mainloop(Engine& engine) : engine(engine) {
}
void Mainloop::run() {
const auto& coreParams = engine.getCoreParameters();
auto& time = engine.getTime();
auto& window = engine.getWindow();
auto& settings = engine.getSettings();
double targetDelta = 1.0 / static_cast<double>(coreParams.tps);
engine.setLevelConsumer([this](auto level, int64_t localPlayer) {
if (level == nullptr) {
@ -35,10 +39,13 @@ void Mainloop::run() {
logger.info() << "starting menu screen";
engine.setScreen(std::make_shared<MenuScreen>(engine));
double testTimer = 0.0;
logger.info() << "main loop started";
while (!window.isShouldClose()){
time.update(window.time());
while (!window.isShouldClose()) {
testTimer += targetDelta;
time.update(coreParams.testMode ? testTimer : window.time());
engine.applicationTick();
engine.updateFrontend();

View file

@ -1,13 +1,13 @@
#include "ServerMainloop.hpp"
#include "Engine.hpp"
#include "logic/scripting/scripting.hpp"
#include "logic/LevelController.hpp"
#include "interfaces/Process.hpp"
#include "debug/Logger.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "util/platform.hpp"
#include "devtools/Project.hpp"
#include <chrono>
@ -32,21 +32,12 @@ void ServerMainloop::run() {
setLevel(std::move(level));
});
auto process = scripting::start_app_script(
"script:" + coreParams.scriptFile.filename().u8string()
);
double targetDelta = 1.0 / static_cast<double>(coreParams.tps);
double delta = targetDelta;
auto begin = system_clock::now();
auto startupTime = begin;
while (process->isActive()) {
if (engine.isQuitSignal()) {
process->terminate();
logger.info() << "script has been terminated due to quit signal";
break;
}
while (engine.isQuitSignal()) {
if (coreParams.testMode) {
time.step(delta);
} else {
@ -55,7 +46,6 @@ void ServerMainloop::run() {
duration_cast<microseconds>(now - startupTime).count() / 1e6);
delta = time.getDelta();
}
process->update();
if (controller) {
controller->getLevel()->getWorld()->updateTimers(delta);
controller->update(glm::min(delta, 0.2), false);