Browse Source

Merge branch 'master' of https://github.com/taylor001/crown

mikymod 12 years ago
parent
commit
8ddc73bdeb

+ 5 - 1
engine/CMakeLists.txt

@@ -60,6 +60,7 @@ set (CROWN_INCLUDES
 	${CMAKE_SOURCE_DIR}/engine/compilers/lua
 	${CMAKE_SOURCE_DIR}/engine/compilers/lua
 	${CMAKE_SOURCE_DIR}/engine/compilers/texture
 	${CMAKE_SOURCE_DIR}/engine/compilers/texture
 	${CMAKE_SOURCE_DIR}/engine/compilers/mesh
 	${CMAKE_SOURCE_DIR}/engine/compilers/mesh
+	${CMAKE_SOURCE_DIR}/engine/compilers/package
 )
 )
 
 
 set (SRC
 set (SRC
@@ -284,7 +285,6 @@ else ()
 endif (CROWN_DEBUG OR CROWN_DEVELOPMENT)
 endif (CROWN_DEBUG OR CROWN_DEVELOPMENT)
 
 
 set (RESOURCE_HEADERS
 set (RESOURCE_HEADERS
-
 	resource/Resource.h
 	resource/Resource.h
 	resource/ResourceFormat.h
 	resource/ResourceFormat.h
 	resource/ResourceLoader.h
 	resource/ResourceLoader.h
@@ -297,6 +297,8 @@ set (RESOURCE_HEADERS
 	resource/FontResource.h
 	resource/FontResource.h
 	resource/SoundResource.h
 	resource/SoundResource.h
 	resource/MaterialResource.h
 	resource/MaterialResource.h
+	resource/PackageResource.h
+	resource/ResourcePackage.h
 )
 )
 
 
 set (OS_SRC
 set (OS_SRC
@@ -336,6 +338,7 @@ set (COMPILER_SRC
 	compilers/BundleCompiler.cpp
 	compilers/BundleCompiler.cpp
 	compilers/lua/LuaCompiler.cpp
 	compilers/lua/LuaCompiler.cpp
 	compilers/texture/TextureCompiler.cpp
 	compilers/texture/TextureCompiler.cpp
+	compilers/package/PackageCompiler.cpp
 )
 )
 
 
 set (COMPILER_HEADER
 set (COMPILER_HEADER
@@ -343,6 +346,7 @@ set (COMPILER_HEADER
 	compilers/BundleCompiler.h
 	compilers/BundleCompiler.h
 	compilers/lua/LuaCompiler.h
 	compilers/lua/LuaCompiler.h
 	compilers/texture/TextureCompiler.h
 	compilers/texture/TextureCompiler.h
+	compilers/package/PackageCompiler.h
 )
 )
 
 
 set (CROWN_LIBRARIES)
 set (CROWN_LIBRARIES)

+ 4 - 0
engine/compilers/BundleCompiler.cpp

@@ -99,6 +99,10 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir)
 		{
 		{
 			result = m_lua.compile(source_dir, bundle_dir, filename, out_name);
 			result = m_lua.compile(source_dir, bundle_dir, filename, out_name);
 		}
 		}
+		else if (resource_type_hash == PACKAGE_TYPE)
+		{
+			result = m_package.compile(source_dir, bundle_dir, filename, out_name);
+		}
 		else
 		else
 		{
 		{
 			Log::e("Oops, unknown resource type!");
 			Log::e("Oops, unknown resource type!");

+ 2 - 0
engine/compilers/BundleCompiler.h

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #include "TextureCompiler.h"
 #include "TextureCompiler.h"
 #include "LuaCompiler.h"
 #include "LuaCompiler.h"
+#include "PackageCompiler.h"
 #include "DynamicString.h"
 #include "DynamicString.h"
 #include "Vector.h"
 #include "Vector.h"
 #include "DiskFilesystem.h"
 #include "DiskFilesystem.h"
@@ -51,6 +52,7 @@ private:
 
 
 	TextureCompiler	m_texture;
 	TextureCompiler	m_texture;
 	LuaCompiler 	m_lua;
 	LuaCompiler 	m_lua;
+	PackageCompiler m_package;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 131 - 0
engine/compilers/package/PackageCompiler.cpp

@@ -0,0 +1,131 @@
+/*
+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 "Allocator.h"
+#include "File.h"
+#include "Filesystem.h"
+#include "Hash.h"
+#include "JSONParser.h"
+#include "PackageCompiler.h"
+#include "PackageResource.h"
+#include "TempAllocator.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+PackageCompiler::PackageCompiler()
+	: m_has_texture(false), m_has_lua(false), m_textures(default_allocator()), m_scripts(default_allocator())
+{
+}
+
+//-----------------------------------------------------------------------------
+size_t PackageCompiler::compile_impl(Filesystem& fs, const char* resource_path)
+{
+	File* file = fs.open(resource_path, FOM_READ);
+
+	char file_buf[4096];
+	file->read(file_buf, file->size());
+
+	JSONParser json(file_buf);
+	JSONElement root = json.root();
+
+	// Check for resource types
+	if (root.has_key("texture"))
+	{
+		JSONElement texture_array = root.key("texture");
+		uint32_t texture_array_size = texture_array.size();
+
+		for (uint32_t i = 0; i < texture_array_size; i++)
+		{
+			TempAllocator256 alloc;
+			DynamicString texture_name(alloc);
+			texture_name += texture_array[i].string_value();
+			texture_name += ".texture";
+
+			if (!fs.is_file(texture_name.c_str()))
+			{
+				Log::e("Texture '%s' does not exist.", texture_name.c_str());
+				return 0;
+			}
+
+			ResourceId id;
+			id.id = hash::murmur2_64(texture_name.c_str(), string::strlen(texture_name.c_str()), 0);
+			m_textures.push_back(id);
+		}
+	}
+
+	// Check for scripts
+	if (root.has_key("lua"))
+	{
+		JSONElement lua_array = root.key("lua");
+		//lua_array = root.key("lua");
+		uint32_t lua_array_size = lua_array.size();
+
+		for (uint32_t i = 0; i < lua_array_size; i++)
+		{
+			TempAllocator256 alloc;
+			DynamicString lua_name(alloc);
+			lua_name += lua_array[i].string_value();
+			lua_name += ".lua";
+
+			if (!fs.is_file(lua_name.c_str()))
+			{
+				Log::e("Lua script '%s' does not exist.", lua_name.c_str());
+				return 0;
+			}
+
+			ResourceId id;
+			id.id = hash::murmur2_64(lua_name.c_str(), string::strlen(lua_name.c_str()), 0);
+			m_scripts.push_back(id);
+		}
+	}
+
+	return sizeof(PackageHeader) +
+			m_textures.size() * sizeof(ResourceId) +
+			m_scripts.size() * sizeof(ResourceId);
+}
+
+//-----------------------------------------------------------------------------
+void PackageCompiler::write_impl(File* out_file)
+{
+	PackageHeader header;
+	header.num_textures = m_textures.size();
+	header.num_scripts = m_scripts.size();
+
+	header.textures_offset = sizeof(PackageHeader);
+	header.scripts_offset  = header.textures_offset + sizeof(ResourceId) * header.num_textures;
+
+	out_file->write((char*) &header, sizeof(PackageHeader));
+	out_file->write((char*) m_textures.begin(), sizeof(ResourceId) * header.num_textures);
+	out_file->write((char*) m_scripts.begin(), sizeof(ResourceId) * header.num_scripts);
+
+	// Cleanup
+	m_textures.clear();
+	m_scripts.clear();
+}
+
+} // namespace crown

+ 54 - 0
engine/compilers/package/PackageCompiler.h

@@ -0,0 +1,54 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Compiler.h"
+#include "Resource.h"
+#include "List.h"
+
+namespace crown
+{
+
+class PackageCompiler : public Compiler
+{
+public:
+
+	PackageCompiler();
+
+	size_t compile_impl(Filesystem& fs, const char* resource_path);
+	void write_impl(File* out_file);
+
+private:
+
+	bool m_has_texture;
+	bool m_has_lua;
+
+	List<ResourceId> m_textures;
+	List<ResourceId> m_scripts;
+};
+
+} // namespace crown

+ 0 - 1
engine/resource/LuaResource.h

@@ -31,7 +31,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Bundle.h"
 #include "Bundle.h"
 #include "Allocator.h"
 #include "Allocator.h"
 #include "File.h"
 #include "File.h"
-#include "Log.h"
 
 
 namespace crown
 namespace crown
 {
 {

+ 122 - 0
engine/resource/PackageResource.h

@@ -0,0 +1,122 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Resource.h"
+#include "Bundle.h"
+#include "Allocator.h"
+#include "File.h"
+
+namespace crown
+{
+
+// All offsets are absolute
+struct PackageHeader
+{
+	uint32_t num_textures;
+	uint32_t textures_offset;
+	uint32_t num_scripts;
+	uint32_t scripts_offset;
+};
+
+class PackageResource
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
+	{
+		File* file = bundle.open(id);
+
+		const size_t file_size = file->size() - 12;
+		PackageResource* res = (PackageResource*) allocator.allocate(sizeof(PackageResource));
+		res->m_data = (char*) allocator.allocate(file_size);
+		file->read(res->m_data, file_size);
+
+		bundle.close(file);
+
+		return res;
+	}
+
+	//-----------------------------------------------------------------------------
+	static void online(void* /*resource*/) {}
+
+	//-----------------------------------------------------------------------------
+	static void unload(Allocator& allocator, void* resource)
+	{
+		CE_ASSERT_NOT_NULL(resource);
+
+		allocator.deallocate(((PackageResource*)resource)->m_data);
+		allocator.deallocate(resource);
+	}
+
+	//-----------------------------------------------------------------------------
+	static void offline(void* /*resource*/) {}
+
+public:
+
+	//-----------------------------------------------------------------------------
+	uint32_t num_textures() const
+	{
+		CE_ASSERT_NOT_NULL(m_data);
+
+		return ((PackageHeader*)m_data)->num_textures;
+	}
+
+	//-----------------------------------------------------------------------------
+	uint32_t num_scripts() const
+	{
+		CE_ASSERT_NOT_NULL(m_data);
+
+		return ((PackageHeader*)m_data)->num_scripts;
+	}
+
+	//-----------------------------------------------------------------------------
+	ResourceId get_texture_id(uint32_t i) const
+	{
+		CE_ASSERT(i < num_textures(), "Index out of bounds");
+
+		ResourceId* begin = (ResourceId*) (m_data + ((PackageHeader*)m_data)->textures_offset);
+		return begin[i];
+	}
+
+	//-----------------------------------------------------------------------------
+	ResourceId get_script_id(uint32_t i) const
+	{
+		CE_ASSERT(i < num_scripts(), "Index out of bounds");
+
+		ResourceId* begin = (ResourceId*) (m_data + ((PackageHeader*)m_data)->scripts_offset);
+		return begin[i];
+	}
+
+private:
+
+	char* m_data;
+};
+
+} // namespace crown

+ 2 - 0
engine/resource/Resource.h

@@ -41,6 +41,7 @@ const char* const TEXT_EXTENSION			= "text";
 const char* const MATERIAL_EXTENSION		= "material";
 const char* const MATERIAL_EXTENSION		= "material";
 const char* const SOUND_EXTENSION			= "sound";
 const char* const SOUND_EXTENSION			= "sound";
 const char* const CONFIG_EXTENSION			= "config";
 const char* const CONFIG_EXTENSION			= "config";
+const char* const PACKAGE_EXTENSION			= "package";
 
 
 const uint32_t TEXTURE_TYPE					= 0xDEED4F7;
 const uint32_t TEXTURE_TYPE					= 0xDEED4F7;
 const uint32_t MESH_TYPE					= 0xA6E48B29;
 const uint32_t MESH_TYPE					= 0xA6E48B29;
@@ -49,6 +50,7 @@ const uint32_t TEXT_TYPE					= 0x45CC650;
 const uint32_t MATERIAL_TYPE				= 0x46807A92;
 const uint32_t MATERIAL_TYPE				= 0x46807A92;
 const uint32_t SOUND_TYPE					= 0xD196AB6E;
 const uint32_t SOUND_TYPE					= 0xD196AB6E;
 const uint32_t CONFIG_TYPE					= 0x17DEA5E1;
 const uint32_t CONFIG_TYPE					= 0x17DEA5E1;
+const uint32_t PACKAGE_TYPE					= 0xC0A2212C;
 
 
 /// ResourceId uniquely identifies a resource by its name and type.
 /// ResourceId uniquely identifies a resource by its name and type.
 /// In order to speed up the lookup by the manager, it also keeps
 /// In order to speed up the lookup by the manager, it also keeps

+ 1 - 0
engine/resource/ResourceManager.h

@@ -122,6 +122,7 @@ private:
 
 
 private:
 private:
 
 
+	friend class			ResourcePackage;
 	friend class			Device;
 	friend class			Device;
 };
 };
 
 

+ 98 - 0
engine/resource/ResourcePackage.h

@@ -0,0 +1,98 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "ResourceManager.h"
+#include "PackageResource.h"
+#include "Resource.h"
+
+namespace crown
+{
+
+class ResourcePackage
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	ResourcePackage(ResourceManager& resman, const PackageResource* package)
+		: m_resource_manager(&resman), m_package(package), m_has_loaded(false)
+	{
+		CE_ASSERT_NOT_NULL(package);
+	}
+
+	//-----------------------------------------------------------------------------
+	void load()
+	{
+		Log::i("ResourcePackage: loading %d textures", m_package->num_textures());
+		Log::i("ResourcePackage: loading %d scripts", m_package->num_scripts());
+		for (uint32_t i = 0; i < m_package->num_textures(); i++)
+		{
+			m_resource_manager->load(TEXTURE_TYPE, m_package->get_texture_id(i));
+		}
+
+		for (uint32_t i = 0; i < m_package->num_scripts(); i++)
+		{
+			m_resource_manager->load(LUA_TYPE, m_package->get_script_id(i));
+		}
+	}
+
+	//-----------------------------------------------------------------------------
+	void unload()
+	{
+		for (uint32_t i = 0; i < m_package->num_textures(); i++)
+		{
+			m_resource_manager->unload(m_package->get_texture_id(i));
+		}
+
+		for (uint32_t i = 0; i < m_package->num_scripts(); i++)
+		{
+			m_resource_manager->unload(m_package->get_script_id(i));
+		}		
+	}
+
+	//-----------------------------------------------------------------------------
+	void flush()
+	{
+		m_resource_manager->flush();
+		m_has_loaded = true;
+	}
+
+	//-----------------------------------------------------------------------------
+	bool has_loaded() const
+	{
+		return m_has_loaded;
+	}
+
+private:
+
+	ResourceManager* m_resource_manager;
+	const PackageResource* m_package;
+	bool m_has_loaded;
+};
+
+} // namespace crown

+ 2 - 0
engine/resource/ResourceRegistry.cpp

@@ -29,6 +29,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "TextureResource.h"
 #include "TextureResource.h"
 #include "MeshResource.h"
 #include "MeshResource.h"
 #include "SoundResource.h"
 #include "SoundResource.h"
+#include "PackageResource.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -39,6 +40,7 @@ static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 	{ TEXTURE_TYPE, TextureResource::load, TextureResource::unload, TextureResource::online, TextureResource::offline },
 	{ TEXTURE_TYPE, TextureResource::load, TextureResource::unload, TextureResource::online, TextureResource::offline },
 	{ MESH_TYPE, MeshResource::load, MeshResource::unload, MeshResource::online, MeshResource::offline },
 	{ MESH_TYPE, MeshResource::load, MeshResource::unload, MeshResource::online, MeshResource::offline },
 	{ SOUND_TYPE, SoundResource::load, SoundResource::unload, SoundResource::online, SoundResource::offline },
 	{ SOUND_TYPE, SoundResource::load, SoundResource::unload, SoundResource::online, SoundResource::offline },
+	{ PACKAGE_TYPE, PackageResource::load, PackageResource::unload, PackageResource::online, PackageResource::offline },	
 	{ 0, NULL, NULL, NULL, NULL }
 	{ 0, NULL, NULL, NULL, NULL }
 };
 };