Merge pull request #787 from MihailRis/fix-assets-access-attempts-in-headless-mode

fix: assets access attempts in headless mode leading to segfaults
This commit is contained in:
MihailRis 2026-02-12 02:19:47 +03:00 • committed by GitHub
commit 34b87ac596
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 10 deletions

View file

@ -8,7 +8,7 @@
#include <thread>
#include <iostream>
static debug::Logger logger("stdin-reader");
static debug::Logger logger("stdin-cmd");
static std::thread reader_thread;

View file

@ -420,10 +420,20 @@ EngineSettings& Engine::getSettings() {
}
Assets* Engine::getAssets() {
return assets->getStorage();
return assets ? assets->getStorage() : nullptr;
}
Assets& Engine::requireAssets() {
if (isHeadless()) {
throw std::runtime_error("assets are not available in headless mode");
}
return *assets->getStorage();
}
AssetsLoader& Engine::acquireBackgroundLoader() {
if (isHeadless()) {
throw std::runtime_error("assets are not available in headless mode");
}
return assets->acquireBackgroundLoader();
}

View file

@ -110,6 +110,7 @@ public:
/// @brief Get active assets storage instance
Assets* getAssets();
Assets& requireAssets();
AssetsLoader& acquireBackgroundLoader();

View file

@ -15,10 +15,10 @@
using namespace scripting;
static void load_texture(
const ubyte* bytes, size_t size, const std::string& destname
Assets& assets, const ubyte* bytes, size_t size, const std::string& destname
) {
try {
engine->getAssets()->store(png::load_texture(bytes, size), destname);
assets.store(png::load_texture(bytes, size), destname);
} catch (const std::runtime_error& err) {
debug::Logger logger("lua.assetslib");
logger.error() << err.what();
@ -34,6 +34,8 @@ static int l_request_texture(lua::State* L) {
}
static int l_load_texture(lua::State* L) {
auto& assets = engine->requireAssets();
if (lua::isstring(L, 3) && lua::require_lstring(L, 3) != "png") {
throw std::runtime_error("unsupportd image format");
}
@ -47,10 +49,13 @@ static int l_load_texture(lua::State* L) {
lua::pop(L);
}
lua::pop(L);
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
load_texture(
assets, buffer.data(), buffer.size(), lua::require_string(L, 2)
);
} else {
auto string = lua::bytearray_as_string(L, 1);
load_texture(
assets,
reinterpret_cast<const ubyte*>(string.data()),
string.size(),
lua::require_string(L, 2)
@ -61,13 +66,15 @@ static int l_load_texture(lua::State* L) {
}
static int l_parse_model(lua::State* L) {
auto& assets = engine->requireAssets();
auto format = lua::require_lstring(L, 1);
auto string = lua::require_lstring(L, 2);
auto name = lua::require_string(L, 3);
if (format == "xml" || format == "vcm") {
auto vcmModel = vcm::parse(name, string, format == "xml");
engine->getAssets()->store(
assets.store(
std::make_unique<model::Model>(std::move(vcmModel.squash())), name
);
} else {
@ -79,7 +86,7 @@ static int l_parse_model(lua::State* L) {
}
static int l_to_canvas(lua::State* L) {
auto& assets = *engine->getAssets();
auto& assets = engine->requireAssets();
auto alias = lua::require_lstring(L, 1);
size_t sep = alias.rfind(':');

View file

@ -32,7 +32,7 @@ using namespace scripting;
static DocumentNode get_document_node_impl(
lua::State*, const std::string& name, const std::string& nodeName, bool throwable=true
) {
auto doc = engine->getAssets()->get<UiDocument>(name);
auto doc = engine->requireAssets().get<UiDocument>(name);
if (doc == nullptr) {
if (throwable) {
throw std::runtime_error("document '" + name + "' not found");
@ -1026,7 +1026,7 @@ static int l_gui_str(lua::State* L) {
static int l_gui_reindex(lua::State* L) {
auto name = lua::require_string(L, 1);
auto doc = engine->getAssets()->get<UiDocument>(name);
auto doc = engine->requireAssets().get<UiDocument>(name);
if (doc == nullptr) {
throw std::runtime_error(
"document '" + std::string(name) + "' not found"
@ -1124,7 +1124,7 @@ static int l_gui_load_document(lua::State* L) {
engine->getGUI(), std::move(env), alias, filename, filename.string()
);
auto document = documentPtr.get();
engine->getAssets()->store(std::move(documentPtr), alias);
engine->requireAssets().store(std::move(documentPtr), alias);
// namespace extension
if (lua::istable(L, 4)) {