Skip to content

Commit

Permalink
Convert project to run under latest BepInEx builds (#1)
Browse files Browse the repository at this point in the history
* Add nuget.config

* Move projects to .NET6 and target latest Bep6 builds

* Replace netFm with net6 and rename files to match

* Update README.md

* Fix dll paths

* Re-add netFm projects

* Update readme
  • Loading branch information
ManlyMarco authored Sep 21, 2023
1 parent 11fb5dc commit 00c5c2a
Show file tree
Hide file tree
Showing 38 changed files with 952 additions and 13 deletions.
54 changes: 54 additions & 0 deletions EnableFullScreenToggleIL2CPP_net6/EnableFullScreenToggle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using BepInEx.Unity.IL2CPP;
using Il2CppInterop.Runtime.Injection;
using UnityEngine;
using Input = UnityEngine.Input;
using KeyCode = UnityEngine.KeyCode;

namespace BepInEx
{
/// <summary>
/// Allow toggling full screen with alt+enter in games where that has been disabled
/// </summary>
[BepInPlugin(GUID, PluginName, PluginVersion)]
public class EnableFullScreenToggle : BasePlugin
{
internal const string GUID = "BepInEx.EnableFullScreenToggleIL2CPP_net6";
internal const string PluginName = "Enable Full Screen Toggle";
internal const string PluginVersion = "0.7";

//Game Object shared between all BepInExUtility plugins
public GameObject BepInExUtility;

public override void Load()
{
//IL2CPP don't automatically inherits Monobehavior, so needs to add separatelly
ClassInjector.RegisterTypeInIl2Cpp<FullScreenToggleComponent>();

BepInExUtility = GameObject.Find("BepInExUtility");

if (BepInExUtility == null)
{
BepInExUtility = new GameObject("BepInExUtility");
GameObject.DontDestroyOnLoad(BepInExUtility);
BepInExUtility.hideFlags = HideFlags.HideAndDontSave;
BepInExUtility.AddComponent<FullScreenToggleComponent>();
}
else BepInExUtility.AddComponent<FullScreenToggleComponent>();
}
}

public class FullScreenToggleComponent : MonoBehaviour
{
//Got this from BepInEx Discord pinned messages
public FullScreenToggleComponent(IntPtr handle) : base(handle) { }

internal void Update()
{
if ((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)))
//This section of code is never reached on Unity builds where full screen can be toggled, it seems
//We can safely toggle full screen without risk of it being toggled twice
Screen.fullScreen = !Screen.fullScreen;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.664" />
</ItemGroup>
<ItemGroup>
<Reference Include="Il2Cppmscorlib">
<HintPath>..\IL2CPP_net6\Il2Cppmscorlib.dll</HintPath>
</Reference>
<Reference Include="Il2CppSystem">
<HintPath>..\IL2CPP_net6\Il2CppSystem.dll</HintPath>
</Reference>
<Reference Include="Il2CppSystem.Core">
<HintPath>..\IL2CPP_net6\Il2CppSystem.Core.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\IL2CPP_net6\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.AudioModule">
<HintPath>..\IL2CPP_net6\UnityEngine.AudioModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\IL2CPP_net6\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.IMGUIModule">
<HintPath>..\IL2CPP_net6\UnityEngine.IMGUIModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.InputLegacyModule">
<HintPath>..\IL2CPP_net6\UnityEngine.InputLegacyModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.InputModule">
<HintPath>..\IL2CPP_net6\UnityEngine.InputModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.TextRenderingModule">
<HintPath>..\IL2CPP_net6\UnityEngine.TextRenderingModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>..\IL2CPP_net6\UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UIModule">
<HintPath>..\IL2CPP_net6\UnityEngine.UIModule.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)bin\BepInEx\Plugins\&quot; /s /i /y" />
</Target>
</Project>
8 changes: 8 additions & 0 deletions EnableFullScreenToggleIL2CPP_net6/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;
using static BepInEx.EnableFullScreenToggle;

[assembly: AssemblyTitle(GUID)]
[assembly: AssemblyDescription(PluginName)]
[assembly: AssemblyProduct(GUID)]
[assembly: AssemblyVersion(PluginVersion)]
[assembly: AssemblyFileVersion(PluginVersion)]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F37EF379-2D98-469F-8CBF-EC995F54D7DF}</ProjectGuid>
<ProjectGuid>{8AF94CAA-E027-4AA4-9EE9-87FB1FDF8275}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EnableFullScreenToggleIL2CPP_netFm</RootNamespace>
Expand Down
164 changes: 164 additions & 0 deletions EnableResizeIL2CPP_net6/EnableResize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using BepInEx.Configuration;
using BepInEx.Unity.IL2CPP;
using BepInEx.Unity.IL2CPP.Utils.Collections;
using Il2CppInterop.Runtime.Injection;
using UnityEngine;

namespace BepInEx
{
/// <summary>
/// Enable window resizing in windowed mode.
/// </summary>
[BepInPlugin(GUID, PluginName, PluginVersion)]
public class EnableResize : BasePlugin
{
internal const string GUID = "BepInEx.EnableResizeIL2CPP_net6";
internal const string PluginName = "Enable Resize";
internal const string PluginVersion = "0.7";

//Game Object shared between all BepInExUtility plugins
public GameObject BepInExUtility;

internal static ConfigEntry<bool> ConfigEnableResize { get; private set; }

public override void Load()
{
ConfigEnableResize = Config.Bind("Config", "Enable Resize", true, "Whether to allow the game window to be resized.");

//IL2CPP don't automatically inherits Monobehavior, so needs to add separatelly
ClassInjector.RegisterTypeInIl2Cpp<EnableResizeComponent>();

BepInExUtility = GameObject.Find("BepInExUtility");
if (BepInExUtility == null)
{
BepInExUtility = new GameObject("BepInExUtility");
GameObject.DontDestroyOnLoad(BepInExUtility);
BepInExUtility.hideFlags = HideFlags.HideAndDontSave;
BepInExUtility.AddComponent<EnableResizeComponent>();
}
else BepInExUtility.AddComponent<EnableResizeComponent>();
}
}

public class EnableResizeComponent : MonoBehaviour
{
//Got this from BepInEx Discord pinned messages
public EnableResizeComponent(IntPtr handle) : base(handle) { }


//MonoBehaviour code starts here
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

// Almost the same: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptra
private const int GWL_STYLE = -16;

// https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles
private const int WS_CAPTION = 0XC00000;
private const int WS_MAXIMIZEBOX = 0x10000;
private const int WS_MINIMIZEBOX = 0x20000;
private const int WS_SYSMENU = 0x80000;
private const int WS_THICKFRAME = 0x40000;

private const string GET_CLASS_NAME_MAGIC = "UnityWndClass";
private IntPtr WindowHandle = IntPtr.Zero;

private int windowStyle = 0;
private bool fullScreen = false;
private int borderlessStyle = 1;
private const int borderlessMask = WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_THICKFRAME;
private int resizableStyle = 1;
private const int resizableMask = WS_THICKFRAME | WS_MAXIMIZEBOX;

private WaitForSecondsRealtime oneSecond = new WaitForSecondsRealtime(1f);
private bool isInitialized = false;

internal void Awake()
{
EnableResize.ConfigEnableResize.SettingChanged += (sender, args) => Initialize();
Initialize();
}

private void Initialize()
{
if (!EnableResize.ConfigEnableResize.Value) return;

if (isInitialized)
{
StartCoroutine(TestScreen().WrapToIl2Cpp());
return;
}

var pid = Process.GetCurrentProcess().Id;
EnumWindows((w, param) =>
{
if (w == IntPtr.Zero) return true;
if (GetWindowThreadProcessId(w, out uint lpdwProcessId) == 0) return true;
if (lpdwProcessId != pid) return true;
var cn = new StringBuilder(256);
if (GetClassName(w, cn, cn.Capacity) == 0) return true;
if (cn.ToString() != GET_CLASS_NAME_MAGIC) return true;
WindowHandle = w;
return false;
}, IntPtr.Zero);

if (WindowHandle == IntPtr.Zero) return;
isInitialized = true;
StartCoroutine(TestScreen().WrapToIl2Cpp());
}

private IEnumerator TestScreen()
{
while (true)
{
if (!EnableResize.ConfigEnableResize.Value) yield break;

fullScreen = Screen.fullScreen;
windowStyle = GetWindowLong(WindowHandle, GWL_STYLE);

// If zero, is in borderless mode
borderlessStyle = windowStyle & borderlessMask;

// if zero, is not resizable
resizableStyle = windowStyle & resizableMask;

if (resizableStyle == 0 &&
borderlessStyle != 0 &&
fullScreen == false)
{
ResizeWindow();
}

yield return oneSecond;
}
}

private void ResizeWindow()
{
if (fullScreen) return;
if (borderlessStyle == 0) return;
windowStyle = GetWindowLong(WindowHandle, GWL_STYLE);
windowStyle |= WS_THICKFRAME | WS_MAXIMIZEBOX;
SetWindowLong(WindowHandle, GWL_STYLE, windowStyle);
}
}
}
30 changes: 30 additions & 0 deletions EnableResizeIL2CPP_net6/EnableResizeIL2CPP_net6.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.664" />
</ItemGroup>
<ItemGroup>
<Reference Include="Il2Cppmscorlib">
<HintPath>..\IL2CPP_net6\Il2Cppmscorlib.dll</HintPath>
</Reference>
<Reference Include="Il2CppSystem">
<HintPath>..\IL2CPP_net6\Il2CppSystem.dll</HintPath>
</Reference>
<Reference Include="Il2CppSystem.Core">
<HintPath>..\IL2CPP_net6\Il2CppSystem.Core.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\IL2CPP_net6\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\IL2CPP_net6\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy &quot;$(TargetPath)&quot; &quot;$(SolutionDir)bin\BepInEx\Plugins\&quot; /s /i /y" />
</Target>
</Project>
8 changes: 8 additions & 0 deletions EnableResizeIL2CPP_net6/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Reflection;
using static BepInEx.EnableResize;

[assembly: AssemblyTitle(GUID)]
[assembly: AssemblyDescription(PluginName)]
[assembly: AssemblyProduct(GUID)]
[assembly: AssemblyVersion(PluginVersion)]
[assembly: AssemblyFileVersion(PluginVersion)]
2 changes: 1 addition & 1 deletion EnableResizeIL2CPP_netFm/EnableResizeIL2CPP_netFm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9921DE3B-DEF2-4470-BD8F-23F207128844}</ProjectGuid>
<ProjectGuid>{3E576FAF-8682-44A3-BD1A-71B0924E9EEE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EnableResizeIL2CPP_netFm</RootNamespace>
Expand Down
Loading

0 comments on commit 00c5c2a

Please sign in to comment.