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

131 lines
3.0 KiB
C#
Raw Normal View History

2022-11-04 22:29:12 +09:00
using System;
using System.Threading;
using System.Threading.Tasks;
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
{
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;
2022-11-03 21:17:09 +09:00
private static bool _isFocused;
2022-11-03 20:59:53 +09:00
/// <summary>
/// Whether or not the event will be received only if the game window is focused.
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once FieldCanBeMadeReadOnly.Global
public static bool RequireFocus = true;
2022-11-05 23:22:59 +09:00
public bool isHookActive;
2022-11-03 20:59:53 +09:00
/// <summary>
/// The key updated event data
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public static readonly UnityEvent<SkyHookEvent> KeyUpdated = new();
/// <summary>
/// The instance of sky hook manager. The instance will be crated if it does not exist
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public static SkyHookManager Instance
{
get
{
if (_instance) return _instance;
var obj = new GameObject("SkyHook Manager");
_instance = obj.AddComponent<SkyHookManager>();
DontDestroyOnLoad(_instance);
return _instance;
}
}
private void HookCallback(SkyHookEvent ev)
{
2022-11-03 21:17:09 +09:00
if (RequireFocus && !_isFocused)
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(() =>
{
2022-11-05 23:22:59 +09:00
if (isHookActive) return;
2022-11-05 00:54:42 +09:00
var result = SkyHookNative.StartHook(HookCallback);
if (result != null)
{
exception = new SkyHookException(result);
}
2022-11-05 23:22:59 +09:00
isHookActive = true;
2022-11-05 00:54:42 +09:00
started = true;
2022-11-05 01:52:26 +09:00
2022-11-05 23:22:59 +09:00
while (isHookActive)
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
{
2022-11-05 23:22:59 +09:00
if (!isHookActive) return;
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-04 22:29:12 +09:00
public static void StartHook()
2022-11-03 20:59:53 +09:00
{
2022-11-04 22:29:12 +09:00
Instance._StartHook();
2022-11-03 20:59:53 +09:00
}
2022-11-04 22:29:12 +09:00
public static void StopHook()
2022-11-03 20:59:53 +09:00
{
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-03 21:00:12 +09:00
if (RequireFocus)
2022-11-03 20:59:53 +09:00
{
2022-11-03 21:17:09 +09:00
_isFocused = Application.isFocused;
2022-11-03 20:59:53 +09:00
}
}
2022-11-03 20:55:22 +09:00
}
}