Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/json_resource_format.cpp
45997 views
1
/**************************************************************************/
2
/* json_resource_format.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 "json_resource_format.h"
32
33
#include "core/config/engine.h"
34
#include "core/io/file_access.h"
35
#include "core/io/json.h"
36
37
Ref<Resource> ResourceFormatLoaderJSON::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
38
if (r_error) {
39
*r_error = ERR_FILE_CANT_OPEN;
40
}
41
42
if (!FileAccess::exists(p_path)) {
43
if (r_error) {
44
*r_error = ERR_FILE_NOT_FOUND;
45
}
46
return Ref<Resource>();
47
}
48
49
Ref<JSON> json;
50
json.instantiate();
51
52
Error err = json->parse(FileAccess::get_file_as_string(p_path), Engine::get_singleton()->is_editor_hint());
53
if (err != OK) {
54
String err_text = "Error parsing JSON file at '" + p_path + "', on line " + itos(json->get_error_line()) + ": " + json->get_error_message();
55
56
if (Engine::get_singleton()->is_editor_hint()) {
57
// If running on editor, still allow opening the JSON so the code editor can edit it.
58
WARN_PRINT(err_text);
59
} else {
60
if (r_error) {
61
*r_error = err;
62
}
63
ERR_PRINT(err_text);
64
return Ref<Resource>();
65
}
66
}
67
68
if (r_error) {
69
*r_error = OK;
70
}
71
72
return json;
73
}
74
75
void ResourceFormatLoaderJSON::get_recognized_extensions(List<String> *p_extensions) const {
76
p_extensions->push_back("json");
77
}
78
79
bool ResourceFormatLoaderJSON::handles_type(const String &p_type) const {
80
return (p_type == "JSON");
81
}
82
83
String ResourceFormatLoaderJSON::get_resource_type(const String &p_path) const {
84
if (p_path.has_extension("json")) {
85
return "JSON";
86
}
87
return "";
88
}
89
90
Error ResourceFormatSaverJSON::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
91
Ref<JSON> json = p_resource;
92
ERR_FAIL_COND_V(json.is_null(), ERR_INVALID_PARAMETER);
93
94
String source = json->get_parsed_text().is_empty() ? JSON::stringify(json->get_data(), "\t", false, true) : json->get_parsed_text();
95
96
Error err;
97
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
98
99
ERR_FAIL_COND_V_MSG(err, err, vformat("Cannot save json '%s'.", p_path));
100
101
file->store_string(source);
102
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
103
return ERR_CANT_CREATE;
104
}
105
106
return OK;
107
}
108
109
void ResourceFormatSaverJSON::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
110
Ref<JSON> json = p_resource;
111
if (json.is_valid()) {
112
p_extensions->push_back("json");
113
}
114
}
115
116
bool ResourceFormatSaverJSON::recognize(const Ref<Resource> &p_resource) const {
117
return p_resource->get_class_name() == "JSON"; //only json, not inherited
118
}
119
120