From 14a404ed37f7db2e5244b32955de294c0aec78c7 Mon Sep 17 00:00:00 2001 From: Herp Derpinstine Date: Sun, 30 Aug 2020 16:48:42 -0500 Subject: [PATCH] v0.2.7.1 Update, See CHANGELOG for Full Details --- CHANGELOG.md | 1 + MelonLoader.ModHandler/BuildInfo.cs | 2 +- MelonLoader.Support.Il2Cpp/Main.cs | 23 +++++++++++++++++++++++ MelonLoader/Console.cpp | 11 +++++------ MelonLoader/Console.h | 4 ++-- MelonLoader/DisableAnalytics.cpp | 2 +- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e6d757b6..fb4baa4ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ 6. Exposed new Console Output Handle and assign it to Managed Console Class Output. (Credits to knah :D) 7. Added Unity 2019.4.3 Dependencies. 8. Bumped Il2CppAssemblyUnhollower Version. +9. Use a different approach to console cleaning for better interop with other tools. (Credits to knah :D) --- diff --git a/MelonLoader.ModHandler/BuildInfo.cs b/MelonLoader.ModHandler/BuildInfo.cs index ce5b02504..43839e31a 100644 --- a/MelonLoader.ModHandler/BuildInfo.cs +++ b/MelonLoader.ModHandler/BuildInfo.cs @@ -6,6 +6,6 @@ public static class BuildInfo public const string Description = "MelonLoader"; public const string Author = "Lava Gang"; public const string Company = "discord.gg/2Wn3N2P"; - public const string Version = "0.2.7"; + public const string Version = "0.2.7.1"; } } \ No newline at end of file diff --git a/MelonLoader.Support.Il2Cpp/Main.cs b/MelonLoader.Support.Il2Cpp/Main.cs index b8dc243ba..b6981003b 100644 --- a/MelonLoader.Support.Il2Cpp/Main.cs +++ b/MelonLoader.Support.Il2Cpp/Main.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Reflection; using UnhollowerBaseLib; using UnhollowerBaseLib.Runtime; using UnhollowerRuntimeLib; @@ -30,6 +31,28 @@ private static ISupportModule Initialize() GetUnityVersionNumbers(out var major, out var minor, out var patch); UnityVersionHandler.Initialize(major, minor, patch); + // Il2CppSystem.Console.SetOut(new Il2CppSystem.IO.StreamWriter(Il2CppSystem.IO.Stream.Null)); + try + { + var il2CppSystemAssembly = Assembly.Load("Il2Cppmscorlib"); + + var consoleType = il2CppSystemAssembly.GetType("Il2CppSystem.Console"); + var streamWriterType = il2CppSystemAssembly.GetType("Il2CppSystem.IO.StreamWriter"); + var streamType = il2CppSystemAssembly.GetType("Il2CppSystem.IO.Stream"); + + var setOutMethod = consoleType.GetMethod("SetOut", BindingFlags.Static | BindingFlags.Public); + var nullStreamField = streamType.GetProperty("Null", BindingFlags.Static | BindingFlags.Public).GetGetMethod(); + var streamWriterCtor = streamWriterType.GetConstructor(new[] {streamType}); + + var nullStream = nullStreamField.Invoke(null, new object[0]); + var steamWriter = streamWriterCtor.Invoke(new[] {nullStream}); + setOutMethod.Invoke(null, new[] {steamWriter}); + } + catch (Exception ex) + { + MelonLogger.LogError($"Console cleaning failed: {ex}"); + } + SetAsLastSiblingDelegateField = IL2CPP.ResolveICall("UnityEngine.Transform::SetAsLastSibling"); ClassInjector.RegisterTypeInIl2Cpp(); diff --git a/MelonLoader/Console.cpp b/MelonLoader/Console.cpp index 89fbb59f8..f42cebba4 100644 --- a/MelonLoader/Console.cpp +++ b/MelonLoader/Console.cpp @@ -21,12 +21,11 @@ void Console::Create() hwndConsole = GetConsoleWindow(); SetTitle(("MelonLoader " + (MelonLoader::DebugMode ? std::string("Debug") : std::string("Normal")) + " Console").c_str()); SetForegroundWindow(hwndConsole); - AlwaysOnTopCheck(); - OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE); - SetStdHandle(STD_OUTPUT_HANDLE, NULL); + AlwaysOnTopCheck(); + freopen_s(reinterpret_cast(stdout), "CONOUT$", "w", stdout); } - else - MessageBox(NULL, ("Failed to Create the " + (MelonLoader::DebugMode ? std::string("Debug") : std::string("Normal")) + " Console!").c_str(), NULL, MB_OK | MB_ICONEXCLAMATION); + // if it returned false, console was already allocated, just grab the handle from it + OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE); } } @@ -70,7 +69,7 @@ void Console::Write(const char* txt) ChromiumCheck(); RainbowCheck(); AlwaysOnTopCheck(); - WriteConsole(OutputHandle, txt, strlen(txt), NULL, NULL); + std::cout << txt; ResetColor(); } }; diff --git a/MelonLoader/Console.h b/MelonLoader/Console.h index 288134f6b..34397789d 100644 --- a/MelonLoader/Console.h +++ b/MelonLoader/Console.h @@ -100,8 +100,8 @@ class Console static void Write(std::string txt) { Write(txt.c_str()); } static void Write(std::string txt, ConsoleColor color) { Write(txt.c_str(), color); } - static void WriteLine(const char* txt) { Write(txt); Write("\n"); } - static void WriteLine(const char* txt, ConsoleColor color) { Write(txt, color); Write("\n"); } + static void WriteLine(const char* txt) { Write(txt); std::cout << std::endl; } + static void WriteLine(const char* txt, ConsoleColor color) { Write(txt, color); std::cout << std::endl; } static void WriteLine(std::string txt) { WriteLine(txt.c_str()); } static void WriteLine(std::string txt, ConsoleColor color) { WriteLine(txt.c_str(), color); } }; \ No newline at end of file diff --git a/MelonLoader/DisableAnalytics.cpp b/MelonLoader/DisableAnalytics.cpp index 00c3c7da0..122ebd906 100644 --- a/MelonLoader/DisableAnalytics.cpp +++ b/MelonLoader/DisableAnalytics.cpp @@ -95,4 +95,4 @@ int DisableAnalytics::Hooked_getaddrinfo(PCSTR pNodeName, PCSTR pServiceName, vo Logger::LogError("Exception caught in getaddrinfo! Returning WSATRY_AGAIN"); return WSATRY_AGAIN; } -} +} \ No newline at end of file