1/**************************************************************************/
2/* resource_preloader.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "resource_preloader.h"
32#include "core/templates/rb_set.h"
33void ResourcePreloader::_set_resources(const Array &p_data) {
34 resources.clear();
35
36 ERR_FAIL_COND(p_data.size() != 2);
37 Vector<String> names = p_data[0];
38 Array resdata = p_data[1];
39
40 ERR_FAIL_COND(names.size() != resdata.size());
41
42 for (int i = 0; i < resdata.size(); i++) {
43 String name = names[i];
44 Ref<Resource> resource = resdata[i];
45 ERR_CONTINUE(!resource.is_valid());
46 resources[name] = resource;
47
48 //add_resource(name,resource);
49 }
50}
51
52Array ResourcePreloader::_get_resources() const {
53 Vector<String> names;
54 Array arr;
55 arr.resize(resources.size());
56 names.resize(resources.size());
57
58 RBSet<String> sorted_names;
59
60 for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
61 sorted_names.insert(E.key);
62 }
63
64 int i = 0;
65 for (const String &E : sorted_names) {
66 names.set(i, E);
67 arr[i] = resources[E];
68 i++;
69 }
70
71 Array res;
72 res.push_back(names);
73 res.push_back(arr);
74 return res;
75}
76
77void ResourcePreloader::add_resource(const StringName &p_name, const Ref<Resource> &p_resource) {
78 ERR_FAIL_COND(p_resource.is_null());
79 if (resources.has(p_name)) {
80 StringName new_name;
81 int idx = 2;
82
83 while (true) {
84 new_name = p_name.operator String() + " " + itos(idx);
85 if (resources.has(new_name)) {
86 idx++;
87 continue;
88 }
89
90 break;
91 }
92
93 add_resource(new_name, p_resource);
94 } else {
95 resources[p_name] = p_resource;
96 }
97}
98
99void ResourcePreloader::remove_resource(const StringName &p_name) {
100 ERR_FAIL_COND(!resources.has(p_name));
101 resources.erase(p_name);
102}
103
104void ResourcePreloader::rename_resource(const StringName &p_from_name, const StringName &p_to_name) {
105 ERR_FAIL_COND(!resources.has(p_from_name));
106
107 Ref<Resource> res = resources[p_from_name];
108
109 resources.erase(p_from_name);
110 add_resource(p_to_name, res);
111}
112
113bool ResourcePreloader::has_resource(const StringName &p_name) const {
114 return resources.has(p_name);
115}
116
117Ref<Resource> ResourcePreloader::get_resource(const StringName &p_name) const {
118 ERR_FAIL_COND_V(!resources.has(p_name), Ref<Resource>());
119 return resources[p_name];
120}
121
122Vector<String> ResourcePreloader::_get_resource_list() const {
123 Vector<String> res;
124 res.resize(resources.size());
125 int i = 0;
126 for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
127 res.set(i, E.key);
128 i++;
129 }
130
131 return res;
132}
133
134void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
135 for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
136 p_list->push_back(E.key);
137 }
138}
139
140void ResourcePreloader::_bind_methods() {
141 ClassDB::bind_method(D_METHOD("_set_resources", "resources"), &ResourcePreloader::_set_resources);
142 ClassDB::bind_method(D_METHOD("_get_resources"), &ResourcePreloader::_get_resources);
143
144 ClassDB::bind_method(D_METHOD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
145 ClassDB::bind_method(D_METHOD("remove_resource", "name"), &ResourcePreloader::remove_resource);
146 ClassDB::bind_method(D_METHOD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
147 ClassDB::bind_method(D_METHOD("has_resource", "name"), &ResourcePreloader::has_resource);
148 ClassDB::bind_method(D_METHOD("get_resource", "name"), &ResourcePreloader::get_resource);
149 ClassDB::bind_method(D_METHOD("get_resource_list"), &ResourcePreloader::_get_resource_list);
150
151 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_resources", "_get_resources");
152}
153
154ResourcePreloader::ResourcePreloader() {
155}
156