From: Mikko Rasa Date: Sat, 5 Sep 2015 19:57:58 +0000 (+0300) Subject: Make resource locations externally accessible X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=0071618b8eb3553948a2601b6f9e28bd7d2cd817 Make resource locations externally accessible This makes it easier to load resources e.g. from network on demand. --- diff --git a/source/resourcemanager.cpp b/source/resourcemanager.cpp index 6461c336..40dcff9b 100644 --- a/source/resourcemanager.cpp +++ b/source/resourcemanager.cpp @@ -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; } diff --git a/source/resourcemanager.h b/source/resourcemanager.h index e83a6132..f554b348 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -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 &);