Просмотр исходного кода

Improve texture hash generation and reliability

Daniele Bartolini 12 лет назад
Родитель
Сommit
610d076d0a

+ 5 - 9
src/FileResourceArchive.cpp

@@ -49,22 +49,18 @@ FileResourceArchive::~FileResourceArchive()
 FileStream* FileResourceArchive::find(ResourceId name)
 {
 	// Convert name/type into strings
-	char name_string[512];
-	char type_string[512];
+	char resource_name[512];
 
-	// FIXME
-	snprintf(name_string, 512, "%X", name.name);
-	snprintf(type_string, 512, "%X", name.type);
-
-	string::strncat(name_string, type_string, 512);
+	// Fixme
+	snprintf(resource_name, 512, "%.8X%.8X", name.name, name.type);
 
 	// Search the resource in the filesystem
-	if (m_filesystem.exists(name_string) == false)
+	if (m_filesystem.exists(resource_name) == false)
 	{
 		return NULL;
 	}
 
-	FileStream* file = (FileStream*)m_filesystem.open(name_string, SOM_READ);
+	FileStream* file = (FileStream*)m_filesystem.open(resource_name, SOM_READ);
 
 	/// FIXME harcoded!!!
 	file->skip(sizeof(uint32_t) * 3);

+ 12 - 4
src/Resource.h

@@ -27,15 +27,23 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "Hash.h"
+#include "String.h"
 
 namespace crown
 {
 
 /// Hashed values for supported resource types
-const uint32_t TEXTURE_TYPE	= hash::murmur2_32("tga", 3, 0);
-const uint32_t MESH_TYPE	= hash::murmur2_32("dae", 3, 0);
-const uint32_t SCRIPT_TYPE	= hash::murmur2_32("lua", 3, 0);
-const uint32_t TEXT_TYPE	= hash::murmur2_32("txt", 3, 0);
+const char* const TEXTURE_EXTENSION		= "tga";
+const char* const MESH_EXTENSION		= "dae";
+const char* const SCRIPT_EXTENSION		= "lua";
+const char* const TEXT_EXTENSION		= "txt";
+const char* const MATERIAL_EXTENSION	= "material";
+
+const uint32_t TEXTURE_TYPE		= hash::murmur2_32(TEXTURE_EXTENSION,  string::strlen(TEXTURE_EXTENSION),  0);
+const uint32_t MESH_TYPE		= hash::murmur2_32(MESH_EXTENSION,     string::strlen(MESH_EXTENSION),     0);
+const uint32_t SCRIPT_TYPE		= hash::murmur2_32(SCRIPT_EXTENSION,   string::strlen(SCRIPT_EXTENSION),   0);
+const uint32_t TEXT_TYPE		= hash::murmur2_32(TEXT_EXTENSION,     string::strlen(TEXT_EXTENSION),     0);
+const uint32_t MATERIAL_TYPE	= hash::murmur2_32(MATERIAL_EXTENSION, string::strlen(MATERIAL_EXTENSION), 0);
 
 /// Enumerates the loading states of a resource
 enum ResourceState

+ 15 - 5
tools/compilers/Compiler.cpp

@@ -23,17 +23,19 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
+#include <cstring>
+#include <cstdlib>
 #include "Compiler.h"
 #include "Hash.h"
 #include "Path.h"
 #include "FileStream.h"
-#include <cstring>
+#include "Log.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Compiler::Compiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
+Compiler::Compiler(const char* root_path, const char* dest_path, const char* resource, uint32_t type_expected, uint32_t seed) :
 	m_name_hash(0),
 	m_type_hash(0),
 	m_seed(seed),
@@ -64,14 +66,22 @@ Compiler::Compiler(const char* root_path, const char* dest_path, const char* res
 	path::filename_without_extension(m_resource, m_name, MAX_RESOURCE_NAME_LENGTH);
 	path::extension(m_resource, m_type, MAX_RESOURCE_TYPE_LENGTH);
 
-	// Compute hashes
+	// Compute the resource hashes
 	m_name_hash = hash::murmur2_32(m_name, string::strlen(m_name), m_seed);
-	m_type_hash = hash::murmur2_32(m_type, string::strlen(m_type), m_seed);
+
+	// NOTE: The type hash _MUST_ be generated with seed = 0
+	m_type_hash = hash::murmur2_32(m_type, string::strlen(m_type), 0);
+
+	if (m_type_hash != type_expected)
+	{
+		Log::e("Compiler: Trying to compile '%s' with the wrong compiler. Aborting.", resource_path());
+		exit(-1);
+	}
 
 	char dest_name[17];
 	memset(dest_name, 0, 17);
 
-	snprintf(dest_name, 17, "%X%X", m_name_hash, m_type_hash);
+	snprintf(dest_name, 17, "%.8X%.8X", m_name_hash, m_type_hash);
 
 	// Open streams
 	m_src_file = (FileStream*)m_root_fs.open(m_resource, SOM_READ);

+ 7 - 4
tools/compilers/Compiler.h

@@ -54,12 +54,15 @@ class Compiler
 {
 public:
 
-						/// Looks for the @resource int the @root_path and prepares it to
-						/// compilation using @seed to generate string hashes.
-						Compiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed);
+	/// Looks for the @resource int the @root_path and prepares it to
+	/// compilation using @seed to generate hashes for the resource name.
+	/// Implementation must declare the type of resource they are expecting
+	/// to work on by setting @type_expected appropriately.
+						Compiler(const char* root_path, const char* dest_path, const char* resource,
+								 uint32_t type_expected, uint32_t seed);
 	virtual				~Compiler();
 
-						/// Actually compiles the resource.
+	/// Actually compiles the resource.
 	virtual bool		compile() = 0;
 
 	virtual void		write() = 0;

+ 2 - 1
tools/compilers/lua/LuaCompiler.cpp

@@ -1,12 +1,13 @@
 #include "LuaCompiler.h"
 #include "FileStream.h"
+#include "Resource.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 LuaCompiler::LuaCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
-	Compiler(root_path, dest_path, resource, seed),
+	Compiler(root_path, dest_path, resource, SCRIPT_TYPE, seed),
 	m_file_size(0),
 	m_file_data(NULL)
 {

+ 1 - 1
tools/compilers/mat/MATCompiler.cpp

@@ -31,7 +31,7 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 MATCompiler::MATCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
-	Compiler(root_path, dest_path, resource, seed)
+	Compiler(root_path, dest_path, resource, MATERIAL_TYPE, seed)
 {
 }
 

+ 1 - 0
tools/compilers/mat/MATCompiler.h

@@ -27,6 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Compiler.h"
 #include "FileStream.h"
+#include "Resource.h"
 
 namespace crown
 {

+ 12 - 6
tools/compilers/resource-hash.cpp

@@ -29,17 +29,23 @@ int main(int argc, char** argv)
 	path::extension(resource_in, resource_extension, 256);
 	
 	uint32_t resource_basename_hash = hash::murmur2_32(resource_basename, string::strlen(resource_basename), hash_seed);
-	uint32_t resource_extension_hash = hash::murmur2_32(resource_extension, string::strlen(resource_extension), hash_seed);
+	uint32_t resource_extension_hash = hash::murmur2_32(resource_extension, string::strlen(resource_extension), 0);
+
+	if (resource_extension_hash != TEXTURE_TYPE &&
+		resource_extension_hash != MESH_TYPE &&
+		resource_extension_hash != SCRIPT_TYPE &&
+		resource_extension_hash != TEXT_TYPE &&
+		resource_extension_hash != MATERIAL_TYPE)
+	{
+		printf("%s: ERROR: cannot generate hash for resource '%s': Unknown type.\n", argv[0], resource_in);
+		exit(-1);
+	}
 
 	char out_filename[512];
 	out_filename[0] = '\0';
 
-	snprintf(resource_basename, 256, "%X", resource_basename_hash);
-	snprintf(resource_extension, 256, "%X", resource_extension_hash);
+	snprintf(out_filename, 256, "%.8X%.8X", resource_basename_hash, resource_extension_hash);
 	
-	string::strncat(out_filename, resource_basename, 512);
-	string::strncat(out_filename, resource_extension, 512);
-
 	printf("%s\n", out_filename);
 
 	return 0;

+ 2 - 1
tools/compilers/tga/TGACompiler.cpp

@@ -26,13 +26,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "TGACompiler.h"
 #include "FileStream.h"
 #include "Pixel.h"
+#include "Resource.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 TGACompiler::TGACompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
-	Compiler(root_path, dest_path, resource, seed),
+	Compiler(root_path, dest_path, resource, TEXTURE_TYPE, seed),
 	m_image_format(PF_UNKNOWN),
 	m_image_channels(0),
 	m_image_size(0),

+ 2 - 1
tools/compilers/txt/TXTCompiler.cpp

@@ -25,13 +25,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "TXTCompiler.h"
 #include "FileStream.h"
+#include "Resource.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 TXTCompiler::TXTCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
-	Compiler(root_path, dest_path, resource, seed),
+	Compiler(root_path, dest_path, resource, TEXT_TYPE, seed),
 	m_file_size(0),
 	m_file_data(NULL)
 {