1 /** 2 Singleton used to load resource files. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.resourceloader; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.resource; 24 import godot.resourceinteractiveloader; 25 /** 26 Singleton used to load resource files. 27 28 Singleton used to load resource files from the filesystem. 29 It uses the many $(D ResourceFormatLoader) classes registered in the engine (either built-in or from a plugin) to load files into memory and convert them to a format that can be used by the engine. 30 */ 31 @GodotBaseClass struct ResourceLoaderSingleton 32 { 33 package(godot) enum string _GODOT_internal_name = "_ResourceLoader"; 34 public: 35 @nogc nothrow: 36 union { /** */ godot_object _godot_object; /** */ GodotObject _GODOT_base; } 37 alias _GODOT_base this; 38 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 39 package(godot) __gshared bool _classBindingInitialized = false; 40 package(godot) static struct GDNativeClassBinding 41 { 42 __gshared: 43 godot_object _singleton; 44 immutable char* _singletonName = "ResourceLoader"; 45 @GodotName("exists") GodotMethod!(bool, String, String) exists; 46 @GodotName("get_dependencies") GodotMethod!(PoolStringArray, String) getDependencies; 47 @GodotName("get_recognized_extensions_for_type") GodotMethod!(PoolStringArray, String) getRecognizedExtensionsForType; 48 @GodotName("has") GodotMethod!(bool, String) has; 49 @GodotName("has_cached") GodotMethod!(bool, String) hasCached; 50 @GodotName("load") GodotMethod!(Resource, String, String, bool) load; 51 @GodotName("load_interactive") GodotMethod!(ResourceInteractiveLoader, String, String) loadInteractive; 52 @GodotName("set_abort_on_missing_resources") GodotMethod!(void, bool) setAbortOnMissingResources; 53 } 54 /// 55 pragma(inline, true) bool opEquals(in ResourceLoaderSingleton other) const 56 { return _godot_object.ptr is other._godot_object.ptr; } 57 /// 58 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 59 { _godot_object.ptr = n; return null; } 60 /// 61 pragma(inline, true) bool opEquals(typeof(null) n) const 62 { return _godot_object.ptr is n; } 63 /// 64 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 65 mixin baseCasts; 66 /// Construct a new instance of ResourceLoaderSingleton. 67 /// Note: use `memnew!ResourceLoaderSingleton` instead. 68 static ResourceLoaderSingleton _new() 69 { 70 static godot_class_constructor constructor; 71 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_ResourceLoader"); 72 if(constructor is null) return typeof(this).init; 73 return cast(ResourceLoaderSingleton)(constructor()); 74 } 75 @disable new(size_t s); 76 /** 77 Returns whether a recognized resource exists for the given `path`. 78 An optional `type_hint` can be used to further specify the $(D Resource) type that should be handled by the $(D ResourceFormatLoader). 79 */ 80 bool exists(in String path, in String type_hint = gs!"") 81 { 82 checkClassBinding!(typeof(this))(); 83 return ptrcall!(bool)(GDNativeClassBinding.exists, _godot_object, path, type_hint); 84 } 85 /** 86 Returns the dependencies for the resource at the given `path`. 87 */ 88 PoolStringArray getDependencies(in String path) 89 { 90 checkClassBinding!(typeof(this))(); 91 return ptrcall!(PoolStringArray)(GDNativeClassBinding.getDependencies, _godot_object, path); 92 } 93 /** 94 Returns the list of recognized extensions for a resource type. 95 */ 96 PoolStringArray getRecognizedExtensionsForType(in String type) 97 { 98 checkClassBinding!(typeof(this))(); 99 return ptrcall!(PoolStringArray)(GDNativeClassBinding.getRecognizedExtensionsForType, _godot_object, type); 100 } 101 /** 102 $(I Deprecated method.) Use $(D hasCached) or $(D exists) instead. 103 */ 104 bool has(in String path) 105 { 106 checkClassBinding!(typeof(this))(); 107 return ptrcall!(bool)(GDNativeClassBinding.has, _godot_object, path); 108 } 109 /** 110 Returns whether a cached resource is available for the given `path`. 111 Once a resource has been loaded by the engine, it is cached in memory for faster access, and future calls to the $(D load) or $(D loadInteractive) methods will use the cached version. The cached resource can be overridden by using $(D Resource.takeOverPath) on a new resource for that same path. 112 */ 113 bool hasCached(in String path) 114 { 115 checkClassBinding!(typeof(this))(); 116 return ptrcall!(bool)(GDNativeClassBinding.hasCached, _godot_object, path); 117 } 118 /** 119 Loads a resource at the given `path`, caching the result for further access. 120 The registered $(D ResourceFormatLoader)s are queried sequentially to find the first one which can handle the file's extension, and then attempt loading. If loading fails, the remaining ResourceFormatLoaders are also attempted. 121 An optional `type_hint` can be used to further specify the $(D Resource) type that should be handled by the $(D ResourceFormatLoader). Anything that inherits from $(D Resource) can be used as a type hint, for example $(D Image). 122 If `no_cache` is `true`, the resource cache will be bypassed and the resource will be loaded anew. Otherwise, the cached resource will be returned if it exists. 123 Returns an empty resource if no $(D ResourceFormatLoader) could handle the file. 124 GDScript has a simplified $(D @GDScript.load) built-in method which can be used in most situations, leaving the use of $(D ResourceLoader) for more advanced scenarios. 125 */ 126 Ref!Resource load(in String path, in String type_hint = gs!"", in bool no_cache = false) 127 { 128 checkClassBinding!(typeof(this))(); 129 return ptrcall!(Resource)(GDNativeClassBinding.load, _godot_object, path, type_hint, no_cache); 130 } 131 /** 132 Starts loading a resource interactively. The returned $(D ResourceInteractiveLoader) object allows to load with high granularity, calling its $(D ResourceInteractiveLoader.poll) method successively to load chunks. 133 An optional `type_hint` can be used to further specify the $(D Resource) type that should be handled by the $(D ResourceFormatLoader). Anything that inherits from $(D Resource) can be used as a type hint, for example $(D Image). 134 */ 135 Ref!ResourceInteractiveLoader loadInteractive(in String path, in String type_hint = gs!"") 136 { 137 checkClassBinding!(typeof(this))(); 138 return ptrcall!(ResourceInteractiveLoader)(GDNativeClassBinding.loadInteractive, _godot_object, path, type_hint); 139 } 140 /** 141 Changes the behavior on missing sub-resources. The default behavior is to abort loading. 142 */ 143 void setAbortOnMissingResources(in bool abort) 144 { 145 checkClassBinding!(typeof(this))(); 146 ptrcall!(void)(GDNativeClassBinding.setAbortOnMissingResources, _godot_object, abort); 147 } 148 } 149 /// Returns: the ResourceLoaderSingleton 150 @property @nogc nothrow pragma(inline, true) 151 ResourceLoaderSingleton ResourceLoader() 152 { 153 checkClassBinding!ResourceLoaderSingleton(); 154 return ResourceLoaderSingleton(ResourceLoaderSingleton.GDNativeClassBinding._singleton); 155 }