浏览代码

Mono: Runtime main args and assembly search fixes
- Setup runtime main args during initialization. This must be done manually by embedders who do not call mono_runtime_run_main. Fixes NullReferenceException in System.Environment.
- Continue to search the assembly in the rest of the search locations if loading it from one of them failed.

Ignacio Etcheverry 7 年之前
父节点
当前提交
fa1d656af4
共有 2 个文件被更改,包括 42 次插入15 次删除
  1. 29 2
      modules/mono/mono_gd/gd_mono.cpp
  2. 13 13
      modules/mono/mono_gd/gd_mono_assembly.cpp

+ 29 - 2
modules/mono/mono_gd/gd_mono.cpp

@@ -74,7 +74,31 @@ void gdmono_MonoPrintCallback(const char *string, mono_bool is_stdout) {
 
 
 GDMono *GDMono::singleton = NULL;
 GDMono *GDMono::singleton = NULL;
 
 
+namespace {
+
+void setup_runtime_main_args() {
+	CharString execpath = OS::get_singleton()->get_executable_path().utf8();
+
+	List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
+
+	List<CharString> cmdline_args_utf8;
+	Vector<char *> main_args;
+	main_args.resize(cmdline_args.size() + 1);
+
+	main_args[0] = execpath.ptrw();
+
+	int i = 1;
+	for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
+		CharString &stored = cmdline_args_utf8.push_back(E->get().utf8())->get();
+		main_args[i] = stored.ptrw();
+		i++;
+	}
+
+	mono_runtime_set_main_args(main_args.size(), main_args.ptrw());
+}
+
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
+
 static bool _wait_for_debugger_msecs(uint32_t p_msecs) {
 static bool _wait_for_debugger_msecs(uint32_t p_msecs) {
 
 
 	do {
 	do {
@@ -96,9 +120,7 @@ static bool _wait_for_debugger_msecs(uint32_t p_msecs) {
 
 
 	return mono_is_debugger_attached();
 	return mono_is_debugger_attached();
 }
 }
-#endif
 
 
-#ifdef DEBUG_ENABLED
 void gdmono_debug_init() {
 void gdmono_debug_init() {
 
 
 	mono_debug_init(MONO_DEBUG_FORMAT_MONO);
 	mono_debug_init(MONO_DEBUG_FORMAT_MONO);
@@ -125,8 +147,11 @@ void gdmono_debug_init() {
 	};
 	};
 	mono_jit_parse_options(2, (char **)options);
 	mono_jit_parse_options(2, (char **)options);
 }
 }
+
 #endif
 #endif
 
 
+} // namespace
+
 void GDMono::initialize() {
 void GDMono::initialize() {
 
 
 	ERR_FAIL_NULL(Engine::get_singleton());
 	ERR_FAIL_NULL(Engine::get_singleton());
@@ -179,6 +204,8 @@ void GDMono::initialize() {
 
 
 	GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread());
 	GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread());
 
 
+	setup_runtime_main_args(); // Required for System.Environment.GetCommandLineArgs
+
 	runtime_initialized = true;
 	runtime_initialized = true;
 
 
 	OS::get_singleton()->print("Mono: Runtime initialized\n");
 	OS::get_singleton()->print("Mono: Runtime initialized\n");

+ 13 - 13
modules/mono/mono_gd/gd_mono_assembly.cpp

@@ -85,19 +85,22 @@ MonoAssembly *GDMonoAssembly::_search_hook(MonoAssemblyName *aname, void *user_d
 			path = search_dir.plus_file(name);
 			path = search_dir.plus_file(name);
 			if (FileAccess::exists(path)) {
 			if (FileAccess::exists(path)) {
 				res = _load_assembly_from(name.get_basename(), path, refonly);
 				res = _load_assembly_from(name.get_basename(), path, refonly);
-				break;
+				if (res != NULL)
+					break;
 			}
 			}
 		} else {
 		} else {
 			path = search_dir.plus_file(name + ".dll");
 			path = search_dir.plus_file(name + ".dll");
 			if (FileAccess::exists(path)) {
 			if (FileAccess::exists(path)) {
 				res = _load_assembly_from(name, path, refonly);
 				res = _load_assembly_from(name, path, refonly);
-				break;
+				if (res != NULL)
+					break;
 			}
 			}
 
 
 			path = search_dir.plus_file(name + ".exe");
 			path = search_dir.plus_file(name + ".exe");
 			if (FileAccess::exists(path)) {
 			if (FileAccess::exists(path)) {
 				res = _load_assembly_from(name, path, refonly);
 				res = _load_assembly_from(name, path, refonly);
-				break;
+				if (res != NULL)
+					break;
 			}
 			}
 		}
 		}
 	}
 	}
@@ -151,19 +154,15 @@ MonoAssembly *GDMonoAssembly::_preload_hook(MonoAssemblyName *aname, char **asse
 				path = search_dir.plus_file(name);
 				path = search_dir.plus_file(name);
 				if (FileAccess::exists(path)) {
 				if (FileAccess::exists(path)) {
 					res = _load_assembly_from(name.get_basename(), path, refonly);
 					res = _load_assembly_from(name.get_basename(), path, refonly);
-					break;
+					if (res != NULL)
+						break;
 				}
 				}
 			} else {
 			} else {
 				path = search_dir.plus_file(name + ".dll");
 				path = search_dir.plus_file(name + ".dll");
 				if (FileAccess::exists(path)) {
 				if (FileAccess::exists(path)) {
 					res = _load_assembly_from(name, path, refonly);
 					res = _load_assembly_from(name, path, refonly);
-					break;
-				}
-
-				path = search_dir.plus_file(name + ".exe");
-				if (FileAccess::exists(path)) {
-					res = _load_assembly_from(name, path, refonly);
-					break;
+					if (res != NULL)
+						break;
 				}
 				}
 			}
 			}
 		}
 		}
@@ -212,14 +211,15 @@ Error GDMonoAssembly::load(bool p_refonly) {
 
 
 	String image_filename(path);
 	String image_filename(path);
 
 
-	MonoImageOpenStatus status;
+	MonoImageOpenStatus status = MONO_IMAGE_OK;
 
 
 	image = mono_image_open_from_data_with_name(
 	image = mono_image_open_from_data_with_name(
 			(char *)&data[0], data.size(),
 			(char *)&data[0], data.size(),
 			true, &status, refonly,
 			true, &status, refonly,
 			image_filename.utf8().get_data());
 			image_filename.utf8().get_data());
 
 
-	ERR_FAIL_COND_V(status != MONO_IMAGE_OK || image == NULL, ERR_FILE_CANT_OPEN);
+	ERR_FAIL_COND_V(status != MONO_IMAGE_OK, ERR_FILE_CANT_OPEN);
+	ERR_FAIL_NULL_V(image, ERR_FILE_CANT_OPEN);
 
 
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
 	String pdb_path(path + ".pdb");
 	String pdb_path(path + ".pdb");