@@ -13,6 +13,8 @@ public abstract class ButtplugDevice : IButtplugDevice
1313
1414 public string Identifier { get ; }
1515
16+ public uint Index { get ; set ; }
17+
1618 public bool IsConnected
1719 {
1820 get
@@ -24,18 +26,36 @@ public bool IsConnected
2426 [ CanBeNull ]
2527 public event EventHandler DeviceRemoved ;
2628
29+ [ CanBeNull ]
30+ public event EventHandler < MessageReceivedEventArgs > MessageEmitted ;
31+
2732 [ NotNull ]
2833 protected readonly IButtplugLog BpLogger ;
34+
2935 [ NotNull ]
30- protected readonly Dictionary < Type , Func < ButtplugDeviceMessage , Task < ButtplugMessage > > > MsgFuncs ;
36+ protected readonly Dictionary < Type , ButtplugDeviceWrapper > MsgFuncs ;
37+
3138 private bool _isDisconnected ;
3239
40+ public class ButtplugDeviceWrapper
41+ {
42+ public Func < ButtplugDeviceMessage , Task < ButtplugMessage > > Function ;
43+ public MessageAttributes Attrs ;
44+
45+ public ButtplugDeviceWrapper ( Func < ButtplugDeviceMessage , Task < ButtplugMessage > > aFunction ,
46+ MessageAttributes aAttrs = null )
47+ {
48+ Function = aFunction ;
49+ Attrs = aAttrs ?? new MessageAttributes ( ) ;
50+ }
51+ }
52+
3353 protected ButtplugDevice ( [ NotNull ] IButtplugLogManager aLogManager ,
3454 [ NotNull ] string aName ,
3555 [ NotNull ] string aIdentifier )
3656 {
3757 BpLogger = aLogManager . GetLogger ( GetType ( ) ) ;
38- MsgFuncs = new Dictionary < Type , Func < ButtplugDeviceMessage , Task < ButtplugMessage > > > ( ) ;
58+ MsgFuncs = new Dictionary < Type , ButtplugDeviceWrapper > ( ) ;
3959 Name = aName ;
4060 Identifier = aIdentifier ;
4161 }
@@ -45,6 +65,16 @@ public IEnumerable<Type> GetAllowedMessageTypes()
4565 return MsgFuncs . Keys ;
4666 }
4767
68+ public MessageAttributes GetMessageAttrs ( Type aMsg )
69+ {
70+ if ( MsgFuncs . TryGetValue ( aMsg , out var wrapper ) )
71+ {
72+ return wrapper . Attrs ?? new MessageAttributes ( ) ;
73+ }
74+
75+ return new MessageAttributes ( ) ;
76+ }
77+
4878 protected void InvokeDeviceRemoved ( )
4979 {
5080 _isDisconnected = true ;
@@ -67,7 +97,7 @@ public async Task<ButtplugMessage> ParseMessage([NotNull] ButtplugDeviceMessage
6797
6898 // We just checked whether the key exists above, so we're ok.
6999 // ReSharper disable once PossibleNullReferenceException
70- return await MsgFuncs [ aMsg . GetType ( ) ] . Invoke ( aMsg ) ;
100+ return await MsgFuncs [ aMsg . GetType ( ) ] . Function . Invoke ( aMsg ) ;
71101 }
72102
73103 public virtual Task < ButtplugMessage > Initialize ( )
@@ -77,5 +107,10 @@ public virtual Task<ButtplugMessage> Initialize()
77107 }
78108
79109 public abstract void Disconnect ( ) ;
110+
111+ protected void EmitMessage ( ButtplugMessage aMsg )
112+ {
113+ MessageEmitted ? . Invoke ( this , new MessageReceivedEventArgs ( aMsg ) ) ;
114+ }
80115 }
81116}
0 commit comments