2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_loader.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef RESOURCE_LOADER_H
|
|
|
|
#define RESOURCE_LOADER_H
|
|
|
|
|
2019-02-16 20:38:09 +00:00
|
|
|
#include "core/os/thread.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/resource.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
|
|
|
@author Juan Linietsky <reduzio@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ResourceInteractiveLoader : public Reference {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(ResourceInteractiveLoader, Reference);
|
2019-01-27 22:24:55 +00:00
|
|
|
friend class ResourceLoader;
|
|
|
|
String path_loading;
|
2019-02-16 20:38:09 +00:00
|
|
|
Thread::ID path_loading_thread;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
protected:
|
2014-02-10 01:10:30 +00:00
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
|
|
|
virtual void set_local_path(const String &p_local_path) = 0;
|
|
|
|
virtual Ref<Resource> get_resource() = 0;
|
|
|
|
virtual Error poll() = 0;
|
|
|
|
virtual int get_stage() const = 0;
|
|
|
|
virtual int get_stage_count() const = 0;
|
2017-06-28 20:00:18 +00:00
|
|
|
virtual void set_translation_remapped(bool p_remapped) = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
virtual Error wait();
|
|
|
|
|
|
|
|
ResourceInteractiveLoader() {}
|
2019-01-27 22:24:55 +00:00
|
|
|
~ResourceInteractiveLoader();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
class ResourceFormatLoader : public Reference {
|
|
|
|
|
|
|
|
GDCLASS(ResourceFormatLoader, Reference)
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
public:
|
2017-08-31 21:57:03 +00:00
|
|
|
virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
|
2017-03-05 15:44:50 +00:00
|
|
|
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
|
2018-08-10 18:57:43 +00:00
|
|
|
virtual bool exists(const String &p_path) const;
|
2018-06-11 00:59:53 +00:00
|
|
|
virtual void get_recognized_extensions(List<String> *p_extensions) const;
|
2017-03-05 15:44:50 +00:00
|
|
|
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
|
|
|
|
virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
|
2018-06-11 00:59:53 +00:00
|
|
|
virtual bool handles_type(const String &p_type) const;
|
|
|
|
virtual String get_resource_type(const String &p_path) const;
|
2017-03-05 15:44:50 +00:00
|
|
|
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
|
2018-06-11 00:59:53 +00:00
|
|
|
virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
|
2017-08-29 22:50:58 +00:00
|
|
|
virtual bool is_import_valid(const String &p_path) const { return true; }
|
2017-09-20 23:59:19 +00:00
|
|
|
virtual int get_import_order(const String &p_path) const { return 0; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
virtual ~ResourceFormatLoader() {}
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
typedef void (*ResourceLoadErrorNotify)(void *p_ud, const String &p_text);
|
|
|
|
typedef void (*DependencyErrorNotify)(void *p_ud, const String &p_loading, const String &p_which, const String &p_type);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-10-05 02:00:02 +00:00
|
|
|
typedef Error (*ResourceLoaderImport)(const String &p_path);
|
2018-10-29 19:36:31 +00:00
|
|
|
typedef void (*ResourceLoadedCallback)(RES p_resource, const String &p_path);
|
2018-10-05 02:00:02 +00:00
|
|
|
|
2016-03-08 23:00:52 +00:00
|
|
|
class ResourceLoader {
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
enum {
|
2017-03-05 15:44:50 +00:00
|
|
|
MAX_LOADERS = 64
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
|
2014-02-10 01:10:30 +00:00
|
|
|
static int loader_count;
|
|
|
|
static bool timestamp_on_load;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static void *err_notify_ud;
|
2014-02-10 01:10:30 +00:00
|
|
|
static ResourceLoadErrorNotify err_notify;
|
2017-03-05 15:44:50 +00:00
|
|
|
static void *dep_err_notify_ud;
|
2015-08-23 23:15:56 +00:00
|
|
|
static DependencyErrorNotify dep_err_notify;
|
2014-02-10 01:10:30 +00:00
|
|
|
static bool abort_on_missing_resource;
|
2017-06-28 20:00:18 +00:00
|
|
|
static HashMap<String, Vector<String> > translation_remaps;
|
2017-12-14 18:33:54 +00:00
|
|
|
static HashMap<String, String> path_remaps;
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
static String _path_remap(const String &p_path, bool *r_translation_remapped = NULL);
|
|
|
|
friend class Resource;
|
|
|
|
|
|
|
|
static SelfList<Resource>::List remapped_list;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-31 21:57:03 +00:00
|
|
|
friend class ResourceFormatImporter;
|
2019-01-27 22:24:55 +00:00
|
|
|
friend class ResourceInteractiveLoader;
|
2017-08-31 21:57:03 +00:00
|
|
|
//internal load function
|
|
|
|
static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error);
|
|
|
|
|
2018-10-29 19:36:31 +00:00
|
|
|
static ResourceLoadedCallback _loaded_callback;
|
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path);
|
2019-01-27 22:24:55 +00:00
|
|
|
static Mutex *loading_map_mutex;
|
2019-02-16 20:38:09 +00:00
|
|
|
|
|
|
|
//used to track paths being loaded in a thread, avoids cyclic recursion
|
|
|
|
struct LoadingMapKey {
|
|
|
|
String path;
|
|
|
|
Thread::ID thread;
|
|
|
|
bool operator==(const LoadingMapKey &p_key) const {
|
|
|
|
return (thread == p_key.thread && path == p_key.path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct LoadingMapKeyHasher {
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ uint32_t hash(const LoadingMapKey &p_key) { return p_key.path.hash() + HashMapHasherDefault::hash(p_key.thread); }
|
|
|
|
};
|
|
|
|
|
|
|
|
static HashMap<LoadingMapKey, int, LoadingMapKeyHasher> loading_map;
|
2019-01-27 22:24:55 +00:00
|
|
|
|
|
|
|
static bool _add_to_loading_map(const String &p_path);
|
|
|
|
static void _remove_from_loading_map(const String &p_path);
|
2019-02-16 20:38:09 +00:00
|
|
|
static void _remove_from_loading_map_and_thread(const String &p_path, Thread::ID p_thread);
|
2018-06-11 00:59:53 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
|
|
|
|
static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
|
2018-08-10 18:57:43 +00:00
|
|
|
static bool exists(const String &p_path, const String &p_type_hint = "");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
|
2018-06-11 00:59:53 +00:00
|
|
|
static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
|
|
|
|
static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
|
2014-02-10 01:10:30 +00:00
|
|
|
static String get_resource_type(const String &p_path);
|
2017-03-05 15:44:50 +00:00
|
|
|
static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
|
|
|
|
static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
|
2017-08-29 22:50:58 +00:00
|
|
|
static bool is_import_valid(const String &p_path);
|
2017-09-20 23:59:19 +00:00
|
|
|
static int get_import_order(const String &p_path);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
|
2018-12-02 16:24:23 +00:00
|
|
|
static bool get_timestamp_on_load() { return timestamp_on_load; }
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
static void notify_load_error(const String &p_err) {
|
|
|
|
if (err_notify) err_notify(err_notify_ud, p_err);
|
|
|
|
}
|
|
|
|
static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
|
|
|
|
err_notify = p_err_notify;
|
|
|
|
err_notify_ud = p_ud;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
|
|
|
|
if (dep_err_notify) dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
|
|
|
|
}
|
|
|
|
static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
|
|
|
|
dep_err_notify = p_err_notify;
|
|
|
|
dep_err_notify_ud = p_ud;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
|
2014-02-10 01:10:30 +00:00
|
|
|
static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
static String path_remap(const String &p_path);
|
|
|
|
static String import_remap(const String &p_path);
|
|
|
|
|
2017-12-14 18:33:54 +00:00
|
|
|
static void load_path_remaps();
|
|
|
|
static void clear_path_remaps();
|
|
|
|
|
2017-06-28 20:00:18 +00:00
|
|
|
static void reload_translation_remaps();
|
|
|
|
static void load_translation_remaps();
|
|
|
|
static void clear_translation_remaps();
|
2018-10-05 02:00:02 +00:00
|
|
|
|
2018-10-29 19:36:31 +00:00
|
|
|
static void set_load_callback(ResourceLoadedCallback p_callback);
|
2018-10-05 02:00:02 +00:00
|
|
|
static ResourceLoaderImport import;
|
2018-06-11 00:59:53 +00:00
|
|
|
|
|
|
|
static bool add_custom_resource_format_loader(String script_path);
|
|
|
|
static void remove_custom_resource_format_loader(String script_path);
|
|
|
|
static void add_custom_loaders();
|
|
|
|
static void remove_custom_loaders();
|
2019-01-27 22:24:55 +00:00
|
|
|
|
|
|
|
static void initialize();
|
|
|
|
static void finalize();
|
2014-02-10 01:10:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|