59 lines
2.4 KiB
C#
59 lines
2.4 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using Bd2.Client;
|
|
using BD2.GameNames;
|
|
using BepInEx.Configuration;
|
|
using HarmonyLib;
|
|
|
|
namespace Bd2BetterExperience.Window;
|
|
|
|
internal sealed class WindowPreferences : IModule
|
|
{
|
|
private enum StartupMode { GameDefault, Fullscreen, Windowed }
|
|
private static ConfigEntry<StartupMode> mode;
|
|
private static MethodInfo fullscreen;
|
|
private static MethodInfo windowed;
|
|
|
|
public string Id => "WindowPreferences";
|
|
public string Description => "Choose the startup window mode while respecting launcher instance parameters.";
|
|
|
|
public void Install(Harmony harmony)
|
|
{
|
|
mode = Plugin.Settings.Bind("Window", "StartupMode", StartupMode.Fullscreen,
|
|
"GameDefault preserves the game's setting. Fullscreen and Windowed apply once at startup. Launcher instance window parameters take priority. Restart the game after changing this setting.");
|
|
if (ClientSettings.CurrentInstance != null)
|
|
{
|
|
Plugin.Log.LogInfo("Startup window mode follows launcher instance parameters");
|
|
return;
|
|
}
|
|
const BindingFlags all = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
|
MethodInfo initialize = typeof(AppManager).GetGameMethod("InitPCResolution", all, null, Type.EmptyTypes, null)
|
|
?? throw new MissingMethodException("AppManager.InitPCResolution");
|
|
fullscreen = typeof(AppManager).GetGameMethod(nameof(AppManager.SwitchToFullScreen), all, null, [typeof(bool)], null)
|
|
?? throw new MissingMethodException("AppManager.SwitchToFullScreen");
|
|
windowed = typeof(AppManager).GetGameMethod(nameof(AppManager.SwitchToWindowScreen), all, null, [typeof(bool)], null)
|
|
?? throw new MissingMethodException("AppManager.SwitchToWindowScreen");
|
|
harmony.Patch(initialize, postfix: new HarmonyMethod(typeof(WindowPreferences), nameof(AfterResolution)));
|
|
}
|
|
|
|
private static void AfterResolution(AppManager __instance)
|
|
{
|
|
try
|
|
{
|
|
switch (mode.Value)
|
|
{
|
|
case StartupMode.Fullscreen:
|
|
fullscreen.Invoke(__instance, [false]);
|
|
break;
|
|
case StartupMode.Windowed:
|
|
windowed.Invoke(__instance, [true]);
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception error)
|
|
{
|
|
Plugin.Log.LogError("Could not apply startup window mode: " + error);
|
|
}
|
|
}
|
|
}
|