Daniele Bartolini 10 лет назад
Родитель
Сommit
ada3cd24c3

+ 7 - 5
src/compilers/bundle_compiler.cpp

@@ -14,7 +14,6 @@
 #include "disk_filesystem.h"
 #include "compile_options.h"
 #include "resource_registry.h"
-#include "resource_id.h"
 #include "temp_allocator.h"
 
 namespace crown
@@ -30,7 +29,8 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 
 bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum platform)
 {
-	ResourceId id(type, name);
+	StringId64 _type(type);
+	StringId64 _name(name);
 
 	TempAllocator512 alloc;
 	DynamicString path(alloc);
@@ -41,8 +41,10 @@ bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum
 	src_path += ".";
 	src_path += type;
 
-	char res_name[64];
-	id.to_string(res_name);
+	char res_name[1 + 2*StringId64::STRING_LENGTH];
+	_type.to_string(res_name);
+	res_name[16] = '-';
+	_name.to_string(res_name + 17);
 
 	path::join(CROWN_DATA_DIRECTORY, res_name, path);
 
@@ -50,7 +52,7 @@ bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum
 
 	File* outf = _bundle_fs.open(path.c_str(), FOM_WRITE);
 	CompileOptions opts(_source_fs, outf, platform);
-	resource_on_compile(id.type, src_path.c_str(), opts);
+	resource_on_compile(_type, src_path.c_str(), opts);
 	_bundle_fs.close(outf);
 	return true;
 }

+ 2 - 4
src/core/json/json_parser.cpp

@@ -214,13 +214,11 @@ StringId32 JSONElement::to_string_id(const StringId32 def) const
 	return str.to_string_id();
 }
 
-ResourceId JSONElement::to_resource_id(const char* type) const
+ResourceId JSONElement::to_resource_id() const
 {
-	CE_ASSERT_NOT_NULL(type);
-	// TempAllocator1024 alloc;
 	DynamicString str(default_allocator());
 	njson::parse_string(_at, str);
-	return ResourceId(type, str.c_str());
+	return ResourceId(str.c_str());
 }
 
 void JSONElement::to_array(Array<bool>& array) const

+ 1 - 2
src/core/json/json_parser.h

@@ -11,7 +11,6 @@
 #include "matrix4x4.h"
 #include "quaternion.h"
 #include "macros.h"
-#include "resource_id.h"
 
 namespace crown
 {
@@ -125,7 +124,7 @@ public:
 
 	/// Returns the resource id value of the element.
 	/// If @a type is NULL then the string element is assumed to already contain extension.
-	ResourceId to_resource_id(const char* type) const;
+	ResourceId to_resource_id() const;
 
 	/// Returns the array value of the element.
 	/// @note

+ 13 - 0
src/core/string_id.cpp

@@ -6,6 +6,7 @@
 #include "types.h"
 #include "murmur.h"
 #include "string_utils.h"
+#include <inttypes.h>
 
 namespace crown
 {
@@ -20,6 +21,12 @@ StringId32::StringId32(const char* str, uint32_t len)
 {
 }
 
+const char* StringId32::to_string(char* buf)
+{
+	snprintf(buf, STRING_LENGTH, "%.8x", _id);
+	return buf;
+}
+
 StringId64::StringId64(const char* str)
 	: _id(murmur64(str, strlen(str)))
 {
@@ -30,4 +37,10 @@ StringId64::StringId64(const char* str, uint32_t len)
 {
 }
 
+const char* StringId64::to_string(char* buf)
+{
+	snprintf(buf, STRING_LENGTH, "%.16" PRIx64, _id);
+	return buf;
+}
+
 } // namespace crown

+ 8 - 0
src/core/types.h

@@ -26,6 +26,9 @@ struct StringId32
 	bool operator!=(StringId32 a) const { return _id != a._id; }
 	bool operator<(StringId32 a) const { return _id < a._id; }
 	uint32_t id() const { return _id; }
+	const char* to_string(char* buf);
+
+	static const uint32_t STRING_LENGTH = 32;
 };
 
 struct StringId64
@@ -41,8 +44,13 @@ struct StringId64
 	bool operator!=(StringId64 a) const { return _id != a._id; }
 	bool operator<(StringId64 a) const { return _id < a._id; }
 	uint64_t id() const { return _id; }
+	const char* to_string(char* buf);
+
+	static const uint32_t STRING_LENGTH = 32;
 };
 
+typedef StringId64 ResourceId;
+
 #define INVALID_ID 65535
 
 struct Id

+ 2 - 2
src/crown.cpp

@@ -180,8 +180,8 @@ void parse_config_file(Filesystem& fs, ConfigSettings& cs)
 		cs.window_height = max((uint16_t)1, (uint16_t)window_height.to_int());
 	}
 
-	cs.boot_script = root.key("boot_script").to_resource_id("lua").name;
-	cs.boot_package = root.key("boot_package").to_resource_id("package").name;
+	cs.boot_script = root.key("boot_script").to_resource_id();
+	cs.boot_package = root.key("boot_package").to_resource_id();
 }
 
 bool init(Filesystem& fs, const ConfigSettings& cs)

+ 1 - 2
src/renderers/gui.cpp

@@ -93,8 +93,7 @@ Gui::Gui(uint16_t width, uint16_t height, const char* material)
 {
 	set_orthographic(m_projection, 0, width, 0, height, -0.01f, 100.0f);
 
-	ResourceId id("material", material);
-	m_material = material_manager::get()->create_material(id.name);
+	m_material = material_manager::get()->create_material(ResourceId(material));
 }
 
 const GuiId Gui::id() const

+ 2 - 7
src/resource/level_resource.cpp

@@ -112,11 +112,6 @@ template <> inline BinaryReader& operator&(BinaryReader& br, StringId64& id)
 	return br & id._id;
 }
 
-template <> inline BinaryReader& operator&(BinaryReader& br, ResourceId& id)
-{
-	return br & id.type & id.name;
-}
-
 template <> inline BinaryWriter& operator&(BinaryWriter& bw, Vector3& v)
 {
 	return bw & v.x & v.y & v.z;
@@ -196,7 +191,7 @@ namespace level_resource
 			{
 				JSONElement e = sounds_arr[i];
 				LevelSound ls;
-				ls.name = e.key("name").to_resource_id("sound").name;
+				ls.name = e.key("name").to_resource_id();
 				ls.position = e.key("position").to_vector3();
 				ls.volume = e.key("volume").to_float();
 				ls.range = e.key("range").to_float();
@@ -212,7 +207,7 @@ namespace level_resource
 			{
 				JSONElement e = units_arr[i];
 				LevelUnit lu;
-				lu.name = e.key("name").to_resource_id("unit").name;
+				lu.name = e.key("name").to_resource_id();
 				lu.position = e.key("position").to_vector3();
 				lu.rotation = e.key("rotation").to_quaternion();
 				array::push_back(units, lu);

+ 4 - 4
src/resource/material_resource.cpp

@@ -54,11 +54,11 @@ namespace material_resource
 			th.sampler_handle = 0;
 			th.texture_handle = 0;
 
-			ResourceId texid = root.key("textures").key(keys[i].c_str()).to_resource_id("texture");
+			ResourceId texid = root.key("textures").key(keys[i].c_str()).to_resource_id();
 
 			TextureData td;
 			td.sampler_name_offset = array::size(names); array::push(names, keys[i].c_str(), keys[i].length()); array::push_back(names, '\0');
-			td.id = texid.name;
+			td.id = texid;
 			td.data_offset = reserve_dynamic_data(th, dynamic);
 
 			array::push_back(textures, td);
@@ -158,13 +158,13 @@ namespace material_resource
 		Array<char> names(default_allocator());
 		Array<char> dynblob(default_allocator());
 
-		ResourceId shader = root.key("shader").to_resource_id("shader");
+		ResourceId shader = root.key("shader").to_resource_id();
 		parse_textures(root, texdata, names, dynblob);
 		parse_uniforms(root, unidata, names, dynblob);
 
 		MaterialResource mr;
 		mr.version = VERSION;
-		mr.shader = shader.name;
+		mr.shader = shader;
 		mr.num_textures = array::size(texdata);
 		mr.texture_data_offset = sizeof(mr);
 		mr.num_uniforms = array::size(unidata);

+ 13 - 13
src/resource/package_resource.cpp

@@ -107,43 +107,43 @@ namespace package_resource
 
 		// Write resource ids
 		for (uint32_t i = 0; i < num_textures; i++)
-			opts.write(texture[i].to_resource_id("texture").name);
+			opts.write(texture[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_scripts; i++)
-			opts.write(script[i].to_resource_id("lua").name);
+			opts.write(script[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_sounds; i++)
-			opts.write(sound[i].to_resource_id("sound").name);
+			opts.write(sound[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_meshes; i++)
-			opts.write(mesh[i].to_resource_id("mesh").name);
+			opts.write(mesh[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_units; i++)
-			opts.write(unit[i].to_resource_id("unit").name);
+			opts.write(unit[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_sprites; i++)
-			opts.write(sprite[i].to_resource_id("sprite").name);
+			opts.write(sprite[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_physics; i++)
-			opts.write(physics[i].to_resource_id("physics").name);
+			opts.write(physics[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_materials; i++)
-			opts.write(material[i].to_resource_id("material").name);
+			opts.write(material[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_fonts; i++)
-			opts.write(font[i].to_resource_id("font").name);
+			opts.write(font[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_levels; i++)
-			opts.write(level[i].to_resource_id("level").name);
+			opts.write(level[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_phyconfs; i++)
-			opts.write(phyconf[i].to_resource_id("physics_config").name);
+			opts.write(phyconf[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_shaders; i++)
-			opts.write(shader[i].to_resource_id("shader").name);
+			opts.write(shader[i].to_resource_id());
 
 		for (uint32_t i = 0; i < num_sprite_animations; i++)
-			opts.write(sprite_animation[i].to_resource_id("sprite_animation").name);
+			opts.write(sprite_animation[i].to_resource_id());
 	}
 
 	void* load(File& file, Allocator& a)

+ 0 - 1
src/resource/physics_resource.h

@@ -10,7 +10,6 @@
 #include "filesystem_types.h"
 #include "math_types.h"
 #include "compiler_types.h"
-#include "resource_id.h"
 
 namespace crown
 {

+ 0 - 58
src/resource/resource_id.h

@@ -1,58 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "string_utils.h"
-#include "murmur.h"
-#include "path.h"
-#include <inttypes.h>
-
-namespace crown
-{
-
-struct ResourceId
-{
-	ResourceId()
-		: type(uint64_t(0))
-		, name(uint64_t(0))
-	{
-	}
-
-	ResourceId(StringId64 type, StringId64 name)
-		: type(type)
-		, name(name)
-	{
-	}
-
-	ResourceId(const char* type, const char* name)
-		: type(murmur64(type, strlen(type), 0))
-		, name(murmur64(name, strlen(name), SEED))
-	{
-	}
-
-	const char* to_string(char out[64])
-	{
-		snprintf(out, 64, "%.16" PRIx64 "-%.16" PRIx64, type.id(), name.id());
-		return out;
-	}
-
-	bool operator==(const ResourceId& a) const
-	{
-		return type == a.type && name == a.name;
-	}
-
-	bool operator<(const ResourceId& a) const
-	{
-		return type < a.type || (type == a.type && name < a.name);
-	}
-
-	static const uint64_t SEED = 0;
-
-	StringId64 type;
-	StringId64 name;
-};
-
-} // namespace crown

+ 11 - 8
src/resource/resource_loader.cpp

@@ -30,9 +30,9 @@ ResourceLoader::~ResourceLoader()
 	_thread.stop();
 }
 
-void ResourceLoader::load(ResourceId id)
+void ResourceLoader::load(StringId64 type, StringId64 name)
 {
-	add_request(id);
+	add_request(type, name);
 }
 
 void ResourceLoader::flush()
@@ -40,10 +40,10 @@ void ResourceLoader::flush()
 	while (num_requests()) {}
 }
 
-void ResourceLoader::add_request(ResourceId id)
+void ResourceLoader::add_request(StringId64 type, StringId64 name)
 {
 	ScopedMutex sm(_mutex);
-	queue::push_back(_requests, id);
+	queue::push_back(_requests, make_request(type, name));
 }
 
 uint32_t ResourceLoader::num_requests()
@@ -79,14 +79,17 @@ int32_t ResourceLoader::run()
 			_mutex.unlock();
 			continue;
 		}
-		ResourceId id = queue::front(_requests);
+		ResourceRequest id = queue::front(_requests);
 		_mutex.unlock();
 
 		ResourceData rd;
-		rd.id = id;
+		rd.type = id.type;
+		rd.name = id.name;
 
-		char name[64];
-		id.to_string(name);
+		char name[1 + 2*StringId64::STRING_LENGTH];
+		id.type.to_string(name);
+		name[16] = '-';
+		id.name.to_string(name + 17);
 
 		TempAllocator256 alloc;
 		DynamicString path(alloc);

+ 17 - 5
src/resource/resource_loader.h

@@ -11,14 +11,14 @@
 #include "container_types.h"
 #include "mutex.h"
 #include "memory_types.h"
-#include "resource_id.h"
 
 namespace crown
 {
 
 struct ResourceData
 {
-	ResourceId id;
+	StringId64 type;
+	StringId64 name;
 	void* data;
 };
 
@@ -35,7 +35,7 @@ public:
 	~ResourceLoader();
 
 	/// Loads the @a resource in a background thread.
-	void load(ResourceId id);
+	void load(StringId64 type, StringId64 name);
 
 	/// Blocks until all pending requests have been processed.
 	void flush();
@@ -44,7 +44,7 @@ public:
 
 private:
 
-	void add_request(ResourceId id);
+	void add_request(StringId64 type, StringId64 name);
 	uint32_t num_requests();
 	void add_loaded(ResourceData data);
 
@@ -59,11 +59,23 @@ private:
 
 private:
 
+	struct ResourceRequest
+	{
+		StringId64 type;
+		StringId64 name;
+	};
+
+	ResourceRequest make_request(StringId64 type, StringId64 name)
+	{
+		ResourceRequest request = { type, name };
+		return request;
+	}
+
 	Thread _thread;
 	Filesystem& _fs;
 	Allocator& _resource_heap;
 
-	Queue<ResourceId> _requests;
+	Queue<ResourceRequest> _requests;
 	Queue<ResourceData> _loaded;
 	Mutex _mutex;
 	Mutex _loaded_mutex;

+ 16 - 13
src/resource/resource_manager.cpp

@@ -7,6 +7,7 @@
 #include "resource_registry.h"
 #include "temp_allocator.h"
 #include "sort_map.h"
+#include "array.h"
 
 namespace crown
 {
@@ -35,12 +36,12 @@ ResourceManager::~ResourceManager()
 
 void ResourceManager::load(StringId64 type, StringId64 name)
 {
-	ResourceId id(type, name);
+	ResourcePair id = make_pair(type, name);
 	ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
 
 	if (entry == ResourceEntry::NOT_FOUND)
 	{
-		_loader.load(id);
+		_loader.load(type, name);
 		return;
 	}
 
@@ -51,7 +52,7 @@ void ResourceManager::unload(StringId64 type, StringId64 name)
 {
 	flush();
 
-	ResourceId id(type, name);
+	ResourcePair id = make_pair(type, name);
 	ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
 
 	if (--entry.references == 0)
@@ -66,7 +67,7 @@ void ResourceManager::unload(StringId64 type, StringId64 name)
 
 void ResourceManager::reload(StringId64 type, StringId64 name)
 {
-	const ResourceId id(type, name);
+	const ResourcePair id = make_pair(type, name);
 	const ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
 	const uint32_t old_refs = entry.references;
 
@@ -80,16 +81,18 @@ void ResourceManager::reload(StringId64 type, StringId64 name)
 
 bool ResourceManager::can_get(StringId64 type, StringId64 name)
 {
-	return _autoload ? true : sort_map::has(_rm, ResourceId(type, name));
+	return _autoload ? true : sort_map::has(_rm, make_pair(type, name));
 }
 
 const void* ResourceManager::get(StringId64 type, StringId64 name)
 {
-	ResourceId id(type, name);
+	const ResourcePair id = make_pair(type, name);
+	char type_buf[StringId64::STRING_LENGTH];
+	char name_buf[StringId64::STRING_LENGTH];
 
-	char buf[64];
-	CE_ASSERT(can_get(type, name), "Resource not loaded #ID(%s)", id.to_string(buf));
-	CE_UNUSED(buf);
+	CE_ASSERT(can_get(type, name), "Resource not loaded #ID(%s-%s)", type.to_string(type_buf), name.to_string(name_buf));
+	CE_UNUSED(type_buf);
+	CE_UNUSED(name_buf);
 
 	if (_autoload && !sort_map::has(_rm, id))
 	{
@@ -119,19 +122,19 @@ void ResourceManager::complete_requests()
 	_loader.get_loaded(loaded);
 
 	for (uint32_t i = 0; i < array::size(loaded); i++)
-		complete_request(loaded[i].id, loaded[i].data);
+		complete_request(loaded[i].type, loaded[i].name, loaded[i].data);
 }
 
-void ResourceManager::complete_request(ResourceId id, void* data)
+void ResourceManager::complete_request(StringId64 type, StringId64 name, void* data)
 {
 	ResourceEntry entry;
 	entry.references = 1;
 	entry.data = data;
 
-	sort_map::set(_rm, id, entry);
+	sort_map::set(_rm, make_pair(type, name), entry);
 	sort_map::sort(_rm);
 
-	resource_on_online(id.type, id.name, *this);
+	resource_on_online(type, name, *this);
 }
 
 } // namespace crown

+ 19 - 3
src/resource/resource_manager.h

@@ -54,10 +54,27 @@ public:
 
 private:
 
-	void complete_request(ResourceId id, void* data);
+	void complete_request(StringId64 type, StringId64 name, void* data);
 
 private:
 
+	struct ResourcePair
+	{
+		StringId64 type;
+		StringId64 name;
+
+		bool operator<(const ResourcePair& a) const
+		{
+			return type < a.type || (type == a.type && name < a.name);
+		}
+	};
+
+	ResourcePair make_pair(StringId64 type, StringId64 name)
+	{
+		ResourcePair pair = { type, name };
+		return pair;
+	}
+
 	struct ResourceEntry
 	{
 		bool operator==(const ResourceEntry& e)
@@ -71,13 +88,12 @@ private:
 		static const ResourceEntry NOT_FOUND;
 	};
 
-	typedef SortMap<ResourceId, ResourceEntry> ResourceMap;
+	typedef SortMap<ResourcePair, ResourceEntry> ResourceMap;
 
 	ProxyAllocator _resource_heap;
 	ResourceLoader _loader;
 	ResourceMap _rm;
 	bool _autoload;
-
 };
 
 } // namespace crown

+ 5 - 4
src/resource/resource_registry.cpp

@@ -4,6 +4,7 @@
  */
 
 #include "resource_registry.h"
+#include "error.h"
 #include "lua_resource.h"
 #include "texture_resource.h"
 #include "mesh_resource.h"
@@ -100,14 +101,14 @@ void resource_on_unload(StringId64 type, Allocator& allocator, void* resource)
 	return find_callback(type)->on_unload(allocator, resource);
 }
 
-void resource_on_online(StringId64 type, StringId64 id, ResourceManager& rm)
+void resource_on_online(StringId64 type, StringId64 name, ResourceManager& rm)
 {
-	return find_callback(type)->on_online(id, rm);
+	return find_callback(type)->on_online(name, rm);
 }
 
-void resource_on_offline(StringId64 type, StringId64 id, ResourceManager& rm)
+void resource_on_offline(StringId64 type, StringId64 name, ResourceManager& rm)
 {
-	return find_callback(type)->on_offline(id, rm);
+	return find_callback(type)->on_offline(name, rm);
 }
 
 } // namespace crown

+ 2 - 2
src/resource/resource_registry.h

@@ -16,8 +16,8 @@ namespace crown
 
 void resource_on_compile(StringId64 type, const char* path, CompileOptions& opts);
 void* resource_on_load(StringId64 type, File& file, Allocator& a);
-void resource_on_online(StringId64 type, StringId64 id, ResourceManager& rm);
-void resource_on_offline(StringId64 type, StringId64 id, ResourceManager& rm);
+void resource_on_online(StringId64 type, StringId64 name, ResourceManager& rm);
+void resource_on_offline(StringId64 type, StringId64 name, ResourceManager& rm);
 void resource_on_unload(StringId64 type, Allocator& allocator, void* resource);
 
 } // namespace crown

+ 8 - 8
src/resource/unit_resource.cpp

@@ -210,12 +210,12 @@ namespace unit_resource
 			if (res_type == "mesh")
 			{
 				rn.type = UnitRenderable::MESH;
-				rn.resource = renderable.key("resource").to_resource_id("mesh").name;
+				rn.resource = renderable.key("resource").to_resource_id();
 			}
 			else if (res_type == "sprite")
 			{
 				rn.type = UnitRenderable::SPRITE;
-				rn.resource = renderable.key("resource").to_resource_id("sprite").name;
+				rn.resource = renderable.key("resource").to_resource_id();
 			}
 			else
 			{
@@ -230,9 +230,9 @@ namespace unit_resource
 	{
 		for (uint32_t i = 0; i < e.size(); i++)
 		{
-			ResourceId mat_id = e[i].to_resource_id("material");
+			ResourceId mat_id = e[i].to_resource_id();
 			UnitMaterial um;
-			um.id = mat_id.name;
+			um.id = mat_id;
 			array::push_back(materials, um);
 		}
 	}
@@ -273,7 +273,7 @@ namespace unit_resource
 		physics_name += ".physics";
 		if (opts._fs.exists(physics_name.c_str()))
 		{
-			m_physics_resource = ResourceId("physics", unit_name.c_str());
+			m_physics_resource = ResourceId(unit_name.c_str());
 		}
 		else
 		{
@@ -282,13 +282,13 @@ namespace unit_resource
 
 		ResourceId sprite_anim;
 		if (root.has_key("sprite_animation"))
-			sprite_anim = root.key("sprite_animation").to_resource_id("sprite_animation");
+			sprite_anim = root.key("sprite_animation").to_resource_id();
 
 		UnitResource ur;
 		ur.version = VERSION;
 		ur.name = StringId64(unit_name.c_str());
-		ur.physics_resource = m_physics_resource.name;
-		ur.sprite_animation = sprite_anim.name;
+		ur.physics_resource = m_physics_resource;
+		ur.sprite_animation = sprite_anim;
 		ur.num_renderables = array::size(m_renderables);
 		ur.num_materials = array::size(m_materials);
 		ur.num_cameras = array::size(m_cameras);