Quellcode durchsuchen

Fix Godot returned status code on unexpected error

Emmanuel Leblond vor 5 Jahren
Ursprung
Commit
0e4abcb77f
3 geänderte Dateien mit 13 neuen und 1 gelöschten Zeilen
  1. 5 0
      core/os/os.cpp
  2. 4 1
      core/os/os.h
  3. 4 0
      scene/main/scene_tree.cpp

+ 5 - 0
core/os/os.cpp

@@ -226,6 +226,11 @@ int OS::get_exit_code() const {
 
 void OS::set_exit_code(int p_code) {
 	_exit_code = p_code;
+	_is_custom_exit_code = true;
+}
+
+bool OS::is_custom_exit_code() {
+	return _is_custom_exit_code;
 }
 
 String OS::get_locale() const {

+ 4 - 1
core/os/os.h

@@ -40,6 +40,7 @@
 #include "core/templates/vector.h"
 
 #include <stdarg.h>
+#include <stdlib.h>
 
 class OS {
 	static OS *singleton;
@@ -53,7 +54,8 @@ class OS {
 	bool _debug_stdout = false;
 	String _local_clipboard;
 	bool _no_window = false;
-	int _exit_code = 0;
+	int _exit_code = EXIT_FAILURE; // unexpected exit is marked as failure
+	bool _is_custom_exit_code = false;
 	int _orientation;
 	bool _allow_hidpi = false;
 	bool _allow_layered = false;
@@ -268,6 +270,7 @@ public:
 
 	virtual int get_exit_code() const;
 	virtual void set_exit_code(int p_code);
+	virtual bool is_custom_exit_code();
 
 	virtual int get_processor_count() const;
 

+ 4 - 0
scene/main/scene_tree.cpp

@@ -54,6 +54,7 @@
 #include "window.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
 void SceneTreeTimer::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
@@ -538,6 +539,9 @@ void SceneTree::quit(int p_exit_code) {
 		// Override the exit code if a positive argument is given (the default is `-1`).
 		// This is a shorthand for calling `set_exit_code()` on the OS singleton then quitting.
 		OS::get_singleton()->set_exit_code(p_exit_code);
+	} else if (!OS::get_singleton()->is_custom_exit_code()) {
+		// Must customize exit code, otherwise it will default to a non-zero value
+		OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
 	}
 
 	_quit = true;