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

Merge branch 'master' of github.com:taylor001/crown

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

+ 30 - 14
src/compilers/bundle_compiler.cpp

@@ -27,6 +27,7 @@
 #include "font_resource.h"
 #include "level_resource.h"
 #include "shader.h"
+#include <setjmp.h>
 
 namespace crown
 {
@@ -93,13 +94,28 @@ bool BundleCompiler::compile(const char* type, const char* name, const char* pla
 
 	CE_LOGI("%s <= %s.%s", res_name, name, type);
 
-	File* outf = _bundle_fs.open(path.c_str(), FOM_WRITE);
+	bool success = true;
+	jmp_buf buf;
 
-	CompileOptions opts(_source_fs, outf, platform);
-	compile(_type, src_path.c_str(), opts);
+	Buffer output(default_allocator());
+	array::reserve(output, 4*1024*1024);
 
-	_bundle_fs.close(outf);
-	return true;
+	if (!setjmp(buf))
+	{
+		CompileOptions opts(_source_fs, output, platform, &buf);
+		compile(_type, src_path.c_str(), opts);
+		File* outf = _bundle_fs.open(path.c_str(), FOM_WRITE);
+		uint32_t size = array::size(output);
+		uint32_t written = outf->write(array::begin(output), size);
+		_bundle_fs.close(outf);
+		success = size == written;
+	}
+	else
+	{
+		success = false;
+	}
+
+	return success;
 }
 
 bool BundleCompiler::compile_all(const char* platform)
@@ -119,8 +135,8 @@ bool BundleCompiler::compile_all(const char* platform)
 	_source_fs.close(src);
 	_bundle_fs.close(dst);
 
-	if (!_bundle_fs.exists("data"))
-		_bundle_fs.create_directory("data");
+	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++)
@@ -143,7 +159,8 @@ bool BundleCompiler::compile_all(const char* platform)
 		strncpy(name, filename, size);
 		name[size] = '\0';
 
-		compile(type, name, platform);
+		if (!compile(type, name, platform))
+			return false;
 	}
 
 	return true;
@@ -194,16 +211,15 @@ namespace bundle_compiler
 {
 	bool main(bool do_compile, bool do_continue, const char* platform)
 	{
+		bool can_proceed = true;
+
 		if (do_compile)
 		{
-			bool ok = bundle_compiler_globals::compiler()->compile_all(platform);
-			if (!ok || !do_continue)
-			{
-				return false;
-			}
+			bool success = bundle_compiler_globals::compiler()->compile_all(platform);
+			can_proceed = !(!success || !do_continue);
 		}
 
-		return true;
+		return can_proceed;
 	}
 } // namespace bundle_compiler
 

+ 30 - 15
src/compilers/compile_options.h

@@ -6,18 +6,37 @@
 #pragma once
 
 #include "filesystem.h"
-#include "reader_writer.h"
+#include "log.h"
+#include <setjmp.h>
+
+#define RESOURCE_COMPILER_ASSERT(condition, opts, msg, ...) do { if (!(condition))\
+	{ opts.error(msg, ##__VA_ARGS__); } } while(0)
 
 namespace crown
 {
 
 struct CompileOptions
 {
-	CompileOptions(Filesystem& fs, File* out, const char* platform)
+	CompileOptions(Filesystem& fs, Buffer& output, const char* platform, jmp_buf* buf)
 		: _fs(fs)
-		, _bw(*out)
+		, _output(output)
 		, _platform(platform)
+		, _jmpbuf(buf)
+	{
+	}
+
+	void error(const char* msg, va_list args)
 	{
+		CE_LOGEV(msg, args);
+		longjmp(*_jmpbuf, 1);
+	}
+
+	void error(const char* msg, ...)
+	{
+		va_list args;
+		va_start(args, msg);
+		error(msg, args);
+		va_end(args);
 	}
 
 	Buffer read(const char* path)
@@ -41,25 +60,20 @@ struct CompileOptions
 		_fs.delete_file(path);
 	}
 
-	BinaryWriter& write(const void* data, uint32_t size)
+	void write(const void* data, uint32_t size)
 	{
-		_bw.write(data, size);
-		return _bw;
+		array::push(_output, (const char*)data, size);
 	}
 
 	template <typename T>
-	BinaryWriter& write(const T& data)
+	void write(const T& data)
 	{
-		_bw.write(data);
-		return _bw;
+		write(&data, sizeof(data));
 	}
 
-	BinaryWriter& write(const Buffer& data)
+	void write(const Buffer& data)
 	{
-		if (array::size(data))
-			_bw.write(array::begin(data), array::size(data));
-
-		return _bw;
+		array::push(_output, array::begin(data), array::size(data));
 	}
 
 	const char* platform() const
@@ -68,8 +82,9 @@ struct CompileOptions
 	}
 
 	Filesystem& _fs;
-	BinaryWriter _bw;
+	Buffer& _output;
 	const char* _platform;
+	jmp_buf* _jmpbuf;
 };
 
 } // namespace crown

+ 4 - 5
src/core/filesystem/apk_file.cpp

@@ -54,17 +54,16 @@ void ApkFile::skip(uint32_t bytes)
 	CE_UNUSED(seek_result);
 }
 
-void ApkFile::read(void* buffer, uint32_t size)
+uint32_t ApkFile::read(void* buffer, uint32_t size)
 {
 	CE_ASSERT_NOT_NULL(buffer);
-	uint32_t bytes_read = (uint32_t)AAsset_read(_asset, buffer, size);
-	CE_ASSERT(bytes_read == size, "AAsset_read: requested: %lu, read: %lu", size, bytes_read);
-	CE_UNUSED(bytes_read);
+	return (uint32_t)AAsset_read(_asset, buffer, size);
 }
 
-void ApkFile::write(const void* /*buffer*/, uint32_t /*size*/)
+uint32_t ApkFile::write(const void* /*buffer*/, uint32_t /*size*/)
 {
 	CE_ASSERT(false, "Apk files are read only!");
+	return 0;
 }
 
 bool ApkFile::copy_to(File& /*file*/, uint32_t /*size = 0*/)

+ 2 - 2
src/core/filesystem/apk_file.h

@@ -30,10 +30,10 @@ public:
 	void skip(uint32_t bytes);
 
 	/// @copydoc File::read()
-	void read(void* buffer, uint32_t size);
+	uint32_t read(void* buffer, uint32_t size);
 
 	/// @copydoc File::write()
-	void write(const void* buffer, uint32_t size);
+	uint32_t write(const void* buffer, uint32_t size);
 
 	/// @copydoc File::copy_to()
 	bool copy_to(File& file, uint32_t size = 0);

+ 4 - 6
src/core/filesystem/disk_file.cpp

@@ -44,7 +44,7 @@ void DiskFile::skip(uint32_t bytes)
 	_file.skip(bytes);
 }
 
-void DiskFile::read(void* buffer, uint32_t size)
+uint32_t DiskFile::read(void* buffer, uint32_t size)
 {
 	check_valid();
 
@@ -54,11 +54,10 @@ void DiskFile::read(void* buffer, uint32_t size)
 		_file.seek(0);
 	}
 
-	/*uint32_t bytes_read =*/ _file.read(buffer, size);
-	//CE_ASSERT(bytes_read == size, "Failed to read from file: requested: %llu, read: %llu", size, bytes_read);
+	return _file.read(buffer, size);
 }
 
-void DiskFile::write(const void* buffer, uint32_t size)
+uint32_t DiskFile::write(const void* buffer, uint32_t size)
 {
 	check_valid();
 
@@ -68,8 +67,7 @@ void DiskFile::write(const void* buffer, uint32_t size)
 		_file.seek(0);
 	}
 
-	/*uint32_t bytes_written =*/ _file.write(buffer, size);
-	//CE_ASSERT(bytes_written == size, "Failed to write to file: requested: %llu, written: %llu", size, bytes_written);
+	return _file.write(buffer, size);
 }
 
 bool DiskFile::copy_to(File& file, uint32_t size)

+ 2 - 2
src/core/filesystem/disk_file.h

@@ -33,10 +33,10 @@ public:
 	void skip(uint32_t bytes);
 
 	/// @copydoc File::read()
-	void read(void* buffer, uint32_t size);
+	uint32_t read(void* buffer, uint32_t size);
 
 	/// @copydoc File::write()
-	void write(const void* buffer, uint32_t size);
+	uint32_t write(const void* buffer, uint32_t size);
 
 	/// @copydoc File::copy_to()
 	bool copy_to(File& file, uint32_t size = 0);

+ 2 - 2
src/core/filesystem/file.h

@@ -45,10 +45,10 @@ public:
 	virtual void skip(uint32_t bytes) = 0;
 
 	/// Reads a block of data from the file.
-	virtual void read(void* buffer, uint32_t size) = 0;
+	virtual uint32_t read(void* buffer, uint32_t size) = 0;
 
 	/// Writes a block of data to the file.
-	virtual void write(const void* buffer, uint32_t size) = 0;
+	virtual uint32_t write(const void* buffer, uint32_t size) = 0;
 
 	/// Copies a chunk of 'size' bytes of data from this to another file.
 	virtual bool copy_to(File& file, uint32_t size = 0) = 0;

+ 6 - 2
src/core/filesystem/null_file.h

@@ -37,16 +37,20 @@ public:
 	/// @copydoc File::read()
 	/// @note
 	///	Fills buffer with zeroes
-	void read(void* buffer, uint32_t size)
+	uint32_t read(void* buffer, uint32_t size)
 	{
 		for (uint32_t i = 0; i < size; i++)
 		{
 			((uint8_t*)buffer)[i] = 0;
 		}
+		return size;
 	}
 
 	/// @copydoc File::write()
-	void write(const void* buffer, uint32_t size) { (void)buffer; (void)size; }
+	uint32_t write(const void* /*buffer*/, uint32_t size)
+	{
+		return size;
+	}
 
 	/// @copydoc File::copy_to()
 	/// @note

+ 15 - 13
src/resource/lua_resource.cpp

@@ -36,25 +36,27 @@ namespace lua_resource
 	{
 		using namespace string_stream;
 
-		TempAllocator1024 alloc;
-		DynamicString res_abs_path(alloc);
-		TempAllocator1024 alloc2;
-		DynamicString bc_abs_path(alloc2);
-		opts.get_absolute_path(path, res_abs_path);
-		opts.get_absolute_path("bc.tmp", bc_abs_path);
-
 		TempAllocator1024 ta;
+		DynamicString luasrc(ta);
+		DynamicString luabin(ta);
+		opts.get_absolute_path(path, luasrc);
+		opts.get_absolute_path("luabin.tmp", luabin);
+
 		StringStream args(ta);
 		args << " " << LUAJIT_FLAGS;
-		args << " " << res_abs_path.c_str();
-		args << " " << bc_abs_path.c_str();
+		args << " " << luasrc.c_str();
+		args << " " << luabin.c_str();
 
 		StringStream output(ta);
 		int exitcode = os::execute_process(LUAJIT_EXE, c_str(args), output);
-		CE_ASSERT(exitcode == 0, "Failed to compile lua:\n%s", c_str(output));
-
-		Buffer blob = opts.read(bc_abs_path.c_str());
-		opts.delete_file(bc_abs_path.c_str());
+		RESOURCE_COMPILER_ASSERT(exitcode == 0
+			, opts
+			, "Failed to compile lua:\n%s"
+			, c_str(output)
+			);
+
+		Buffer blob = opts.read(luabin.c_str());
+		opts.delete_file(luabin.c_str());
 
 		LuaResource lr;
 		lr.version = SCRIPT_VERSION;

+ 1 - 0
src/resource/mesh_resource.cpp

@@ -13,6 +13,7 @@
 #include "resource_manager.h"
 #include "compile_options.h"
 #include "vector2.h"
+#include "reader_writer.h"
 
 namespace crown
 {