Skip to content

Commit c4733e8

Browse files
committed
Merge pull request #90860 from vnen/gdscript-get-dependencies
GDScript: Implement `get_dependencies()`
2 parents 296758a + dc73440 commit c4733e8

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

modules/gdscript/gdscript.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,7 @@ String ResourceFormatLoaderGDScript::get_resource_type(const String &p_path) con
28922892
return "";
28932893
}
28942894

2895-
void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
2895+
void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<String> *r_dependencies, bool p_add_types) {
28962896
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ);
28972897
ERR_FAIL_COND_MSG(file.is_null(), "Cannot open file '" + p_path + "'.");
28982898

@@ -2906,8 +2906,13 @@ void ResourceFormatLoaderGDScript::get_dependencies(const String &p_path, List<S
29062906
return;
29072907
}
29082908

2909+
GDScriptAnalyzer analyzer(&parser);
2910+
if (OK != analyzer.analyze()) {
2911+
return;
2912+
}
2913+
29092914
for (const String &E : parser.get_dependencies()) {
2910-
p_dependencies->push_back(E);
2915+
r_dependencies->push_back(E);
29112916
}
29122917
}
29132918

modules/gdscript/gdscript.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ class ResourceFormatLoaderGDScript : public ResourceFormatLoader {
633633
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
634634
virtual bool handles_type(const String &p_type) const override;
635635
virtual String get_resource_type(const String &p_path) const override;
636-
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
636+
virtual void get_dependencies(const String &p_path, List<String> *r_dependencies, bool p_add_types = false) override;
637637
};
638638

639639
class ResourceFormatSaverGDScript : public ResourceFormatSaver {

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,11 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
562562
class_type.native_type = result.native_type;
563563
p_class->set_datatype(class_type);
564564

565+
// Add base class to the list of dependencies.
566+
if (result.kind == GDScriptParser::DataType::CLASS) {
567+
parser->add_dependency(result.script_path);
568+
}
569+
565570
// Apply annotations.
566571
for (GDScriptParser::AnnotationNode *&E : p_class->annotations) {
567572
resolve_annotation(E);
@@ -868,6 +873,11 @@ GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::Type
868873
}
869874

870875
p_type->set_datatype(result);
876+
877+
if (result.kind == GDScriptParser::DataType::CLASS || result.kind == GDScriptParser::DataType::SCRIPT) {
878+
parser->add_dependency(result.script_path);
879+
}
880+
871881
return result;
872882
}
873883

@@ -4082,6 +4092,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
40824092

40834093
if (ScriptServer::is_global_class(name)) {
40844094
p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
4095+
parser->add_dependency(p_identifier->get_datatype().script_path);
40854096
return;
40864097
}
40874098

@@ -4124,6 +4135,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
41244135
}
41254136
result.is_constant = true;
41264137
p_identifier->set_datatype(result);
4138+
parser->add_dependency(autoload.path);
41274139
return;
41284140
}
41294141
}
@@ -4243,7 +4255,6 @@ void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
42434255
push_error("Preloaded path must be a constant string.", p_preload->path);
42444256
} else {
42454257
p_preload->resolved_path = p_preload->path->reduced_value;
4246-
// TODO: Save this as script dependency.
42474258
if (p_preload->resolved_path.is_relative_path()) {
42484259
p_preload->resolved_path = parser->script_path.get_base_dir().path_join(p_preload->resolved_path);
42494260
}
@@ -4274,6 +4285,8 @@ void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
42744285
push_error(vformat(R"(Could not preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
42754286
}
42764287
}
4288+
4289+
parser->add_dependency(p_preload->resolved_path);
42774290
}
42784291
}
42794292

modules/gdscript/gdscript_parser.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,8 @@ class GDScriptParser {
14291429
void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
14301430
void reset_extents(Node *p_node, Node *p_from);
14311431

1432+
HashSet<String> dependencies;
1433+
14321434
template <typename T>
14331435
T *alloc_node() {
14341436
T *node = memnew(T);
@@ -1572,9 +1574,11 @@ class GDScriptParser {
15721574
bool annotation_exists(const String &p_annotation_name) const;
15731575

15741576
const List<ParserError> &get_errors() const { return errors; }
1575-
const List<String> get_dependencies() const {
1576-
// TODO: Keep track of deps.
1577-
return List<String>();
1577+
const HashSet<String> &get_dependencies() const {
1578+
return dependencies;
1579+
}
1580+
void add_dependency(const String &p_dependency) {
1581+
dependencies.insert(p_dependency);
15781582
}
15791583
#ifdef DEBUG_ENABLED
15801584
const List<GDScriptWarning> &get_warnings() const { return warnings; }

0 commit comments

Comments
 (0)