GDScript: Allow preload() to be used with constant expressions

This commit is contained in:
George Marques 2020-08-19 10:45:00 -03:00
parent a52e457ada
commit f9ad0b30fa
No known key found for this signature in database
GPG Key ID: 046BD46A3201E43D
2 changed files with 32 additions and 23 deletions

View File

@ -33,6 +33,7 @@
#include "core/class_db.h"
#include "core/hash_map.h"
#include "core/io/resource_loader.h"
#include "core/os/file_access.h"
#include "core/project_settings.h"
#include "core/script_language.h"
#include "gdscript.h"
@ -2301,6 +2302,37 @@ void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
}
void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
if (!p_preload->path) {
return;
}
reduce_expression(p_preload->path);
if (!p_preload->path->is_constant) {
push_error("Preloaded path must be a constant string.", p_preload->path);
return;
}
if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
push_error("Preloaded path must be a constant string.", p_preload->path);
} else {
p_preload->resolved_path = p_preload->path->reduced_value;
// TODO: Save this as script dependency.
if (p_preload->resolved_path.is_rel_path()) {
p_preload->resolved_path = parser->script_path.get_base_dir().plus_file(p_preload->resolved_path);
}
p_preload->resolved_path = p_preload->resolved_path.simplify_path();
if (!FileAccess::exists(p_preload->resolved_path)) {
push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
} else {
// TODO: Don't load if validating: use completion cache.
p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
if (p_preload->resource.is_null()) {
push_error(vformat(R"(Could not p_preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
}
}
}
p_preload->is_constant = true;
p_preload->reduced_value = p_preload->resource;
p_preload->set_datatype(type_from_variant(p_preload->reduced_value));

View File

@ -2524,29 +2524,6 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_preload(ExpressionNode *p_
if (preload->path == nullptr) {
push_error(R"(Expected resource path after "(".)");
} else if (preload->path->type != Node::LITERAL) {
push_error("Preloaded path must be a constant string.");
} else {
LiteralNode *path = static_cast<LiteralNode *>(preload->path);
if (path->value.get_type() != Variant::STRING) {
push_error("Preloaded path must be a constant string.");
} else {
preload->resolved_path = path->value;
// TODO: Save this as script dependency.
if (preload->resolved_path.is_rel_path()) {
preload->resolved_path = script_path.get_base_dir().plus_file(preload->resolved_path);
}
preload->resolved_path = preload->resolved_path.simplify_path();
if (!FileAccess::exists(preload->resolved_path)) {
push_error(vformat(R"(Preload file "%s" does not exist.)", preload->resolved_path));
} else {
// TODO: Don't load if validating: use completion cache.
preload->resource = ResourceLoader::load(preload->resolved_path);
if (preload->resource.is_null()) {
push_error(vformat(R"(Could not preload resource file "%s".)", preload->resolved_path));
}
}
}
}
pop_completion_call();