From 37ef0ed868d2a3692148502e857ce177fcc0bfeb Mon Sep 17 00:00:00 2001
From: biwa <6475593+biwa@users.noreply.github.com>
Date: Sat, 10 Apr 2021 19:45:57 +0200
Subject: [PATCH] Fixed a problem where saving a map would hang when the
 nodesbuilder produced too much output on stdout and strerr. Fixes #417. Fixes
 #498

---
 Source/Core/Compilers/NodesCompiler.cs | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/Source/Core/Compilers/NodesCompiler.cs b/Source/Core/Compilers/NodesCompiler.cs
index 23140d9fc..33e70b353 100755
--- a/Source/Core/Compilers/NodesCompiler.cs
+++ b/Source/Core/Compilers/NodesCompiler.cs
@@ -96,12 +96,23 @@ namespace CodeImp.DoomBuilder.Compilers
 			General.WriteLogLine("Program:    " + processinfo.FileName);
 			General.WriteLogLine("Arguments:  " + processinfo.Arguments);
 
-			Process process;
+			string outErr = "";
+			string outMsg = "";
+
+			Process process = new Process();
+			process.StartInfo = processinfo;
+
+			// Redirect stdout and strerr, so that they can be analyzed. This has to be done asynchronously since the output can be
+			// very long, which would cause StandardOutput.ReadToEnd() and StandardError.ReadToEnd() to hang
+			process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => outMsg += e.Data;
+			process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => outErr += e.Data;
 
 			try
 			{
 				// Start the compiler
-				process = Process.Start(processinfo);
+				process.Start();
+				process.BeginOutputReadLine();
+				process.BeginErrorReadLine();
 			}
 			catch(Exception e)
 			{
@@ -110,13 +121,13 @@ namespace CodeImp.DoomBuilder.Compilers
 				return false;
 			}
 
-			//mxd
-			string outErr = process.StandardError.ReadToEnd().Trim().Replace("\b", "");
-			string outMsg = process.StandardOutput.ReadToEnd().Trim().Replace("\b", "");
-			
 			// Wait for compiler to complete
 			process.WaitForExit();
 
+			outErr = outErr.Trim().Replace("\b", "");
+			outMsg = outMsg.Trim().Replace("\b", "");
+
+
 			//mxd
 			bool errorsInNormalOurput = (outMsg.Length > 0 && outMsg.ToLowerInvariant().IndexOf("error") != -1);
 			//zdbsp actually writes building process here, not error info
-- 
GitLab