Browse Source

Merge pull request #46422 from touilleMan/exit-status-on-godot-error-cherry-pick-3.2

[3.2] Fix Godot returned status code on unexpected error
Rémi Verschelde 4 years ago
parent
commit
1aa02d5ceb
3 changed files with 13 additions and 1 deletions
  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

@@ -296,6 +296,11 @@ int OS::get_exit_code() const {
 void OS::set_exit_code(int p_code) {
 void OS::set_exit_code(int p_code) {
 
 
 	_exit_code = 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 {
 String OS::get_locale() const {

+ 4 - 1
core/os/os.h

@@ -40,6 +40,7 @@
 #include "core/vector.h"
 #include "core/vector.h"
 
 
 #include <stdarg.h>
 #include <stdarg.h>
+#include <stdlib.h>
 
 
 class OS {
 class OS {
 
 
@@ -55,7 +56,8 @@ class OS {
 	String _local_clipboard;
 	String _local_clipboard;
 	uint64_t _msec_splash;
 	uint64_t _msec_splash;
 	bool _no_window;
 	bool _no_window;
-	int _exit_code;
+	int _exit_code = EXIT_FAILURE; // unexpected exit is marked as failure
+	bool _is_custom_exit_code = false;
 	int _orientation;
 	int _orientation;
 	bool _allow_hidpi;
 	bool _allow_hidpi;
 	bool _allow_layered;
 	bool _allow_layered;
@@ -488,6 +490,7 @@ public:
 
 
 	virtual int get_exit_code() const;
 	virtual int get_exit_code() const;
 	virtual void set_exit_code(int p_code);
 	virtual void set_exit_code(int p_code);
+	virtual bool is_custom_exit_code();
 
 
 	virtual int get_processor_count() const;
 	virtual int get_processor_count() const;
 
 

+ 4 - 0
scene/main/scene_tree.cpp

@@ -51,6 +51,7 @@
 #include "viewport.h"
 #include "viewport.h"
 
 
 #include <stdio.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 
 void SceneTreeTimer::_bind_methods() {
 void SceneTreeTimer::_bind_methods() {
 
 
@@ -638,6 +639,9 @@ void SceneTree::quit(int p_exit_code) {
 		// Override the exit code if a positive argument is given (the default is `-1`).
 		// 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.
 		// This is a shorthand for calling `set_exit_code()` on the OS singleton then quitting.
 		OS::get_singleton()->set_exit_code(p_exit_code);
 		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;
 	_quit = true;