Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/resource_preloader.cpp
9896 views
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"
33
void 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
Ref<Resource> resource = resdata[i];
44
ERR_CONTINUE(resource.is_null());
45
resources[names[i]] = resource;
46
47
//add_resource(names[i],resource);
48
}
49
}
50
51
Array ResourcePreloader::_get_resources() const {
52
Vector<String> names;
53
Array arr;
54
arr.resize(resources.size());
55
names.resize(resources.size());
56
57
RBSet<String> sorted_names;
58
59
for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
60
sorted_names.insert(E.key);
61
}
62
63
int i = 0;
64
for (const String &E : sorted_names) {
65
names.set(i, E);
66
arr[i] = resources[E];
67
i++;
68
}
69
70
Array res = { names, arr };
71
return res;
72
}
73
74
void ResourcePreloader::add_resource(const StringName &p_name, const Ref<Resource> &p_resource) {
75
ERR_FAIL_COND(p_resource.is_null());
76
if (resources.has(p_name)) {
77
StringName new_name;
78
int idx = 2;
79
80
while (true) {
81
new_name = p_name.operator String() + " " + itos(idx);
82
if (resources.has(new_name)) {
83
idx++;
84
continue;
85
}
86
87
break;
88
}
89
90
add_resource(new_name, p_resource);
91
} else {
92
resources[p_name] = p_resource;
93
}
94
}
95
96
void ResourcePreloader::remove_resource(const StringName &p_name) {
97
ERR_FAIL_COND(!resources.has(p_name));
98
resources.erase(p_name);
99
}
100
101
void ResourcePreloader::rename_resource(const StringName &p_from_name, const StringName &p_to_name) {
102
ERR_FAIL_COND(!resources.has(p_from_name));
103
104
Ref<Resource> res = resources[p_from_name];
105
106
resources.erase(p_from_name);
107
add_resource(p_to_name, res);
108
}
109
110
bool ResourcePreloader::has_resource(const StringName &p_name) const {
111
return resources.has(p_name);
112
}
113
114
Ref<Resource> ResourcePreloader::get_resource(const StringName &p_name) const {
115
ERR_FAIL_COND_V(!resources.has(p_name), Ref<Resource>());
116
return resources[p_name];
117
}
118
119
Vector<String> ResourcePreloader::_get_resource_list() const {
120
Vector<String> res;
121
res.resize(resources.size());
122
int i = 0;
123
for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
124
res.set(i, E.key);
125
i++;
126
}
127
128
return res;
129
}
130
131
void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
132
for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
133
p_list->push_back(E.key);
134
}
135
}
136
137
void ResourcePreloader::_bind_methods() {
138
ClassDB::bind_method(D_METHOD("_set_resources", "resources"), &ResourcePreloader::_set_resources);
139
ClassDB::bind_method(D_METHOD("_get_resources"), &ResourcePreloader::_get_resources);
140
141
ClassDB::bind_method(D_METHOD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
142
ClassDB::bind_method(D_METHOD("remove_resource", "name"), &ResourcePreloader::remove_resource);
143
ClassDB::bind_method(D_METHOD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
144
ClassDB::bind_method(D_METHOD("has_resource", "name"), &ResourcePreloader::has_resource);
145
ClassDB::bind_method(D_METHOD("get_resource", "name"), &ResourcePreloader::get_resource);
146
ClassDB::bind_method(D_METHOD("get_resource_list"), &ResourcePreloader::_get_resource_list);
147
148
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_resources", "_get_resources");
149
}
150
151
ResourcePreloader::ResourcePreloader() {
152
}
153
154