editor_run_native.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**************************************************************************/
  2. /* editor_run_native.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. #include "editor_run_native.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/export/editor_export.h"
  33. #include "editor/export/editor_export_platform.h"
  34. #include "editor/settings/editor_settings.h"
  35. #include "editor/themes/editor_scale.h"
  36. void EditorRunNative::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_THEME_CHANGED: {
  39. remote_debug->set_button_icon(get_editor_theme_icon(SNAME("PlayRemote")));
  40. } break;
  41. case NOTIFICATION_PROCESS: {
  42. bool changed = EditorExport::get_singleton()->poll_export_platforms() || first;
  43. if (changed) {
  44. PopupMenu *popup = remote_debug->get_popup();
  45. popup->clear();
  46. int device_shortcut_id = 1;
  47. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  48. Ref<EditorExportPreset> preset = EditorExport::get_singleton()->get_export_preset(i);
  49. Ref<EditorExportPlatform> eep = preset->get_platform();
  50. if (eep.is_null()) {
  51. continue;
  52. }
  53. const int platform_idx = EditorExport::get_singleton()->get_export_platform_index_by_name(eep->get_name());
  54. const int device_count = MIN(eep->get_options_count(), 9000);
  55. String error;
  56. if (device_count > 0 && preset->is_runnable()) {
  57. popup->add_icon_item(eep->get_run_icon(), eep->get_name(), -1);
  58. popup->set_item_disabled(-1, true);
  59. for (int j = 0; j < device_count; j++) {
  60. popup->add_icon_item(eep->get_option_icon(j), eep->get_option_label(j), EditorExport::encode_platform_device_id(platform_idx, j));
  61. popup->set_item_tooltip(-1, eep->get_option_tooltip(j));
  62. popup->set_item_indent(-1, 2);
  63. if (device_shortcut_id <= 4 && eep->is_option_runnable(j)) {
  64. // Assign shortcuts for the first 4 devices added in the list.
  65. popup->set_item_shortcut(-1, ED_GET_SHORTCUT(vformat("remote_deploy/deploy_to_device_%d", device_shortcut_id)), true);
  66. device_shortcut_id += 1;
  67. }
  68. }
  69. }
  70. }
  71. if (popup->get_item_count() == 0) {
  72. remote_debug->set_disabled(true);
  73. remote_debug->set_tooltip_text(TTRC("No Remote Deploy export presets configured."));
  74. } else {
  75. remote_debug->set_disabled(false);
  76. remote_debug->set_tooltip_text(TTRC("Remote Deploy"));
  77. }
  78. first = false;
  79. }
  80. } break;
  81. }
  82. }
  83. void EditorRunNative::_confirm_run_native() {
  84. run_confirmed = true;
  85. resume_run_native();
  86. }
  87. Error EditorRunNative::start_run_native(int p_id) {
  88. ERR_FAIL_COND_V(p_id < 0, FAILED);
  89. const int platform = EditorExport::decode_platform_from_id(p_id);
  90. const int idx = EditorExport::decode_device_from_id(p_id);
  91. resume_id = p_id;
  92. if (!EditorNode::get_singleton()->ensure_main_scene(true)) {
  93. return OK;
  94. }
  95. Ref<EditorExportPlatform> eep = EditorExport::get_singleton()->get_export_platform(platform);
  96. ERR_FAIL_COND_V(eep.is_null(), ERR_UNAVAILABLE);
  97. Ref<EditorExportPreset> preset;
  98. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  99. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  100. if (ep->is_runnable() && ep->get_platform() == eep) {
  101. preset = ep;
  102. break;
  103. }
  104. }
  105. if (preset.is_null()) {
  106. EditorNode::get_singleton()->show_warning(TTR("No runnable export preset found for this platform.\nPlease add a runnable preset in the Export menu or define an existing preset as runnable."));
  107. return ERR_UNAVAILABLE;
  108. }
  109. String architecture = eep->get_device_architecture(idx);
  110. if (!run_confirmed && !architecture.is_empty()) {
  111. String preset_arch = "architectures/" + architecture;
  112. bool is_arch_enabled = preset->get(preset_arch);
  113. if (!is_arch_enabled) {
  114. run_native_confirm->set_text(vformat(TTR("Warning: The CPU architecture \"%s\" is not active in your export preset.\n\nRun \"Remote Deploy\" anyway?"), architecture));
  115. run_native_confirm->popup_centered();
  116. return OK;
  117. }
  118. }
  119. run_confirmed = false;
  120. preset->update_value_overrides();
  121. if (eep->is_option_runnable(idx)) {
  122. emit_signal(SNAME("native_run"), preset);
  123. }
  124. BitField<EditorExportPlatform::DebugFlags> flags = 0;
  125. bool deploy_debug_remote = is_deploy_debug_remote_enabled();
  126. bool deploy_dumb = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_file_server", false);
  127. bool debug_collisions = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_collisions", false);
  128. bool debug_navigation = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_navigation", false);
  129. if (deploy_debug_remote) {
  130. flags.set_flag(EditorExportPlatform::DEBUG_FLAG_REMOTE_DEBUG);
  131. }
  132. if (deploy_dumb) {
  133. flags.set_flag(EditorExportPlatform::DEBUG_FLAG_DUMB_CLIENT);
  134. }
  135. if (debug_collisions) {
  136. flags.set_flag(EditorExportPlatform::DEBUG_FLAG_VIEW_COLLISIONS);
  137. }
  138. if (debug_navigation) {
  139. flags.set_flag(EditorExportPlatform::DEBUG_FLAG_VIEW_NAVIGATION);
  140. }
  141. eep->clear_messages();
  142. Error err = eep->run(preset, idx, flags);
  143. result_dialog_log->clear();
  144. if (eep->fill_log_messages(result_dialog_log, err)) {
  145. if (eep->get_worst_message_type() >= EditorExportPlatform::EXPORT_MESSAGE_ERROR) {
  146. result_dialog->popup_centered_ratio(0.5);
  147. }
  148. }
  149. return err;
  150. }
  151. void EditorRunNative::resume_run_native() {
  152. start_run_native(resume_id);
  153. }
  154. void EditorRunNative::_bind_methods() {
  155. ADD_SIGNAL(MethodInfo("native_run", PropertyInfo(Variant::OBJECT, "preset", PROPERTY_HINT_RESOURCE_TYPE, "EditorExportPreset")));
  156. }
  157. bool EditorRunNative::is_deploy_debug_remote_enabled() const {
  158. return EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_deploy_remote_debug", true);
  159. }
  160. EditorRunNative::EditorRunNative() {
  161. ED_SHORTCUT("remote_deploy/deploy_to_device_1", TTRC("Deploy to First Device in List"), KeyModifierMask::SHIFT | Key::F5);
  162. ED_SHORTCUT_OVERRIDE("remote_deploy/deploy_to_device_1", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
  163. ED_SHORTCUT("remote_deploy/deploy_to_device_2", TTRC("Deploy to Second Device in List"));
  164. ED_SHORTCUT("remote_deploy/deploy_to_device_3", TTRC("Deploy to Third Device in List"));
  165. ED_SHORTCUT("remote_deploy/deploy_to_device_4", TTRC("Deploy to Fourth Device in List"));
  166. remote_debug = memnew(MenuButton);
  167. remote_debug->set_flat(false);
  168. remote_debug->set_theme_type_variation("RunBarButton");
  169. remote_debug->get_popup()->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  170. remote_debug->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditorRunNative::start_run_native));
  171. remote_debug->set_tooltip_text(TTRC("Remote Deploy"));
  172. remote_debug->set_disabled(true);
  173. add_child(remote_debug);
  174. result_dialog = memnew(AcceptDialog);
  175. result_dialog->set_title(TTR("Project Run"));
  176. result_dialog_log = memnew(RichTextLabel);
  177. result_dialog_log->set_custom_minimum_size(Size2(300, 80) * EDSCALE);
  178. result_dialog->add_child(result_dialog_log);
  179. add_child(result_dialog);
  180. result_dialog->hide();
  181. run_native_confirm = memnew(ConfirmationDialog);
  182. add_child(run_native_confirm);
  183. run_native_confirm->connect(SceneStringName(confirmed), callable_mp(this, &EditorRunNative::_confirm_run_native));
  184. set_process(true);
  185. }