Exemplo n.º 1
0
bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {

	String local_path;
	if (p_path.is_rel_path())
		local_path = "res://" + p_path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(p_path);

	if (ResourceCache::has(local_path)) {

		return true; // If cached, it probably exists
	}

	bool xl_remapped = false;
	String path = _path_remap(local_path, &xl_remapped);

	// Try all loaders and pick the first match for the type hint
	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(path, p_type_hint)) {
			continue;
		}

		if (loader[i]->exists(path))
			return true;
	}

	return false;
}
Exemplo n.º 2
0
Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {

	if (r_error)
		*r_error = ERR_CANT_OPEN;

	String local_path;
	if (p_path.is_rel_path())
		local_path = "res://" + p_path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(p_path);

	bool xl_remapped = false;
	String path = _path_remap(local_path, &xl_remapped);

	ERR_FAIL_COND_V(path == "", Ref<ResourceInteractiveLoader>());

	if (!p_no_cache && ResourceCache::has(path)) {

		if (OS::get_singleton()->is_stdout_verbose())
			print_line("load resource: " + path + " (cached)");

		Ref<Resource> res_cached = ResourceCache::get(path);
		Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));

		ril->resource = res_cached;
		return ril;
	}

	if (OS::get_singleton()->is_stdout_verbose())
		print_line("load resource: ");

	bool found = false;

	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(path, p_type_hint))
			continue;
		found = true;
		Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, r_error);
		if (ril.is_null())
			continue;
		if (!p_no_cache)
			ril->set_local_path(local_path);
		if (xl_remapped)
			ril->set_translation_remapped(true);

		return ril;
	}

	if (found) {
		ERR_EXPLAIN("Failed loading resource: " + path);
	} else {
		ERR_EXPLAIN("No loader found for resource: " + path);
	}
	ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
	return Ref<ResourceInteractiveLoader>();
}
Exemplo n.º 3
0
void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {

	String path = _path_remap(p_path);

	String local_path;
	if (path.is_rel_path())
		local_path = "res://" + path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(path);

	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(local_path))
			continue;
		/*
		if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
			continue;
		*/

		loader[i]->get_dependencies(local_path, p_dependencies, p_add_types);
	}
}
Exemplo n.º 4
0
String ResourceLoader::get_import_group_file(const String &p_path) {
	String path = _path_remap(p_path);

	String local_path;
	if (path.is_rel_path())
		local_path = "res://" + path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(path);

	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(local_path))
			continue;
		/*
		if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
			continue;
		*/

		return loader[i]->get_import_group_file(p_path);
	}

	return String(); //not found
}
Exemplo n.º 5
0
Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {

	String path = _path_remap(p_path);

	String local_path;
	if (path.is_rel_path())
		local_path = "res://" + path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(path);

	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(local_path))
			continue;
		/*
		if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
			continue;
		*/

		return loader[i]->rename_dependencies(local_path, p_map);
	}

	return OK; // ??
}
Exemplo n.º 6
0
String ResourceLoader::path_remap(const String &p_path) {
	return _path_remap(p_path);
}
Exemplo n.º 7
0
Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {

	if (r_error)
		*r_error = ERR_CANT_OPEN;

	String local_path;
	if (p_path.is_rel_path())
		local_path = "res://" + p_path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(p_path);

	if (!p_no_cache) {

		bool success = _add_to_loading_map(local_path);
		if (!success) {
			ERR_EXPLAIN("Resource: '" + local_path + "' is already being loaded. Cyclic reference?");
			ERR_FAIL_V(RES());
		}

		if (ResourceCache::has(local_path)) {

			print_verbose("Loading resource: " + local_path + " (cached)");
			Ref<Resource> res_cached = ResourceCache::get(local_path);
			Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));

			ril->resource = res_cached;
			ril->path_loading = local_path;
			ril->path_loading_thread = Thread::get_caller_id();
			return ril;
		}
	}

	bool xl_remapped = false;
	String path = _path_remap(local_path, &xl_remapped);
	if (path == "") {
		if (!p_no_cache) {
			_remove_from_loading_map(local_path);
		}
		ERR_EXPLAIN("Remapping '" + local_path + "'failed.");
		ERR_FAIL_V(RES());
	}

	print_verbose("Loading resource: " + path);

	bool found = false;
	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(path, p_type_hint))
			continue;
		found = true;
		Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, local_path, r_error);
		if (ril.is_null())
			continue;
		if (!p_no_cache) {
			ril->set_local_path(local_path);
			ril->path_loading = local_path;
			ril->path_loading_thread = Thread::get_caller_id();
		}

		if (xl_remapped)
			ril->set_translation_remapped(true);

		return ril;
	}

	if (!p_no_cache) {
		_remove_from_loading_map(local_path);
	}

	if (found) {
		ERR_EXPLAIN("Failed loading resource: " + path);
	} else {
		ERR_EXPLAIN("No loader found for resource: " + path);
	}
	ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
	return Ref<ResourceInteractiveLoader>();
}
Exemplo n.º 8
0
RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {

	if (r_error)
		*r_error = ERR_CANT_OPEN;

	String local_path;
	if (p_path.is_rel_path())
		local_path = "res://" + p_path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(p_path);

	if (!p_no_cache) {

		{
			bool success = _add_to_loading_map(local_path);
			if (!success) {
				ERR_EXPLAIN("Resource: '" + local_path + "' is already being loaded. Cyclic reference?");
				ERR_FAIL_V(RES());
			}
		}

		//lock first if possible
		if (ResourceCache::lock) {
			ResourceCache::lock->read_lock();
		}

		//get ptr
		Resource **rptr = ResourceCache::resources.getptr(local_path);

		if (rptr) {
			RES res(*rptr);
			//it is possible this resource was just freed in a thread. If so, this referencing will not work and resource is considered not cached
			if (res.is_valid()) {
				//referencing is fine
				if (r_error)
					*r_error = OK;
				if (ResourceCache::lock) {
					ResourceCache::lock->read_unlock();
				}
				_remove_from_loading_map(local_path);
				return res;
			}
		}
		if (ResourceCache::lock) {
			ResourceCache::lock->read_unlock();
		}
	}

	bool xl_remapped = false;
	String path = _path_remap(local_path, &xl_remapped);

	if (path == "") {
		if (!p_no_cache) {
			_remove_from_loading_map(local_path);
		}
		ERR_EXPLAIN("Remapping '" + local_path + "'failed.");
		ERR_FAIL_V(RES());
	}

	print_verbose("Loading resource: " + path);
	RES res = _load(path, local_path, p_type_hint, p_no_cache, r_error);

	if (res.is_null()) {
		if (!p_no_cache) {
			_remove_from_loading_map(local_path);
		}
		return RES();
	}
	if (!p_no_cache)
		res->set_path(local_path);

	if (xl_remapped)
		res->set_as_translation_remapped(true);

#ifdef TOOLS_ENABLED

	res->set_edited(false);
	if (timestamp_on_load) {
		uint64_t mt = FileAccess::get_modified_time(path);
		//printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
		res->set_last_modified_time(mt);
	}
#endif

	if (!p_no_cache) {
		_remove_from_loading_map(local_path);
	}

	if (_loaded_callback) {
		_loaded_callback(res, p_path);
	}

	return res;
}
Exemplo n.º 9
0
RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {

	if (r_error)
		*r_error = ERR_CANT_OPEN;

	String local_path;
	if (p_path.is_rel_path())
		local_path = "res://" + p_path;
	else
		local_path = ProjectSettings::get_singleton()->localize_path(p_path);

	bool xl_remapped = false;
	String path = _path_remap(local_path, &xl_remapped);

	ERR_FAIL_COND_V(path == "", RES());

	if (!p_no_cache && ResourceCache::has(path)) {

		if (OS::get_singleton()->is_stdout_verbose())
			print_line("load resource: " + path + " (cached)");

		return RES(ResourceCache::get(path));
	}

	if (OS::get_singleton()->is_stdout_verbose())
		print_line("load resource: " + path);
	bool found = false;

	// Try all loaders and pick the first match for the type hint
	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(path, p_type_hint)) {
			continue;
		}
		found = true;
		RES res = loader[i]->load(path, path, r_error);
		if (res.is_null()) {
			continue;
		}
		if (!p_no_cache)
			res->set_path(local_path);

		if (xl_remapped)
			res->set_as_translation_remapped(true);

#ifdef TOOLS_ENABLED

		res->set_edited(false);
		if (timestamp_on_load) {
			uint64_t mt = FileAccess::get_modified_time(path);
			//printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
			res->set_last_modified_time(mt);
		}
#endif

		return res;
	}

	if (found) {
		ERR_EXPLAIN("Failed loading resource: " + path);
	} else {
		ERR_EXPLAIN("No loader found for resource: " + path);
	}
	ERR_FAIL_V(RES());
	return RES();
}