• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6602636543

22 Oct 2023 08:16AM UTC coverage: 44.104% (-8.4%) from 52.519%
6602636543

Pull #50

github

web-flow
Merge cf342a7b3 into 14b7ce9a7
Pull Request #50: Add XInput in preparation for gamepad triggers + add xmldoc

764 of 2383 branches covered (0.0%)

Branch coverage included in aggregate %.

3060 of 3060 new or added lines in 106 files covered. (100.0%)

3896 of 8183 relevant lines covered (47.61%)

15812.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.17
/Core/Key2Joy.Core/LowLevelInput/SimulatedGamePad/SimulatedGamePadService.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using CommonServiceLocator;
5
using Key2Joy.LowLevelInput.XInput;
6
using SimWinInput;
7

8
namespace Key2Joy.LowLevelInput.SimulatedGamePad;
9

10
/// <inheritdoc />
11
public class SimulatedGamePadService : ISimulatedGamePadService
12
{
13
    private const int MAX_GAMEPADS = 4;
14

15
    private readonly ISimulatedGamePad[] gamePads;
16

17
    public SimulatedGamePadService(ISimulatedGamePad[] gamePads = null)
13✔
18
    {
19
        if (gamePads != null)
13✔
20
        {
21
            this.gamePads = gamePads;
4✔
22
            return;
4✔
23
        }
24

25
        this.gamePads = new ISimulatedGamePad[MAX_GAMEPADS];
9✔
26

27
        for (var index = 0; index < MAX_GAMEPADS; index++)
90✔
28
        {
29
            this.gamePads[index] = new SimulatedGamePad(index);
36✔
30
        }
31
    }
9✔
32

33
    /// <inheritdoc />
34
    public void Initialize()
35
        => SimGamePad.Instance.Initialize();
×
36

37
    /// <inheritdoc />
38
    public void ShutDown()
39
        => SimGamePad.Instance.ShutDown();
2✔
40

41
    /// <inheritdoc />
42
    public ISimulatedGamePad GetGamePad(int gamePadIndex)
43
        => this.gamePads[gamePadIndex];
1✔
44

45
    /// <inheritdoc />
46
    public ISimulatedGamePad[] GetAllGamePads(bool onlyPluggedIn = true)
47
    {
48
        if (onlyPluggedIn)
6✔
49
        {
50
            return this.gamePads.Where(gamePad => gamePad.GetIsPluggedIn()).ToArray();
25✔
51
        }
52

53
        return this.gamePads;
1✔
54
    }
55

56
    /// <inheritdoc />
57
    public IList<IGamePadInfo> GetActiveDevicesInfo()
58
        => this.gamePads
×
59
            .Where(gamePad => gamePad.GetIsPluggedIn())
×
60
            .Select(gamePad => gamePad.GetInfo()).ToList();
×
61

62
    /// <inheritdoc />
63
    public void EnsurePluggedIn(int gamePadIndex)
64
    {
65
        if (gamePadIndex is < 0 or >= MAX_GAMEPADS)
2✔
66
        {
67
            throw new ArgumentOutOfRangeException(nameof(gamePadIndex));
1✔
68
        }
69

70
        var xInputService = ServiceLocator.Current.GetInstance<IXInputService>();
1✔
71
        var physicalDeviceIndexes = xInputService.GetActiveDevicesInfo();
1✔
72

73
        // If the physical device is active at the index, then we can't use that index
74
        if (physicalDeviceIndexes.FirstOrDefault(info => info.Index == gamePadIndex) is IGamePadInfo info)
1!
75
        {
76
            throw new MappingArmingFailedException(
×
77
                $"There is a physical gamepad in use at index {gamePadIndex}. Cannot simulate at that index.");
×
78
        }
79

80
        var gamePad = this.gamePads[gamePadIndex];
1✔
81

82
        if (gamePad.GetIsPluggedIn())
1!
83
        {
84
            return;
×
85
        }
86

87
        gamePad.PlugIn();
1✔
88
    }
1✔
89

90
    /// <inheritdoc />
91
    public void EnsureUnplugged(int gamePadIndex)
92
    {
93
        if (gamePadIndex is < 0 or >= MAX_GAMEPADS)
×
94
        {
95
            throw new ArgumentOutOfRangeException(nameof(gamePadIndex));
×
96
        }
97

98
        var xInputService = ServiceLocator.Current.GetInstance<IXInputService>();
×
99
        var physicalDeviceIndexes = xInputService.GetActiveDevicesInfo();
×
100

101
        // If the physical device is active at the index, then we can't use that index
102
        if (physicalDeviceIndexes.FirstOrDefault(info => info.Index == gamePadIndex) is IGamePadInfo info)
×
103
        {
104
            throw new MappingArmingFailedException(
×
105
                $"There is a physical gamepad in use at index {gamePadIndex}. Cannot simulate at that index.");
×
106
        }
107

108
        var gamePad = this.gamePads[gamePadIndex];
×
109

110
        if (!gamePad.GetIsPluggedIn())
×
111
        {
112
            return;
×
113
        }
114

115
        gamePad.Unplug();
×
116
    }
×
117

118
    /// <inheritdoc />
119
    public void EnsureAllUnplugged()
120
    {
121
        foreach (var gamePad in this.GetAllGamePads(true))
6!
122
        {
123
            gamePad.Unplug();
×
124
        }
125
    }
3✔
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc