@@ -69,12 +69,14 @@ public GamelogicEngineSwitch[] RequestedSwitches {
69
69
return _game ? . AvailableSwitches ?? Array . Empty < GamelogicEngineSwitch > ( ) ;
70
70
}
71
71
}
72
+
72
73
public GamelogicEngineCoil [ ] RequestedCoils {
73
74
get {
74
75
UpdateCaches ( ) ;
75
76
return _coils . Values . ToArray ( ) ;
76
77
}
77
78
}
79
+
78
80
public GamelogicEngineLamp [ ] RequestedLamps {
79
81
get {
80
82
UpdateCaches ( ) ;
@@ -101,8 +103,15 @@ public GamelogicEngineLamp[] RequestedLamps {
101
103
[ SerializeReference ] private PinMameGame _game ;
102
104
103
105
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 ( ) ;
106
115
107
116
private bool _isRunning ;
108
117
private int _numMechs ;
@@ -164,17 +173,17 @@ private void Update()
164
173
165
174
// lamps
166
175
foreach ( var changedLamp in _pinMame . GetChangedLamps ( ) ) {
167
- if ( _lamps . ContainsKey ( changedLamp . Id ) ) {
176
+ if ( _pinMameIdToLampIdMapping . ContainsKey ( changedLamp . Id ) ) {
168
177
//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 ) ) ;
170
179
}
171
180
}
172
181
173
182
// gi
174
183
foreach ( var changedGi in _pinMame . GetChangedGIs ( ) ) {
175
- if ( _lamps . ContainsKey ( changedGi . Id ) ) {
184
+ if ( _pinMameIdToLampIdMapping . ContainsKey ( changedGi . Id ) ) {
176
185
//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 ) ) ;
178
187
} else {
179
188
Debug . Log ( $ "No GI { changedGi . Id } found.") ;
180
189
}
@@ -279,11 +288,17 @@ private void OnGameStarted()
279
288
Logger . Info ( $ "[PinMAME] Game started.") ;
280
289
_isRunning = true ;
281
290
282
- SendInitialSwitches ( ) ;
283
- SendMechs ( ) ;
284
-
285
291
_solenoidDelayStart = DateTimeOffset . Now . ToUnixTimeMilliseconds ( ) ;
286
292
293
+ try {
294
+ SendInitialSwitches ( ) ;
295
+ SendMechs ( ) ;
296
+ }
297
+
298
+ catch ( Exception e ) {
299
+ Logger . Error ( $ "[PinMAME] OnGameStarted: { e . Message } ") ;
300
+ }
301
+
287
302
lock ( _dispatchQueue ) {
288
303
_dispatchQueue . Enqueue ( ( ) => OnStarted ? . Invoke ( this , EventArgs . Empty ) ) ;
289
304
}
@@ -300,17 +315,61 @@ private void UpdateCaches()
300
315
if ( _game == null ) {
301
316
return ;
302
317
}
318
+
303
319
_lamps . Clear ( ) ;
320
+ _pinMameIdToLampIdMapping . Clear ( ) ;
321
+
304
322
_coils . Clear ( ) ;
323
+ _pinMameIdToCoilIdMapping . Clear ( ) ;
324
+
305
325
_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
+ }
308
346
}
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
+
309
358
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
+ }
311
365
}
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
+ }
314
373
}
315
374
}
316
375
@@ -568,15 +627,15 @@ public void SetCoil(string n, bool value)
568
627
{
569
628
OnCoilChanged ? . Invoke ( this , new CoilEventArgs ( n , value ) ) ;
570
629
}
630
+
571
631
public bool GetCoil ( string id )
572
632
{
573
633
return _player != null && _player . CoilStatuses . ContainsKey ( id ) && _player . CoilStatuses [ id ] ;
574
634
}
575
635
576
- private void OnSolenoidUpdated ( int internalId , bool isActive )
636
+ private void OnSolenoidUpdated ( int id , bool isActive )
577
637
{
578
- if ( _coils . ContainsKey ( internalId ) ) {
579
-
638
+ if ( _pinMameIdToCoilIdMapping . ContainsKey ( id ) ) {
580
639
if ( ! _solenoidsEnabled ) {
581
640
_solenoidsEnabled = DateTimeOffset . Now . ToUnixTimeMilliseconds ( ) - _solenoidDelayStart >= SolenoidDelay ;
582
641
@@ -585,25 +644,21 @@ private void OnSolenoidUpdated(int internalId, bool isActive)
585
644
}
586
645
}
587
646
647
+ var coil = _coils [ _pinMameIdToCoilIdMapping [ id ] ] ;
648
+
588
649
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 } ") ;
593
651
594
652
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 ) ) ) ;
597
654
}
598
655
}
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 } ") ;
602
658
}
603
659
}
604
- else
605
- {
606
- Logger . Warn ( $ "[PinMAME] <= coil UNMAPPED { internalId } : { isActive } ") ;
660
+ else {
661
+ Logger . Warn ( $ "[PinMAME] <= coil UNMAPPED { id } : { isActive } ") ;
607
662
}
608
663
}
609
664
@@ -635,22 +690,23 @@ public void SendInitialSwitches()
635
690
if ( ! isClosed ) {
636
691
continue ;
637
692
}
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 ) ;
641
697
}
642
698
}
643
699
}
644
700
645
701
public void Switch ( string id , bool isClosed )
646
702
{
647
703
if ( _switches . ContainsKey ( id ) ) {
648
- if ( _mechSwitches . Contains ( _switches [ id ] . InternalId ) ) {
704
+ if ( _mechSwitches . Contains ( _switchIdToPinMameIdMappings [ _switches [ id ] . Id ] ) ) {
649
705
// mech switches are triggered internally by pinmame.
650
706
return ;
651
707
}
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 ) ;
654
710
} else {
655
711
Logger . Error ( $ "[PinMAME] Unknown switch \" { id } \" .") ;
656
712
}
@@ -698,7 +754,7 @@ private void SendMechs()
698
754
Logger . Error ( $ "PinMAME only supports up to { max } custom mechs, ignoring { mech . name } .") ;
699
755
return ;
700
756
}
701
- var mechConfig = mech . Config ( _player . SwitchMapping , _player . CoilMapping ) ;
757
+ var mechConfig = mech . Config ( _player . SwitchMapping , _player . CoilMapping , _switchIdToPinMameIdMappings , _coilIdToPinMameIdMapping ) ;
702
758
_pinMame . SetMech ( id , mechConfig ) ;
703
759
foreach ( var c in mechConfig . SwitchList ) {
704
760
_mechSwitches . Add ( c . SwNo ) ;
0 commit comments