Daniele Bartolini 6 лет назад
Родитель
Сommit
145cba8859

+ 7 - 1
src/core/guid.cpp

@@ -81,7 +81,7 @@ namespace guid
 		return num == 6;
 	}
 
-	void to_string(char* buf, u32 len, const Guid& guid)
+	const char* to_string(char* buf, u32 len, const Guid& guid)
 	{
 		snprintf(buf, len, "%.8x-%.4x-%.4x-%.4x-%.4x%.8x"
 			, guid.data1
@@ -91,6 +91,7 @@ namespace guid
 			, (u16)((guid.data4 & 0x0000ffff00000000u) >> 32)
 			, (u32)((guid.data4 & 0x00000000ffffffffu) >>  0)
 			);
+		return buf;
 	}
 
 } // namespace guid
@@ -105,4 +106,9 @@ bool operator<(const Guid& a, const Guid& b)
 	return memcmp(&a, &b, sizeof(a)) < 0;
 }
 
+u32 hash<Guid>::operator()(const Guid& id) const
+{
+	return id.data1;
+}
+
 } // namespace crown

+ 15 - 2
src/core/guid.h

@@ -7,6 +7,8 @@
 
 #include "core/types.h"
 
+#define GUID_BUF_LEN 37
+
 namespace crown
 {
 /// Holds a globally unique identifier.
@@ -45,8 +47,10 @@ namespace guid
 	/// Parses the @a guid from @a str and returns true if success.
 	bool try_parse(Guid& guid, const char* str);
 
-	/// Fills @a buf with the string representation of the @a guid.
-	void to_string(char* buf, u32 len, const Guid& guid);
+	/// Returns @a guid converted to ASCIIZ.
+	/// @a buf size must be greater than or equal to GUID_BUF_LEN or the
+	/// returned string will be truncated.
+	const char* to_string(char* buf, u32 len, const Guid& guid);
 
 } // namespace guid
 
@@ -56,6 +60,15 @@ bool operator==(const Guid& a, const Guid& b);
 /// Returns whether Guid @a is lesser than @b.
 bool operator<(const Guid& a, const Guid& b);
 
+template <typename T>
+struct hash;
+
+template<>
+struct hash<Guid>
+{
+	u32 operator()(const Guid& id) const;
+};
+
 static const Guid GUID_ZERO = { 0u, 0u, 0u, 0u };
 
 } // namespace crown

+ 8 - 7
src/core/strings/dynamic_string.cpp

@@ -5,28 +5,29 @@
 
 #include "core/containers/array.inl"
 #include "core/guid.h"
-#include "core/strings/dynamic_string.inl"
+#include "core/strings/dynamic_string.h"
+#include "core/strings/string_id.h"
 
 namespace crown
 {
 void DynamicString::from_guid(const Guid& guid)
 {
-	array::resize(_data, 37);
-	guid::to_string(array::begin(_data), 37, guid);
+	array::resize(_data, GUID_BUF_LEN);
+	guid::to_string(array::begin(_data), GUID_BUF_LEN, guid);
 	array::pop_back(_data);
 }
 
 void DynamicString::from_string_id(const StringId32& id)
 {
-	array::resize(_data, 9);
-	id.to_string(array::begin(_data), 9);
+	array::resize(_data, STRING_ID32_BUF_LEN);
+	id.to_string(array::begin(_data), STRING_ID32_BUF_LEN);
 	array::pop_back(_data);
 }
 
 void DynamicString::from_string_id(const StringId64& id)
 {
-	array::resize(_data, 17);
-	id.to_string(array::begin(_data), 17);
+	array::resize(_data, STRING_ID64_BUF_LEN);
+	id.to_string(array::begin(_data), STRING_ID64_BUF_LEN);
 	array::pop_back(_data);
 }
 

+ 9 - 2
src/core/strings/string_id.h

@@ -7,6 +7,9 @@
 
 #include "core/types.h"
 
+#define STRING_ID32_BUF_LEN 9
+#define STRING_ID64_BUF_LEN 17
+
 namespace crown
 {
 /// Hashed string.
@@ -26,7 +29,9 @@ struct StringId32
 	/// Parses the id from @a str.
 	void parse(const char* str);
 
-	/// Fills @a buf with the string representation of this id.
+	/// Returns this string converted to ASCIIZ.
+	/// @a buf size must be greater than or equal to STRING_ID32_BUF_LEN or the
+	/// returned string will be truncated.
 	const char* to_string(char* buf, u32 len) const;
 };
 
@@ -47,7 +52,9 @@ struct StringId64
 	/// Parses the id from @a str.
 	void parse(const char* str);
 
-	/// Fills @a buf with the string representation of this id.
+	/// Returns this string converted to ASCIIZ.
+	/// @a buf size must be greater than or equal to STRING_ID64_BUF_LEN or the
+	/// returned string will be truncated.
 	const char* to_string(char* buf, u32 len) const;
 };
 

+ 2 - 2
src/device/device.cpp

@@ -689,7 +689,7 @@ void Device::reload(StringId64 type, StringId64 name)
 {
 	ResourceId res_id = resource_id(type, name);
 
-	logi(DEVICE, "Reloading: " RESOURCE_ID, res_id._id);
+	logi(DEVICE, "Reloading: " RESOURCE_ID_FMT, res_id._id);
 
 	_resource_manager->reload(type, name);
 	const void* new_resource = _resource_manager->get(type, name);
@@ -699,7 +699,7 @@ void Device::reload(StringId64 type, StringId64 name)
 		_lua_environment->execute((const LuaResource*)new_resource, 0);
 	}
 
-	logi(DEVICE, "Reloaded: " RESOURCE_ID, res_id._id);
+	logi(DEVICE, "Reloaded: " RESOURCE_ID_FMT, res_id._id);
 }
 
 void Device::log(const char* msg)

+ 6 - 6
src/lua/lua_api.cpp

@@ -1418,10 +1418,10 @@ void load_api(LuaEnvironment& env)
 			const Quaternion& rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
 			const Vector3& scl    = nargs > 4 ? stack.get_vector3(5)    : VECTOR3_ONE;
 
-			char name_str[RESOURCE_ID_STRING_LENGTH];
+			char name_str[RESOURCE_ID_BUF_LEN];
 			LUA_ASSERT(device()->_resource_manager->can_get(RESOURCE_TYPE_UNIT, name)
 				, stack
-				, "Unit not loaded: " RESOURCE_ID_STRING
+				, "Unit not loaded: " RESOURCE_ID_FMT_STR
 				, resource_id(RESOURCE_TYPE_UNIT, name).to_string(name_str, sizeof(name_str))
 				);
 			CE_UNUSED(name_str);
@@ -1608,10 +1608,10 @@ void load_api(LuaEnvironment& env)
 			const Vector3& pos    = nargs > 4 ? stack.get_vector3(5) : VECTOR3_ZERO;
 			const f32 range       = nargs > 5 ? stack.get_float(6)   : 1000.0f;
 
-			char name_str[RESOURCE_ID_STRING_LENGTH];
+			char name_str[RESOURCE_ID_BUF_LEN];
 			LUA_ASSERT(device()->_resource_manager->can_get(RESOURCE_TYPE_SOUND, name)
 				, stack
-				, "Sound not loaded: " RESOURCE_ID_STRING
+				, "Sound not loaded: " RESOURCE_ID_FMT_STR
 				, resource_id(RESOURCE_TYPE_SOUND, name).to_string(name_str, sizeof(name_str))
 				);
 			CE_UNUSED(name_str);
@@ -1692,10 +1692,10 @@ void load_api(LuaEnvironment& env)
 			const Vector3& pos    = nargs > 2 ? stack.get_vector3(3)    : VECTOR3_ZERO;
 			const Quaternion& rot = nargs > 3 ? stack.get_quaternion(4) : QUATERNION_IDENTITY;
 
-			char name_str[RESOURCE_ID_STRING_LENGTH];
+			char name_str[RESOURCE_ID_BUF_LEN];
 			LUA_ASSERT(device()->_resource_manager->can_get(RESOURCE_TYPE_LEVEL, name)
 				, stack
-				, "Level not loaded: " RESOURCE_ID_STRING
+				, "Level not loaded: " RESOURCE_ID_FMT_STR
 				, resource_id(RESOURCE_TYPE_LEVEL, name).to_string(name_str, sizeof(name_str))
 				);
 			CE_UNUSED(name_str);

+ 3 - 3
src/resource/resource_id.h

@@ -8,9 +8,9 @@
 #include "core/strings/string_id.h"
 #include <inttypes.h> // PRIx64
 
-#define RESOURCE_ID "#ID(%.16" PRIx64 ")"
-#define RESOURCE_ID_STRING "#ID(%s)"
-#define RESOURCE_ID_STRING_LENGTH 17
+#define RESOURCE_ID_FMT "#ID(%.16" PRIx64 ")"
+#define RESOURCE_ID_FMT_STR "#ID(%s)"
+#define RESOURCE_ID_BUF_LEN 17
 
 namespace crown
 {

+ 2 - 2
src/resource/resource_loader.cpp

@@ -107,7 +107,7 @@ s32 ResourceLoader::run()
 		File* file = _data_filesystem.open(path.c_str(), FileOpenMode::READ);
 		if (!file->is_open())
 		{
-			logw(RESOURCE_LOADER, "Can't load resource: " RESOURCE_ID ". Falling back...", res_id._id);
+			logw(RESOURCE_LOADER, "Can't load resource: " RESOURCE_ID_FMT ". Falling back...", res_id._id);
 
 			StringId64 fallback_name;
 			fallback_name = hash_map::get(_fallback, rr.type, fallback_name);
@@ -119,7 +119,7 @@ s32 ResourceLoader::run()
 			_data_filesystem.close(*file);
 			file = _data_filesystem.open(path.c_str(), FileOpenMode::READ);
 		}
-		CE_ASSERT(file->is_open(), "Can't load resource: " RESOURCE_ID, res_id._id);
+		CE_ASSERT(file->is_open(), "Can't load resource: " RESOURCE_ID_FMT, res_id._id);
 
 		if (rr.load_function)
 		{

+ 1 - 1
src/resource/resource_manager.cpp

@@ -142,7 +142,7 @@ const void* ResourceManager::get(StringId64 type, StringId64 name)
 
 	const ResourceId res_id = resource_id(type, name);
 
-	CE_ASSERT(can_get(type, name), "Resource not loaded: " RESOURCE_ID, res_id._id);
+	CE_ASSERT(can_get(type, name), "Resource not loaded: " RESOURCE_ID_FMT, res_id._id);
 
 	if (_autoload && !hash_map::has(_rm, id))
 	{

+ 0 - 9
src/resource/state_machine_resource.cpp

@@ -111,15 +111,6 @@ namespace state_machine
 } // namespace state_machine
 
 #if CROWN_CAN_COMPILE
-template <>
-struct hash<Guid>
-{
-	u32 operator()(const Guid& id) const
-	{
-		return id.data1;
-	}
-};
-
 namespace state_machine_internal
 {
 	struct TransitionModeInfo