From f487f2d9349e7928402d62c52ae4175c425a2f9e Mon Sep 17 00:00:00 2001
From: hinto-janaiyo <hinto.janaiyo@protonmail.com>
Date: Sat, 10 Dec 2022 15:35:20 -0500
Subject: [PATCH] windows/pty: include static [VCRUNTIME140.dll], change PTY
 size

Bare metal windows was complaining about this DLL, so it is now
included statically using [https://docs.rs/static_vcruntime/].
I tried statically linking everything for Windows but:
	1. It's not necessary, pretty much all DLLs needed
	   (except this one) are included in Windows 7+ by default
	2. It's a pain in the ass to build everything
	   statically, especially since Gupax needs OpenSSL.
	   (building OpenSSL on Windows == hell)

Windows/macOS were having console artifacts, escape codes and
random newlines would show up in P2Pool/XMRig output. After
thinking about it for a while, I realized I left the PTY
size to the default [24 rows/80 columns], hence the PTYs
prematurely inserting newlines and weird escape codes.

It works fine after setting it to [100/1000]. Interestingly,
Linux worked completely fine on 24/80, probably resizes internally.
---
 Cargo.toml    | 3 +++
 build.rs      | 5 +++++
 src/README.md | 2 +-
 src/helper.rs | 8 ++++----
 src/main.rs   | 5 +++--
 5 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 8abc93e..78f2745 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -21,6 +21,7 @@ dirs = "4.0.0"
 eframe = "0.19.0"
 egui = "0.19.0"
 egui_extras = { version = "0.19.0", features = ["image"] }
+#--------------------------------------------------------------------------------
 ## [external/egui/crates/eframe/src/native/run.rs] line 41: [.with_srgb(true)]
 ## This line causes a [panic!] inside a Windows VM, from a Linux host.
 ## There are many issue threads and PRs to fix it but for now,
@@ -30,6 +31,7 @@ egui_extras = { version = "0.19.0", features = ["image"] }
 #egui = { path = "external/egui/crates/egui" }
 #egui_glow = { path = "external/egui/crates/egui_glow"}
 #egui_extras = { path = "external/egui/crates/egui_extras", features = ["image"] }
+#--------------------------------------------------------------------------------
 env_logger = "0.9.1"
 figment = { version = "0.10.8", features = ["toml"] }
 hyper = "0.14.20"
@@ -66,6 +68,7 @@ is_elevated = "0.1.2"
 # For Windows build (icon)
 [target.'cfg(windows)'.build-dependencies]
 winres = "0.1.12"
+static_vcruntime = "2.0"
 
 # For macOS build (cargo-bundle)
 [package.metadata.bundle]
diff --git a/build.rs b/build.rs
index 29b9b6a..e5ca769 100755
--- a/build.rs
+++ b/build.rs
@@ -5,8 +5,13 @@
 // pre-compiled bytes using [include_bytes!()] on the images in [images/].
 #[cfg(windows)]
 fn main() -> std::io::Result<()> {
+	static_vcruntime::metabuild();
 	let mut res = winres::WindowsResource::new();
+	// This sets the icon.
 	res.set_icon("images/icons/icon.ico");
+	// This sets the [Run as Administrator] metadata flag for Windows.
+	// Why do I do this?: [https://github.com/hinto-janaiyo/gupax/tree/main/src#why-does-gupax-need-to-be-admin-on-windows]
+	// TL;DR: Because Windows.
 	res.set_manifest(r#"
 	<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
 		<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
diff --git a/src/README.md b/src/README.md
index 0859397..7e5c976 100644
--- a/src/README.md
+++ b/src/README.md
@@ -205,4 +205,4 @@ Depending on the privilege used, Gupax will error/panic:
 - Windows: If not admin, warn the user about potential lower XMRig hashrate
 - Unix: IF admin, panic! Don't allow anything. As it should be.
 
-If you're reading this and have a solution (that isn't using Win32) then please, please, teach me. 
+If you're reading this and have a solution (that isn't using Win32), please... please teach me. 
diff --git a/src/helper.rs b/src/helper.rs
index 463ea8b..d7afae8 100644
--- a/src/helper.rs
+++ b/src/helper.rs
@@ -411,8 +411,8 @@ impl Helper {
 		// 1a. Create PTY
 		let pty = portable_pty::native_pty_system();
 		let pair = pty.openpty(portable_pty::PtySize {
-			rows: 24,
-			cols: 80,
+			rows: 100,
+			cols: 1000,
 			pixel_width: 0,
 			pixel_height: 0,
 		}).unwrap();
@@ -703,8 +703,8 @@ impl Helper {
 		// 1a. Create PTY
 		let pty = portable_pty::native_pty_system();
 		let pair = pty.openpty(portable_pty::PtySize {
-			rows: 24,
-			cols: 80,
+			rows: 100,
+			cols: 1000,
 			pixel_width: 0,
 			pixel_height: 0,
 		}).unwrap();
diff --git a/src/main.rs b/src/main.rs
index 12734fc..a636774 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -345,8 +345,9 @@ impl App {
 
 		// Check for privilege. Should be Admin on [Windows] and NOT root on Unix.
 		#[cfg(target_os = "windows")]
-		if !is_elevated::is_elevated() {
-			app.admin = false;
+		if is_elevated::is_elevated() {
+			app.admin = true;
+		} else {
 			error!("Windows | Admin user not detected!");
 			app.error_state.set(format!("Gupax was not launched as Administrator!\nBe warned, XMRig might have less hashrate!"), ErrorFerris::Sudo, ErrorButtons::WindowsAdmin);
 		}