@@ -69,12 +69,14 @@ public GamelogicEngineSwitch[] RequestedSwitches {
6969 return _game ? . AvailableSwitches ?? Array . Empty < GamelogicEngineSwitch > ( ) ;
7070 }
7171 }
72+
7273 public GamelogicEngineCoil [ ] RequestedCoils {
7374 get {
7475 UpdateCaches ( ) ;
7576 return _coils . Values . ToArray ( ) ;
7677 }
7778 }
79+
7880 public GamelogicEngineLamp [ ] RequestedLamps {
7981 get {
8082 UpdateCaches ( ) ;
@@ -101,8 +103,15 @@ public GamelogicEngineLamp[] RequestedLamps {
101103 [ SerializeReference ] private PinMameGame _game ;
102104
103105 private Dictionary < string , GamelogicEngineSwitch > _switches = new ( ) ;
104- private Dictionary < int , GamelogicEngineCoil > _coils = new ( ) ;
105- private Dictionary < int , GamelogicEngineLamp > _lamps = new ( ) ;
106+ private Dictionary < int , string > _pinMameIdToSwitchIdMappings = new ( ) ;
107+ private Dictionary < string , int > _switchIdToPinMameIdMappings = new ( ) ;
108+
109+ private Dictionary < string , GamelogicEngineCoil > _coils = new ( ) ;
110+ private Dictionary < int , string > _pinMameIdToCoilIdMapping = new ( ) ;
111+ private Dictionary < string , int > _coilIdToPinMameIdMapping = new ( ) ;
112+
113+ private Dictionary < string , GamelogicEngineLamp > _lamps = new ( ) ;
114+ private Dictionary < int , string > _pinMameIdToLampIdMapping = new ( ) ;
106115
107116 private bool _isRunning ;
108117 private int _numMechs ;
@@ -164,17 +173,17 @@ private void Update()
164173
165174 // lamps
166175 foreach ( var changedLamp in _pinMame . GetChangedLamps ( ) ) {
167- if ( _lamps . ContainsKey ( changedLamp . Id ) ) {
176+ if ( _pinMameIdToLampIdMapping . ContainsKey ( changedLamp . Id ) ) {
168177 //Logger.Info($"[PinMAME] <= lamp {changedLamp.Id}: {changedLamp.Value}");
169- OnLampChanged ? . Invoke ( this , new LampEventArgs ( _lamps [ changedLamp . Id ] . Id , changedLamp . Id , changedLamp . Value ) ) ;
178+ OnLampChanged ? . Invoke ( this , new LampEventArgs ( _lamps [ _pinMameIdToLampIdMapping [ changedLamp . Id ] ] . Id , changedLamp . Value ) ) ;
170179 }
171180 }
172181
173182 // gi
174183 foreach ( var changedGi in _pinMame . GetChangedGIs ( ) ) {
175- if ( _lamps . ContainsKey ( changedGi . Id ) ) {
184+ if ( _pinMameIdToLampIdMapping . ContainsKey ( changedGi . Id ) ) {
176185 //Logger.Info($"[PinMAME] <= gi {changedGi.Id}: {changedGi.Value}");
177- OnLampChanged ? . Invoke ( this , new LampEventArgs ( _lamps [ changedGi . Id ] . Id , _lamps [ changedGi . Id ] . InternalId , changedGi . Value , LampSource . GI ) ) ;
186+ OnLampChanged ? . Invoke ( this , new LampEventArgs ( _lamps [ _pinMameIdToLampIdMapping [ changedGi . Id ] ] . Id , changedGi . Value , LampSource . GI ) ) ;
178187 } else {
179188 Debug . Log ( $ "No GI { changedGi . Id } found.") ;
180189 }
@@ -279,11 +288,17 @@ private void OnGameStarted()
279288 Logger . Info ( $ "[PinMAME] Game started.") ;
280289 _isRunning = true ;
281290
282- SendInitialSwitches ( ) ;
283- SendMechs ( ) ;
284-
285291 _solenoidDelayStart = DateTimeOffset . Now . ToUnixTimeMilliseconds ( ) ;
286292
293+ try {
294+ SendInitialSwitches ( ) ;
295+ SendMechs ( ) ;
296+ }
297+
298+ catch ( Exception e ) {
299+ Logger . Error ( $ "[PinMAME] OnGameStarted: { e . Message } ") ;
300+ }
301+
287302 lock ( _dispatchQueue ) {
288303 _dispatchQueue . Enqueue ( ( ) => OnStarted ? . Invoke ( this , EventArgs . Empty ) ) ;
289304 }
@@ -300,17 +315,61 @@ private void UpdateCaches()
300315 if ( _game == null ) {
301316 return ;
302317 }
318+
303319 _lamps . Clear ( ) ;
320+ _pinMameIdToLampIdMapping . Clear ( ) ;
321+
304322 _coils . Clear ( ) ;
323+ _pinMameIdToCoilIdMapping . Clear ( ) ;
324+
305325 _switches . Clear ( ) ;
306- foreach ( var lamp in _game . AvailableLamps ) {
307- _lamps [ lamp . InternalId ] = lamp ;
326+ _pinMameIdToSwitchIdMappings . Clear ( ) ;
327+ _switchIdToPinMameIdMappings . Clear ( ) ;
328+
329+ foreach ( var alias in _game . AvailableAliases ) {
330+ switch ( alias . AliasType ) {
331+ case AliasType . Switch :
332+ _pinMameIdToSwitchIdMappings [ alias . Id ] = alias . Name ;
333+ _switchIdToPinMameIdMappings [ alias . Name ] = alias . Id ;
334+ break ;
335+
336+
337+ case AliasType . Coil :
338+ _pinMameIdToCoilIdMapping [ alias . Id ] = alias . Name ;
339+ _coilIdToPinMameIdMapping [ alias . Name ] = alias . Id ;
340+ break ;
341+
342+ case AliasType . Lamp :
343+ _pinMameIdToLampIdMapping [ alias . Id ] = alias . Name ;
344+ break ;
345+ }
308346 }
347+
348+
349+ foreach ( var @switch in _game . AvailableSwitches ) {
350+ _switches [ @switch . Id ] = @switch ;
351+
352+ if ( int . TryParse ( @switch . Id , out int pinMameId ) ) {
353+ _pinMameIdToSwitchIdMappings [ pinMameId ] = @switch . Id ;
354+ _switchIdToPinMameIdMappings [ @switch . Id ] = pinMameId ;
355+ }
356+ }
357+
309358 foreach ( var coil in _game . AvailableCoils ) {
310- _coils [ coil . InternalId ] = coil ;
359+ _coils [ coil . Id ] = coil ;
360+
361+ if ( int . TryParse ( coil . Id , out int pinMameId ) ) {
362+ _pinMameIdToCoilIdMapping [ pinMameId ] = coil . Id ;
363+ _coilIdToPinMameIdMapping [ coil . Id ] = pinMameId ;
364+ }
311365 }
312- foreach ( var sw in _game . AvailableSwitches ) {
313- _switches [ sw . Id ] = sw ;
366+
367+ foreach ( var lamp in _game . AvailableLamps ) {
368+ _lamps [ lamp . Id ] = lamp ;
369+
370+ if ( int . TryParse ( lamp . Id , out int pinMameId ) ) {
371+ _pinMameIdToLampIdMapping [ pinMameId ] = lamp . Id ;
372+ }
314373 }
315374 }
316375
@@ -568,15 +627,15 @@ public void SetCoil(string n, bool value)
568627 {
569628 OnCoilChanged ? . Invoke ( this , new CoilEventArgs ( n , value ) ) ;
570629 }
630+
571631 public bool GetCoil ( string id )
572632 {
573633 return _player != null && _player . CoilStatuses . ContainsKey ( id ) && _player . CoilStatuses [ id ] ;
574634 }
575635
576- private void OnSolenoidUpdated ( int internalId , bool isActive )
636+ private void OnSolenoidUpdated ( int id , bool isActive )
577637 {
578- if ( _coils . ContainsKey ( internalId ) ) {
579-
638+ if ( _pinMameIdToCoilIdMapping . ContainsKey ( id ) ) {
580639 if ( ! _solenoidsEnabled ) {
581640 _solenoidsEnabled = DateTimeOffset . Now . ToUnixTimeMilliseconds ( ) - _solenoidDelayStart >= SolenoidDelay ;
582641
@@ -585,25 +644,21 @@ private void OnSolenoidUpdated(int internalId, bool isActive)
585644 }
586645 }
587646
647+ var coil = _coils [ _pinMameIdToCoilIdMapping [ id ] ] ;
648+
588649 if ( _solenoidsEnabled ) {
589- var coil = _coils [ internalId ] ;
590- var id = coil != null ? coil . Id : internalId . ToString ( ) ;
591- var desc = coil != null ? coil . Description : "-" ;
592- Logger . Info ( $ "[PinMAME] <= coil { id } ({ internalId } ): { isActive } | { desc } ") ;
650+ Logger . Info ( $ "[PinMAME] <= coil { coil . Id } : { isActive } | { coil . Description } ") ;
593651
594652 lock ( _dispatchQueue ) {
595- id = _coils [ internalId ] . Id ;
596- _dispatchQueue . Enqueue ( ( ) => OnCoilChanged ? . Invoke ( this , new CoilEventArgs ( id , isActive ) ) ) ;
653+ _dispatchQueue . Enqueue ( ( ) => OnCoilChanged ? . Invoke ( this , new CoilEventArgs ( coil . Id , isActive ) ) ) ;
597654 }
598655 }
599- else
600- {
601- Logger . Info ( $ "[PinMAME] <= solenoids disabled, coil { _coils [ internalId ] . Id } ({ internalId } ): { isActive } | { _coils [ internalId ] . Description } ") ;
656+ else {
657+ Logger . Info ( $ "[PinMAME] <= solenoids disabled, coil { coil . Id } : { isActive } | { coil . Description } ") ;
602658 }
603659 }
604- else
605- {
606- Logger . Warn ( $ "[PinMAME] <= coil UNMAPPED { internalId } : { isActive } ") ;
660+ else {
661+ Logger . Warn ( $ "[PinMAME] <= coil UNMAPPED { id } : { isActive } ") ;
607662 }
608663 }
609664
@@ -635,22 +690,23 @@ public void SendInitialSwitches()
635690 if ( ! isClosed ) {
636691 continue ;
637692 }
638- if ( _switches . ContainsKey ( id ) && ! _mechSwitches . Contains ( _switches [ id ] . InternalId ) ) {
639- Logger . Info ( $ "[PinMAME] => sw { id } ({ _switches [ id ] . InternalId } ): { true } | { _switches [ id ] . Description } ") ;
640- _pinMame . SetSwitch ( _switches [ id ] . InternalId , true ) ;
693+ if ( _switches . ContainsKey ( id ) && ! _mechSwitches . Contains ( _switchIdToPinMameIdMappings [ _switches [ id ] . Id ] ) ) {
694+ Logger . Info ( $ "[PinMAME] => sw { id } ({ _switches [ id ] . Id } ): { true } | { _switches [ id ] . Description } ") ;
695+
696+ _pinMame . SetSwitch ( _switchIdToPinMameIdMappings [ _switches [ id ] . Id ] , true ) ;
641697 }
642698 }
643699 }
644700
645701 public void Switch ( string id , bool isClosed )
646702 {
647703 if ( _switches . ContainsKey ( id ) ) {
648- if ( _mechSwitches . Contains ( _switches [ id ] . InternalId ) ) {
704+ if ( _mechSwitches . Contains ( _switchIdToPinMameIdMappings [ _switches [ id ] . Id ] ) ) {
649705 // mech switches are triggered internally by pinmame.
650706 return ;
651707 }
652- Logger . Info ( $ "[PinMAME] => sw { id } ( { _switches [ id ] . InternalId } ) : { isClosed } | { _switches [ id ] . Description } ") ;
653- _pinMame . SetSwitch ( _switches [ id ] . InternalId , isClosed ) ;
708+ Logger . Info ( $ "[PinMAME] => sw { id } : { isClosed } | { _switches [ id ] . Description } ") ;
709+ _pinMame . SetSwitch ( _switchIdToPinMameIdMappings [ _switches [ id ] . Id ] , isClosed ) ;
654710 } else {
655711 Logger . Error ( $ "[PinMAME] Unknown switch \" { id } \" .") ;
656712 }
@@ -698,7 +754,7 @@ private void SendMechs()
698754 Logger . Error ( $ "PinMAME only supports up to { max } custom mechs, ignoring { mech . name } .") ;
699755 return ;
700756 }
701- var mechConfig = mech . Config ( _player . SwitchMapping , _player . CoilMapping ) ;
757+ var mechConfig = mech . Config ( _player . SwitchMapping , _player . CoilMapping , _switchIdToPinMameIdMappings , _coilIdToPinMameIdMapping ) ;
702758 _pinMame . SetMech ( id , mechConfig ) ;
703759 foreach ( var c in mechConfig . SwitchList ) {
704760 _mechSwitches . Add ( c . SwNo ) ;
0 commit comments