Skip to content

Instantly share code, notes, and snippets.

@SoRA-X7
Created November 3, 2020 11:54
Show Gist options
  • Save SoRA-X7/6bf295a23d8f6b4da41ab9419e37f2b5 to your computer and use it in GitHub Desktop.
Save SoRA-X7/6bf295a23d8f6b4da41ab9419e37f2b5 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Hikari.PPT {
public class PPTSync : IDisposable {
private readonly NamedPipeClientStream pipe;
private bool firstFrame;
private const string PipeName = @"ppt-sync";
private bool disposed;
public PPTSync() {
pipe = new NamedPipeClientStream(PipeName);
try {
pipe.Connect(1000);
} catch (Exception) {
Application.OpenURL(Path.Combine(Application.streamingAssetsPath, "ppt-sync.exe"));
Thread.Sleep(1000);
try {
pipe.Connect(100);
} catch (Exception) {
Dispose();
throw;
}
}
Debug.Log("PPTSync connected");
firstFrame = true;
}
public bool Wait() {
if (!firstFrame) {
try {
pipe.WriteByte(0);
pipe.Flush();
} catch (IOException e) {
return false;
} catch (ObjectDisposedException e) {
return false;
}
}
firstFrame = false;
try {
pipe.ReadByte();
} catch (Exception e) {
return false;
}
return true;
}
public void Dispose() {
if (disposed) return;
pipe.Dispose();
disposed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment