| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <algorithm>
- #include <inttypes.h>
- #include "Types.h"
- #include "ResourceManager.h"
- #include "ResourceRegistry.h"
- #include "StringUtils.h"
- #include "StringUtils.h"
- #include "TempAllocator.h"
- #include "DynamicString.h"
- #include "Queue.h"
- #include "Log.h"
- namespace crown
- {
- ResourceId::ResourceId(const char* type, const char* name)
- : type(string::murmur2_64(type, string::strlen(type), 0))
- , name(string::murmur2_64(name, string::strlen(name), 0))
- {
- }
- //-----------------------------------------------------------------------------
- ResourceManager::ResourceManager(Bundle& bundle)
- : m_resource_heap("resource", default_allocator())
- , m_loader(bundle, m_resource_heap)
- , m_resources(default_allocator())
- {
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::load(ResourceId id)
- {
- // Search for an already existent resource
- ResourceEntry* entry = find(id);
- // If resource not found, post load request
- if (entry == NULL)
- {
- m_loader.load(id);
- return;
- }
- // Else, increment its reference count
- entry->references++;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::unload(ResourceId id, bool force)
- {
- CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
- flush();
- ResourceEntry* entry = find(id);
- entry->references--;
- if (entry->references == 0 || force)
- {
- resource_on_offline(id.type, entry->resource);
- resource_on_unload(id.type, m_resource_heap, entry->resource);
- // Swap with last
- ResourceEntry temp = m_resources[array::size(m_resources) - 1];
- (*entry) = temp;
- array::pop_back(m_resources);
- }
- }
- //-----------------------------------------------------------------------------
- bool ResourceManager::has(ResourceId id) const
- {
- ResourceEntry* entry = find(id);
- return entry != NULL;
- }
- //-----------------------------------------------------------------------------
- const void* ResourceManager::get(const char* type, const char* name) const
- {
- ResourceEntry* entry = find(ResourceId(type, name));
- CE_ASSERT(entry != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
- CE_ASSERT(entry->resource != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
- return entry->resource;
- }
- //-----------------------------------------------------------------------------
- const void* ResourceManager::get(ResourceId id) const
- {
- CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
- return find(id)->resource;
- }
- //-----------------------------------------------------------------------------
- bool ResourceManager::is_loaded(ResourceId id) const
- {
- CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
- return find(id)->resource != NULL;
- }
- //-----------------------------------------------------------------------------
- uint32_t ResourceManager::references(ResourceId id) const
- {
- CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
- return find(id)->references;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::flush()
- {
- m_loader.flush();
- complete_requests();
- }
- //-----------------------------------------------------------------------------
- ResourceEntry* ResourceManager::find(ResourceId id) const
- {
- const ResourceEntry* entry = std::find(array::begin(m_resources), array::end(m_resources), id);
- return entry != array::end(m_resources) ? const_cast<ResourceEntry*>(entry) : NULL;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::complete_requests()
- {
- TempAllocator1024 ta;
- Array<ResourceData> loaded(ta);
- m_loader.get_loaded(loaded);
- for (uint32_t i = 0; i < array::size(loaded); i++)
- complete_request(loaded[i].id, loaded[i].data);
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::complete_request(ResourceId id, void* data)
- {
- resource_on_online(id.type, data);
- ResourceEntry entry;
- entry.id = id;
- entry.references = 1;
- entry.resource = data;
- array::push_back(m_resources, entry);
- }
- } // namespace crown
|