1
0
mirror of synced 2024-12-13 23:11:07 +01:00
WACVR/Assets/Script/IPCManager/IPCManager.cs

105 lines
3.3 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Security.Principal;
using UnityEngine;
public class IPCManager : MonoBehaviour
{
public static MemoryMappedFile sharedBuffer;
public static MemoryMappedViewAccessor sharedBufferAccessor;
public static bool isInitialized = false;
public static bool[] TouchData = new bool[240];
private void Awake()
{
EnsureInitialization();
}
private static void EnsureInitialization()
{
if (!isInitialized)
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
}
private IEnumerator ReconnectWait()
{
yield return new WaitForSeconds(5);
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
}
private void Reconnect()
{
InitializeIPC("Local\\WACVR_SHARED_BUFFER", 2164);
}
private static void InitializeIPC(string sharedMemoryName, int sharedMemorySize)
{
MemoryMappedFileSecurity CustomSecurity = new MemoryMappedFileSecurity();
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var acct = sid.Translate(typeof(NTAccount)) as NTAccount;
CustomSecurity.AddAccessRule(new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>(acct.ToString(), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
sharedBuffer = MemoryMappedFile.CreateOrOpen(sharedMemoryName, sharedMemorySize, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, CustomSecurity, System.IO.HandleInheritability.Inheritable);
sharedBufferAccessor = sharedBuffer.CreateViewAccessor();
isInitialized = true;
}
public static byte[] GetLightData()
{
EnsureInitialization();
byte[] bytes = new byte[1920];
IPCManager.sharedBufferAccessor.ReadArray<byte>(244, bytes, 0, 1920);
if (bytes[3] == 0)
return null;
return bytes;
}
private void OnDestroy() {
Debug.Log("Disposing IPC");
//CallAfterDelay.Create(0.1f, () => {
//StartCoroutine(DisposeWait());
//});
Dispose();
}
private static IEnumerator DisposeWait()
{
IPCManager.sharedBufferAccessor.Write(244 + 3, 0); // Clear the flag
yield return new WaitForSeconds(0.1f); // Wait if the IPC is still in use
IPCManager.sharedBufferAccessor.Read<byte>(244 + 3, out byte flag); // Get the flag again
if (flag == 0)
{
Dispose();
}
}
private static void Dispose()
{
if (sharedBuffer != null)
{
IPCManager.sharedBuffer.Dispose();
IPCManager.sharedBuffer = null;
IPCManager.sharedBufferAccessor = null;
IPCManager.isInitialized = false;
}
Debug.Log("IPC Disposed");
}
private static void SetTouchData(bool[] datas)
{
EnsureInitialization();
IPCManager.sharedBufferAccessor.WriteArray<bool>(4, datas, 0, 240);
}
public static void SetTouch(int Area, bool State)
{
Area -= 1; //0-239
if (Area < 120) //right side
TouchData[Area + 120] = State;
else if (Area >= 120) //left side
TouchData[Area - 120] = State;
SetTouchData(TouchData);
}
}