Skip to content

Commit

Permalink
Merge pull request godotengine#34 from lawnjelly/plus_sync
Browse files Browse the repository at this point in the history
Plus sync
  • Loading branch information
lawnjelly authored Apr 25, 2023
2 parents 881ac09 + b7b8de7 commit d8de7a7
Show file tree
Hide file tree
Showing 42 changed files with 905 additions and 175 deletions.
53 changes: 53 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "os.h"

#include "core/io/json.h"
#include "core/os/dir_access.h"
#include "core/os/file_access.h"
#include "core/os/input.h"
Expand Down Expand Up @@ -931,6 +932,58 @@ void OS::add_frame_delay(bool p_can_draw) {
}
}

void OS::set_use_benchmark(bool p_use_benchmark) {
use_benchmark = p_use_benchmark;
}

bool OS::is_use_benchmark_set() {
return use_benchmark;
}

void OS::set_benchmark_file(const String &p_benchmark_file) {
benchmark_file = p_benchmark_file;
}

String OS::get_benchmark_file() {
return benchmark_file;
}

void OS::benchmark_begin_measure(const String &p_what) {
#ifdef TOOLS_ENABLED
start_benchmark_from[p_what] = OS::get_singleton()->get_ticks_usec();
#endif
}
void OS::benchmark_end_measure(const String &p_what) {
#ifdef TOOLS_ENABLED
uint64_t total = OS::get_singleton()->get_ticks_usec() - start_benchmark_from[p_what];
double total_f = double(total) / double(1000000);

startup_benchmark_json[p_what] = total_f;
#endif
}

void OS::benchmark_dump() {
#ifdef TOOLS_ENABLED
if (!use_benchmark) {
return;
}
if (!benchmark_file.empty()) {
FileAccess *f = FileAccess::open(benchmark_file, FileAccess::WRITE);
if (f) {
f->store_string(JSON::print(startup_benchmark_json, "\t", false));
}
} else {
List<Variant> keys;
startup_benchmark_json.get_key_list(&keys);
print_line("BENCHMARK:");
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
Variant &K = E->get();
print_line("\t-" + K.operator String() + ": " + startup_benchmark_json[K] + " sec.");
}
}
#endif
}

OS::OS() {
void *volatile stack_bottom;

Expand Down
15 changes: 15 additions & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class OS {
bool restart_on_exit;
List<String> restart_commandline;

// For tracking benchmark data
bool use_benchmark = false;
String benchmark_file;
HashMap<String, uint64_t> start_benchmark_from;
Dictionary startup_benchmark_json;

protected:
bool _update_pending;

Expand Down Expand Up @@ -663,6 +669,15 @@ class OS {
virtual bool request_permissions() { return true; }
virtual Vector<String> get_granted_permissions() const { return Vector<String>(); }

// For recording / measuring benchmark data. Only enabled with tools
void set_use_benchmark(bool p_use_benchmark);
bool is_use_benchmark_set();
void set_benchmark_file(const String &p_benchmark_file);
String get_benchmark_file();
virtual void benchmark_begin_measure(const String &p_what);
virtual void benchmark_end_measure(const String &p_what);
virtual void benchmark_dump();

virtual void process_and_drop_events() {}
OS();
virtual ~OS();
Expand Down
7 changes: 7 additions & 0 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ extern void register_variant_methods();
extern void unregister_variant_methods();

void register_core_types() {
OS::get_singleton()->benchmark_begin_measure("register_core_types");
MemoryPool::setup();

StringName::setup();
Expand Down Expand Up @@ -225,6 +226,8 @@ void register_core_types() {
_classdb = memnew(_ClassDB);
_marshalls = memnew(_Marshalls);
_json = memnew(_JSON);

OS::get_singleton()->benchmark_end_measure("register_core_types");
}

void register_core_settings() {
Expand Down Expand Up @@ -272,6 +275,8 @@ void register_core_singletons() {
}

void unregister_core_types() {
OS::get_singleton()->benchmark_begin_measure("unregister_core_types");

memdelete(_resource_loader);
memdelete(_resource_saver);
memdelete(_os);
Expand Down Expand Up @@ -320,4 +325,6 @@ void unregister_core_types() {
StringName::cleanup();

MemoryPool::cleanup();

OS::get_singleton()->benchmark_end_measure("unregister_core_types");
}
7 changes: 6 additions & 1 deletion doc/classes/VisibilityEnabler2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<member name="process_parent" type="bool" setter="set_enabler" getter="is_enabler_enabled" default="false">
If [code]true[/code], the parent's [method Node._process] will be stopped.
</member>
<member name="visibility_parent" type="bool" setter="set_enabler" getter="is_enabler_enabled" default="true">
If [code]true[/code] and the parent is a [CanvasItem], the parent will be hidden.
</member>
</members>
<constants>
<constant name="ENABLER_PAUSE_ANIMATIONS" value="0" enum="Enabler">
Expand All @@ -67,7 +70,9 @@
<constant name="ENABLER_PAUSE_ANIMATED_SPRITES" value="5" enum="Enabler">
This enabler will stop [AnimatedSprite] nodes animations.
</constant>
<constant name="ENABLER_MAX" value="6" enum="Enabler">
<constant name="ENABLER_PARENT_VISIBILITY" value="6" enum="Enabler">
</constant>
<constant name="ENABLER_MAX" value="7" enum="Enabler">
Represents the size of the [enum Enabler] enum.
</constant>
</constants>
Expand Down
178 changes: 178 additions & 0 deletions editor/directory_create_dialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**************************************************************************/
/* directory_create_dialog.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "directory_create_dialog.h"

#include "core/os/dir_access.h"
#include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "scene/gui/box_container.h"
#include "scene/gui/label.h"
#include "scene/gui/line_edit.h"
#include "scene/gui/panel_container.h"

static String sanitize_input(const String &p_path) {
String path = p_path.strip_edges();
if (path.ends_with("/")) {
path = path.left(path.length() - 1);
}
return path;
}

String DirectoryCreateDialog::_validate_path(const String &p_path) const {
if (p_path.empty()) {
return TTR("Folder name cannot be empty.");
}

const Vector<String> parts = p_path.split("/");
for (int i = 0; i < parts.size(); i++) {
const String part = parts[i];
if (part.empty()) {
return TTR("Folder name cannot be empty.");
}
if (p_path.find("\\") != -1 || p_path.find(":") != -1 || p_path.find("*") != -1 ||
p_path.find("|") != -1 || p_path.find(">") != -1 || p_path.ends_with(".") || p_path.ends_with(" ")) {
return TTR("Folder name contains invalid characters.");
}
}

DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
da->change_dir(base_dir);
if (da->file_exists(p_path)) {
return TTR("File with that name already exists.");
}
if (da->dir_exists(p_path)) {
return TTR("Folder with that name already exists.");
}

return String();
}

void DirectoryCreateDialog::_on_dir_path_changed(const String &p_text) {
const String path = sanitize_input(p_text);
const String error = _validate_path(path);

if (error.empty()) {
status_label->add_color_override("font_color", get_color("success_color", "Editor"));

if (path.find("/") != -1) {
status_label->set_text(TTR("Using slashes in folder names will create subfolders recursively."));
} else {
status_label->set_text(TTR("Folder name is valid."));
}
} else {
status_label->add_color_override("font_color", get_color("error_color", "Editor"));
status_label->set_text(error);
}

get_ok()->set_disabled(!error.empty());
}

void DirectoryCreateDialog::ok_pressed() {
const String path = sanitize_input(dir_path->get_text());

// The OK button should be disabled if the path is invalid, but just in case.
const String error = _validate_path(path);
ERR_FAIL_COND_MSG(!error.empty(), error);

Error err;
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);

err = da->change_dir(base_dir);
ERR_FAIL_COND_MSG(err != OK, "Cannot open directory '" + base_dir + "'.");

print_verbose("Making folder " + path + " in " + base_dir);
err = da->make_dir_recursive(path);

if (err == OK) {
emit_signal("dir_created");
} else {
EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
}
hide();
}

void DirectoryCreateDialog::_post_popup() {
ConfirmationDialog::_post_popup();
label->set_text(vformat(TTR("Create new folder in %s:"), base_dir));
minimum_size_changed();
dir_path->grab_focus();
}

void DirectoryCreateDialog::config(const String &p_base_dir) {
base_dir = p_base_dir;
label->set_text(""); // Set the correct text later in _post_popup to avoid GH-47005.
dir_path->set_text("new folder");
dir_path->select_all();
_on_dir_path_changed(dir_path->get_text());
}

void DirectoryCreateDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("_on_dir_path_changed"), &DirectoryCreateDialog::_on_dir_path_changed);

ADD_SIGNAL(MethodInfo("dir_created"));
}

void DirectoryCreateDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
status_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
} break;
}
}

DirectoryCreateDialog::DirectoryCreateDialog() {
set_title(TTR("Create Folder"));
set_custom_minimum_size(Size2i(480, 0) * EDSCALE);

VBoxContainer *vb = memnew(VBoxContainer);
add_child(vb);

label = memnew(Label);
label->set_autowrap(true);
vb->add_child(label);

dir_path = memnew(LineEdit);
dir_path->connect("text_changed", this, "_on_dir_path_changed");
vb->add_child(dir_path);
register_text_enter(dir_path);

Control *spacing = memnew(Control);
spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
vb->add_child(spacing);

status_panel = memnew(PanelContainer);
status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
vb->add_child(status_panel);

status_label = memnew(Label);
status_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
status_panel->add_child(status_label);
}
68 changes: 68 additions & 0 deletions editor/directory_create_dialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**************************************************************************/
/* directory_create_dialog.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef DIRECTORY_CREATE_DIALOG_H
#define DIRECTORY_CREATE_DIALOG_H

#include "scene/gui/dialogs.h"

class Label;
class LineEdit;
class PanelContainer;

class DirectoryCreateDialog : public ConfirmationDialog {
GDCLASS(DirectoryCreateDialog, ConfirmationDialog);

String base_dir;

Label *label = nullptr;
LineEdit *dir_path = nullptr;

PanelContainer *status_panel = nullptr;
Label *status_label = nullptr;

String _validate_path(const String &p_path) const;

void _on_dir_path_changed(const String &p_text);

protected:
static void _bind_methods();
void _notification(int p_what);

virtual void ok_pressed();
virtual void _post_popup();

public:
void config(const String &p_base_dir);

DirectoryCreateDialog();
};

#endif // DIRECTORY_CREATE_DIALOG_H
Loading

0 comments on commit d8de7a7

Please sign in to comment.