Add a dependency search mode for GDScript parser

- This mode avoids loading any other resource.
- Search for class_name now uses this mode, to avoid loading in the scan
thread.
- Implement get_dependencies() for GDScript loader, now exporting
dependencies only should include the preloaded resources.
This commit is contained in:
George Marques 2019-03-03 16:36:42 -03:00
parent 4f0590338f
commit bda60bfa29
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
4 changed files with 140 additions and 79 deletions

View file

@ -473,29 +473,31 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
Ref<Resource> res;
if (!validating) {
dependencies.push_back(path);
if (!dependencies_only) {
if (!validating) {
//this can be too slow for just validating code
if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
} else if (!for_completion || FileAccess::exists(path)) {
res = ResourceLoader::load(path);
//this can be too slow for just validating code
if (for_completion && ScriptCodeCompletionCache::get_singleton() && FileAccess::exists(path)) {
res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
} else if (!for_completion || FileAccess::exists(path)) {
res = ResourceLoader::load(path);
}
} else {
if (!FileAccess::exists(path)) {
_set_error("Can't preload resource at path: " + path);
return NULL;
} else if (ScriptCodeCompletionCache::get_singleton()) {
res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
}
}
} else {
if (!FileAccess::exists(path)) {
if (!res.is_valid()) {
_set_error("Can't preload resource at path: " + path);
return NULL;
} else if (ScriptCodeCompletionCache::get_singleton()) {
res = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
}
}
if (!res.is_valid()) {
_set_error("Can't preload resource at path: " + path);
return NULL;
}
if (tokenizer->get_token() != GDScriptTokenizer::TK_PARENTHESIS_CLOSE) {
_set_error("Expected ')' after 'preload' path");
return NULL;
@ -812,17 +814,19 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
bfn = true;
}
// Check parents for the constant
if (!bfn && cln->extends_file != StringName()) {
Ref<GDScript> parent = ResourceLoader::load(cln->extends_file);
if (parent.is_valid() && parent->is_valid()) {
Map<StringName, Variant> parent_constants;
parent->get_constants(&parent_constants);
if (parent_constants.has(identifier)) {
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = parent_constants[identifier];
expr = constant;
bfn = true;
if (!dependencies_only) {
// Check parents for the constant
if (!bfn && cln->extends_file != StringName()) {
Ref<GDScript> parent = ResourceLoader::load(cln->extends_file);
if (parent.is_valid() && parent->is_valid()) {
Map<StringName, Variant> parent_constants;
parent->get_constants(&parent_constants);
if (parent_constants.has(identifier)) {
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = parent_constants[identifier];
expr = constant;
bfn = true;
}
}
}
}
@ -3378,6 +3382,13 @@ void GDScriptParser::_parse_extends(ClassNode *p_class) {
p_class->extends_file = constant;
tokenizer->advance();
// Add parent script as a dependency
String parent = constant;
if (parent.is_rel_path()) {
parent = base_path.plus_file(parent).simplify_path();
}
dependencies.push_back(parent);
if (tokenizer->get_token() != GDScriptTokenizer::TK_PERIOD) {
return;
} else
@ -8149,6 +8160,10 @@ Error GDScriptParser::_parse(const String &p_base_path) {
return ERR_PARSE_ERROR;
}
if (dependencies_only) {
return OK;
}
_determine_inheritance(main_class);
if (error_set) {
@ -8227,7 +8242,7 @@ Error GDScriptParser::parse_bytecode(const Vector<uint8_t> &p_bytecode, const St
return ret;
}
Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion, Set<int> *r_safe_lines) {
Error GDScriptParser::parse(const String &p_code, const String &p_base_path, bool p_just_validate, const String &p_self_path, bool p_for_completion, Set<int> *r_safe_lines, bool p_dependencies_only) {
clear();
@ -8237,6 +8252,7 @@ Error GDScriptParser::parse(const String &p_code, const String &p_base_path, boo
validating = p_just_validate;
for_completion = p_for_completion;
dependencies_only = p_dependencies_only;
#ifdef DEBUG_ENABLED
safe_lines = r_safe_lines;
#endif // DEBUG_ENABLED
@ -8293,6 +8309,8 @@ void GDScriptParser::clear() {
parenthesis = 0;
current_export.type = Variant::NIL;
check_types = true;
dependencies_only = false;
dependencies.clear();
error = "";
#ifdef DEBUG_ENABLED
safe_lines = NULL;