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