Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_cache.h
10277 views
1
/**************************************************************************/
2
/* gdscript_cache.h */
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
#pragma once
32
33
#include "gdscript.h"
34
35
#include "core/object/ref_counted.h"
36
#include "core/os/safe_binary_mutex.h"
37
#include "core/templates/hash_map.h"
38
#include "core/templates/hash_set.h"
39
40
class GDScriptAnalyzer;
41
class GDScriptParser;
42
43
class GDScriptParserRef : public RefCounted {
44
GDSOFTCLASS(GDScriptParserRef, RefCounted);
45
46
public:
47
enum Status {
48
EMPTY,
49
PARSED,
50
INHERITANCE_SOLVED,
51
INTERFACE_SOLVED,
52
FULLY_SOLVED,
53
};
54
55
private:
56
GDScriptParser *parser = nullptr;
57
GDScriptAnalyzer *analyzer = nullptr;
58
Status status = EMPTY;
59
Error result = OK;
60
String path;
61
uint32_t source_hash = 0;
62
bool clearing = false;
63
bool abandoned = false;
64
65
friend class GDScriptCache;
66
friend class GDScript;
67
68
public:
69
Status get_status() const;
70
String get_path() const;
71
uint32_t get_source_hash() const;
72
GDScriptParser *get_parser();
73
GDScriptAnalyzer *get_analyzer();
74
Error raise_status(Status p_new_status);
75
void clear();
76
77
GDScriptParserRef() {}
78
~GDScriptParserRef();
79
};
80
81
class GDScriptCache {
82
// String key is full path.
83
HashMap<String, GDScriptParserRef *> parser_map;
84
HashMap<String, Vector<ObjectID>> abandoned_parser_map;
85
HashMap<String, Ref<GDScript>> shallow_gdscript_cache;
86
HashMap<String, Ref<GDScript>> full_gdscript_cache;
87
HashMap<String, Ref<GDScript>> static_gdscript_cache;
88
HashMap<String, HashSet<String>> dependencies;
89
HashMap<String, HashSet<String>> parser_inverse_dependencies;
90
91
friend class GDScript;
92
friend class GDScriptParserRef;
93
friend class GDScriptInstance;
94
95
static GDScriptCache *singleton;
96
97
bool cleared = false;
98
99
public:
100
static const int BINARY_MUTEX_TAG = 2;
101
102
private:
103
static SafeBinaryMutex<BINARY_MUTEX_TAG> mutex;
104
friend SafeBinaryMutex<BINARY_MUTEX_TAG> &_get_gdscript_cache_mutex();
105
106
public:
107
static void move_script(const String &p_from, const String &p_to);
108
static void remove_script(const String &p_path);
109
static Ref<GDScriptParserRef> get_parser(const String &p_path, GDScriptParserRef::Status status, Error &r_error, const String &p_owner = String());
110
static bool has_parser(const String &p_path);
111
static void remove_parser(const String &p_path);
112
static String get_source_code(const String &p_path);
113
static Vector<uint8_t> get_binary_tokens(const String &p_path);
114
static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String());
115
static Ref<GDScript> get_full_script(const String &p_path, Error &r_error, const String &p_owner = String(), bool p_update_from_disk = false);
116
static Ref<GDScript> get_cached_script(const String &p_path);
117
static Error finish_compiling(const String &p_owner);
118
static void add_static_script(Ref<GDScript> p_script);
119
static void remove_static_script(const String &p_fqcn);
120
121
static void clear();
122
123
GDScriptCache();
124
~GDScriptCache();
125
};
126
127