| 444 | } |
| 445 | |
| 446 | String ResourceCompatLoader::resource_to_string(const String &p_path, bool p_skip_cr) { |
| 447 | ERR_FAIL_COND_V_MSG(p_path.is_empty(), "", "Path is empty"); |
| 448 | String orig_path; |
| 449 | String path = p_path; |
| 450 | if (!FileAccess::exists(path)) { |
| 451 | orig_path = path; |
| 452 | path = GDRESettings::get_singleton()->get_mapped_path(path); |
| 453 | if (!FileAccess::exists(path)) { |
| 454 | return "ERROR: File does not exist: " + path; |
| 455 | } |
| 456 | } |
| 457 | if (path.get_file().to_lower() == "engine.cfb" || path.get_file().to_lower() == "project.binary") { |
| 458 | return ProjectConfigLoader::get_project_settings_as_string(path); |
| 459 | } |
| 460 | String ext = path.get_extension().to_lower(); |
| 461 | if (ext == "tres" || ext == "tscn" || !handles_resource(path, "")) { |
| 462 | Error err; |
| 463 | Ref<FileAccess> f = FileAccess::open(path, FileAccess::READ, &err); |
| 464 | if (f.is_null() || err != OK) { |
| 465 | if (err == ERR_UNAUTHORIZED || err == ERR_FILE_CORRUPT) { |
| 466 | return "ERROR: Can't read encrypted file: " + path; |
| 467 | } |
| 468 | return "ERROR: Failed to open file: " + path; |
| 469 | } |
| 470 | auto buf = f->get_buffer(f->get_length()); |
| 471 | if (ext == "tres" || ext == "tscn" || gdre::detect_utf8(buf)) { |
| 472 | String str; |
| 473 | str.append_utf8((const char *)buf.ptr(), buf.size()); |
| 474 | return str; |
| 475 | } |
| 476 | return "ERROR: Failed to detect UTF-8 encoding for " + path; |
| 477 | } |
| 478 | |
| 479 | Error err = OK; |
| 480 | Ref<Resource> res = _load_for_text_conversion(path, orig_path, &err); |
| 481 | if (err != OK || res.is_null()) { |
| 482 | if (err == ERR_UNAUTHORIZED || err == ERR_FILE_CORRUPT) { |
| 483 | return "ERROR: Can't read encrypted file: " + path; |
| 484 | } |
| 485 | return "ERROR: Failed to load " + path; |
| 486 | } |
| 487 | |
| 488 | int64_t length = FileAccess::get_size(path); |
| 489 | Ref<Script> script = res; |
| 490 | if (script.is_valid()) { |
| 491 | return script->get_source_code(); |
| 492 | } |
| 493 | |
| 494 | Ref<FileAccessBuffer> f = FileAccessBuffer::create(FileAccessBuffer::RESIZE_OPTIMIZED); |
| 495 | f->reserve(length * 3); |
| 496 | |
| 497 | String save_path; |
| 498 | String base_ext = path.get_basename().get_basename().get_extension(); |
| 499 | if (path.contains(".converted.") && (base_ext == "xml" || base_ext == "x" + path.get_extension().to_lower())) { |
| 500 | ResourceFormatSaverXMLInstance saver; |
| 501 | save_path = path.get_basename() + ".xml"; |
| 502 | err = saver.save_to_file(f, save_path, res, 0); |
| 503 | } else { |
nothing calls this directly
no test coverage detected