Răsfoiți Sursa

ScriptResource implemented

mikymod 12 ani în urmă
părinte
comite
7cd69778c8
4 a modificat fișierele cu 125 adăugiri și 7 ștergeri
  1. 15 7
      src/ResourceManager.cpp
  2. 1 0
      src/ResourceManager.h
  3. 54 0
      src/ScriptResource.cpp
  4. 55 0
      src/ScriptResource.h

+ 15 - 7
src/ResourceManager.cpp

@@ -33,6 +33,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "TextResource.h"
 #include "TextureResource.h"
+#include "ScriptResource.h"
 
 namespace crown
 {
@@ -47,10 +48,11 @@ ResourceManager::ResourceManager(ResourceArchive& archive, Allocator& 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_config_hash = hash::murmur2_32("config", 6, 0);
+	m_texture_hash = hash::murmur2_32("tga", 3, 0);
+	m_mesh_hash = hash::murmur2_32("mesh", 4, 0);
 	m_txt_hash = hash::murmur2_32("txt", 3, 0);
+	m_script_hash = hash::murmur2_32("lua", 3, 0);
 }
 
 //-----------------------------------------------------------------------------
@@ -250,6 +252,10 @@ void* ResourceManager::load_by_type(ResourceId name) const
 	{
 		return TextResource::load(m_resource_allocator, m_resource_archive, name);
 	}
+	else if (name.type == m_script_hash)
+	{
+		return ScriptResource::load(m_resource_allocator, m_resource_archive, name);
+	}
 
 	return NULL;
 }
@@ -261,16 +267,18 @@ void ResourceManager::unload_by_type(ResourceId name, void* resource) const
 	{
 		return;
 	}
-
-	if (name.type == m_texture_hash)
+	else if (name.type == m_texture_hash)
 	{
 		TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
 	}
-
-	if (name.type == m_txt_hash)
+	else if (name.type == m_txt_hash)
 	{
 		TextResource::unload(m_resource_allocator, (TextResource*)resource);
 	}
+	else if (name.type == m_script_hash)
+	{
+		ScriptResource::unload(m_resource_allocator, (ScriptResource*)resource);
+	}
 
 	return;
 }

+ 1 - 0
src/ResourceManager.h

@@ -160,6 +160,7 @@ private:
 	uint32_t			m_texture_hash;
 	uint32_t			m_mesh_hash;
 	uint32_t			m_txt_hash;
+	uint32_t			m_script_hash;
 };
 
 } // namespace crown

+ 54 - 0
src/ScriptResource.cpp

@@ -0,0 +1,54 @@
+#include <cassert>
+
+#include "ScriptResource.h"
+#include "ResourceArchive.h"
+#include "Log.h"
+#include "FileStream.h"
+#include "Allocator.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+void* ScriptResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
+{
+	FileStream* stream = archive.find(id);
+
+	if (stream != NULL)
+	{
+		ScriptResource* resource = (ScriptResource*)allocator.allocate(sizeof(ScriptResource));
+	
+		size_t size = stream->size() - stream->position();
+
+		resource->m_data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
+
+		stream->read(resource->m_data, size);
+
+		return resource;
+	}
+
+	return NULL;
+}
+
+//-----------------------------------------------------------------------------
+void ScriptResource::online(void* resource)
+{
+	(void) resource;
+}
+
+//-----------------------------------------------------------------------------
+void ScriptResource::unload(Allocator& allocator, void* resource)
+{
+	assert(resource != NULL);
+
+	allocator.deallocate(((ScriptResource*)resource)->m_data);
+	allocator.deallocate(resource);
+}
+
+//-----------------------------------------------------------------------------
+void ScriptResource::offline()
+{
+
+}
+
+} // namespace crown

+ 55 - 0
src/ScriptResource.h

@@ -0,0 +1,55 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Resource.h"
+
+namespace crown
+{
+
+class ResourceArchive;
+class Allocator;
+
+class ScriptResource
+{
+public:
+
+	static void*		load(Allocator& allocator, ResourceArchive& archive, ResourceId id);
+	static void			online(void* script);
+	static void			unload(Allocator& allocator, void* resource);
+	static void			offline();
+
+public:
+
+	const uint8_t*		data() const { return m_data; }
+
+private:
+
+	uint8_t*			m_data;
+};
+
+} // namespace crown