Sfoglia il codice sorgente

Merge pull request #109 from notfood/master

Allow Console to fail gracefully if missing extern
Geoffrey Horsington 4 anni fa
parent
commit
6f2efa5f7e
1 ha cambiato i file con 21 aggiunte e 13 eliminazioni
  1. 21 13
      BepInEx/ConsoleUtil/Kon.cs

+ 21 - 13
BepInEx/ConsoleUtil/Kon.cs

@@ -64,20 +64,28 @@ namespace BepInEx.ConsoleUtil
 			succeeded = false;
 			if (!(conOut == INVALID_HANDLE_VALUE))
 			{
-				CONSOLE_SCREEN_BUFFER_INFO console_SCREEN_BUFFER_INFO;
-				if (!GetConsoleScreenBufferInfo(conOut, out console_SCREEN_BUFFER_INFO))
+				try
 				{
-					bool consoleScreenBufferInfo = GetConsoleScreenBufferInfo(GetStdHandle(-12), out console_SCREEN_BUFFER_INFO);
-					if (!consoleScreenBufferInfo)
-						consoleScreenBufferInfo = GetConsoleScreenBufferInfo(GetStdHandle(-10), out console_SCREEN_BUFFER_INFO);
-
-					if (!consoleScreenBufferInfo)
-						if (Marshal.GetLastWin32Error() == 6 && !throwOnNoConsole)
-							return default(CONSOLE_SCREEN_BUFFER_INFO);
+					// FIXME: Windows console shouldn't be used in other OSs in the first place
+					CONSOLE_SCREEN_BUFFER_INFO console_SCREEN_BUFFER_INFO;
+					if (!GetConsoleScreenBufferInfo(conOut, out console_SCREEN_BUFFER_INFO))
+					{
+						bool consoleScreenBufferInfo = GetConsoleScreenBufferInfo(GetStdHandle(-12), out console_SCREEN_BUFFER_INFO);
+						if (!consoleScreenBufferInfo)
+							consoleScreenBufferInfo = GetConsoleScreenBufferInfo(GetStdHandle(-10), out console_SCREEN_BUFFER_INFO);
+						
+						if (!consoleScreenBufferInfo)
+							if (Marshal.GetLastWin32Error() == 6 && !throwOnNoConsole)
+								return default(CONSOLE_SCREEN_BUFFER_INFO);
+							
+							succeeded = true;
+						return console_SCREEN_BUFFER_INFO;
+					}
+				}
+				catch (EntryPointNotFoundException)
+				{
+					// Fails under unsupported OSes
 				}
-
-				succeeded = true;
-				return console_SCREEN_BUFFER_INFO;
 			}
 
 			if (!throwOnNoConsole)
@@ -141,4 +149,4 @@ namespace BepInEx.ConsoleUtil
 
 		#endregion
 	}
-}
+}