|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using BepInEx.Configuration; |
| 7 | +using BepInEx.Unity.IL2CPP; |
| 8 | +using BepInEx.Unity.IL2CPP.Utils.Collections; |
| 9 | +using Il2CppInterop.Runtime.Injection; |
| 10 | +using UnityEngine; |
| 11 | + |
| 12 | +namespace BepInEx |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Enable window resizing in windowed mode. |
| 16 | + /// </summary> |
| 17 | + [BepInPlugin(GUID, PluginName, PluginVersion)] |
| 18 | + public class EnableResize : BasePlugin |
| 19 | + { |
| 20 | + internal const string GUID = "BepInEx.EnableResizeIL2CPP_net6"; |
| 21 | + internal const string PluginName = "Enable Resize"; |
| 22 | + internal const string PluginVersion = "0.7"; |
| 23 | + |
| 24 | + //Game Object shared between all BepInExUtility plugins |
| 25 | + public GameObject BepInExUtility; |
| 26 | + |
| 27 | + internal static ConfigEntry<bool> ConfigEnableResize { get; private set; } |
| 28 | + |
| 29 | + public override void Load() |
| 30 | + { |
| 31 | + ConfigEnableResize = Config.Bind("Config", "Enable Resize", true, "Whether to allow the game window to be resized."); |
| 32 | + |
| 33 | + //IL2CPP don't automatically inherits Monobehavior, so needs to add separatelly |
| 34 | + ClassInjector.RegisterTypeInIl2Cpp<EnableResizeComponent>(); |
| 35 | + |
| 36 | + BepInExUtility = GameObject.Find("BepInExUtility"); |
| 37 | + if (BepInExUtility == null) |
| 38 | + { |
| 39 | + BepInExUtility = new GameObject("BepInExUtility"); |
| 40 | + GameObject.DontDestroyOnLoad(BepInExUtility); |
| 41 | + BepInExUtility.hideFlags = HideFlags.HideAndDontSave; |
| 42 | + BepInExUtility.AddComponent<EnableResizeComponent>(); |
| 43 | + } |
| 44 | + else BepInExUtility.AddComponent<EnableResizeComponent>(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public class EnableResizeComponent : MonoBehaviour |
| 49 | + { |
| 50 | + //Got this from BepInEx Discord pinned messages |
| 51 | + public EnableResizeComponent(IntPtr handle) : base(handle) { } |
| 52 | + |
| 53 | + |
| 54 | + //MonoBehaviour code starts here |
| 55 | + public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); |
| 56 | + |
| 57 | + [DllImport("user32.dll")] |
| 58 | + private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); |
| 59 | + |
| 60 | + [DllImport("user32.dll", SetLastError = true)] |
| 61 | + private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); |
| 62 | + |
| 63 | + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] |
| 64 | + private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); |
| 65 | + |
| 66 | + [DllImport("user32.dll")] |
| 67 | + public static extern int GetWindowLong(IntPtr hWnd, int nIndex); |
| 68 | + |
| 69 | + [DllImport("user32.dll")] |
| 70 | + public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); |
| 71 | + |
| 72 | + // Almost the same: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptra |
| 73 | + private const int GWL_STYLE = -16; |
| 74 | + |
| 75 | + // https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles |
| 76 | + private const int WS_CAPTION = 0XC00000; |
| 77 | + private const int WS_MAXIMIZEBOX = 0x10000; |
| 78 | + private const int WS_MINIMIZEBOX = 0x20000; |
| 79 | + private const int WS_SYSMENU = 0x80000; |
| 80 | + private const int WS_THICKFRAME = 0x40000; |
| 81 | + |
| 82 | + private const string GET_CLASS_NAME_MAGIC = "UnityWndClass"; |
| 83 | + private IntPtr WindowHandle = IntPtr.Zero; |
| 84 | + |
| 85 | + private int windowStyle = 0; |
| 86 | + private bool fullScreen = false; |
| 87 | + private int borderlessStyle = 1; |
| 88 | + private const int borderlessMask = WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_THICKFRAME; |
| 89 | + private int resizableStyle = 1; |
| 90 | + private const int resizableMask = WS_THICKFRAME | WS_MAXIMIZEBOX; |
| 91 | + |
| 92 | + private WaitForSecondsRealtime oneSecond = new WaitForSecondsRealtime(1f); |
| 93 | + private bool isInitialized = false; |
| 94 | + |
| 95 | + internal void Awake() |
| 96 | + { |
| 97 | + EnableResize.ConfigEnableResize.SettingChanged += (sender, args) => Initialize(); |
| 98 | + Initialize(); |
| 99 | + } |
| 100 | + |
| 101 | + private void Initialize() |
| 102 | + { |
| 103 | + if (!EnableResize.ConfigEnableResize.Value) return; |
| 104 | + |
| 105 | + if (isInitialized) |
| 106 | + { |
| 107 | + StartCoroutine(TestScreen().WrapToIl2Cpp()); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + var pid = Process.GetCurrentProcess().Id; |
| 112 | + EnumWindows((w, param) => |
| 113 | + { |
| 114 | + if (w == IntPtr.Zero) return true; |
| 115 | + if (GetWindowThreadProcessId(w, out uint lpdwProcessId) == 0) return true; |
| 116 | + if (lpdwProcessId != pid) return true; |
| 117 | + var cn = new StringBuilder(256); |
| 118 | + if (GetClassName(w, cn, cn.Capacity) == 0) return true; |
| 119 | + if (cn.ToString() != GET_CLASS_NAME_MAGIC) return true; |
| 120 | + WindowHandle = w; |
| 121 | + return false; |
| 122 | + }, IntPtr.Zero); |
| 123 | + |
| 124 | + if (WindowHandle == IntPtr.Zero) return; |
| 125 | + isInitialized = true; |
| 126 | + StartCoroutine(TestScreen().WrapToIl2Cpp()); |
| 127 | + } |
| 128 | + |
| 129 | + private IEnumerator TestScreen() |
| 130 | + { |
| 131 | + while (true) |
| 132 | + { |
| 133 | + if (!EnableResize.ConfigEnableResize.Value) yield break; |
| 134 | + |
| 135 | + fullScreen = Screen.fullScreen; |
| 136 | + windowStyle = GetWindowLong(WindowHandle, GWL_STYLE); |
| 137 | + |
| 138 | + // If zero, is in borderless mode |
| 139 | + borderlessStyle = windowStyle & borderlessMask; |
| 140 | + |
| 141 | + // if zero, is not resizable |
| 142 | + resizableStyle = windowStyle & resizableMask; |
| 143 | + |
| 144 | + if (resizableStyle == 0 && |
| 145 | + borderlessStyle != 0 && |
| 146 | + fullScreen == false) |
| 147 | + { |
| 148 | + ResizeWindow(); |
| 149 | + } |
| 150 | + |
| 151 | + yield return oneSecond; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void ResizeWindow() |
| 156 | + { |
| 157 | + if (fullScreen) return; |
| 158 | + if (borderlessStyle == 0) return; |
| 159 | + windowStyle = GetWindowLong(WindowHandle, GWL_STYLE); |
| 160 | + windowStyle |= WS_THICKFRAME | WS_MAXIMIZEBOX; |
| 161 | + SetWindowLong(WindowHandle, GWL_STYLE, windowStyle); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments