This repository has been archived on 2023-05-28. You can view files and clone it, but cannot push or open issues/pull-requests.
SkyHook-Unity/Runtime/SkyHookManager.cs

173 lines
4.6 KiB
C#
Raw Normal View History

2022-11-04 22:29:12 +09:00
using System;
using System.Threading;
2022-11-03 20:55:22 +09:00
using UnityEngine;
2022-11-03 20:56:56 +09:00
using UnityEngine.Events;
2022-11-03 20:55:22 +09:00
namespace SkyHook
{
/// <summary>
/// Manages SkyHook activity.
/// A "<see cref="GameObject.DontDestroyOnLoad"/>ed" instance will be created automatically upon use.
/// </summary>
2022-11-03 20:59:53 +09:00
public class SkyHookManager : MonoBehaviour
2022-11-03 20:55:22 +09:00
{
2022-11-03 20:59:53 +09:00
private static SkyHookManager _instance;
/// <summary>
/// Whether this process is focused.
/// </summary>
2022-11-06 00:26:57 +09:00
public static bool IsFocused;
2022-11-03 20:59:53 +09:00
/// <summary>
2022-11-06 22:11:11 +09:00
/// Whether or the event will be received only if the game window is focused.
/// Note that only down key events will be ignored.
2022-11-03 20:59:53 +09:00
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once FieldCanBeMadeReadOnly.Global
2022-11-05 23:27:35 +09:00
public bool requireFocus = true;
2022-11-03 20:59:53 +09:00
/// <summary>
/// Whether the hook is active now.
/// </summary>
2022-11-05 23:22:59 +09:00
public bool isHookActive;
2022-11-03 20:59:53 +09:00
/// <summary>
2022-11-06 22:11:11 +09:00
/// Your callback for each key updated events.
/// Use <see cref="UnityEvent.AddListener"/> to register your callback.
2022-11-03 20:59:53 +09:00
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public static readonly UnityEvent<SkyHookEvent> KeyUpdated = new();
/// <summary>
2022-11-06 22:11:11 +09:00
/// The instance of <see cref="SkyHookManager"/>.
/// A new instance will be created if it does not exist.
/// <br/>
/// This will return <c>null</c> if <see cref="Application.isPlaying"/> is <c>false</c>.
2022-11-03 20:59:53 +09:00
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public static SkyHookManager Instance
{
get
{
if (!Application.isPlaying) return null;
2022-11-03 20:59:53 +09:00
if (_instance) return _instance;
var obj = new GameObject("SkyHook Manager");
_instance = obj.AddComponent<SkyHookManager>();
DontDestroyOnLoad(_instance);
return _instance;
}
}
private void HookCallback(SkyHookEvent ev)
{
if (requireFocus && !IsFocused && ev.Type == EventType.KeyPressed)
2022-11-03 20:59:53 +09:00
{
return;
}
KeyUpdated.Invoke(ev);
}
2022-11-04 22:29:12 +09:00
private void _StartHook()
2022-11-03 20:59:53 +09:00
{
2022-11-05 00:54:42 +09:00
var started = false;
Exception exception = null;
2022-11-03 20:59:53 +09:00
2022-11-05 00:54:42 +09:00
new Thread(() =>
{
try
{
var result = SkyHookNative.StartHook(HookCallback);
2022-11-05 00:54:42 +09:00
if (result != null)
{
exception = new SkyHookException(result);
}
2022-11-05 00:54:42 +09:00
isHookActive = true;
started = true;
2022-11-05 01:52:26 +09:00
while (isHookActive)
{
}
}
catch (Exception e)
2022-11-05 01:52:26 +09:00
{
exception = e;
throw;
2022-11-05 01:52:26 +09:00
}
2022-11-05 00:54:42 +09:00
}).Start();
while (!started && exception == null)
2022-11-03 20:59:53 +09:00
{
}
2022-11-05 00:54:42 +09:00
if (exception != null)
{
throw exception;
}
2022-11-03 20:59:53 +09:00
}
2022-11-04 22:29:12 +09:00
private void _StopHook()
2022-11-03 20:59:53 +09:00
{
var result = SkyHookNative.StopHook();
if (result != null)
{
throw new SkyHookException(result);
}
2022-11-05 23:22:59 +09:00
isHookActive = false;
2022-11-03 20:59:53 +09:00
}
2022-11-06 22:11:11 +09:00
/// <summary>
/// Starts the native hook.
/// <br/>
/// This will only work if <see cref="Application.isPlaying"/> is <c>true</c>.
2022-11-06 22:11:11 +09:00
/// </summary>
2022-11-04 22:29:12 +09:00
public static void StartHook()
2022-11-03 20:59:53 +09:00
{
if (!Application.isPlaying)
{
Debug.LogWarning("You may not call StartHook() if the application is not playing.");
return;
}
2022-11-04 22:29:12 +09:00
Instance._StartHook();
2022-11-03 20:59:53 +09:00
}
2022-11-06 22:11:11 +09:00
/// <summary>
/// Stops the native hook.
/// <br/>
/// This will only work if <see cref="Application.isPlaying"/> is <c>true</c>.
2022-11-06 22:11:11 +09:00
/// </summary>
2022-11-04 22:29:12 +09:00
public static void StopHook()
2022-11-03 20:59:53 +09:00
{
if (!Application.isPlaying)
{
Debug.LogWarning("You may not call StopHook() if the application is not playing.");
return;
}
2022-11-04 22:29:12 +09:00
Instance._StopHook();
2022-11-03 20:59:53 +09:00
}
private void OnDestroy()
{
2022-11-04 22:29:12 +09:00
_StopHook();
2022-11-03 20:59:53 +09:00
}
2022-11-03 21:17:09 +09:00
private void Update()
2022-11-03 20:59:53 +09:00
{
2022-11-05 23:27:35 +09:00
if (requireFocus)
2022-11-03 20:59:53 +09:00
{
2022-11-06 00:26:57 +09:00
IsFocused = Application.isFocused;
2022-11-03 20:59:53 +09:00
}
}
2022-11-03 20:55:22 +09:00
}
2022-11-06 22:47:16 +09:00
}