Debian Codegraph @

modules/objectdb_profiler/editor/snapshot_data.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* snapshot_data.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 "snapshot_data.h"
32
33 #include "core/core_bind.h"
34 #include "core/io/compression.h"
35 #include "core/object/script_language.h"
36 #include "scene/debugger/scene_debugger.h"
37
38 #if defined(MODULE_GDSCRIPT_ENABLED) && defined(DEBUG_ENABLED)
39 #include "modules/gdscript/gdscript.h"
40 #endif
41
42 SnapshotDataObject::SnapshotDataObject(SceneDebuggerObject &p_obj, GameStateSnapshot *p_snapshot, ResourceCache &resource_cache) :
43 snapshot(p_snapshot) {
44 remote_object_id = p_obj.id;
45 type_name = p_obj.class_name;
46
47 for (const SceneDebuggerObject::SceneDebuggerProperty &prop : p_obj.properties) {
48 PropertyInfo pinfo = prop.first;
49 Variant pvalue = prop.second;
50
51 if (pinfo.type == Variant::OBJECT && pvalue.is_string()) {
52 String path = pvalue;
53 // If a resource is followed by a ::, it is a nested resource (like a sub_resource in a .tscn file).
54 // To get a reference to it, first we load the parent resource (the .tscn, for example), then,
55 // we load the child resource. The parent resource (dependency) should not be destroyed before the child
56 // resource (pvalue) is loaded.
57 if (path.is_resource_file()) {
58 // Built-in resource.
59 String base_path = path.get_slice("::", 0);
60 if (!resource_cache.cache.has(base_path)) {
61 if (ResourceLoader::exists(path)) {
62 resource_cache.cache[base_path] = ResourceLoader::load(base_path);
63 }
64 resource_cache.misses++;
65 } else {
66 resource_cache.hits++;
67 }
68 }
69 if (!resource_cache.cache.has(path)) {
70 if (ResourceLoader::exists(path)) {
71 resource_cache.cache[path] = ResourceLoader::load(path);
72 }
73 resource_cache.misses++;
74 } else {
75 resource_cache.hits++;
76 }
77 pvalue = resource_cache.cache[path];
78
79 if (pinfo.hint_string == "Script") {
80 if (get_script() != pvalue) {
81 set_script(Ref<RefCounted>());
82 Ref<Script> scr(pvalue);
83 if (scr.is_valid()) {
84 ScriptInstance *scr_instance = scr->placeholder_instance_create(this);
85 if (scr_instance) {
86 set_script_instance(scr_instance);
87 }
88 }
89 }
90 }
91 }
92 prop_list.push_back(pinfo);
93 prop_values[pinfo.name] = pvalue;
94 }
95 }
96
97 bool SnapshotDataObject::_get(const StringName &p_name, Variant &r_ret) const {
98 String name = p_name;
99
100 if (name.begins_with("Metadata/")) {
101 name = name.replace_first("Metadata/", "metadata/");
102 }
103 if (!prop_values.has(name)) {
104 return false;
105 }
106
107 r_ret = prop_values[p_name];
108 return true;
109 }
110
111 void SnapshotDataObject::_get_property_list(List<PropertyInfo> *p_list) const {
112 p_list->clear(); // Sorry, don't want any categories.
113 for (const PropertyInfo &prop : prop_list) {
114 if (prop.name == "script") {
115 // Skip the script property, it's always added by the non-virtual method.
116 continue;
117 }
118
119 p_list->push_back(prop);
120 }
121 }
122
123 void SnapshotDataObject::_bind_methods() {
124 ClassDB::bind_method(D_METHOD("_is_read_only"), &SnapshotDataObject::_is_read_only);
125 }
126
127 String SnapshotDataObject::get_node_path() {
128 if (!is_node()) {
129 return "";
130 }
131 SnapshotDataObject *current = this;
132 String path;
133
134 while (true) {
135 String current_node_name = current->extra_debug_data["node_name"];
136 if (current_node_name != "") {
137 if (path != "") {
138 path = current_node_name + "/" + path;
139 } else {
140 path = current_node_name;
141 }
142 }
143 if (!current->extra_debug_data.has("node_parent")) {
144 break;
145 }
146 current = snapshot->objects[current->extra_debug_data["node_parent"]];
147 }
148 return path;
149 }
150
151 String SnapshotDataObject::_get_script_name(Ref<Script> p_script) {
152 #if defined(MODULE_GDSCRIPT_ENABLED) && defined(DEBUG_ENABLED)
153 // GDScripts have more specific names than base scripts, so use those names if possible.
154 return GDScript::debug_get_script_name(p_script);
155 #else
156 // Otherwise fallback to the base script's name.
157 return p_script->get_global_name();
158 #endif
159 }
160
161 String SnapshotDataObject::get_name() {
162 String found_type_name = type_name;
163
164 // Ideally, we will name it after the script attached to it.
165 Ref<Script> maybe_script = get_script();
166 if (maybe_script.is_valid()) {
167 String full_name;
168 while (maybe_script.is_valid()) {
169 String global_name = _get_script_name(maybe_script);
170 if (global_name != "") {
171 if (full_name != "") {
172 full_name = global_name + "/" + full_name;
173 } else {
174 full_name = global_name;
175 }
176 }
177 maybe_script = maybe_script->get_base_script().ptr();
178 }
179
180 found_type_name = type_name + "/" + full_name;
181 }
182
183 return found_type_name + "_" + uitos(remote_object_id);
184 }
185
186 bool SnapshotDataObject::is_refcounted() {
187 return is_class(RefCounted::get_class_static());
188 }
189
190 bool SnapshotDataObject::is_node() {
191 return is_class(Node::get_class_static());
192 }
193
194 bool SnapshotDataObject::is_class(const String &p_base_class) {
195 return ClassDB::is_parent_class(type_name, p_base_class);
196 }
197
198 HashSet<ObjectID> SnapshotDataObject::_unique_references(const HashMap<String, ObjectID> &p_refs) {
199 HashSet<ObjectID> obj_set;
200
201 for (const KeyValue<String, ObjectID> &pair : p_refs) {
202 obj_set.insert(pair.value);
203 }
204
205 return obj_set;
206 }
207
208 HashSet<ObjectID> SnapshotDataObject::get_unique_outbound_refernces() {
209 return _unique_references(outbound_references);
210 }
211
212 HashSet<ObjectID> SnapshotDataObject::get_unique_inbound_references() {
213 return _unique_references(inbound_references);
214 }
215
216 void GameStateSnapshot::_get_outbound_references(Variant &p_var, HashMap<String, ObjectID> &r_ret_val, const String &p_current_path) {
217 String path_divider = p_current_path.size() > 0 ? "/" : ""; // Make sure we don't start with a /.
218 switch (p_var.get_type()) {
219 case Variant::Type::INT:
220 case Variant::Type::OBJECT: { // Means ObjectID.
221 ObjectID as_id = ObjectID((uint64_t)p_var);
222 if (!objects.has(as_id)) {
223 return;
224 }
225 r_ret_val[p_current_path] = as_id;
226 break;
227 }
228 case Variant::Type::DICTIONARY: {
229 Dictionary dict = (Dictionary)p_var;
230 LocalVector<Variant> keys = dict.get_key_list();
231 for (Variant &k : keys) {
232 // The dictionary key _could be_ an object. If it is, we name the key property with the same name as the value, but with _key appended to it.
233 _get_outbound_references(k, r_ret_val, p_current_path + path_divider + (String)k + "_key");
234 Variant v = dict.get(k, Variant());
235 _get_outbound_references(v, r_ret_val, p_current_path + path_divider + (String)k);
236 }
237 break;
238 }
239 case Variant::Type::ARRAY: {
240 Array arr = (Array)p_var;
241 int i = 0;
242 for (Variant &v : arr) {
243 _get_outbound_references(v, r_ret_val, p_current_path + path_divider + itos(i));
244 i++;
245 }
246 break;
247 }
248 default: {
249 break;
250 }
251 }
252 }
253
254 void GameStateSnapshot::_get_rc_cycles(
255 SnapshotDataObject *p_obj,
256 SnapshotDataObject *p_source_obj,
257 HashSet<SnapshotDataObject *> p_traversed_objs,
258 LocalVector<String> &r_ret_val,
259 const String &p_current_path) {
260 // We're at the end of this branch and it was a cycle.
261 if (p_obj == p_source_obj && p_current_path != "") {
262 r_ret_val.push_back(p_current_path);
263 return;
264 }
265
266 // Go through each of our children and try traversing them.
267 for (const KeyValue<String, ObjectID> &next_child : p_obj->outbound_references) {
268 SnapshotDataObject *next_obj = p_obj->snapshot->objects[next_child.value];
269 String next_name = next_obj == p_source_obj ? "self" : next_obj->get_name();
270 String current_name = p_obj == p_source_obj ? "self" : p_obj->get_name();
271 String child_path = current_name + "[\"" + next_child.key + U"\"] → " + next_name;
272 if (p_current_path != "") {
273 child_path = p_current_path + "\n" + child_path;
274 }
275
276 SnapshotDataObject *next = objects[next_child.value];
277 if (next != nullptr && next->is_class(RefCounted::get_class_static()) && !next->is_class(WeakRef::get_class_static()) && !p_traversed_objs.has(next)) {
278 HashSet<SnapshotDataObject *> traversed_copy = p_traversed_objs;
279 if (p_obj != p_source_obj) {
280 traversed_copy.insert(p_obj);
281 }
282 _get_rc_cycles(next, p_source_obj, traversed_copy, r_ret_val, child_path);
283 }
284 }
285 }
286
287 void GameStateSnapshot::recompute_references() {
288 for (const KeyValue<ObjectID, SnapshotDataObject *> &obj : objects) {
289 Dictionary values;
290 for (const KeyValue<StringName, Variant> &kv : obj.value->prop_values) {
291 // Should only ever be one entry in this context.
292 values[kv.key] = kv.value;
293 }
294
295 Variant values_variant(values);
296 HashMap<String, ObjectID> refs;
297 _get_outbound_references(values_variant, refs);
298
299 obj.value->outbound_references = refs;
300
301 for (const KeyValue<String, ObjectID> &kv : refs) {
302 // Get the guy we are pointing to, and indicate the name of _our_ property that is pointing to them.
303 if (objects.has(kv.value)) {
304 objects[kv.value]->inbound_references[kv.key] = obj.key;
305 }
306 }
307 }
308
309 for (const KeyValue<ObjectID, SnapshotDataObject *> &obj : objects) {
310 if (!obj.value->is_class(RefCounted::get_class_static()) || obj.value->is_class(WeakRef::get_class_static())) {
311 continue;
312 }
313 HashSet<SnapshotDataObject *> traversed_objs;
314 LocalVector<String> cycles;
315
316 _get_rc_cycles(obj.value, obj.value, traversed_objs, cycles, "");
317 Array cycles_array;
318 for (const String &cycle : cycles) {
319 cycles_array.push_back(cycle);
320 }
321 obj.value->extra_debug_data["ref_cycles"] = cycles_array;
322 }
323 }
324
325 Ref<GameStateSnapshot> GameStateSnapshot::create_ref(const String &p_snapshot_name, const Vector<uint8_t> &p_snapshot_buffer) {
326 Ref<GameStateSnapshot> snapshot;
327 snapshot.instantiate();
328 snapshot->name = p_snapshot_name;
329
330 // Snapshots may have been created by an older version of the editor. Handle parsing old snapshot versions here based on the version number.
331
332 Vector<uint8_t> snapshot_buffer_decompressed;
333 int success = Compression::decompress_dynamic(&snapshot_buffer_decompressed, -1, p_snapshot_buffer.ptr(), p_snapshot_buffer.size(), Compression::MODE_DEFLATE);
334 ERR_FAIL_COND_V_MSG(success != Z_OK, nullptr, "ObjectDB Snapshot could not be parsed. Failed to decompress snapshot.");
335 CoreBind::Marshalls *m = CoreBind::Marshalls::get_singleton();
336 Array snapshot_data = m->base64_to_variant(m->raw_to_base64(snapshot_buffer_decompressed));
337 ERR_FAIL_COND_V_MSG(snapshot_data.is_empty(), nullptr, "ObjectDB Snapshot could not be parsed. Variant array is empty.");
338 const Variant &first_item = snapshot_data[0];
339 ERR_FAIL_COND_V_MSG(first_item.get_type() != Variant::DICTIONARY, nullptr, "ObjectDB Snapshot could not be parsed. First item is not a Dictionary.");
340 snapshot->snapshot_context = first_item;
341
342 SnapshotDataObject::ResourceCache resource_cache;
343 for (int i = 1; i < snapshot_data.size(); i += 4) {
344 SceneDebuggerObject obj;
345 obj.deserialize(uint64_t(snapshot_data[i + 0]), snapshot_data[i + 1], snapshot_data[i + 2]);
346 ERR_FAIL_COND_V_MSG(snapshot_data[i + 3].get_type() != Variant::DICTIONARY, nullptr, "ObjectDB Snapshot could not be parsed. Extra debug data is not a Dictionary.");
347
348 if (obj.id.is_null()) {
349 continue;
350 }
351
352 snapshot->objects[obj.id] = memnew(SnapshotDataObject(obj, snapshot.ptr(), resource_cache));
353 snapshot->objects[obj.id]->extra_debug_data = (Dictionary)snapshot_data[i + 3];
354 }
355
356 snapshot->recompute_references();
357 print_verbose("Resource cache hits: " + String::num(resource_cache.hits) + ". Resource cache misses: " + String::num(resource_cache.misses));
358 return snapshot;
359 }
360
361 GameStateSnapshot::~GameStateSnapshot() {
362 for (const KeyValue<ObjectID, SnapshotDataObject *> &item : objects) {
363 memdelete(item.value);
364 }
365 }