1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
/*************************************************************************/
/* resource.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#include "resource.h"
#include "core/core_string_names.h"
#include "core/io/resource_loader.h"
#include "core/os/file_access.h"
#include "core/os/os.h"
#include "core/script_language.h"
#include "scene/main/node.h" //only so casting works
#include <stdio.h>
void Resource::emit_changed() {
emit_signal(CoreStringNames::get_singleton()->changed);
}
void Resource::_resource_path_changed() {
}
void Resource::set_path(const String &p_path, bool p_take_over) {
if (path_cache == p_path)
return;
if (path_cache != "") {
ResourceCache::lock->write_lock();
ResourceCache::resources.erase(path_cache);
ResourceCache::lock->write_unlock();
}
path_cache = "";
ResourceCache::lock->read_lock();
bool has_path = ResourceCache::resources.has(p_path);
ResourceCache::lock->read_unlock();
if (has_path) {
if (p_take_over) {
ResourceCache::lock->write_lock();
Resource **res = ResourceCache::resources.getptr(p_path);
if (res) {
(*res)->set_name("");
}
ResourceCache::lock->write_unlock();
} else {
ResourceCache::lock->read_lock();
bool exists = ResourceCache::resources.has(p_path);
ResourceCache::lock->read_unlock();
ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion).");
}
}
path_cache = p_path;
if (path_cache != "") {
ResourceCache::lock->write_lock();
ResourceCache::resources[path_cache] = this;
ResourceCache::lock->write_unlock();
}
_change_notify("resource_path");
_resource_path_changed();
}
String Resource::get_path() const {
return path_cache;
}
void Resource::set_subindex(int p_sub_index) {
subindex = p_sub_index;
}
int Resource::get_subindex() const {
return subindex;
}
void Resource::set_name(const String &p_name) {
name = p_name;
_change_notify("resource_name");
}
String Resource::get_name() const {
return name;
}
bool Resource::editor_can_reload_from_file() {
return true; //by default yes
}
void Resource::reload_from_file() {
String path = get_path();
if (!path.is_resource_file())
return;
Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), true);
if (!s.is_valid())
return;
List<PropertyInfo> pi;
s->get_property_list(&pi);
for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
continue;
if (E->get().name == "resource_path")
continue; //do not change path
set(E->get().name, s->get(E->get().name));
}
}
Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {
List<PropertyInfo> plist;
get_property_list(&plist);
Resource *r = Object::cast_to<Resource>(ClassDB::instance(get_class()));
ERR_FAIL_COND_V(!r, Ref<Resource>());
r->local_scene = p_for_scene;
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
continue;
Variant p = get(E->get().name);
if (p.get_type() == Variant::OBJECT) {
RES sr = p;
if (sr.is_valid()) {
if (sr->is_local_to_scene()) {
if (remap_cache.has(sr)) {
p = remap_cache[sr];
} else {
RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
p = dupe;
remap_cache[sr] = dupe;
}
}
}
}
r->set(E->get().name, p);
}
RES res = Ref<Resource>(r);
return res;
}
void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {
List<PropertyInfo> plist;
get_property_list(&plist);
local_scene = p_for_scene;
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
continue;
Variant p = get(E->get().name);
if (p.get_type() == Variant::OBJECT) {
RES sr = p;
if (sr.is_valid()) {
if (sr->is_local_to_scene()) {
if (!remap_cache.has(sr)) {
sr->configure_for_local_scene(p_for_scene, remap_cache);
remap_cache[sr] = sr;
}
}
}
}
}
}
Ref<Resource> Resource::duplicate(bool p_subresources) const {
List<PropertyInfo> plist;
get_property_list(&plist);
Resource *r = (Resource *)ClassDB::instance(get_class());
ERR_FAIL_COND_V(!r, Ref<Resource>());
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
continue;
Variant p = get(E->get().name);
if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
r->set(E->get().name, p.duplicate(p_subresources));
} else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
RES sr = p;
if (sr.is_valid()) {
r->set(E->get().name, sr->duplicate(p_subresources));
}
} else {
r->set(E->get().name, p);
}
}
return Ref<Resource>(r);
}
void Resource::_set_path(const String &p_path) {
set_path(p_path, false);
}
void Resource::_take_over_path(const String &p_path) {
set_path(p_path, true);
}
RID Resource::get_rid() const {
return RID();
}
void Resource::register_owner(Object *p_owner) {
owners.insert(p_owner->get_instance_id());
}
void Resource::unregister_owner(Object *p_owner) {
owners.erase(p_owner->get_instance_id());
}
void Resource::notify_change_to_owners() {
for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
Object *obj = ObjectDB::get_instance(E->get());
ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
//TODO store string
obj->call("resource_changed", RES(this));
}
}
#ifdef TOOLS_ENABLED
uint32_t Resource::hash_edited_version() const {
uint32_t hash = hash_djb2_one_32(get_edited_version());
List<PropertyInfo> plist;
get_property_list(&plist);
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
RES res = get(E->get().name);
if (res.is_valid()) {
hash = hash_djb2_one_32(res->hash_edited_version(), hash);
}
}
}
return hash;
}
#endif
void Resource::set_local_to_scene(bool p_enable) {
local_to_scene = p_enable;
}
bool Resource::is_local_to_scene() const {
return local_to_scene;
}
Node *Resource::get_local_scene() const {
if (local_scene)
return local_scene;
if (_get_local_scene_func) {
return _get_local_scene_func();
}
return NULL;
}
void Resource::setup_local_to_scene() {
if (get_script_instance())
get_script_instance()->call("_setup_local_to_scene");
}
Node *(*Resource::_get_local_scene_func)() = NULL;
void Resource::set_as_translation_remapped(bool p_remapped) {
if (remapped_list.in_list() == p_remapped)
return;
if (ResourceCache::lock) {
ResourceCache::lock->write_lock();
}
if (p_remapped) {
ResourceLoader::remapped_list.add(&remapped_list);
} else {
ResourceLoader::remapped_list.remove(&remapped_list);
}
if (ResourceCache::lock) {
ResourceCache::lock->write_unlock();
}
}
bool Resource::is_translation_remapped() const {
return remapped_list.in_list();
}
#ifdef TOOLS_ENABLED
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
void Resource::set_id_for_path(const String &p_path, int p_id) {
if (p_id == -1) {
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->write_lock();
}
ResourceCache::resource_path_cache[p_path].erase(get_path());
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->write_unlock();
}
} else {
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->write_lock();
}
ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->write_unlock();
}
}
}
int Resource::get_id_for_path(const String &p_path) const {
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->read_lock();
}
if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
int result = ResourceCache::resource_path_cache[p_path][get_path()];
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->read_unlock();
}
return result;
} else {
if (ResourceCache::path_cache_lock) {
ResourceCache::path_cache_lock->read_unlock();
}
return -1;
}
}
#endif
void Resource::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
ADD_SIGNAL(MethodInfo("changed"));
ADD_GROUP("Resource", "resource_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name");
BIND_VMETHOD(MethodInfo("_setup_local_to_scene"));
}
Resource::Resource() :
remapped_list(this) {
#ifdef TOOLS_ENABLED
last_modified_time = 0;
import_last_modified_time = 0;
#endif
subindex = 0;
local_to_scene = false;
local_scene = NULL;
}
Resource::~Resource() {
if (path_cache != "") {
ResourceCache::lock->write_lock();
ResourceCache::resources.erase(path_cache);
ResourceCache::lock->write_unlock();
}
if (owners.size()) {
WARN_PRINT("Resource is still owned.");
}
}
HashMap<String, Resource *> ResourceCache::resources;
#ifdef TOOLS_ENABLED
HashMap<String, HashMap<String, int> > ResourceCache::resource_path_cache;
#endif
RWLock *ResourceCache::lock = NULL;
#ifdef TOOLS_ENABLED
RWLock *ResourceCache::path_cache_lock = NULL;
#endif
void ResourceCache::setup() {
lock = RWLock::create();
#ifdef TOOLS_ENABLED
path_cache_lock = RWLock::create();
#endif
}
void ResourceCache::clear() {
if (resources.size()) {
ERR_PRINT("Resources still in use at exit (run with --verbose for details).");
if (OS::get_singleton()->is_stdout_verbose()) {
const String *K = nullptr;
while ((K = resources.next(K))) {
Resource *r = resources[*K];
print_line(vformat("Resource still in use: %s (%s)", *K, r->get_class()));
}
}
}
resources.clear();
memdelete(lock);
}
void ResourceCache::reload_externals() {
}
bool ResourceCache::has(const String &p_path) {
lock->read_lock();
bool b = resources.has(p_path);
lock->read_unlock();
return b;
}
Resource *ResourceCache::get(const String &p_path) {
lock->read_lock();
Resource **res = resources.getptr(p_path);
lock->read_unlock();
if (!res) {
return NULL;
}
return *res;
}
void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
lock->read_lock();
const String *K = NULL;
while ((K = resources.next(K))) {
Resource *r = resources[*K];
p_resources->push_back(Ref<Resource>(r));
}
lock->read_unlock();
}
int ResourceCache::get_cached_resource_count() {
lock->read_lock();
int rc = resources.size();
lock->read_unlock();
return rc;
}
void ResourceCache::dump(const char *p_file, bool p_short) {
#ifdef DEBUG_ENABLED
lock->read_lock();
Map<String, int> type_count;
FileAccess *f = NULL;
if (p_file) {
f = FileAccess::open(p_file, FileAccess::WRITE);
ERR_FAIL_COND_MSG(!f, "Cannot create file at path '" + String(p_file) + "'.");
}
const String *K = NULL;
while ((K = resources.next(K))) {
Resource *r = resources[*K];
if (!type_count.has(r->get_class())) {
type_count[r->get_class()] = 0;
}
type_count[r->get_class()]++;
if (!p_short) {
if (f)
f->store_line(r->get_class() + ": " + r->get_path());
}
}
for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
if (f)
f->store_line(E->key() + " count: " + itos(E->get()));
}
if (f) {
f->close();
memdelete(f);
}
lock->read_unlock();
#endif
}
|