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

Treat .config like regular resource

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

+ 24 - 35
src/compilers/bundle_compiler.cpp

@@ -27,6 +27,7 @@
 #include "font_resource.h"
 #include "level_resource.h"
 #include "shader.h"
+#include "config_resource.h"
 #include <setjmp.h>
 
 namespace crown
@@ -36,6 +37,7 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 	: _source_fs(source_dir)
 	, _bundle_fs(bundle_dir)
 	, _compilers(default_allocator())
+	, _files(default_allocator())
 {
 	namespace pcr = physics_config_resource;
 	namespace phr = physics_resource;
@@ -51,6 +53,7 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 	namespace spr = sprite_resource;
 	namespace shr = shader_resource;
 	namespace sar = sprite_animation_resource;
+	namespace cor = config_resource;
 
 	register_resource_compiler(SCRIPT_TYPE,           SCRIPT_VERSION,           lur::compile);
 	register_resource_compiler(TEXTURE_TYPE,          TEXTURE_VERSION,          txr::compile);
@@ -66,9 +69,15 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 	register_resource_compiler(LEVEL_TYPE,            LEVEL_VERSION,            lvr::compile);
 	register_resource_compiler(SHADER_TYPE,           SHADER_VERSION,           shr::compile);
 	register_resource_compiler(SPRITE_ANIMATION_TYPE, SPRITE_ANIMATION_VERSION, sar::compile);
+	register_resource_compiler(CONFIG_TYPE,           CONFIG_VERSION,           cor::compile);
 
 	DiskFilesystem temp;
 	temp.create_directory(bundle_dir);
+
+	scan_source_dir("");
+
+	if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
+		_bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
 }
 
 bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
@@ -120,38 +129,18 @@ bool BundleCompiler::compile(const char* type, const char* name, const char* pla
 
 bool BundleCompiler::compile_all(const char* platform)
 {
-	Vector<DynamicString> files(default_allocator());
-	BundleCompiler::scan("", files);
-
-	if (!_source_fs.is_file("crown.config"))
+	for (uint32_t i = 0; i < vector::size(_files); ++i)
 	{
-		CE_LOGD("'crown.config' does not exist.");
-		return false;
-	}
-
-	File* src = _source_fs.open("crown.config", FOM_READ);
-	File* dst = _bundle_fs.open("crown.config", FOM_WRITE);
-	src->copy_to(*dst, src->size());
-	_source_fs.close(src);
-	_bundle_fs.close(dst);
-
-	if (!_bundle_fs.exists(CROWN_DATA_DIRECTORY))
-		_bundle_fs.create_directory(CROWN_DATA_DIRECTORY);
-
-	// Compile all resources
-	for (uint32_t i = 0; i < vector::size(files); i++)
-	{
-		if (files[i].ends_with(".tga")
-			|| files[i].ends_with(".dds")
-			|| files[i].ends_with(".sh")
-			|| files[i].ends_with(".sc")
-			|| files[i].starts_with(".")
-			|| files[i].ends_with(".config")
-			|| files[i].ends_with(".tmp")
-			|| files[i].ends_with(".wav"))
+		if (_files[i].ends_with(".tga")
+			|| _files[i].ends_with(".dds")
+			|| _files[i].ends_with(".sh")
+			|| _files[i].ends_with(".sc")
+			|| _files[i].starts_with(".")
+			|| _files[i].ends_with(".tmp")
+			|| _files[i].ends_with(".wav"))
 		continue;
 
-		const char* filename = files[i].c_str();
+		const char* filename = _files[i].c_str();
 		const char* type = path::extension(filename);
 
 		char name[256];
@@ -193,15 +182,15 @@ uint32_t BundleCompiler::version(StringId64 type)
 	return sort_map::get(_compilers, type, ResourceTypeData()).version;
 }
 
-void BundleCompiler::scan(const char* cur_dir, Vector<DynamicString>& files)
+void BundleCompiler::scan_source_dir(const char* cur_dir)
 {
 	Vector<DynamicString> my_files(default_allocator());
-
 	_source_fs.list_files(cur_dir, my_files);
 
-	for (uint32_t i = 0; i < vector::size(my_files); i++)
+	for (uint32_t i = 0; i < vector::size(my_files); ++i)
 	{
-		DynamicString file_i(default_allocator());
+		TempAllocator512 ta;
+		DynamicString file_i(ta);
 
 		if (strcmp(cur_dir, "") != 0)
 		{
@@ -212,11 +201,11 @@ void BundleCompiler::scan(const char* cur_dir, Vector<DynamicString>& files)
 
 		if (_source_fs.is_directory(file_i.c_str()))
 		{
-			BundleCompiler::scan(file_i.c_str(), files);
+			BundleCompiler::scan_source_dir(file_i.c_str());
 		}
 		else // Assume a regular file
 		{
-			vector::push_back(files, file_i);
+			vector::push_back(_files, file_i);
 		}
 	}
 }

+ 3 - 1
src/compilers/bundle_compiler.h

@@ -24,7 +24,8 @@ public:
 	/// Returns true on success, false otherwise.
 	bool compile_all(const char* platform);
 
-	void scan(const char* cur_dir, Vector<DynamicString>& files);
+	/// Scans the source directory for resources.
+	void scan_source_dir(const char* path);
 
 private:
 
@@ -48,6 +49,7 @@ private:
 	};
 
 	SortMap<StringId64, ResourceTypeData> _compilers;
+	Vector<DynamicString> _files;
 };
 
 namespace bundle_compiler

+ 16 - 10
src/device.cpp

@@ -79,9 +79,13 @@ void Device::init()
 	_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(_device_options.bundle_dir());
 #endif // CROWN_PLATFORM_ANDROID
 
+	profiler_globals::init();
+
+	_resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
+	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
+
 	read_config();
 
-	profiler_globals::init();
 	audio_globals::init();
 	physics_globals::init();
 
@@ -92,9 +96,6 @@ void Device::init()
 		, &_bgfx_allocator
 		);
 
-	_resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
-	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
-
 	CE_LOGD("Creating material manager...");
 	material_manager::init();
 	debug_line::init();
@@ -423,18 +424,23 @@ void Device::read_config()
 	if (_device_options.project() != NULL)
 	{
 		project_path += _device_options.project();
-		project_path += path::SEPARATOR;
+		project_path += '/';
 	}
 
-	project_path += "crown.config";
+	project_path += "crown";
 
-	File* tmpfile = _bundle_filesystem->open(project_path.c_str(), FOM_READ);
-	JSONParser config(*tmpfile);
-	_bundle_filesystem->close(tmpfile);
-	JSONElement root = config.root();
+	const StringId64 config_name(project_path.c_str());
 
+	_resource_manager->load(CONFIG_TYPE, config_name);
+	_resource_manager->flush();
+	const char* config_file = (const char*)_resource_manager->get(CONFIG_TYPE, config_name);
+
+	JSONParser config(config_file);
+	JSONElement root = config.root();
 	_boot_script_id = root.key("boot_script").to_resource_id();
 	_boot_package_id = root.key("boot_package").to_resource_id();
+
+	_resource_manager->unload(CONFIG_TYPE, config_name);
 }
 
 char _buffer[sizeof(Device)];

+ 36 - 0
src/resource/config_resource.cpp

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "config_resource.h"
+#include "allocator.h"
+#include "compile_options.h"
+
+namespace crown
+{
+
+namespace config_resource
+{
+	void compile(const char* path, CompileOptions& opts)
+	{
+		Buffer config = opts.read(path);
+		array::push_back(config, '\0');
+		opts.write(config);
+	}
+
+	void* load(File& file, Allocator& a)
+	{
+		const uint32_t file_size = file.size();
+		void* res = a.allocate(file_size);
+		file.read(res, file_size);
+		return res;
+	}
+
+	void unload(Allocator& allocator, void* resource)
+	{
+		allocator.deallocate(resource);
+	}
+} // namespace config_resource
+
+} // namespace crown

+ 23 - 0
src/resource/config_resource.h

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "types.h"
+#include "memory_types.h"
+#include "filesystem_types.h"
+#include "compiler_types.h"
+
+namespace crown
+{
+
+namespace config_resource
+{
+	void compile(const char* path, CompileOptions& opts);
+	void* load(File& file, Allocator& a);
+	void unload(Allocator& allocator, void* resource);
+} // namespace config_resource
+
+} // namespace crown

+ 3 - 0
src/resource/resource_manager.cpp

@@ -20,6 +20,7 @@
 #include "font_resource.h"
 #include "level_resource.h"
 #include "shader.h"
+#include "config_resource.h"
 
 namespace crown
 {
@@ -47,6 +48,7 @@ ResourceManager::ResourceManager(ResourceLoader& rl)
 	namespace spr = sprite_resource;
 	namespace shr = shader_resource;
 	namespace sar = sprite_animation_resource;
+	namespace cor = config_resource;
 
 	register_resource_type(SCRIPT_TYPE,           lur::load, NULL,        NULL,         lur::unload);
 	register_resource_type(TEXTURE_TYPE,          txr::load, txr::online, txr::offline, txr::unload);
@@ -62,6 +64,7 @@ ResourceManager::ResourceManager(ResourceLoader& rl)
 	register_resource_type(LEVEL_TYPE,            lvr::load, NULL,        NULL,         lvr::unload);
 	register_resource_type(SHADER_TYPE,           shr::load, shr::online, shr::offline, shr::unload);
 	register_resource_type(SPRITE_ANIMATION_TYPE, sar::load, NULL,        NULL,         sar::unload);
+	register_resource_type(CONFIG_TYPE,           cor::load, NULL,        NULL,         cor::unload);
 }
 
 ResourceManager::~ResourceManager()

+ 3 - 0
src/resource/resource_types.h

@@ -19,6 +19,7 @@
 #define SPRITE_EXTENSION           "sprite"
 #define TEXTURE_EXTENSION          "texture"
 #define UNIT_EXTENSION             "unit"
+#define CONFIG_EXTENSION           "config"
 
 #define FONT_TYPE                  StringId64(0x9efe0a916aae7880)
 #define LEVEL_TYPE                 StringId64(0x2a690fd348fe9ac5)
@@ -34,6 +35,7 @@
 #define SPRITE_TYPE                StringId64(0x8d5871f9ebdb651c)
 #define TEXTURE_TYPE               StringId64(0xcd4238c6a0c69e32)
 #define UNIT_TYPE                  StringId64(0xe0a48d0be9a7453f)
+#define CONFIG_TYPE                StringId64(0x82645835e6b73232)
 
 #define FONT_VERSION               uint32_t(1)
 #define LEVEL_VERSION              uint32_t(1)
@@ -49,6 +51,7 @@
 #define SPRITE_VERSION             uint32_t(1)
 #define TEXTURE_VERSION            uint32_t(1)
 #define UNIT_VERSION               uint32_t(1)
+#define CONFIG_VERSION             uint32_t(1)
 
 namespace crown
 {