Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_resource_format.cpp
45997 views
1
/**************************************************************************/
2
/* gdscript_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 "gdscript_resource_format.h"
32
33
#include "gdscript_cache.h"
34
#include "gdscript_parser.h"
35
36
#include "core/io/file_access.h"
37
#include "core/object/class_db.h"
38
39
Ref<Resource> ResourceFormatLoaderGDScript::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) {
40
Error err;
41
bool ignoring = p_cache_mode == CACHE_MODE_IGNORE || p_cache_mode == CACHE_MODE_IGNORE_DEEP;
42
Ref<GDScript> scr = GDScriptCache::get_full_script(p_original_path, err, "", ignoring);
43
44
if (err && scr.is_valid()) {
45
// If !scr.is_valid(), the error was likely from scr->load_source_code(), which already generates an error.
46
ERR_PRINT_ED(vformat(R"(Failed to load script "%s" with error "%s".)", p_original_path, error_names[err]));
47
}
48
49
if (r_error) {
50
// Don't fail loading because of parsing error.
51
*r_error = scr.is_valid() ? OK : err;
52
}
53
54
return scr;
55
}
56
57
void ResourceFormatLoaderGDScript::get_recognized_extensions(List<String> *p_extensions) const {
58
p_extensions->push_back("gd");
59
p_extensions->push_back("gdc");
60
}
61
62
bool ResourceFormatLoaderGDScript::handles_type(const String &p_type) const {
63
return (p_type == "Script" || p_type == "GDScript");
64
}
65
66
String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) const {
67
String el = p_path.get_extension().to_lower();
68
if (el == "gd" || el == "gdc") {
69
return "GDScript";
70
}
71
return "";
72
}
73
74
void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
75
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ);
76
ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_path + "'.");
77
78
String source = file->get_as_utf8_string();
79
if (source.is_empty()) {
80
return;
81
}
82
83
GDScriptParser parser;
84
if (OK != parser.parse(source, p_path, false)) {
85
return;
86
}
87
88
for (const String &E : parser.get_dependencies()) {
89
p_dependencies->push_back(E);
90
}
91
}
92
93
void ResourceFormatLoaderGDScript::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
94
Ref<GDScript> scr = ResourceLoader::load(p_path);
95
if (scr.is_null()) {
96
return;
97
}
98
99
const String source = scr->get_source_code();
100
GDScriptTokenizerText tokenizer;
101
tokenizer.set_source_code(source);
102
GDScriptTokenizer::Token current = tokenizer.scan();
103
while (current.type != GDScriptTokenizer::Token::TK_EOF) {
104
if (!current.is_identifier()) {
105
current = tokenizer.scan();
106
continue;
107
}
108
109
int insert_idx = 0;
110
for (int i = 0; i < current.start_line - 1; i++) {
111
insert_idx = source.find("\n", insert_idx) + 1;
112
}
113
// Insert the "cursor" character, needed for the lookup to work.
114
const String source_with_cursor = source.insert(insert_idx + current.start_column, String::chr(0xFFFF));
115
116
ScriptLanguage::LookupResult result;
117
if (scr->get_language()->lookup_code(source_with_cursor, current.get_identifier(), p_path, nullptr, result) == OK) {
118
if (!result.class_name.is_empty() && ClassDB::class_exists(result.class_name)) {
119
r_classes->insert(result.class_name);
120
}
121
122
if (result.type == ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY) {
123
PropertyInfo prop;
124
if (ClassDB::get_property_info(result.class_name, result.class_member, &prop)) {
125
if (!prop.class_name.is_empty() && ClassDB::class_exists(prop.class_name)) {
126
r_classes->insert(prop.class_name);
127
}
128
if (!prop.hint_string.is_empty() && ClassDB::class_exists(prop.hint_string)) {
129
r_classes->insert(prop.hint_string);
130
}
131
}
132
} else if (result.type == ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD) {
133
MethodInfo met;
134
if (ClassDB::get_method_info(result.class_name, result.class_member, &met)) {
135
if (!met.return_val.class_name.is_empty() && ClassDB::class_exists(met.return_val.class_name)) {
136
r_classes->insert(met.return_val.class_name);
137
}
138
if (!met.return_val.hint_string.is_empty() && ClassDB::class_exists(met.return_val.hint_string)) {
139
r_classes->insert(met.return_val.hint_string);
140
}
141
}
142
}
143
}
144
145
current = tokenizer.scan();
146
}
147
}
148
149
Error ResourceFormatSaverGDScript::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
150
Ref<GDScript> sqscr = p_resource;
151
ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
152
153
String source = sqscr->get_source_code();
154
155
{
156
Error err;
157
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
158
159
ERR_FAIL_COND_V_MSG(err, err, "Cannot save GDScript file '" + p_path + "'.");
160
161
file->store_string(source);
162
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
163
return ERR_CANT_CREATE;
164
}
165
}
166
167
if (ScriptServer::is_reload_scripts_on_save_enabled()) {
168
GDScriptLanguage::get_singleton()->reload_tool_script(p_resource, true);
169
}
170
171
return OK;
172
}
173
174
void ResourceFormatSaverGDScript::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
175
if (Object::cast_to<GDScript>(*p_resource)) {
176
p_extensions->push_back("gd");
177
}
178
}
179
180
bool ResourceFormatSaverGDScript::recognize(const Ref<Resource> &p_resource) const {
181
return Object::cast_to<GDScript>(*p_resource) != nullptr;
182
}
183
184