soundvision/UnityProject/Assets/Scripts/PdConnection/PdProcess.cs

70 lines
1.9 KiB
C#
Raw Permalink Normal View History

using System;
2019-09-28 18:17:39 +00:00
using System.Diagnostics;
2019-10-02 08:38:46 +00:00
using System.IO;
2019-09-29 17:47:17 +00:00
using System.Threading;
2019-09-28 18:17:39 +00:00
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace cylvester
{
2019-10-01 20:20:55 +00:00
public class PdProcess
2019-09-28 18:17:39 +00:00
{
private static PdProcess instance_ = null;
private Process pdProcess_;
2019-09-29 16:04:26 +00:00
private PdProcess()
2019-09-28 18:17:39 +00:00
{
2019-09-29 16:04:26 +00:00
} // cannot be instantiate normally
public static PdProcess Instance => instance_ ?? (instance_ = new PdProcess());
2019-09-28 18:17:39 +00:00
2019-10-01 20:20:55 +00:00
public void Start(string mainPatch)
2019-09-28 18:17:39 +00:00
{
2019-09-29 17:47:17 +00:00
2019-09-28 18:17:39 +00:00
if (pdProcess_ != null)
2019-09-29 17:47:17 +00:00
{
pdProcess_.Refresh();
if (!pdProcess_.HasExited)
return;
}
2019-09-28 18:17:39 +00:00
pdProcess_ = new Process();
pdProcess_.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdProcess_.StartInfo.UseShellExecute = false;
2019-09-28 18:17:39 +00:00
pdProcess_.StartInfo.FileName = Application.streamingAssetsPath + "/pd/win/pd.com";
var path = Application.streamingAssetsPath + "/pd/patch/" + mainPatch;
2019-10-02 08:38:46 +00:00
pdProcess_.StartInfo.Arguments = "-nogui -rt \"" + path + "\"";
if (!pdProcess_.Start())
{
throw new Exception("Pd process failed to start");
}
2019-09-29 17:47:17 +00:00
Thread.Sleep(500);
2019-09-28 18:17:39 +00:00
Debug.Log("Pd Process started");
}
public void Stop()
{
2019-09-29 17:47:17 +00:00
if (pdProcess_ == null)
return;
pdProcess_.Kill();
2019-09-28 18:17:39 +00:00
pdProcess_ = null;
Debug.Log("Pd Process stopped");
2019-09-29 17:47:17 +00:00
}
public bool Running
{
get
{
pdProcess_.Refresh();
if (pdProcess_ == null)
return false;
if (pdProcess_.HasExited)
return false;
return true;
}
2019-09-28 18:17:39 +00:00
}
}
}