editor_utils_jni.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**************************************************************************/
  2. /* editor_utils_jni.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_utils_jni.h"
  31. #include "jni_utils.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/debugger/editor_debugger_node.h"
  34. #include "editor/debugger/script_editor_debugger.h"
  35. #include "editor/run/editor_run_bar.h"
  36. #include "main/main.h"
  37. #endif
  38. extern "C" {
  39. JNIEXPORT void JNICALL Java_org_godotengine_godot_editor_utils_EditorUtils_runScene(JNIEnv *p_env, jclass, jstring p_scene, jobjectArray p_scene_args) {
  40. #ifdef TOOLS_ENABLED
  41. Vector<String> scene_args;
  42. jint length = p_env->GetArrayLength(p_scene_args);
  43. for (jint i = 0; i < length; ++i) {
  44. jstring j_arg = (jstring)p_env->GetObjectArrayElement(p_scene_args, i);
  45. String arg = jstring_to_string(j_arg, p_env);
  46. scene_args.push_back(arg);
  47. p_env->DeleteLocalRef(j_arg);
  48. }
  49. String scene = jstring_to_string(p_scene, p_env);
  50. EditorRunBar *editor_run_bar = EditorRunBar::get_singleton();
  51. if (editor_run_bar != nullptr) {
  52. editor_run_bar->stop_playing();
  53. // Ensure that all ScriptEditorDebugger instances are explicitly stopped.
  54. // If not, a closing instance from the previous run session will trigger `_stop_and_notify()`, in turn causing
  55. // the closure of the ScriptEditorDebugger instances of the run session we're about to launch.
  56. EditorDebuggerNode *dbg_node = EditorDebuggerNode::get_singleton();
  57. if (dbg_node != nullptr) {
  58. for (int i = 0; ScriptEditorDebugger *dbg = dbg_node->get_debugger(i); i++) {
  59. dbg->stop();
  60. }
  61. }
  62. if (scene.is_empty()) {
  63. editor_run_bar->play_main_scene(false);
  64. } else {
  65. editor_run_bar->play_custom_scene(scene, scene_args);
  66. }
  67. } else {
  68. List<String> args;
  69. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_PROJECT)) {
  70. args.push_back(a);
  71. }
  72. for (const String &arg : scene_args) {
  73. args.push_back(arg);
  74. }
  75. if (!scene.is_empty()) {
  76. args.push_back("--scene");
  77. args.push_back(scene);
  78. }
  79. Error err = OS::get_singleton()->create_instance(args);
  80. ERR_FAIL_COND(err);
  81. }
  82. #endif
  83. }
  84. }