Use Urho3D::ResourceRouter
This commit is contained in:
parent
5531553fb7
commit
9f7f9bd804
@ -11,6 +11,7 @@ local safe_classes = dofile(buildat.extension_path("urho3d").."/safe_classes.lua
|
||||
local Safe = {}
|
||||
local Unsafe = {}
|
||||
|
||||
--[[
|
||||
--
|
||||
-- ResourceCache support code
|
||||
--
|
||||
@ -108,6 +109,7 @@ function __buildat_file_updated_in_cache(name, hash, cached_path)
|
||||
Unsafe.resave_file(name)
|
||||
end
|
||||
end
|
||||
--]]
|
||||
|
||||
--
|
||||
-- Safe interface
|
||||
@ -194,7 +196,7 @@ safe_classes.define(Safe, {
|
||||
self_function = self_function,
|
||||
simple_property = simple_property,
|
||||
check_safe_resource_name = Unsafe.check_safe_resource_name,
|
||||
resave_file = Unsafe.resave_file,
|
||||
--resave_file = Unsafe.resave_file,
|
||||
})
|
||||
|
||||
setmetatable(Safe, {
|
||||
|
@ -233,6 +233,7 @@ function M.define(dst, util)
|
||||
instance = {
|
||||
GetResource = util.wrap_function({"ResourceCache", "string", "string"},
|
||||
function(self, resource_type, unsafe_resource_name)
|
||||
--[[
|
||||
-- NOTE: resource_type=XMLFile can refer to other resources even
|
||||
-- in absolute and arbitrary relative paths. Make sure file
|
||||
-- access (fopen()) is sandboxed appropriately.
|
||||
@ -241,7 +242,8 @@ function M.define(dst, util)
|
||||
" -> "..dump(resource_name))
|
||||
local saved_path = util.resave_file(resource_name)
|
||||
-- Note: saved_path is ignored
|
||||
local res = cache:GetResource(resource_type, resource_name)
|
||||
--]]
|
||||
local res = cache:GetResource(resource_type, unsafe_resource_name)
|
||||
return util.wrap_instance(resource_type, res)
|
||||
end),
|
||||
},
|
||||
|
@ -53,9 +53,41 @@ void GraphicsOptions::apply(magic::Graphics *magic_graphics)
|
||||
vsync, triple_buffer, multisampling);
|
||||
}
|
||||
|
||||
struct BuildatResourceRouter : public magic::ResourceRouter
|
||||
{
|
||||
sp_<client::State> m_client;
|
||||
public:
|
||||
BuildatResourceRouter(magic::Context *context):
|
||||
magic::ResourceRouter(context)
|
||||
{}
|
||||
void set_client(sp_<client::State> client)
|
||||
{
|
||||
m_client = client;
|
||||
}
|
||||
magic::String Route(const magic::String &name)
|
||||
{
|
||||
if(!m_client){
|
||||
log_w(MODULE, "Resource route access: %s (no routing)",
|
||||
name.CString());
|
||||
return name;
|
||||
}
|
||||
ss_ path = m_client->get_file_path(name.CString());
|
||||
if(path == ""){
|
||||
log_w(MODULE, "Resource route access: %s (assuming local file)",
|
||||
name.CString());
|
||||
// TODO: Check that it is in a safe path
|
||||
return name;
|
||||
}
|
||||
log_w(MODULE, "Resource route access: %s -> %s",
|
||||
name.CString(), cs(path));
|
||||
return path.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
struct CApp: public App, public magic::Application
|
||||
{
|
||||
sp_<client::State> m_state;
|
||||
magic::SharedPtr<BuildatResourceRouter> m_router;
|
||||
magic::LuaScript *m_script;
|
||||
lua_State *L;
|
||||
int64_t m_last_script_tick_us;
|
||||
@ -138,6 +170,9 @@ struct CApp: public App, public magic::Application
|
||||
// Default to auto-loading resources as they are modified
|
||||
magic::ResourceCache *magic_cache = GetSubsystem<magic::ResourceCache>();
|
||||
magic_cache->SetAutoReloadResources(true);
|
||||
m_router = new BuildatResourceRouter(context_);
|
||||
magic_cache->SetResourceRouter(
|
||||
magic::SharedPtr<magic::ResourceRouter>(m_router));
|
||||
}
|
||||
|
||||
~CApp()
|
||||
@ -147,6 +182,7 @@ struct CApp: public App, public magic::Application
|
||||
void set_state(sp_<client::State> state)
|
||||
{
|
||||
m_state = state;
|
||||
m_router->set_client(state);
|
||||
}
|
||||
|
||||
int run()
|
||||
@ -224,7 +260,10 @@ struct CApp: public App, public magic::Application
|
||||
{
|
||||
log_v(MODULE, "file_updated_in_cache(): %s", cs(file_name));
|
||||
|
||||
lua_getfield(L, LUA_GLOBALSINDEX, "__buildat_file_updated_in_cache");
|
||||
magic::ResourceCache *magic_cache = GetSubsystem<magic::ResourceCache>();
|
||||
magic_cache->ReloadResourceWithDependencies(file_name.c_str());
|
||||
|
||||
/*lua_getfield(L, LUA_GLOBALSINDEX, "__buildat_file_updated_in_cache");
|
||||
if(lua_isnil(L, -1)){
|
||||
lua_pop(L, 1);
|
||||
return;
|
||||
@ -232,7 +271,7 @@ struct CApp: public App, public magic::Application
|
||||
lua_pushlstring(L, file_name.c_str(), file_name.size());
|
||||
lua_pushlstring(L, file_hash.c_str(), file_hash.size());
|
||||
lua_pushlstring(L, cached_path.c_str(), cached_path.size());
|
||||
error_logging_pcall(L, 3, 0);
|
||||
error_logging_pcall(L, 3, 0);*/
|
||||
}
|
||||
|
||||
magic::Scene* get_scene()
|
||||
|
Loading…
x
Reference in New Issue
Block a user