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

Move common loading code to ResourceLoader

Daniele Bartolini 8 лет назад
Родитель
Сommit
ac3483c691

+ 9 - 9
src/device/device.cpp

@@ -359,20 +359,20 @@ void Device::run()
 	_resource_loader  = CE_NEW(_allocator, ResourceLoader)(*_data_filesystem);
 	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
 	_resource_manager->register_type(RESOURCE_TYPE_CONFIG,           RESOURCE_VERSION_CONFIG,           cor::load, cor::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_FONT,             RESOURCE_VERSION_FONT,             ftr::load, ftr::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_LEVEL,            RESOURCE_VERSION_LEVEL,            lvr::load, lvr::unload, NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_FONT,             RESOURCE_VERSION_FONT,             NULL,      NULL,        NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_LEVEL,            RESOURCE_VERSION_LEVEL,            NULL,      NULL,        NULL,        NULL        );
 	_resource_manager->register_type(RESOURCE_TYPE_MATERIAL,         RESOURCE_VERSION_MATERIAL,         mtr::load, mtr::unload, mtr::online, mtr::offline);
 	_resource_manager->register_type(RESOURCE_TYPE_MESH,             RESOURCE_VERSION_MESH,             mhr::load, mhr::unload, mhr::online, mhr::offline);
 	_resource_manager->register_type(RESOURCE_TYPE_PACKAGE,          RESOURCE_VERSION_PACKAGE,          pkr::load, pkr::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_PHYSICS,          RESOURCE_VERSION_PHYSICS,          phr::load, phr::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_PHYSICS_CONFIG,   RESOURCE_VERSION_PHYSICS_CONFIG,   pcr::load, pcr::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_SCRIPT,           RESOURCE_VERSION_SCRIPT,           lur::load, lur::unload, NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_PHYSICS,          RESOURCE_VERSION_PHYSICS,          NULL,      NULL,        NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_PHYSICS_CONFIG,   RESOURCE_VERSION_PHYSICS_CONFIG,   NULL,      NULL,        NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_SCRIPT,           RESOURCE_VERSION_SCRIPT,           NULL,      NULL,        NULL,        NULL        );
 	_resource_manager->register_type(RESOURCE_TYPE_SHADER,           RESOURCE_VERSION_SHADER,           shr::load, shr::unload, shr::online, shr::offline);
-	_resource_manager->register_type(RESOURCE_TYPE_SOUND,            RESOURCE_VERSION_SOUND,            sdr::load, sdr::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_SPRITE,           RESOURCE_VERSION_SPRITE,           spr::load, spr::unload, NULL,        NULL        );
-	_resource_manager->register_type(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, sar::load, sar::unload, NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_SOUND,            RESOURCE_VERSION_SOUND,            NULL,      NULL,        NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_SPRITE,           RESOURCE_VERSION_SPRITE,           NULL,      NULL,        NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_SPRITE_ANIMATION, RESOURCE_VERSION_SPRITE_ANIMATION, NULL,      NULL,        NULL,        NULL        );
 	_resource_manager->register_type(RESOURCE_TYPE_TEXTURE,          RESOURCE_VERSION_TEXTURE,          txr::load, txr::unload, txr::online, txr::offline);
-	_resource_manager->register_type(RESOURCE_TYPE_UNIT,             RESOURCE_VERSION_UNIT,             utr::load, utr::unload, NULL,        NULL        );
+	_resource_manager->register_type(RESOURCE_TYPE_UNIT,             RESOURCE_VERSION_UNIT,             NULL,      NULL,        NULL,        NULL        );
 
 	// Read config
 	{

+ 0 - 14
src/resource/font_resource.cpp

@@ -93,20 +93,6 @@ namespace font_resource_internal
 			opts.write(_glyphs[i].gd.x_advance);
 		}
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 size = file.size();
-		void* res = a.allocate(size);
-		file.read(res, size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_FONT, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& allocator, void* resource)
-	{
-		allocator.deallocate(resource);
-	}
 } // namespace font_resource_internal
 
 namespace font_resource

+ 0 - 2
src/resource/font_resource.h

@@ -36,8 +36,6 @@ typedef u32 CodePoint;
 namespace font_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& allocator, void* resource);
 } // namespace font_resource_internal
 
 namespace font_resource

+ 0 - 14
src/resource/level_resource.cpp

@@ -87,20 +87,6 @@ namespace level_resource_internal
 			opts.write(sounds[i]._pad[2]);
 		}
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 file_size = file.size();
-		void* res = a.allocate(file_size);
-		file.read(res, file_size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_LEVEL, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& allocator, void* resource)
-	{
-		allocator.deallocate(resource);
-	}
 } // namespace level_resource_internal
 
 namespace level_resource

+ 0 - 2
src/resource/level_resource.h

@@ -35,8 +35,6 @@ struct LevelSound
 namespace level_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& allocator, void* resource);
 } // namespace level_resource_internal
 
 namespace level_resource

+ 0 - 14
src/resource/lua_resource.cpp

@@ -67,20 +67,6 @@ namespace lua_resource_internal
 		opts.write(lr.size);
 		opts.write(blob);
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 file_size = file.size();
-		void* res = a.allocate(file_size);
-		file.read(res, file_size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_SCRIPT, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& allocator, void* resource)
-	{
-		allocator.deallocate(resource);
-	}
 } // namespace lua_resource_internal
 
 namespace lua_resource

+ 0 - 2
src/resource/lua_resource.h

@@ -22,8 +22,6 @@ struct LuaResource
 namespace lua_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& allocator, void* resource);
 } // namespace lua_resource_internal
 
 namespace lua_resource

+ 1 - 15
src/resource/physics_resource.cpp

@@ -587,21 +587,7 @@ namespace physics_config_resource_internal
 			opts.write(cfc._filters[i].mask);
 		}
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 file_size = file.size();
-		void* res = a.allocate(file_size);
-		file.read(res, file_size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_PHYSICS_CONFIG, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& allocator, void* resource)
-	{
-		allocator.deallocate(resource);
-	}
-} // namespcae physics_config_resource_internal
+} // namespace physics_config_resource_internal
 
 namespace physics_config_resource
 {

+ 0 - 4
src/resource/physics_resource.h

@@ -18,8 +18,6 @@ namespace crown
 namespace physics_resource_internal
 {
 	inline void compile(const char* /*path*/, CompileOptions& /*opts*/) {}
-	inline void* load(File& /*file*/, Allocator& /*a*/) { return NULL; }
-	inline void unload(Allocator& /*a*/, void* /*res*/) {}
 	Buffer compile_controller(const char* json, CompileOptions& opts);
 	Buffer compile_collider(const char* json, CompileOptions& opts);
 	Buffer compile_actor(const char* json, CompileOptions& opts);
@@ -79,8 +77,6 @@ struct PhysicsConfigActor
 namespace physics_config_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& allocator, void* resource);
 } // namespace physics_config_resource_internal
 
 namespace physics_config_resource

+ 15 - 1
src/resource/resource_loader.cpp

@@ -5,6 +5,7 @@
 
 #include "config.h"
 #include "dynamic_string.h"
+#include "file.h"
 #include "filesystem.h"
 #include "memory.h"
 #include "os.h"
@@ -116,7 +117,20 @@ s32 ResourceLoader::run()
 		path::join(path, CROWN_DATA_DIRECTORY, res_path.c_str());
 
 		File* file = _data_filesystem.open(path.c_str(), FileOpenMode::READ);
-		rr.data = rr.load_function(*file, *rr.allocator);
+
+		if (rr.load_function)
+		{
+			rr.data = rr.load_function(*file, *rr.allocator);
+		}
+		else
+		{
+			const u32 size = file->size();
+			void* data = rr.allocator->allocate(size);
+			file->read(data, size);
+			CE_ASSERT(*(u32*)data == rr.version, "Wrong version");
+			rr.data = data;
+		}
+
 		_data_filesystem.close(*file);
 
 		add_loaded(rr);

+ 6 - 4
src/resource/resource_manager.cpp

@@ -181,9 +181,6 @@ void ResourceManager::complete_request(StringId64 type, StringId64 name, void* d
 
 void ResourceManager::register_type(StringId64 type, u32 version, LoadFunction load, UnloadFunction unload, OnlineFunction online, OfflineFunction offline)
 {
-	CE_ENSURE(NULL != load);
-	CE_ENSURE(NULL != unload);
-
 	ResourceTypeData rtd;
 	rtd.version = version;
 	rtd.load = load;
@@ -213,7 +210,12 @@ void ResourceManager::on_offline(StringId64 type, StringId64 name)
 
 void ResourceManager::on_unload(StringId64 type, void* data)
 {
-	sort_map::get(_type_data, type, ResourceTypeData()).unload(_resource_heap, data);
+	UnloadFunction func = sort_map::get(_type_data, type, ResourceTypeData()).unload;
+
+	if (func)
+		func(_resource_heap, data);
+	else
+		_resource_heap.deallocate(data);
 }
 
 } // namespace crown

+ 0 - 14
src/resource/sound_resource.cpp

@@ -70,20 +70,6 @@ namespace sound_resource_internal
 
 		opts.write(wavdata, wav->data_size);
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 file_size = file.size();
-		void* res = a.allocate(file_size);
-		file.read(res, file_size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_SOUND, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& allocator, void* resource)
-	{
-		allocator.deallocate(resource);
-	}
 } // namespace sound_resource_internal
 
 namespace sound_resource

+ 0 - 2
src/resource/sound_resource.h

@@ -38,8 +38,6 @@ struct SoundResource
 namespace sound_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& allocator, void* resource);
 } // namespace	sound_resource_internal
 
 namespace sound_resource

+ 0 - 28
src/resource/sprite_resource.cpp

@@ -132,20 +132,6 @@ namespace sprite_resource_internal
 		for (u32 i = 0; i < array::size(vertices); i++)
 			opts.write(vertices[i]);
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 file_size = file.size();
-		void* res = a.allocate(file_size);
-		file.read(res, file_size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_SPRITE, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& a, void* resource)
-	{
-		a.deallocate(resource);
-	}
 } // namespace sprite_resource_internal
 
 namespace sprite_resource
@@ -191,20 +177,6 @@ namespace sprite_animation_resource_internal
 		for (u32 i = 0; i < array::size(frames); i++)
 			opts.write(frames[i]);
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 file_size = file.size();
-		void* res = a.allocate(file_size);
-		file.read(res, file_size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_SPRITE_ANIMATION, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& a, void* resource)
-	{
-		a.deallocate(resource);
-	}
 } // namespace sprite_animation_resource_internal
 
 namespace sprite_animation_resource

+ 0 - 6
src/resource/sprite_resource.h

@@ -26,8 +26,6 @@ struct SpriteResource
 namespace sprite_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& a, void* resource);
 } // namespace sprite_resource_internal
 
 namespace sprite_resource
@@ -46,10 +44,6 @@ struct SpriteAnimationResource
 namespace sprite_animation_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void online(StringId64 id, ResourceManager& rm);
-	void offline(StringId64 id, ResourceManager& rm);
-	void unload(Allocator& a, void* resource);
 } // namespace sprite_animation_resource_internal
 
 namespace sprite_animation_resource

+ 0 - 15
src/resource/unit_resource.cpp

@@ -24,21 +24,6 @@ namespace unit_resource_internal
 
 		opts.write(uc.blob());
 	}
-
-	void* load(File& file, Allocator& a)
-	{
-		const u32 size = file.size();
-		void* res = a.allocate(size);
-		file.read(res, size);
-		CE_ASSERT(*(u32*)res == RESOURCE_VERSION_UNIT, "Wrong version");
-		return res;
-	}
-
-	void unload(Allocator& a, void* resource)
-	{
-		a.deallocate(resource);
-	}
-
 } // namespace unit_resource_internal
 
 } // namespace crown

+ 0 - 2
src/resource/unit_resource.h

@@ -31,8 +31,6 @@ struct ComponentData
 namespace unit_resource_internal
 {
 	void compile(const char* path, CompileOptions& opts);
-	void* load(File& file, Allocator& a);
-	void unload(Allocator& allocator, void* resource);
 } // namespace unit_resource_internal
 
 } // namespace crown