fix sandbox app script loading and timer execution
- Add relative path resolution for filesystem-loaded documents in kernel.cpp - SetDocumentBasePath() tracks document directory for relative resource resolution - ResolvePath() resolves relative paths like "app.lua" against document base path - Fix RmlUi context name lookup in sandbox test app (use "default" not "main") - Add debug logging to timer_manager.cpp to trace timer creation and execution Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -62,6 +62,9 @@ struct FileHandleWrapper {
|
||||
|
||||
class AssetFilesInterface : public Rml::FileInterface
|
||||
{
|
||||
// Track base path of currently loading document for relative path resolution
|
||||
std::string m_current_document_base;
|
||||
|
||||
public:
|
||||
static AssetFilesInterface& Instance()
|
||||
{
|
||||
@@ -69,6 +72,22 @@ public:
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Set the base path for relative resource resolution (called before LoadDocument)
|
||||
void SetDocumentBasePath(const std::string& doc_path) {
|
||||
// Extract directory from full document path
|
||||
size_t last_slash = doc_path.find_last_of("/\\");
|
||||
if (last_slash != std::string::npos) {
|
||||
m_current_document_base = doc_path.substr(0, last_slash + 1);
|
||||
} else {
|
||||
m_current_document_base = "";
|
||||
}
|
||||
Logger::Log(std::format("Document base path set to: {}", m_current_document_base));
|
||||
}
|
||||
|
||||
void ClearDocumentBasePath() {
|
||||
m_current_document_base = "";
|
||||
}
|
||||
|
||||
// Check if path is a filesystem path (starts with / or file://)
|
||||
static bool IsFilesystemPath(const Rml::String& path) {
|
||||
return !path.empty() && (path[0] == '/' || path.rfind("file://", 0) == 0);
|
||||
@@ -82,15 +101,37 @@ public:
|
||||
return path;
|
||||
}
|
||||
|
||||
// Resolve a path - if it's relative and we have a filesystem base, resolve against it
|
||||
Rml::String ResolvePath(const Rml::String& path) const {
|
||||
// If already absolute or explicitly a filesystem/asset path, return as-is
|
||||
if (IsFilesystemPath(path)) {
|
||||
return path;
|
||||
}
|
||||
|
||||
// If we have a filesystem base path and this is a relative path, resolve it
|
||||
if (!m_current_document_base.empty() && IsFilesystemPath(m_current_document_base)) {
|
||||
Rml::String resolved = m_current_document_base + path;
|
||||
Logger::Log(std::format("Resolved relative path '{}' to '{}'", path, resolved));
|
||||
return resolved;
|
||||
}
|
||||
|
||||
// Otherwise, treat as asset path
|
||||
return path;
|
||||
}
|
||||
|
||||
Rml::FileHandle Open(const Rml::String &path) override
|
||||
{
|
||||
// Resolve relative paths against current document base
|
||||
Rml::String resolved_path = ResolvePath(path);
|
||||
|
||||
auto* wrapper = new FileHandleWrapper();
|
||||
|
||||
if (IsFilesystemPath(path)) {
|
||||
if (IsFilesystemPath(resolved_path)) {
|
||||
// Filesystem path
|
||||
Rml::String fs_path = GetFilesystemPath(path);
|
||||
Rml::String fs_path = GetFilesystemPath(resolved_path);
|
||||
auto* file = new std::ifstream(fs_path, std::ios::binary);
|
||||
if (!file->is_open()) {
|
||||
Logger::Log(std::format("Open failed for filesystem: {}", fs_path));
|
||||
delete file;
|
||||
delete wrapper;
|
||||
return 0;
|
||||
@@ -103,8 +144,9 @@ public:
|
||||
} else {
|
||||
// Asset path
|
||||
AAssetManager* am = AssetsManager::Native();
|
||||
AAsset* asset = AAssetManager_open(am, path.c_str(), AASSET_MODE_BUFFER);
|
||||
AAsset* asset = AAssetManager_open(am, resolved_path.c_str(), AASSET_MODE_BUFFER);
|
||||
if (!asset) {
|
||||
Logger::Log(std::format("Open failed for asset: {}", resolved_path));
|
||||
delete wrapper;
|
||||
return 0;
|
||||
}
|
||||
@@ -185,28 +227,43 @@ public:
|
||||
|
||||
bool LoadFile(const Rml::String &path, Rml::String &out_data) override
|
||||
{
|
||||
if (IsFilesystemPath(path)) {
|
||||
Logger::Log(std::format("LoadFile requested: {}", path));
|
||||
|
||||
// Resolve relative paths against current document base
|
||||
Rml::String resolved_path = ResolvePath(path);
|
||||
|
||||
if (IsFilesystemPath(resolved_path)) {
|
||||
// Load from filesystem
|
||||
Rml::String fs_path = GetFilesystemPath(path);
|
||||
Rml::String fs_path = GetFilesystemPath(resolved_path);
|
||||
Logger::Log(std::format(" -> Loading from filesystem: {}", fs_path));
|
||||
std::ifstream file(fs_path, std::ios::binary | std::ios::ate);
|
||||
if (!file.is_open()) return false;
|
||||
if (!file.is_open()) {
|
||||
Logger::Log(std::format(" -> FAILED to open file: {}", fs_path));
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
out_data.resize(size);
|
||||
file.read(out_data.data(), size);
|
||||
Logger::Log(std::format(" -> Loaded {} bytes from filesystem", size));
|
||||
return true;
|
||||
} else {
|
||||
// Load from assets
|
||||
Logger::Log(std::format(" -> Loading from assets: {}", resolved_path));
|
||||
AAssetManager* am = AssetsManager::Native();
|
||||
AAsset* asset = AAssetManager_open(am, path.c_str(), AASSET_MODE_BUFFER);
|
||||
if (!asset) return false;
|
||||
AAsset* asset = AAssetManager_open(am, resolved_path.c_str(), AASSET_MODE_BUFFER);
|
||||
if (!asset) {
|
||||
Logger::Log(std::format(" -> FAILED to load from assets: {}", resolved_path));
|
||||
return false;
|
||||
}
|
||||
|
||||
out_data.resize(AAsset_getLength(asset));
|
||||
auto data_ptr = static_cast<const char*>(AAsset_getBuffer(asset));
|
||||
std::span data = std::span(data_ptr, out_data.size());
|
||||
std::ranges::copy(data, out_data.begin());
|
||||
AAsset_close(asset);
|
||||
Logger::Log(std::format(" -> Loaded {} bytes from assets", out_data.size()));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -261,6 +318,10 @@ static int LuaLoadScreen(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Set document base path for relative resource resolution (scripts, stylesheets)
|
||||
// This is crucial for filesystem documents to load their scripts correctly
|
||||
AssetFilesInterface::Instance().SetDocumentBasePath(path);
|
||||
|
||||
// Unload current document
|
||||
if (g_document)
|
||||
{
|
||||
@@ -282,6 +343,9 @@ static int LuaLoadScreen(lua_State* L)
|
||||
lua_pushboolean(L, false);
|
||||
}
|
||||
|
||||
// Clear base path after document is fully loaded
|
||||
AssetFilesInterface::Instance().ClearDocumentBasePath();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user