BorderlessWindow/BoardlessWindow/HarmondMod.cs

141 lines
5.9 KiB
C#

/*▄▄▄▄ ███▄ ▄███▓ ▄████ ▄▄▄██▀▀▀▓█████▄▄▄█████▓
▓█████▄ ▓██▒▀█▀ ██▒ ██▒ ▀█▒ ▒██ ▓█ ▀▓ ██▒ ▓▒
▒██▒ ▄██▓██ ▓██░▒██░▄▄▄░ ░██ ▒███ ▒ ▓██░ ▒░
▒██░█▀ ▒██ ▒██ ░▓█ ██▓▓██▄██▓ ▒▓█ ▄░ ▓██▓ ░
░▓█ ▀█▓▒██▒ ░██▒░▒▓███▀▒ ▓███▒ ░▒████▒ ▒██▒ ░
░▒▓███▀▒░ ▒░ ░ ░ ░▒ ▒ ▒▓▒▒░ ░░ ▒░ ░ ▒ ░░
▒░▒ ░ ░ ░ ░ ░ ░ ▒ ░▒░ ░ ░ ░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
░ ░ ░ ░ ░ ░ ░
Version 1.00
Hot Keys: ctrl+t
*/
using HarmonyLib;
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BoardlessWindow
{
public class HarmondMod : IHarmonyModHooks
{
public static string Logo =
@" ____ __ __ _ ___ __
/ __ )____ _________/ /__ _____/ /__ __________| | / (_)___ ____/ /___ _ __
/ __ / __ \/ ___/ __ / _ \/ ___/ / _ \/ ___/ ___/| | /| / / / __ \/ __ / __ \ | /| / /
/ /_/ / /_/ / / / /_/ / __/ / / / __(__ |__ ) | |/ |/ / / / / / /_/ / /_/ / |/ |/ /
/_____/\____/_/ \__,_/\___/_/ /_/\___/____/____/ |__/|__/_/_/ /_/\__,_/\____/|__/|__/
V1.0.0 by bmgjet";
public static bool isTitleBarHidden = false;
private const int GWL_STYLE = -16;
private const int WS_BORDER = 0x00800000;
private const int WS_DLGFRAME = 0x00400000;
private const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_FRAMECHANGED = 0x0020;
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT { public int Left, Top, Right, Bottom; }
private static void HideTitleBar()
{
IntPtr hWnd = GetActiveWindow();
// Get the current window rectangle
if (GetWindowRect(hWnd, out RECT rect))
{
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
// Remove title bar style
int style = GetWindowLong(hWnd, GWL_STYLE);
style &= ~WS_CAPTION;
SetWindowLong(hWnd, GWL_STYLE, style);
// Move window slightly upwards (-30 pixels) to hide any leftover white strip
SetWindowPos(hWnd, IntPtr.Zero, rect.Left, rect.Top - 5, width, height + 5, SWP_NOZORDER | SWP_FRAMECHANGED);
}
isTitleBarHidden = true;
}
private static void ShowTitleBar()
{
IntPtr hWnd = GetActiveWindow();
// Get the current window rectangle
if (GetWindowRect(hWnd, out RECT rect))
{
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
// Restore title bar style
int style = GetWindowLong(hWnd, GWL_STYLE);
style |= WS_CAPTION;
SetWindowLong(hWnd, GWL_STYLE, style);
// Move window back to original position
SetWindowPos(hWnd, IntPtr.Zero, rect.Left, rect.Top + 5, width, height, SWP_NOZORDER | SWP_FRAMECHANGED);
}
isTitleBarHidden = false;
}
public void OnLoaded(OnHarmonyModLoadedArgs args)//Plugin Load Hook
{
HideTitleBar();
//Output to console window if its loaded.
if (ConsoleWindow.Instance != null) { ConsoleWindow.Instance.PostMultiLine(Logo); }
}
public void OnUnloaded(OnHarmonyModUnloadedArgs args) { ShowTitleBar(); Logo = null; } //Plugin Unload Hook
//Hook Keyboard Input
[HarmonyPatch(typeof(CameraManager), "Update")]
public class CameraManager_Update
{
static bool Prefix(CameraManager __instance)
{
try
{
if (__instance.cam == null) { return false; }
if (Keyboard.current.ctrlKey.isPressed && Keyboard.current.tKey.wasPressedThisFrame) //CTRL+T keys
{
if (isTitleBarHidden) { ShowTitleBar(); }
else { HideTitleBar(); }
return false;
}
}
catch { }
return true;
}
}
//Hook ConsoleWindow Load
[HarmonyPatch(typeof(ConsoleWindow), "Startup")]
public class ConsoleWindow_Startup
{
static void Postfix(ConsoleWindow __instance)
{
try { __instance.PostMultiLine(Logo); } //Post logo to console window
catch { }
}
}
}
}