Prechádzať zdrojové kódy

resource: data_compiler: do not use jmpbuf, instead gracefully return from compile function

Daniele Bartolini 6 rokov pred
rodič
commit
be0d6514ca

+ 3 - 0
src/resource/compile_options.h

@@ -16,7 +16,10 @@
 	do                                                  \
 	{                                                   \
 		if (!(condition))                               \
+		{                                               \
 			opts.error(msg, ## __VA_ARGS__);            \
+			return -1;                                  \
+		}                                               \
 	} while(0)
 
 #define DATA_COMPILER_ASSERT_RESOURCE_EXISTS(type, name, opts) \

+ 3 - 1
src/resource/config_resource.cpp

@@ -17,7 +17,7 @@ namespace crown
 {
 namespace config_resource_internal
 {
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -38,6 +38,8 @@ namespace config_resource_internal
 		DATA_COMPILER_ASSERT_RESOURCE_EXISTS("package", boot_package.c_str(), opts);
 
 		opts.write(buf);
+
+		return 0;
 	}
 
 	void* load(File& file, Allocator& a)

+ 1 - 1
src/resource/config_resource.h

@@ -14,7 +14,7 @@ namespace crown
 {
 namespace config_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 	void* load(File& file, Allocator& a);
 	void unload(Allocator& allocator, void* resource);
 

+ 9 - 13
src/resource/data_compiler.cpp

@@ -391,22 +391,19 @@ bool DataCompiler::compile(const char* data_dir, const char* platform)
 		Buffer output(default_allocator());
 		array::reserve(output, 4*1024*1024);
 
-		if (!setjmp(_jmpbuf))
 		{
 			CompileOptions opts(*this, data_filesystem, src_path, output, platform);
 
-			hash_map::get(_compilers, _type, ResourceTypeData()).compiler(opts);
+			success = hash_map::get(_compilers, _type, ResourceTypeData()).compiler(opts) == 0;
 
-			File* outf = data_filesystem.open(path.c_str(), FileOpenMode::WRITE);
-			u32 size = array::size(output);
-			u32 written = outf->write(array::begin(output), size);
-			data_filesystem.close(*outf);
-
-			success = size == written;
-		}
-		else
-		{
-			success = false;
+			if (success)
+			{
+				File* outf = data_filesystem.open(path.c_str(), FileOpenMode::WRITE);
+				u32 size = array::size(output);
+				u32 written = outf->write(array::begin(output), size);
+				data_filesystem.close(*outf);
+				success = size == written;
+			}
 		}
 
 		if (success)
@@ -478,7 +475,6 @@ bool DataCompiler::can_compile(StringId64 type)
 void DataCompiler::error(const char* msg, va_list args)
 {
 	vloge(DATA_COMPILER, msg, args);
-	longjmp(_jmpbuf, 1);
 }
 
 void DataCompiler::filemonitor_callback(FileMonitorEvent::Enum fme, bool is_dir, const char* path, const char* path_renamed)

+ 1 - 3
src/resource/data_compiler.h

@@ -11,7 +11,6 @@
 #include "device/console_server.h"
 #include "device/device_options.h"
 #include "resource/types.h"
-#include <setjmp.h>
 
 namespace crown
 {
@@ -20,7 +19,7 @@ namespace crown
 /// @ingroup Resource
 struct DataCompiler
 {
-	typedef void (*CompileFunction)(CompileOptions& opts);
+	typedef s32 (*CompileFunction)(CompileOptions& opts);
 
 	struct ResourceTypeData
 	{
@@ -36,7 +35,6 @@ struct DataCompiler
 	Vector<DynamicString> _globs;
 	HashMap<DynamicString, DynamicString> _data_index;
 	FileMonitor _file_monitor;
-	jmp_buf _jmpbuf;
 
 	void add_file(const char* path);
 	void add_tree(const char* path);

+ 3 - 1
src/resource/font_resource.cpp

@@ -31,7 +31,7 @@ namespace font_resource_internal
 		}
 	};
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -88,6 +88,8 @@ namespace font_resource_internal
 			opts.write(glyphs[i].gd.y_offset);
 			opts.write(glyphs[i].gd.x_advance);
 		}
+
+		return 0;
 	}
 
 } // namespace font_resource_internal

+ 1 - 1
src/resource/font_resource.h

@@ -35,7 +35,7 @@ typedef u32 CodePoint;
 
 namespace font_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace font_resource_internal
 

+ 3 - 1
src/resource/level_resource.cpp

@@ -19,7 +19,7 @@ namespace crown
 {
 namespace level_resource_internal
 {
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 		TempAllocator4096 ta;
@@ -83,6 +83,8 @@ namespace level_resource_internal
 			opts.write(sounds[i]._pad[1]);
 			opts.write(sounds[i]._pad[2]);
 		}
+
+		return 0;
 	}
 
 } // namespace level_resource_internal

+ 1 - 1
src/resource/level_resource.h

@@ -34,7 +34,7 @@ struct LevelSound
 
 namespace level_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace level_resource_internal
 

+ 3 - 1
src/resource/lua_resource.cpp

@@ -23,7 +23,7 @@ namespace crown
 {
 namespace lua_resource_internal
 {
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		TempAllocator1024 ta;
 		DynamicString luasrc(ta);
@@ -70,6 +70,8 @@ namespace lua_resource_internal
 		opts.write(lr.version);
 		opts.write(lr.size);
 		opts.write(blob);
+
+		return 0;
 	}
 
 } // namespace lua_resource_internal

+ 1 - 1
src/resource/lua_resource.h

@@ -21,7 +21,7 @@ struct LuaResource
 
 namespace lua_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace lua_resource_internal
 

+ 13 - 5
src/resource/material_resource.cpp

@@ -71,7 +71,7 @@ namespace material_resource_internal
 		return offt;
 	}
 
-	static void parse_textures(const char* json, Array<TextureData>& textures, Array<char>& names, Array<char>& dynamic, CompileOptions& opts)
+	static s32 parse_textures(const char* json, Array<TextureData>& textures, Array<char>& names, Array<char>& dynamic, CompileOptions& opts)
 	{
 		TempAllocator4096 ta;
 		JsonObject object(ta);
@@ -108,9 +108,11 @@ namespace material_resource_internal
 
 			array::push_back(textures, td);
 		}
+
+		return 0;
 	}
 
-	static void parse_uniforms(const char* json, Array<UniformData>& uniforms, Array<char>& names, Array<char>& dynamic, CompileOptions& opts)
+	static s32 parse_uniforms(const char* json, Array<UniformData>& uniforms, Array<char>& names, Array<char>& dynamic, CompileOptions& opts)
 	{
 		TempAllocator4096 ta;
 		JsonObject object(ta);
@@ -177,9 +179,11 @@ namespace material_resource_internal
 
 			array::push_back(uniforms, ud);
 		}
+
+		return 0;
 	}
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 		TempAllocator4096 ta;
@@ -194,8 +198,10 @@ namespace material_resource_internal
 		DynamicString shader(ta);
 		sjson::parse_string(object["shader"], shader);
 
-		parse_textures(object["textures"], texdata, names, dynblob, opts);
-		parse_uniforms(object["uniforms"], unidata, names, dynblob, opts);
+		if (parse_textures(object["textures"], texdata, names, dynblob, opts) != 0)
+			return -1;
+		if (parse_uniforms(object["uniforms"], unidata, names, dynblob, opts) != 0)
+			return -1;
 
 		MaterialResource mr;
 		mr.version             = RESOURCE_VERSION_MATERIAL;
@@ -236,6 +242,8 @@ namespace material_resource_internal
 
 		opts.write(dynblob);
 		opts.write(names);
+
+		return 0;
 	}
 
 	void* load(File& file, Allocator& a)

+ 1 - 1
src/resource/material_resource.h

@@ -69,7 +69,7 @@ struct UniformHandle
 
 namespace material_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 	void* load(File& file, Allocator& a);
 	void online(StringId64 id, ResourceManager& rm);
 	void offline(StringId64 id, ResourceManager& rm);

+ 3 - 1
src/resource/mesh_resource.cpp

@@ -253,7 +253,7 @@ namespace mesh_resource_internal
 		}
 	};
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -289,6 +289,8 @@ namespace mesh_resource_internal
 			mc.parse(geometry, node);
 			mc.write();
 		}
+
+		return 0;
 	}
 
 	void* load(File& file, Allocator& a)

+ 1 - 1
src/resource/mesh_resource.h

@@ -66,7 +66,7 @@ struct MeshResource
 
 namespace mesh_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 	void* load(File& file, Allocator& a);
 	void online(StringId64 /*id*/, ResourceManager& /*rm*/);
 	void offline(StringId64 /*id*/, ResourceManager& /*rm*/);

+ 18 - 14
src/resource/package_resource.cpp

@@ -19,7 +19,7 @@ namespace crown
 {
 namespace package_resource_internal
 {
-	void compile_resources(const char* type, const JsonArray& names, Array<PackageResource::Resource>& output, CompileOptions& opts)
+	s32 compile_resources(const char* type, const JsonArray& names, Array<PackageResource::Resource>& output, CompileOptions& opts)
 	{
 		const StringId64 typeh = StringId64(type);
 
@@ -34,9 +34,11 @@ namespace package_resource_internal
 			const StringId64 nameh = sjson::parse_resource_id(names[i]);
 			array::push_back(output, PackageResource::Resource(typeh, nameh));
 		}
+
+		return 0;
 	}
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -72,18 +74,18 @@ namespace package_resource_internal
 
 		Array<PackageResource::Resource> resources(default_allocator());
 
-		compile_resources("texture", texture, resources, opts);
-		compile_resources("lua", script, resources, opts);
-		compile_resources("sound", sound, resources, opts);
-		compile_resources("mesh", mesh, resources, opts);
-		compile_resources("unit", unit, resources, opts);
-		compile_resources("sprite", sprite, resources, opts);
-		compile_resources("material", material, resources, opts);
-		compile_resources("font", font, resources, opts);
-		compile_resources("level", level, resources, opts);
-		compile_resources("physics_config", phyconf, resources, opts);
-		compile_resources("shader", shader, resources, opts);
-		compile_resources("sprite_animation", sprite_animation, resources, opts);
+		if (compile_resources("texture", texture, resources, opts) != 0) return -1;
+		if (compile_resources("lua", script, resources, opts) != 0) return -1;
+		if (compile_resources("sound", sound, resources, opts) != 0) return -1;
+		if (compile_resources("mesh", mesh, resources, opts) != 0) return -1;
+		if (compile_resources("unit", unit, resources, opts) != 0) return -1;
+		if (compile_resources("sprite", sprite, resources, opts) != 0) return -1;
+		if (compile_resources("material", material, resources, opts) != 0) return -1;
+		if (compile_resources("font", font, resources, opts) != 0) return -1;
+		if (compile_resources("level", level, resources, opts) != 0) return -1;
+		if (compile_resources("physics_config", phyconf, resources, opts) != 0) return -1;
+		if (compile_resources("shader", shader, resources, opts) != 0) return -1;
+		if (compile_resources("sprite_animation", sprite_animation, resources, opts) != 0) return -1;
 
 		// Write
 		opts.write(RESOURCE_VERSION_PACKAGE);
@@ -94,6 +96,8 @@ namespace package_resource_internal
 			opts.write(resources[i].type);
 			opts.write(resources[i].name);
 		}
+
+		return 0;
 	}
 
 	void* load(File& file, Allocator& a)

+ 1 - 1
src/resource/package_resource.h

@@ -49,7 +49,7 @@ struct PackageResource
 
 namespace package_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 	void* load(File& file, Allocator& a);
 	void unload(Allocator& allocator, void* resource);
 

+ 18 - 17
src/resource/physics_resource.cpp

@@ -105,7 +105,7 @@ namespace physics_resource_internal
 		sd.box.half_size = (aabb.max - aabb.min) * 0.5f;
 	}
 
-	Buffer compile_collider(const char* json, CompileOptions& opts)
+	s32 compile_collider(Buffer& output, const char* json, CompileOptions& opts)
 	{
 		TempAllocator4096 ta;
 		JsonObject obj(ta);
@@ -208,24 +208,23 @@ namespace physics_resource_internal
 		cd.size += (needs_points ? sizeof(u32) + sizeof(Vector3)*array::size(points) : 0);
 		cd.size += (cd.type == ColliderType::MESH ? sizeof(u32) + sizeof(u16)*array::size(point_indices) : 0);
 
-		Buffer buf(default_allocator());
-		array::push(buf, (char*)&cd, sizeof(cd));
+		array::push(output, (char*)&cd, sizeof(cd));
 
 		if (needs_points)
 		{
-			array::push(buf, (char*)&num_points, sizeof(num_points));
-			array::push(buf, (char*)array::begin(points), sizeof(Vector3)*array::size(points));
+			array::push(output, (char*)&num_points, sizeof(num_points));
+			array::push(output, (char*)array::begin(points), sizeof(Vector3)*array::size(points));
 		}
 		if (cd.type == ColliderType::MESH)
 		{
-			array::push(buf, (char*)&num_indices, sizeof(num_indices));
-			array::push(buf, (char*)array::begin(point_indices), sizeof(u16)*array::size(point_indices));
+			array::push(output, (char*)&num_indices, sizeof(num_indices));
+			array::push(output, (char*)array::begin(point_indices), sizeof(u16)*array::size(point_indices));
 		}
 
-		return buf;
+		return 0;
 	}
 
-	Buffer compile_actor(const char* json, CompileOptions& /*opts*/)
+	s32 compile_actor(Buffer& output, const char* json, CompileOptions& /*opts*/)
 	{
 		TempAllocator4096 ta;
 		JsonObject obj(ta);
@@ -252,12 +251,12 @@ namespace physics_resource_internal
 		if (json_object::has(obj, "lock_rotation_z") && sjson::parse_bool(obj["lock_rotation_z"]))
 			ar.flags |= ActorFlags::LOCK_ROTATION_Z;
 
-		Buffer buf(default_allocator());
-		array::push(buf, (char*)&ar, sizeof(ar));
-		return buf;
+		array::push(output, (char*)&ar, sizeof(ar));
+
+		return 0;
 	}
 
-	Buffer compile_joint(const char* json, CompileOptions& opts)
+	s32 compile_joint(Buffer& output, const char* json, CompileOptions& opts)
 	{
 		TempAllocator4096 ta;
 		JsonObject obj(ta);
@@ -290,9 +289,9 @@ namespace physics_resource_internal
 			break;
 		}
 
-		Buffer buf(default_allocator());
-		array::push(buf, (char*)&jd, sizeof(jd));
-		return buf;
+		array::push(output, (char*)&jd, sizeof(jd));
+
+		return 0;
 	}
 
 } // namespace physics_resource_internal
@@ -489,7 +488,7 @@ namespace physics_config_resource_internal
 		}
 	};
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 		TempAllocator4096 ta;
@@ -559,6 +558,8 @@ namespace physics_config_resource_internal
 			opts.write(cfc._filters[i].me);
 			opts.write(cfc._filters[i].mask);
 		}
+
+		return 0;
 	}
 
 } // namespace physics_config_resource_internal

+ 4 - 4
src/resource/physics_resource.h

@@ -17,9 +17,9 @@ namespace crown
 {
 namespace physics_resource_internal
 {
-	Buffer compile_collider(const char* json, CompileOptions& opts);
-	Buffer compile_actor(const char* json, CompileOptions& opts);
-	Buffer compile_joint(const char* json, CompileOptions& opts);
+	s32 compile_collider(Buffer& output, const char* json, CompileOptions& opts);
+	s32 compile_actor(Buffer& output, const char* json, CompileOptions& opts);
+	s32 compile_joint(Buffer& output, const char* json, CompileOptions& opts);
 
 } // namespace physics_resource_internal
 
@@ -67,7 +67,7 @@ struct PhysicsActor
 
 namespace physics_config_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace physics_config_resource_internal
 

+ 59 - 23
src/resource/shader_resource.cpp

@@ -653,12 +653,12 @@ namespace shader_resource_internal
 			_opts.get_temporary_path("fs_compiled.bin", _fs_compiled_path);
 		}
 
-		void parse(const char* path)
+		s32 parse(const char* path)
 		{
-			parse(_opts.read(path));
+			return parse(_opts.read(path));
 		}
 
-		void parse(Buffer b)
+		s32 parse(Buffer b)
 		{
 			TempAllocator4096 ta;
 			JsonObject object(ta);
@@ -678,22 +678,39 @@ namespace shader_resource_internal
 			}
 
 			if (json_object::has(object, "render_states"))
-				parse_render_states(object["render_states"]);
+			{
+				if (parse_render_states(object["render_states"]) != 0)
+					return -1;
+			}
 
 			if (json_object::has(object, "sampler_states"))
-				parse_sampler_states(object["sampler_states"]);
+			{
+				if (parse_sampler_states(object["sampler_states"]) != 0)
+					return -1;
+			}
 
 			if (json_object::has(object, "bgfx_shaders"))
-				parse_bgfx_shaders(object["bgfx_shaders"]);
+			{
+				if (parse_bgfx_shaders(object["bgfx_shaders"]) != 0)
+					return -1;
+			}
 
 			if (json_object::has(object, "shaders"))
-				parse_shaders(object["shaders"]);
+			{
+				if (parse_shaders(object["shaders"]) != 0)
+					return -1;
+			}
 
 			if (json_object::has(object, "static_compile"))
-				parse_static_compile(object["static_compile"]);
+			{
+				if (parse_static_compile(object["static_compile"]) != 0)
+					return -1;
+			}
+
+			return 0;
 		}
 
-		void parse_render_states(const char* json)
+		s32 parse_render_states(const char* json)
 		{
 			TempAllocator4096 ta;
 			JsonObject render_states(ta);
@@ -813,9 +830,11 @@ namespace shader_resource_internal
 					);
 				hash_map::set(_render_states, key, rs);
 			}
+
+			return 0;
 		}
 
-		void parse_sampler_states(const char* json)
+		s32 parse_sampler_states(const char* json)
 		{
 			TempAllocator4096 ta;
 			JsonObject sampler_states(ta);
@@ -911,9 +930,11 @@ namespace shader_resource_internal
 					);
 				hash_map::set(_sampler_states, key, ss);
 			}
+
+			return 0;
 		}
 
-		void parse_bgfx_shaders(const char* json)
+		s32 parse_bgfx_shaders(const char* json)
 		{
 			TempAllocator4096 ta;
 			JsonObject bgfx_shaders(ta);
@@ -957,9 +978,11 @@ namespace shader_resource_internal
 					);
 				hash_map::set(_bgfx_shaders, key, bgfxshader);
 			}
+
+			return 0;
 		}
 
-		void parse_bgfx_samplers(const char* json, BgfxShader& bgfxshader)
+		s32 parse_bgfx_samplers(const char* json, BgfxShader& bgfxshader)
 		{
 			TempAllocator4096 ta;
 			JsonObject bgfx_samplers(ta);
@@ -994,9 +1017,11 @@ namespace shader_resource_internal
 					);
 				hash_map::set(bgfxshader._samplers, key, sampler_state);
 			}
+
+			return 0;
 		}
 
-		void parse_shaders(const char* json)
+		s32 parse_shaders(const char* json)
 		{
 			TempAllocator4096 ta;
 			JsonObject shaders(ta);
@@ -1026,9 +1051,11 @@ namespace shader_resource_internal
 					);
 				hash_map::set(_shaders, key, shader);
 			}
+
+			return 0;
 		}
 
-		void parse_static_compile(const char* json)
+		s32 parse_static_compile(const char* json)
 		{
 			TempAllocator4096 ta;
 			JsonArray static_compile(ta);
@@ -1053,6 +1080,8 @@ namespace shader_resource_internal
 
 				vector::push_back(_static_compile, sc);
 			}
+
+			return 0;
 		}
 
 		void delete_temp_files()
@@ -1064,7 +1093,7 @@ namespace shader_resource_internal
 			_opts.delete_file(_fs_compiled_path.c_str());
 		}
 
-		void compile()
+		s32 compile()
 		{
 			_opts.write(RESOURCE_VERSION_SHADER);
 			_opts.write(vector::size(_static_compile));
@@ -1109,11 +1138,14 @@ namespace shader_resource_internal
 				const RenderState rs_default;
 				const RenderState& rs = hash_map::get(_render_states, render_state, rs_default);
 
-				_opts.write(shader_name._id);                      // Shader name
-				_opts.write(rs.encode());                          // Render state
-				compile_sampler_states(bgfx_shader.c_str());       // Sampler states
-				compile_bgfx_shader(bgfx_shader.c_str(), defines); // Shader code
+				_opts.write(shader_name._id);                               // Shader name
+				_opts.write(rs.encode());                                   // Render state
+				compile_sampler_states(bgfx_shader.c_str());                // Sampler states
+				if (compile_bgfx_shader(bgfx_shader.c_str(), defines) != 0) // Shader code
+					return -1;
 			}
+
+			return 0;
 		}
 
 		void compile_sampler_states(const char* bgfx_shader)
@@ -1143,7 +1175,7 @@ namespace shader_resource_internal
 			}
 		}
 
-		void compile_bgfx_shader(const char* bgfx_shader, const Vector<DynamicString>& defines)
+		s32 compile_bgfx_shader(const char* bgfx_shader, const Vector<DynamicString>& defines)
 		{
 			TempAllocator512 taa;
 			DynamicString key(taa);
@@ -1279,14 +1311,18 @@ namespace shader_resource_internal
 			_opts.write(tmpvs);
 			_opts.write(array::size(tmpfs));
 			_opts.write(tmpfs);
+
+			return 0;
 		}
 	};
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		ShaderCompiler sc(opts);
-		sc.parse(opts.source_path());
-		sc.compile();
+		if (sc.parse(opts.source_path()) != 0)
+			return -1;
+
+		return sc.compile();
 	}
 
 	void* load(File& file, Allocator& a)

+ 1 - 1
src/resource/shader_resource.h

@@ -43,7 +43,7 @@ struct ShaderResource
 
 namespace shader_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 	void* load(File& file, Allocator& a);
 	void online(StringId64 id, ResourceManager& rm);
 	void offline(StringId64 id, ResourceManager& rm);

+ 3 - 1
src/resource/sound_resource.cpp

@@ -33,7 +33,7 @@ namespace sound_resource_internal
 		s32 data_size;       // Data dimension
 	};
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -69,6 +69,8 @@ namespace sound_resource_internal
 		opts.write(sr.sound_type);
 
 		opts.write(wavdata, wav->data_size);
+
+		return 0;
 	}
 
 } // namespace sound_resource_internal

+ 1 - 1
src/resource/sound_resource.h

@@ -37,7 +37,7 @@ struct SoundResource
 
 namespace sound_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace	sound_resource_internal
 

+ 6 - 2
src/resource/sprite_resource.cpp

@@ -39,7 +39,7 @@ namespace sprite_resource_internal
 		frame.pivot  = sjson::parse_vector2(obj["pivot"]);
 	}
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -132,6 +132,8 @@ namespace sprite_resource_internal
 		opts.write(sr.num_verts);
 		for (u32 i = 0; i < array::size(vertices); i++)
 			opts.write(vertices[i]);
+
+		return 0;
 	}
 
 } // namespace sprite_resource_internal
@@ -147,7 +149,7 @@ namespace sprite_resource
 
 namespace sprite_animation_resource_internal
 {
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -179,6 +181,8 @@ namespace sprite_animation_resource_internal
 
 		for (u32 i = 0; i < array::size(frames); i++)
 			opts.write(frames[i]);
+
+		return 0;
 	}
 
 } // namespace sprite_animation_resource_internal

+ 2 - 2
src/resource/sprite_resource.h

@@ -25,7 +25,7 @@ struct SpriteResource
 
 namespace sprite_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace sprite_resource_internal
 
@@ -45,7 +45,7 @@ struct SpriteAnimationResource
 
 namespace sprite_animation_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace sprite_animation_resource_internal
 

+ 11 - 5
src/resource/state_machine_resource.cpp

@@ -166,7 +166,7 @@ namespace state_machine_internal
 		{
 		}
 
-		void parse(Buffer& buf)
+		s32 parse(Buffer& buf)
 		{
 			TempAllocator4096 ta;
 			JsonObject object(ta);
@@ -349,9 +349,11 @@ namespace state_machine_internal
 
 				default_allocator().deallocate(variables);
 			}
+
+			return 0;
 		}
 
-		void write()
+		s32 write()
 		{
 			StateMachineResource smr;
 			smr.version = RESOURCE_VERSION_STATE_MACHINE;
@@ -425,16 +427,20 @@ namespace state_machine_internal
 			// Write bytecode
 			for (u32 i = 0; i < array::size(_byte_code); ++i)
 				_opts.write(_byte_code[i]);
+
+			return 0;
 		}
 	};
 
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
 		StateMachineCompiler smc(opts);
-		smc.parse(buf);
-		smc.write();
+		if (smc.parse(buf) != 0)
+			return -1;
+
+		return smc.write();
 	}
 
 } // namespace state_machine_internal

+ 1 - 1
src/resource/state_machine_resource.h

@@ -74,7 +74,7 @@ struct TransitionMode
 
 namespace state_machine_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace state_machine_internal
 

+ 3 - 1
src/resource/texture_resource.cpp

@@ -31,7 +31,7 @@ static const char* texturec_paths[] =
 
 namespace texture_resource_internal
 {
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
 
@@ -96,6 +96,8 @@ namespace texture_resource_internal
 		opts.write(RESOURCE_VERSION_TEXTURE);
 		opts.write(array::size(blob));
 		opts.write(blob);
+
+		return 0;
 	}
 
 	void* load(File& file, Allocator& a)

+ 1 - 1
src/resource/texture_resource.h

@@ -22,7 +22,7 @@ struct TextureResource
 
 namespace texture_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 	void* load(File& file, Allocator& a);
 	void offline(StringId64 id, ResourceManager& rm);
 	void online(StringId64 id, ResourceManager& rm);

+ 47 - 37
src/resource/unit_compiler.cpp

@@ -68,7 +68,7 @@ static LightType::Enum light_name_to_enum(const char* name)
 	return LightType::COUNT;
 }
 
-static Buffer compile_transform(const char* json, CompileOptions& /*opts*/)
+static s32 compile_transform(Buffer& output, const char* json, CompileOptions& /*opts*/)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -79,12 +79,12 @@ static Buffer compile_transform(const char* json, CompileOptions& /*opts*/)
 	td.rotation = sjson::parse_quaternion(obj["rotation"]);
 	td.scale    = sjson::parse_vector3   (obj["scale"]);
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&td, sizeof(td));
-	return buf;
+	array::push(output, (char*)&td, sizeof(td));
+
+	return 0;
 }
 
-static Buffer compile_camera(const char* json, CompileOptions& opts)
+static s32 compile_camera(Buffer& output, const char* json, CompileOptions& opts)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -106,12 +106,12 @@ static Buffer compile_camera(const char* json, CompileOptions& opts)
 	cd.near_range = sjson::parse_float(obj["near_range"]);
 	cd.far_range  = sjson::parse_float(obj["far_range"]);
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&cd, sizeof(cd));
-	return buf;
+	array::push(output, (char*)&cd, sizeof(cd));
+
+	return 0;
 }
 
-static Buffer compile_mesh_renderer(const char* json, CompileOptions& opts)
+static s32 compile_mesh_renderer(Buffer& output, const char* json, CompileOptions& opts)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -140,12 +140,12 @@ static Buffer compile_mesh_renderer(const char* json, CompileOptions& opts)
 	mrd._pad0[1]          = 0;
 	mrd._pad0[2]          = 0;
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&mrd, sizeof(mrd));
-	return buf;
+	array::push(output, (char*)&mrd, sizeof(mrd));
+
+	return 0;
 }
 
-static Buffer compile_sprite_renderer(const char* json, CompileOptions& opts)
+static s32 compile_sprite_renderer(Buffer& output, const char* json, CompileOptions& opts)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -179,12 +179,12 @@ static Buffer compile_sprite_renderer(const char* json, CompileOptions& opts)
 	srd._pad1[2]          = 0;
 	srd._pad1[3]          = 0;
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&srd, sizeof(srd));
-	return buf;
+	array::push(output, (char*)&srd, sizeof(srd));
+
+	return 0;
 }
 
-static Buffer compile_light(const char* json, CompileOptions& opts)
+static s32 compile_light(Buffer& output, const char* json, CompileOptions& opts)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -207,12 +207,12 @@ static Buffer compile_light(const char* json, CompileOptions& opts)
 	ld.spot_angle = sjson::parse_float  (obj["spot_angle"]);
 	ld.color      = sjson::parse_vector3(obj["color"]);
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&ld, sizeof(ld));
-	return buf;
+	array::push(output, (char*)&ld, sizeof(ld));
+
+	return 0;
 }
 
-static Buffer compile_script(const char* json, CompileOptions& opts)
+static s32 compile_script(Buffer& output, const char* json, CompileOptions& opts)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -228,12 +228,12 @@ static Buffer compile_script(const char* json, CompileOptions& opts)
 	ScriptDesc sd;
 	sd.script_resource = sjson::parse_resource_id(obj["script_resource"]);
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&sd, sizeof(sd));
-	return buf;
+	array::push(output, (char*)&sd, sizeof(sd));
+
+	return 0;
 }
 
-static Buffer compile_animation_state_machine(const char* json, CompileOptions& opts)
+static s32 compile_animation_state_machine(Buffer& output, const char* json, CompileOptions& opts)
 {
 	TempAllocator4096 ta;
 	JsonObject obj(ta);
@@ -249,9 +249,9 @@ static Buffer compile_animation_state_machine(const char* json, CompileOptions&
 	AnimationStateMachineDesc asmd;
 	asmd.state_machine_resource = sjson::parse_resource_id(obj["state_machine_resource"]);
 
-	Buffer buf(default_allocator());
-	array::push(buf, (char*)&asmd, sizeof(asmd));
-	return buf;
+	array::push(output, (char*)&asmd, sizeof(asmd));
+
+	return 0;
 }
 
 UnitCompiler::UnitCompiler(CompileOptions& opts)
@@ -279,9 +279,9 @@ Buffer UnitCompiler::read_unit(const char* path)
 	return buf;
 }
 
-void UnitCompiler::compile_unit(const char* path)
+s32 UnitCompiler::compile_unit(const char* path)
 {
-	compile_unit_from_json(array::begin(read_unit(path)));
+	return compile_unit_from_json(array::begin(read_unit(path)));
 }
 
 u32 component_index(const JsonArray& components, const FixedString& id)
@@ -303,7 +303,7 @@ u32 component_index(const JsonArray& components, const FixedString& id)
 	return UINT32_MAX;
 }
 
-void UnitCompiler::compile_unit_from_json(const char* json)
+s32 UnitCompiler::compile_unit_from_json(const char* json)
 {
 	Buffer data(default_allocator());
 	array::reserve(data, 1024*1024);
@@ -385,22 +385,32 @@ void UnitCompiler::compile_unit_from_json(const char* json)
 
 			const StringId32 type = sjson::parse_string_id(component["type"]);
 
-			Buffer buf = compile_component(type, component["data"]);
-			add_component_data(type, buf, _num_units);
+			Buffer output(default_allocator());
+			if (compile_component(output, type, component["data"]) != 0)
+				return -1;
+
+			add_component_data(type, output, _num_units);
 		}
 	}
 
 	++_num_units;
+
+	return 0;
 }
 
-void UnitCompiler::compile_multiple_units(const char* json)
+s32 UnitCompiler::compile_multiple_units(const char* json)
 {
 	TempAllocator4096 ta;
 	JsonArray units(ta);
 	sjson::parse_array(json, units);
 
 	for (u32 i = 0; i < array::size(units); ++i)
-		compile_unit_from_json(units[i]);
+	{
+		if (compile_unit_from_json(units[i]) != 0)
+			return -1;
+	}
+
+	return 0;
 }
 
 Buffer UnitCompiler::blob()
@@ -487,11 +497,11 @@ void UnitCompiler::register_component_compiler(StringId32 type, CompileFunction
 	std::sort(array::begin(_component_info), array::end(_component_info));
 }
 
-Buffer UnitCompiler::compile_component(StringId32 type, const char* json)
+s32 UnitCompiler::compile_component(Buffer& output, StringId32 type, const char* json)
 {
 	DATA_COMPILER_ASSERT(hash_map::has(_component_data, type), _opts, "Unknown component");
 
-	return hash_map::get(_component_data, type, ComponentTypeData(default_allocator()))._compiler(json, _opts);
+	return hash_map::get(_component_data, type, ComponentTypeData(default_allocator()))._compiler(output, json, _opts);
 }
 
 } // namespace crown

+ 5 - 5
src/resource/unit_compiler.h

@@ -14,7 +14,7 @@ namespace crown
 {
 struct UnitCompiler
 {
-	typedef Buffer (*CompileFunction)(const char* json, CompileOptions& opts);
+	typedef s32 (*CompileFunction)(Buffer& output, const char* json, CompileOptions& opts);
 
 	struct ComponentTypeData
 	{
@@ -55,16 +55,16 @@ struct UnitCompiler
 
 	void register_component_compiler(const char* type, CompileFunction fn, f32 spawn_order);
 	void register_component_compiler(StringId32 type, CompileFunction fn, f32 spawn_order);
-	Buffer compile_component(StringId32 type, const char* json);
+	s32 compile_component(Buffer& output, StringId32 type, const char* json);
 	void add_component_data(StringId32 type, const Buffer& data, u32 unit_index);
 
 	///
 	UnitCompiler(CompileOptions& opts);
 
 	Buffer read_unit(const char* name);
-	void compile_unit(const char* path);
-	void compile_unit_from_json(const char* json);
-	void compile_multiple_units(const char* json);
+	s32 compile_unit(const char* path);
+	s32 compile_unit_from_json(const char* json);
+	s32 compile_multiple_units(const char* json);
 
 	Buffer blob();
 };

+ 4 - 2
src/resource/unit_resource.cpp

@@ -15,14 +15,16 @@ namespace crown
 {
 namespace unit_resource_internal
 {
-	void compile(CompileOptions& opts)
+	s32 compile(CompileOptions& opts)
 	{
 		Buffer unit_data(default_allocator());
 
 		UnitCompiler uc(opts);
-		uc.compile_unit(opts.source_path());
+		if (uc.compile_unit(opts.source_path()) != 0)
+			return -1;
 
 		opts.write(uc.blob());
+		return 0;
 	}
 
 } // namespace unit_resource_internal

+ 1 - 1
src/resource/unit_resource.h

@@ -30,7 +30,7 @@ struct ComponentData
 
 namespace unit_resource_internal
 {
-	void compile(CompileOptions& opts);
+	s32 compile(CompileOptions& opts);
 
 } // namespace unit_resource_internal