|
@@ -25,24 +25,32 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#include "Types.h"
|
|
#include "Types.h"
|
|
|
#include "ResourceManager.h"
|
|
#include "ResourceManager.h"
|
|
|
-#include "ResourceLoader.h"
|
|
|
|
|
#include "String.h"
|
|
#include "String.h"
|
|
|
#include "Hash.h"
|
|
#include "Hash.h"
|
|
|
#include "Path.h"
|
|
#include "Path.h"
|
|
|
#include "Log.h"
|
|
#include "Log.h"
|
|
|
#include <algorithm>
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
+#include "TextResource.h"
|
|
|
#include "TextureResource.h"
|
|
#include "TextureResource.h"
|
|
|
|
|
|
|
|
namespace crown
|
|
namespace crown
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-ResourceManager::ResourceManager(ResourceLoader& loader) :
|
|
|
|
|
- m_resource_loader(loader),
|
|
|
|
|
|
|
+ResourceManager::ResourceManager(ResourceArchive& archive, Allocator& allocator) :
|
|
|
|
|
+ m_resource_archive(archive),
|
|
|
|
|
+ m_resource_allocator(allocator),
|
|
|
m_resources(m_allocator),
|
|
m_resources(m_allocator),
|
|
|
- m_loading_queue(m_allocator)
|
|
|
|
|
|
|
+ m_loading_queue(m_allocator),
|
|
|
|
|
+ m_loaded_queue(m_allocator),
|
|
|
|
|
+ m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
|
|
|
{
|
|
{
|
|
|
|
|
+ // FIXME hardcoded seed
|
|
|
|
|
+ m_config_hash = hash::murmur2_32("config", string::strlen("config"), 0);
|
|
|
|
|
+ m_texture_hash = hash::murmur2_32("tga", string::strlen("tga"), 0);
|
|
|
|
|
+ m_mesh_hash = hash::murmur2_32("mesh", string::strlen("mesh"), 0);
|
|
|
|
|
+ m_txt_hash = hash::murmur2_32("txt", 3, 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
@@ -89,7 +97,10 @@ ResourceId ResourceManager::load(uint32_t name, uint32_t type)
|
|
|
entry.resource = NULL;
|
|
entry.resource = NULL;
|
|
|
|
|
|
|
|
m_resources.push_back(entry);
|
|
m_resources.push_back(entry);
|
|
|
|
|
+
|
|
|
|
|
+ m_loading_mutex.lock();
|
|
|
m_loading_queue.push_back(id);
|
|
m_loading_queue.push_back(id);
|
|
|
|
|
+ m_loading_mutex.unlock();
|
|
|
|
|
|
|
|
return id;
|
|
return id;
|
|
|
}
|
|
}
|
|
@@ -111,7 +122,7 @@ void ResourceManager::unload(ResourceId name)
|
|
|
|
|
|
|
|
if (entry.references == 0 && entry.state == RS_LOADED)
|
|
if (entry.references == 0 && entry.state == RS_LOADED)
|
|
|
{
|
|
{
|
|
|
- m_resource_loader.unload(name, entry.resource);
|
|
|
|
|
|
|
+ //m_resource_loader.unload(name, entry.resource);
|
|
|
|
|
|
|
|
entry.state = RS_UNLOADED;
|
|
entry.state = RS_UNLOADED;
|
|
|
entry.resource = NULL;
|
|
entry.resource = NULL;
|
|
@@ -169,35 +180,99 @@ uint32_t ResourceManager::references(ResourceId name) const
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
void ResourceManager::flush_load_queue()
|
|
void ResourceManager::flush_load_queue()
|
|
|
{
|
|
{
|
|
|
- while (m_loading_queue.size() > 0)
|
|
|
|
|
|
|
+ m_loading_mutex.lock();
|
|
|
|
|
+
|
|
|
|
|
+ if (m_loading_queue.size() > 0)
|
|
|
{
|
|
{
|
|
|
- ResourceId resource = m_loading_queue.front();
|
|
|
|
|
|
|
+ m_loading_requests.signal();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ m_loading_mutex.unlock();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
|
|
+void ResourceManager::bring_loaded_online()
|
|
|
|
|
+{
|
|
|
|
|
+ m_loaded_mutex.lock();
|
|
|
|
|
+
|
|
|
|
|
+ // Update master table and bring online
|
|
|
|
|
+ while (m_loaded_queue.size() > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ LoadedResource lr = m_loaded_queue.front();
|
|
|
|
|
+ m_loaded_queue.pop_front();
|
|
|
|
|
+
|
|
|
|
|
+ online(lr.resource, lr.data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ m_loaded_mutex.unlock();
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- m_resource_loader.load(resource);
|
|
|
|
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
|
|
+void ResourceManager::background_load()
|
|
|
|
|
+{
|
|
|
|
|
+ // FIXME: Maybe epic crash because of concurrent access to the same allocator?
|
|
|
|
|
+ while (true)
|
|
|
|
|
+ {
|
|
|
|
|
+ m_loading_mutex.lock();
|
|
|
|
|
+ m_loading_requests.wait(m_loading_mutex);
|
|
|
|
|
|
|
|
|
|
+ ResourceId resource = m_loading_queue.front();
|
|
|
m_loading_queue.pop_front();
|
|
m_loading_queue.pop_front();
|
|
|
|
|
|
|
|
- m_resources[resource.index].state = RS_LOADING;
|
|
|
|
|
|
|
+ m_loading_mutex.unlock();
|
|
|
|
|
+
|
|
|
|
|
+ void* data = load_by_type(resource);
|
|
|
|
|
+
|
|
|
|
|
+ LoadedResource lr;
|
|
|
|
|
+ lr.resource = resource;
|
|
|
|
|
+ lr.data = data;
|
|
|
|
|
+
|
|
|
|
|
+ m_loaded_mutex.lock();
|
|
|
|
|
+
|
|
|
|
|
+ m_loaded_queue.push_back(lr);
|
|
|
|
|
+
|
|
|
|
|
+ m_loaded_mutex.unlock();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-void ResourceManager::bring_loaded_online()
|
|
|
|
|
|
|
+void* ResourceManager::load_by_type(ResourceId name) const
|
|
|
{
|
|
{
|
|
|
- m_resource_loader.m_loaded_mutex.lock();
|
|
|
|
|
|
|
+ if (name.type == m_config_hash)
|
|
|
|
|
+ {
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (name.type == m_texture_hash)
|
|
|
|
|
+ {
|
|
|
|
|
+ return TextureResource::load(m_resource_allocator, &m_resource_archive, name);
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (name.type == m_txt_hash)
|
|
|
|
|
+ {
|
|
|
|
|
+ return TextResource::load(m_resource_allocator, &m_resource_archive, name);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- Queue<LoadedResource>& loaded = m_resource_loader.m_loaded_resources;
|
|
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- while (loaded.size() > 0)
|
|
|
|
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
|
|
+void ResourceManager::unload_by_type(ResourceId name, void* resource) const
|
|
|
|
|
+{
|
|
|
|
|
+ if (name.type == m_config_hash)
|
|
|
{
|
|
{
|
|
|
- LoadedResource lr = loaded.front();
|
|
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- online(lr.resource, lr.data);
|
|
|
|
|
|
|
+ if (name.type == m_texture_hash)
|
|
|
|
|
+ {
|
|
|
|
|
+ TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- loaded.pop_front();
|
|
|
|
|
|
|
+ if (name.type == m_txt_hash)
|
|
|
|
|
+ {
|
|
|
|
|
+ TextResource::unload(m_resource_allocator, (TextResource*)resource);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- m_resource_loader.m_loaded_mutex.unlock();
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
@@ -215,5 +290,12 @@ void ResourceManager::online(ResourceId name, void* resource)
|
|
|
entry.state = RS_LOADED;
|
|
entry.state = RS_LOADED;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-} // namespace crown
|
|
|
|
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
|
|
+void* ResourceManager::background_thread(void* thiz)
|
|
|
|
|
+{
|
|
|
|
|
+ ResourceManager* mgr = (ResourceManager*)thiz;
|
|
|
|
|
|
|
|
|
|
+ mgr->background_load();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace crown
|