Decompiled source of Skip Intro v1.0.0

SkipIntro.dll

Decompiled 5 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Logging;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("SkipIntro")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SkipIntro")]
[assembly: AssemblyTitle("SkipIntro")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SkipIntro;

internal class FocusWindow : MonoBehaviour
{
	private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

	private const float GiveUpAfter = 5f;

	private const byte VK_MENU = 18;

	private const uint KEYEVENTF_KEYUP = 2u;

	private const int SW_RESTORE = 9;

	private float elapsed;

	private float nextTry;

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

	[DllImport("user32.dll")]
	private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);

	[DllImport("user32.dll")]
	private static extern bool IsWindowVisible(IntPtr hWnd);

	[DllImport("user32.dll")]
	private static extern IntPtr GetForegroundWindow();

	[DllImport("user32.dll")]
	private static extern bool SetForegroundWindow(IntPtr hWnd);

	[DllImport("user32.dll")]
	private static extern bool BringWindowToTop(IntPtr hWnd);

	[DllImport("user32.dll")]
	private static extern bool ShowWindow(IntPtr hWnd, int cmd);

	[DllImport("user32.dll")]
	private static extern bool IsIconic(IntPtr hWnd);

	[DllImport("user32.dll")]
	private static extern bool AttachThreadInput(uint a, uint b, bool attach);

	[DllImport("user32.dll")]
	private static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra);

	[DllImport("kernel32.dll")]
	private static extern uint GetCurrentThreadId();

	private void Update()
	{
		elapsed += Time.unscaledDeltaTime;
		if (elapsed > 5f)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
		else
		{
			if (elapsed < nextTry)
			{
				return;
			}
			nextTry = elapsed + 0.25f;
			try
			{
				IntPtr intPtr = FindGameWindow();
				if (!(intPtr == IntPtr.Zero))
				{
					if (GetForegroundWindow() == intPtr)
					{
						Object.Destroy((Object)(object)((Component)this).gameObject);
					}
					else
					{
						Focus(intPtr);
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Couldn't focus the game window: " + ex.Message));
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
		}
	}

	private static IntPtr FindGameWindow()
	{
		uint me = (uint)Process.GetCurrentProcess().Id;
		IntPtr found = IntPtr.Zero;
		EnumWindows(delegate(IntPtr h, IntPtr _)
		{
			GetWindowThreadProcessId(h, out var pid);
			if (pid == me && IsWindowVisible(h))
			{
				found = h;
				return false;
			}
			return true;
		}, IntPtr.Zero);
		return found;
	}

	private static void Focus(IntPtr hwnd)
	{
		if (IsIconic(hwnd))
		{
			ShowWindow(hwnd, 9);
		}
		keybd_event(18, 0, 0u, UIntPtr.Zero);
		keybd_event(18, 0, 2u, UIntPtr.Zero);
		uint pid;
		uint windowThreadProcessId = GetWindowThreadProcessId(GetForegroundWindow(), out pid);
		uint currentThreadId = GetCurrentThreadId();
		bool num = windowThreadProcessId != 0 && windowThreadProcessId != currentThreadId && AttachThreadInput(currentThreadId, windowThreadProcessId, attach: true);
		BringWindowToTop(hwnd);
		SetForegroundWindow(hwnd);
		if (num)
		{
			AttachThreadInput(currentThreadId, windowThreadProcessId, attach: false);
		}
	}
}
[BepInPlugin("stickfight.skipintro", "SkipIntro", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "stickfight.skipintro";

	public const string Name = "SkipIntro";

	public const string Version = "1.0.0";

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Expected O, but got Unknown
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		DisableIfPlayed.hasPlayed = true;
		Application.backgroundLoadingPriority = (ThreadPriority)4;
		Log = ((BaseUnityPlugin)this).Logger;
		GameObject val = new GameObject("SkipIntroFocus");
		Object.DontDestroyOnLoad((Object)val);
		((Object)val).hideFlags = (HideFlags)61;
		val.AddComponent<FocusWindow>();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SkipIntro 1.0.0 loaded.");
	}
}