Debian Codegraph @

scene/main/resource_preloader.cpp

raw symbols SCIP
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 return Array{ names, arr };
71 }
72
73 void ResourcePreloader::add_resource(const StringName &p_name, const Ref<Resource> &p_resource) {
74 ERR_FAIL_COND(p_resource.is_null());
75 if (resources.has(p_name)) {
76 StringName new_name;
77 int idx = 2;
78
79 while (true) {
80 new_name = p_name.operator String() + " " + itos(idx);
81 if (resources.has(new_name)) {
82 idx++;
83 continue;
84 }
85
86 break;
87 }
88
89 add_resource(new_name, p_resource);
90 } else {
91 resources[p_name] = p_resource;
92 }
93 }
94
95 void ResourcePreloader::remove_resource(const StringName &p_name) {
96 ERR_FAIL_COND(!resources.has(p_name));
97 resources.erase(p_name);
98 }
99
100 void ResourcePreloader::rename_resource(const StringName &p_from_name, const StringName &p_to_name) {
101 ERR_FAIL_COND(!resources.has(p_from_name));
102
103 Ref<Resource> res = resources[p_from_name];
104
105 resources.erase(p_from_name);
106 add_resource(p_to_name, res);
107 }
108
109 bool ResourcePreloader::has_resource(const StringName &p_name) const {
110 return resources.has(p_name);
111 }
112
113 Ref<Resource> ResourcePreloader::get_resource(const StringName &p_name) const {
114 ERR_FAIL_COND_V(!resources.has(p_name), Ref<Resource>());
115 return resources[p_name];
116 }
117
118 Vector<String> ResourcePreloader::_get_resource_list() const {
119 Vector<String> res;
120 res.resize(resources.size());
121 int i = 0;
122 for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
123 res.set(i, E.key);
124 i++;
125 }
126
127 return res;
128 }
129
130 void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
131 for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
132 p_list->push_back(E.key);
133 }
134 }
135
136 void ResourcePreloader::_bind_methods() {
137 ClassDB::bind_method(D_METHOD("_set_resources", "resources"), &ResourcePreloader::_set_resources);
138 ClassDB::bind_method(D_METHOD("_get_resources"), &ResourcePreloader::_get_resources);
139
140 ClassDB::bind_method(D_METHOD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
141 ClassDB::bind_method(D_METHOD("remove_resource", "name"), &ResourcePreloader::remove_resource);
142 ClassDB::bind_method(D_METHOD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
143 ClassDB::bind_method(D_METHOD("has_resource", "name"), &ResourcePreloader::has_resource);
144 ClassDB::bind_method(D_METHOD("get_resource", "name"), &ResourcePreloader::get_resource);
145 ClassDB::bind_method(D_METHOD("get_resource_list"), &ResourcePreloader::_get_resource_list);
146
147 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_resources", "_get_resources");
148 }
149
150 ResourcePreloader::ResourcePreloader() {
151 }