From 1c8b9ff572ae5fd7edf5a1aef6a873cdb408b037 Mon Sep 17 00:00:00 2001 From: Sarah Date: Sun, 7 Jan 2024 12:54:03 +0100 Subject: [PATCH] replace libc-stdhandle and fix Pastel on windows --- Cargo.lock | 11 ------- MelonLoader/MelonLoader.Shared/Core.cs | 3 +- Rust/MelonBootstrap/Cargo.toml | 1 - .../src/console/os/windows/mod.rs | 31 +++++++------------ build.py | 6 +++- 5 files changed, 19 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ffa638077..e36d74bd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1173,16 +1173,6 @@ version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" -[[package]] -name = "libc-stdhandle" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dac2473dc28934c5e0b82250dab231c9d3b94160d91fe9ff483323b05797551" -dependencies = [ - "cc", - "libc", -] - [[package]] name = "libloading" version = "0.8.1" @@ -1226,7 +1216,6 @@ dependencies = [ "jni", "lazy_static", "libc", - "libc-stdhandle", "libloading", "msgbox", "ndk", diff --git a/MelonLoader/MelonLoader.Shared/Core.cs b/MelonLoader/MelonLoader.Shared/Core.cs index a98ad7b10..263a366f8 100644 --- a/MelonLoader/MelonLoader.Shared/Core.cs +++ b/MelonLoader/MelonLoader.Shared/Core.cs @@ -18,7 +18,8 @@ public static void Startup(string engineModulePath) MelonEnvironment.Initialize(); MelonLaunchOptions.Load(); - Pastel.ConsoleExtensions.Disable(); + if (MelonUtils.IsUnderWineOrSteamProton()) + Pastel.ConsoleExtensions.Disable(); MelonDebug.Msg("MelonLoader.Core.Startup"); diff --git a/Rust/MelonBootstrap/Cargo.toml b/Rust/MelonBootstrap/Cargo.toml index 35dad363e..145a91461 100644 --- a/Rust/MelonBootstrap/Cargo.toml +++ b/Rust/MelonBootstrap/Cargo.toml @@ -15,7 +15,6 @@ lazy_static = "1.4.0" libloading = "0.8.1" dobby-rs = { git = "https://github.com/RinLovesYou/dobby-rs" } libc = "0.2.150" -libc-stdhandle = "0.1.0" utf16string = "0.2.0" [target.'cfg(target_os = "windows")'.dependencies] diff --git a/Rust/MelonBootstrap/src/console/os/windows/mod.rs b/Rust/MelonBootstrap/src/console/os/windows/mod.rs index bcdf31661..b7f0155f7 100644 --- a/Rust/MelonBootstrap/src/console/os/windows/mod.rs +++ b/Rust/MelonBootstrap/src/console/os/windows/mod.rs @@ -1,6 +1,7 @@ //! Windows Console Module. use std::error::Error; +use std::os::windows::io::AsRawHandle; use std::sync::Mutex; use lazy_static::lazy_static; @@ -16,9 +17,9 @@ use windows::{ use crate::{ console_on_top, constants::{IS_ALPHA, MELON_VERSION}, - debug_enabled, + debug, debug_enabled, errors::conerr::ConsoleError, - should_set_title, win_str, debug, + should_set_title, win_str, }; lazy_static! { @@ -48,21 +49,14 @@ pub unsafe fn init() -> Result<(), Box> { SetWindowPos(*window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)?; } - let _ = libc::freopen( - win_str!(b"CONIN$\0"), - win_str!(b"r\0"), - libc_stdhandle::stdin(), - ); - let _ = libc::freopen( - win_str!(b"CONOUT$\0"), - win_str!(b"w\0"), - libc_stdhandle::stdout(), - ); - let _ = libc::freopen( - win_str!(b"CONOUT$\0"), - win_str!(b"w\0"), - libc_stdhandle::stderr(), - ); + let stdout = std::io::stdout(); + let out_handle = stdout.as_raw_handle(); + let out_handle = out_handle as isize; + SetStdHandle(STD_OUTPUT_HANDLE, HANDLE(out_handle)); + let stderr = std::io::stderr(); + let err_handle = stderr.as_raw_handle(); + let err_handle = err_handle as isize; + SetStdHandle(STD_ERROR_HANDLE, HANDLE(err_handle)); // needs to be in its own scope to drop the lock { @@ -77,10 +71,9 @@ pub unsafe fn init() -> Result<(), Box> { let mut mode = CONSOLE_MODE(0); let _ = GetConsoleMode(*output_handle, &mut mode); - mode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; - if SetConsoleMode(*output_handle, mode).is_err() { + if SetConsoleMode(*output_handle, mode).is_err() { mode &= !(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); } else { mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; diff --git a/build.py b/build.py index 72e2e6d3b..06817369f 100644 --- a/build.py +++ b/build.py @@ -91,7 +91,11 @@ def build(target: str): command = CargoCommand command = command.replace("--target=", "--target={}".format(targets[target])) if xwin: - command = command.replace("cargo", "cargo-xwin") + command = command.replace("cargo", "cargo xwin") + if target == "win32": + os.environ["XWIN_ARCH"] = "x86" + os.environ["XWIN_VERSION"] = "17" + print(command)