示例#1
0
static int
lua_import_output(const uuid_t uuid, const luaimport_dump_t* dump) {
	resource_source_t source;
	hash_t checksum;
	tick_t timestamp;
	uint64_t platform;
	string_const_t type = string_const(STRING_CONST("lua"));
	int ret = 0;

	resource_source_initialize(&source);
	resource_source_read(&source, uuid);

	timestamp = time_system();
	platform = 0;

	checksum = hash(dump->bytecode, dump->bytecode_size);
	if (resource_source_write_blob(uuid, timestamp, HASH_SOURCE,
	                               platform, checksum,
	                               dump->bytecode, dump->bytecode_size)) {
		resource_source_set_blob(&source, timestamp, HASH_SOURCE,
		                         platform, checksum, dump->bytecode_size);
	}
	else {
		ret = -1;
		goto finalize;
	}

	resource_source_set(&source, timestamp, HASH_RESOURCE_TYPE,
	                    0, STRING_ARGS(type));

	if (!resource_source_write(&source, uuid, false)) {
		ret = -1;
		goto finalize;
	}

finalize:

	resource_source_finalize(&source);

	return ret;
}
示例#2
0
static int
render_import_glsl_shader(stream_t* stream, const uuid_t uuid, const char* type,
                          size_t type_length) {
	resource_source_t source;
	void* blob = 0;
	size_t size;
	size_t read;
	size_t begin;
	size_t next;
	hash_t checksum;
	tick_t timestamp;
	uint64_t platform;
	size_t maxtokens;
	size_t parameter;
	string_const_t* token = 0;
	string_const_t valstr;
	char buffer[128];
	int ret = 0;
	resource_platform_t platformdecl = { -1, -1, RENDERAPIGROUP_OPENGL, -1, -1, -1};

	resource_source_initialize(&source);
	resource_source_read(&source, uuid);

	read = 0;
	size = stream_size(stream);
	blob = memory_allocate(HASH_RESOURCE, size, 0, MEMORY_PERSISTENT);

	size = stream_read(stream, blob, size);

	platform = resource_platform(platformdecl);
	timestamp = time_system();
	checksum = hash(blob, size);
	if (resource_source_write_blob(uuid, timestamp, HASH_SOURCE,
	                               platform, checksum, blob, size)) {
		resource_source_set_blob(&source, timestamp, HASH_SOURCE,
		                         platform, checksum, size);
	}
	else {
		ret = -1;
		goto finalize;
	}

	//Parse source and set parameters
	maxtokens = 256;
	token = memory_allocate(HASH_RESOURCE, sizeof(string_const_t) * maxtokens, 0, MEMORY_PERSISTENT);
	begin = 0;
	parameter = 0;
	do {
		string_const_t tokens[64], line;
		size_t itok, ntokens;

		next = string_find_first_of(blob, size, STRING_CONST("\n\r"), begin);
		line = string_substr(blob, size, begin, next - begin);
		ntokens = string_explode(STRING_ARGS(line), STRING_CONST(GLSL_TOKEN_DELIM),
		                        tokens, maxtokens, false);

		for (itok = 0; itok < ntokens; ++itok) {
			if ((string_equal(STRING_ARGS(tokens[itok]), STRING_CONST("attribute")) ||
			        string_equal(STRING_ARGS(tokens[itok]), STRING_CONST("uniform"))) &&
			        (itok + 2 < ntokens)) {
				char typebuf[16], dimbuf[16];
				string_const_t typestr = tokens[itok + 1];
				string_const_t namestr = tokens[itok + 2];
				string_const_t dimstr;

				int parameter_type = glsl_type_to_parameter_type(typestr);
				if (parameter_type < 0)
					continue;

				int parameter_dim = glsl_dim_from_token(typestr);
				if (parameter_dim == 1)
					parameter_dim = glsl_dim_from_token(namestr);

				namestr = glsl_name_from_token(namestr);
				typestr = string_to_const(string_from_uint(typebuf, sizeof(typebuf), (unsigned int)parameter_type, false, 0, 0));
				dimstr = string_to_const(string_from_uint(dimbuf, sizeof(dimbuf), (unsigned int)parameter_dim, false, 0, 0));

				log_debugf(HASH_RESOURCE, STRING_CONST("parameter: %.*s type %.*s dim %.*s"),
				           STRING_FORMAT(namestr), STRING_FORMAT(typestr), STRING_FORMAT(dimstr));

				string_t param = string_format(buffer, sizeof(buffer), STRING_CONST("parameter_type_%" PRIsize),
				                               parameter);
				resource_source_set(&source, timestamp, hash(STRING_ARGS(param)),
				                    platform, STRING_ARGS(typestr));

				param = string_format(buffer, sizeof(buffer), STRING_CONST("parameter_name_%" PRIsize),
				                      parameter);
				resource_source_set(&source, timestamp, hash(STRING_ARGS(param)),
				                    platform, STRING_ARGS(namestr));

				param = string_format(buffer, sizeof(buffer), STRING_CONST("parameter_dim_%" PRIsize),
				                      parameter);
				resource_source_set(&source, timestamp, hash(STRING_ARGS(param)),
				                    platform, STRING_ARGS(dimstr));
				++parameter;
			}
		}

		begin = string_find_first_not_of(blob, size, STRING_CONST(STRING_WHITESPACE), next);
	}
	while (next != STRING_NPOS);

	valstr = string_from_uint_static(parameter, false, 0, 0);
	resource_source_set(&source, timestamp, HASH_PARAMETER_COUNT,
	                    platform, STRING_ARGS(valstr));

	resource_source_set(&source, timestamp, HASH_RESOURCE_TYPE,
	                    0, type, type_length);

	if (!resource_source_write(&source, uuid, false)) {
		string_const_t uuidstr = string_from_uuid_static(uuid);
		log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Failed writing imported GLSL shader: %.*s"),
		          STRING_FORMAT(uuidstr));
		ret = -1;
		goto finalize;
	}
	else {
		string_const_t uuidstr = string_from_uuid_static(uuid);
		log_infof(HASH_RESOURCE, STRING_CONST("Wrote imported GLSL shader: %.*s"),
		          STRING_FORMAT(uuidstr));
	}

finalize:
	memory_deallocate(blob);
	memory_deallocate(token);
	resource_source_finalize(&source);

	return ret;
}