]> git.tdb.fi Git - libs/gl.git/commitdiff
Make resource locations externally accessible
authorMikko Rasa <tdb@tdb.fi>
Sat, 5 Sep 2015 19:57:58 +0000 (22:57 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 5 Sep 2015 19:57:58 +0000 (22:57 +0300)
This makes it easier to load resources e.g. from network on demand.

source/resourcemanager.cpp
source/resourcemanager.h

index 6461c33620eb3aa386418afadad48cab8f6a51d6..40dcff9bc36876b649edaec7c0aeecca0aee45c1 100644 (file)
@@ -70,19 +70,29 @@ void *ResourceManager::get_data_for_resource(const Resource &r)
 }
 
 void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n)
+{
+       set_resource_location(r, ResourceLocation(c, n));
+}
+
+void ResourceManager::set_resource_location(Resource &r, const ResourceLocation &l)
 {
        ManagedResource &managed = get_item(resources, &r);
-       managed.collection = &c;
-       managed.name = n;
+       managed.location = l;
 
        if(policy==LOAD_IMMEDIATELY)
                load_resource(r);
 }
 
+const ResourceManager::ResourceLocation *ResourceManager::get_resource_location(const Resource &r) const
+{
+       const ManagedResource &managed = get_item(resources, &r);
+       return managed.location.collection ? &managed.location : 0;
+}
+
 void ResourceManager::load_resource(Resource &r)
 {
        ManagedResource &managed = get_item(resources, &r);
-       if(!managed.collection)
+       if(!managed.location.collection)
                throw runtime_error("no location");
 
        if(managed.state!=ManagedResource::NOT_LOADED)
@@ -242,9 +252,18 @@ bool ResourceManager::age_order(ManagedResource *mr1, ManagedResource *mr2)
 }
 
 
+ResourceManager::ResourceLocation::ResourceLocation():
+       collection(0)
+{ }
+
+ResourceManager::ResourceLocation::ResourceLocation(DataFile::Collection &c, const string &n):
+       collection(&c),
+       name(n)
+{ }
+
+
 ResourceManager::ManagedResource::ManagedResource(Resource &r):
        resource(&r),
-       collection(0),
        io(0),
        loader(0),
        state(NOT_LOADED),
@@ -254,7 +273,7 @@ ResourceManager::ManagedResource::ManagedResource(Resource &r):
 
 void ResourceManager::ManagedResource::start_loading()
 {
-       io = collection->open_raw(name);
+       io = location.collection->open_raw(location.name);
        loader = resource->load(*io);
        if(!loader)
        {
@@ -350,7 +369,7 @@ void ResourceManager::LoadingThread::main()
                        catch(const exception &e)
                        {
                                MutexLock lock(queue_mutex);
-                               error_queue.push_back(resource_load_error(managed->name, e));
+                               error_queue.push_back(resource_load_error(managed->location.name, e));
                                managed->state = ManagedResource::LOAD_ERROR;
                        }
 
index e83a61328e590c528d1c05f7157dea0d523e995d..f554b3484c7e006006d9b292bc803aea136f9e50 100644 (file)
@@ -31,6 +31,15 @@ public:
                LOAD_MANUALLY
        };
 
+       struct ResourceLocation
+       {
+               DataFile::Collection *collection;
+               std::string name;
+
+               ResourceLocation();
+               ResourceLocation(DataFile::Collection &, const std::string &);
+       };
+
 private:
        struct ManagedResource
        {
@@ -45,8 +54,7 @@ private:
                };
 
                Resource *resource;
-               DataFile::Collection *collection;
-               std::string name;
+               ResourceLocation location;
                IO::Seekable *io;
                Resource::AsyncLoader *loader;
                State state;
@@ -126,6 +134,8 @@ public:
        void add_resource(Resource &);
        void *get_data_for_resource(const Resource &);
        void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
+       void set_resource_location(Resource &, const ResourceLocation &);
+       const ResourceLocation *get_resource_location(const Resource &) const;
        void load_resource(Resource &);
        bool is_resource_loaded(const Resource &);
        void resource_used(const Resource &);