|
using DashSharp; |
|
using DashSharp.Exceptions; |
|
using DashSharp.Models; |
|
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEditor; |
|
using UnityEngine; |
|
|
|
public class DashButton : EditorWindow |
|
{ |
|
public static DashNetwork network; |
|
public bool LogAllPossibleDashHits; |
|
public static string DashButtonMAC = "A021B724FB5B"; |
|
|
|
[MenuItem("Window/Dash Window")] |
|
static void Init() |
|
{ |
|
// Get existing open window or if none, make a new one: |
|
DashButton window = (DashButton)EditorWindow.GetWindow(typeof(DashButton)); |
|
window.Show(); |
|
} |
|
|
|
void OnGUI() |
|
{ |
|
// Draw a simple UI in the editor window |
|
GUILayout.Label("Work in progress Dash Button support <3"); |
|
DashButtonMAC = EditorGUILayout.TextField("Dash Button MAC address", DashButtonMAC); |
|
LogAllPossibleDashHits = EditorGUILayout.Toggle("Log all possible dash button presses", LogAllPossibleDashHits); |
|
} |
|
|
|
void OnEnable() |
|
{ |
|
// hook up assembly reload handles so we can kill the dash network when unity triggeres a reload. Unity will hang if we dont do this cos it wont be able |
|
// to shut down the DashNetwork thread. |
|
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload; |
|
|
|
// set an initial timestamp |
|
last = DateTime.Now - TimeSpan.FromSeconds(refireDelay); |
|
|
|
// When the window is loaded up, lets hook up everything we need to listen for dash button presses |
|
var network = new DashNetwork(); |
|
network.ListenerStarted += network_ListenerStarted; |
|
network.DashButtonProbed += network_DashProbed; |
|
try |
|
{ |
|
network.StartListening(); |
|
} |
|
catch (PcapMissingException) |
|
{ |
|
// You need to install WinPcap to use this!!! |
|
Debug.LogError("No Pcap is missing, please install it."); |
|
} |
|
Console.Read(); |
|
} |
|
|
|
void OnDisable() |
|
{ |
|
// unhook the button handlers |
|
AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload; |
|
Debug.Log("On Disable "); |
|
} |
|
|
|
public void OnBeforeAssemblyReload() |
|
{ |
|
// before unity tries to reload all the .net code assemblies, kill off the dash network.... |
|
Debug.Log("Before Assembly Reload"); |
|
|
|
if (network != null) |
|
{ |
|
network.StopListening(); |
|
network = null; |
|
Debug.Log("Unhooked device...."); |
|
} |
|
} |
|
|
|
void network_DashProbed(object sender, EventArgs e) |
|
{ |
|
var probe = (DashResponse)e; |
|
if (LogAllPossibleDashHits)// || probe.DashMac == DashButtonMAC) |
|
Debug.Log("Possible Dash Button Found: " + probe.DashMac + " seen on " + probe.Device + probe.DashId); |
|
if (probe.DashMac == DashButtonMAC) |
|
Response(probe.DashMac); |
|
} |
|
|
|
DateTime last; |
|
float refireDelay = 3; |
|
void Response(string mac) |
|
{ |
|
var ts = DateTime.Now; |
|
var delta = ts - last; |
|
if (delta.TotalSeconds <= refireDelay) |
|
{ |
|
return; |
|
} |
|
|
|
Debug.Log("DASH BUTTON PRESS DETECTED!!! [MAC:" + mac + "]"); |
|
EditorDispatcher.Dispatch(EditorDispatchActions.TogglePlayMode); |
|
|
|
last = ts; |
|
} |
|
|
|
private static void network_ListenerStarted(object sender, EventArgs e) |
|
{ |
|
Debug.Log(((DashListenerResponse)e).Message); |
|
} |
|
} |