update project script methods, replace project script with project client script

This commit is contained in:
MihailRis 2025-08-18 21:20:18 +03:00
parent dc0e388eec
commit c8d760e83e
6 changed files with 92 additions and 88 deletions

View File

@ -1,13 +1,13 @@
local menubg local menubg
local function clear_menu() function on_menu_clear()
if menubg then if menubg then
menubg:destruct() menubg:destruct()
menubg = nil menubg = nil
end end
end end
local function configure_menu() function on_menu_setup()
local controller = {} local controller = {}
function controller.resize_menu_bg() function controller.resize_menu_bg()
local w, h = unpack(gui.get_viewport()) local w, h = unpack(gui.get_viewport())
@ -24,11 +24,3 @@ local function configure_menu()
controller.resize_menu_bg() controller.resize_menu_bg()
menu.page = "main" menu.page = "main"
end end
function on_screen_changed(screen)
if screen ~= "menu" then
clear_menu()
else
configure_menu()
end
end

View File

@ -7,14 +7,14 @@
#include "interfaces/Serializable.hpp" #include "interfaces/Serializable.hpp"
namespace scripting { namespace scripting {
class IProjectScript; class IClientProjectScript;
} }
struct Project : Serializable { struct Project : Serializable {
std::string name; std::string name;
std::string title; std::string title;
std::vector<std::string> basePacks; std::vector<std::string> basePacks;
std::unique_ptr<scripting::IProjectScript> script; std::unique_ptr<scripting::IClientProjectScript> clientScript;
~Project(); ~Project();

View File

@ -60,11 +60,11 @@ static std::unique_ptr<ImageData> load_icon() {
return nullptr; return nullptr;
} }
static std::unique_ptr<scripting::IProjectScript> load_project_script() { static std::unique_ptr<scripting::IClientProjectScript> load_client_project_script() {
io::path scriptFile = "project:project_script.lua"; io::path scriptFile = "project:project_client.lua";
if (io::exists(scriptFile)) { if (io::exists(scriptFile)) {
logger.info() << "starting project script"; logger.info() << "starting project script";
return scripting::load_project_script(scriptFile); return scripting::load_client_project_script(scriptFile);
} else { } else {
logger.warning() << "project script does not exists"; logger.warning() << "project script does not exists";
} }
@ -83,6 +83,68 @@ Engine& Engine::getInstance() {
return *instance; return *instance;
} }
void Engine::onContentLoad() {
editor->loadTools();
langs::setup(langs::get_current(), paths.resPaths.collectRoots());
if (isHeadless()) {
return;
}
for (auto& pack : content->getAllContentPacks()) {
auto configFolder = pack.folder / "config";
auto bindsFile = configFolder / "bindings.toml";
if (io::is_regular_file(bindsFile)) {
input->getBindings().read(
toml::parse(
bindsFile.string(), io::read_string(bindsFile)
),
BindType::BIND
);
}
}
loadAssets();
}
void Engine::initializeClient() {
std::string title = project->title;
if (title.empty()) {
title = "VoxelCore v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
}
if (ENGINE_DEBUG_BUILD) {
title += " [debug]";
}
auto [window, input] = Window::initialize(&settings.display, title);
if (!window || !input){
throw initialize_error("could not initialize window");
}
window->setFramerate(settings.display.framerate.get());
time.set(window->time());
if (auto icon = load_icon()) {
icon->flipY();
window->setIcon(icon.get());
}
this->window = std::move(window);
this->input = std::move(input);
loadControls();
gui = std::make_unique<gui::GUI>(*this);
if (ENGINE_DEBUG_BUILD) {
menus::create_version_label(*gui);
}
keepAlive(settings.display.fullscreen.observe(
[this](bool value) {
if (value != this->window->isFullscreen()) {
this->window->toggleFullscreen();
}
},
true
));
}
void Engine::initialize(CoreParameters coreParameters) { void Engine::initialize(CoreParameters coreParameters) {
params = std::move(coreParameters); params = std::move(coreParameters);
settingsHandler = std::make_unique<SettingsHandler>(settings); settingsHandler = std::make_unique<SettingsHandler>(settings);
@ -111,70 +173,17 @@ void Engine::initialize(CoreParameters coreParameters) {
controller = std::make_unique<EngineController>(*this); controller = std::make_unique<EngineController>(*this);
if (!params.headless) { if (!params.headless) {
std::string title = project->title; initializeClient();
if (title.empty()) {
title = "VoxelCore v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
}
if (ENGINE_DEBUG_BUILD) {
title += " [debug]";
}
auto [window, input] = Window::initialize(&settings.display, title);
if (!window || !input){
throw initialize_error("could not initialize window");
}
window->setFramerate(settings.display.framerate.get());
time.set(window->time());
if (auto icon = load_icon()) {
icon->flipY();
window->setIcon(icon.get());
}
this->window = std::move(window);
this->input = std::move(input);
loadControls();
gui = std::make_unique<gui::GUI>(*this);
if (ENGINE_DEBUG_BUILD) {
menus::create_version_label(*gui);
}
keepAlive(settings.display.fullscreen.observe(
[this](bool value) {
if (value != this->window->isFullscreen()) {
this->window->toggleFullscreen();
}
},
true
));
} }
audio::initialize(!params.headless, settings.audio); audio::initialize(!params.headless, settings.audio);
bool langNotSet = settings.ui.language.get() == "auto"; if (settings.ui.language.get() == "auto") {
if (langNotSet) {
settings.ui.language.set( settings.ui.language.set(
langs::locale_by_envlocale(platform::detect_locale()) langs::locale_by_envlocale(platform::detect_locale())
); );
} }
content = std::make_unique<ContentControl>(*project, paths, *input, [this]() { content = std::make_unique<ContentControl>(*project, paths, *input, [this]() {
editor->loadTools(); onContentLoad();
langs::setup(langs::get_current(), paths.resPaths.collectRoots());
if (!isHeadless()) {
for (auto& pack : content->getAllContentPacks()) {
auto configFolder = pack.folder / "config";
auto bindsFile = configFolder / "bindings.toml";
if (io::is_regular_file(bindsFile)) {
input->getBindings().read(
toml::parse(
bindsFile.string(), io::read_string(bindsFile)
),
BindType::BIND
);
}
}
loadAssets();
}
}); });
scripting::initialize(this); scripting::initialize(this);
@ -185,7 +194,7 @@ void Engine::initialize(CoreParameters coreParameters) {
langs::setup(lang, paths.resPaths.collectRoots()); langs::setup(lang, paths.resPaths.collectRoots());
}, true)); }, true));
project->script = load_project_script(); project->clientScript = load_client_project_script();
} }
void Engine::loadSettings() { void Engine::loadSettings() {
@ -360,6 +369,9 @@ void Engine::loadProject() {
} }
void Engine::setScreen(std::shared_ptr<Screen> screen) { void Engine::setScreen(std::shared_ptr<Screen> screen) {
if (project->clientScript && this->screen) {
project->clientScript->onScreenChange(this->screen->getName(), false);
}
// reset audio channels (stop all sources) // reset audio channels (stop all sources)
audio::reset_channel(audio::get_channel_index("regular")); audio::reset_channel(audio::get_channel_index("regular"));
audio::reset_channel(audio::get_channel_index("ambient")); audio::reset_channel(audio::get_channel_index("ambient"));
@ -367,8 +379,8 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
if (this->screen) { if (this->screen) {
this->screen->onOpen(); this->screen->onOpen();
} }
if (project->script && this->screen) { if (project->clientScript && this->screen) {
project->script->onScreenChange(this->screen->getName()); project->clientScript->onScreenChange(this->screen->getName(), true);
} }
} }

View File

@ -38,10 +38,6 @@ namespace devtools {
class Editor; class Editor;
} }
namespace scripting {
class IProjectScript;
}
class initialize_error : public std::runtime_error { class initialize_error : public std::runtime_error {
public: public:
initialize_error(const std::string& message) : std::runtime_error(message) {} initialize_error(const std::string& message) : std::runtime_error(message) {}
@ -86,6 +82,9 @@ class Engine : public util::ObjectsKeeper {
void updateHotkeys(); void updateHotkeys();
void loadAssets(); void loadAssets();
void loadProject(); void loadProject();
void initializeClient();
void onContentLoad();
public: public:
Engine(); Engine();
~Engine(); ~Engine();

View File

@ -117,20 +117,19 @@ public:
} }
}; };
class LuaProjectScript : public IProjectScript { class LuaProjectScript : public IClientProjectScript {
public: public:
LuaProjectScript(lua::State* L, scriptenv env) : L(L), env(std::move(env)) {} LuaProjectScript(lua::State* L, scriptenv env) : L(L), env(std::move(env)) {}
void onScreenChange(const std::string& name) override { void onScreenChange(const std::string& name, bool show) override {
if (!lua::pushenv(L, *env)) { if (!lua::pushenv(L, *env)) {
return; return;
} }
if (!lua::getfield(L, "on_screen_changed")) { if (!lua::getfield(L, "on_" + name + (show ? "_setup" : "_clear"))) {
lua::pop(L); lua::pop(L);
return; return;
} }
lua::pushlstring(L, name); lua::call_nothrow(L, 0, 0);
lua::call_nothrow(L, 1, 0);
lua::pop(L); lua::pop(L);
} }
private: private:
@ -138,7 +137,7 @@ private:
scriptenv env; scriptenv env;
}; };
std::unique_ptr<IProjectScript> scripting::load_project_script( std::unique_ptr<IClientProjectScript> scripting::load_client_project_script(
const io::path& script const io::path& script
) { ) {
auto L = lua::get_main_state(); auto L = lua::get_main_state();

View File

@ -65,14 +65,16 @@ namespace scripting {
void process_post_runnables(); void process_post_runnables();
class IProjectScript { class IClientProjectScript {
public: public:
virtual ~IProjectScript() {} virtual ~IClientProjectScript() {}
virtual void onScreenChange(const std::string& name) = 0; virtual void onScreenChange(const std::string& name, bool show) = 0;
}; };
std::unique_ptr<IProjectScript> load_project_script(const io::path& script); std::unique_ptr<IClientProjectScript> load_client_project_script(
const io::path& script
);
std::unique_ptr<Process> start_coroutine(const io::path& script); std::unique_ptr<Process> start_coroutine(const io::path& script);