XbpsController.ps1

$mydir = $myinvocation.MyCommand.ScriptBlock.File | Split-Path
$cp = New-Object CodeDom.Compiler.CompilerParameters 
$cp.CompilerOptions = "/unsafe"
#$cp.OutputAssembly = "$mydir\XapController.dll"

$t = Add-Type -PassThru -Name "XapController"  -Namespace Xap -CompilerParameters $cp -UsingNamespace System.IO,
    System.Diagnostics,
    System.Reflection,
    System.Text -MemberDefinition '
    [Flags]
    public enum XboxButton : ushort
    {
        None = 0,
        DPadUp = 0x0001,
        DPadDown = 0x0002,
        DPadLeft = 0x0004,
        DPadRight = 0x0008,
        Start = 0x0010,
        Back = 0x0020,
        LeftThumb = 0x0040,
        RightThumb = 0x0080,
        LeftBumper = 0x100,
        RightBumper = 0x200,
        A = 0x1000,
        B = 0x2000,
        X = 0x4000,
        Y = 0x8000,
        XBox= 0x400,
        Bind = 0x800
    }'
,'
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public unsafe struct DM_XINPUT_GAMEPAD
    {
        [MarshalAs(UnmanagedType.U2)]
        public XboxButton Button;
         
        [MarshalAs(UnmanagedType.U1)]
        public byte RightTrigger;
 
        [MarshalAs(UnmanagedType.U1)]
        public byte LeftTrigger;
         
        [MarshalAs(UnmanagedType.I2)]
        public short LeftThumbstickX;
         
        [MarshalAs(UnmanagedType.I2)]
        public short LeftThumbstickY;
         
        [MarshalAs(UnmanagedType.I2)]
        public short RightThumbstickX;
         
        [MarshalAs(UnmanagedType.I2)]
        public short RightThumbstickY;
    }'
,'
    [DllImport("xbdm")]
    public static extern int DmAutomationSetGamepadState(UInt32 User,
        ref DM_XINPUT_GAMEPAD xGamePad);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationClearGamepadQueue(uint player);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationBindController(uint player, uint length);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationUnbindController(uint player);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationGetInputProcess(uint player, out bool Bound);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationGetUserDefaultProfile(out UInt64 xuid);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationSetUserDefaultProfile(UInt64 xuid);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationConnectController(uint player);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationDisconnectController(uint player);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationQueueGamepadState(uint player,
        DM_XINPUT_GAMEPAD[] gamepads,
        uint[] timers,
        uint[] timerCount,
        uint gamepadCount,
        out uint gamepadsAdded);
    '
,'
    [DllImport("xbdm.dll")]
    public static extern int DmAutomationQueryGamepadQueue(uint player,
        DM_XINPUT_GAMEPAD[] gamepads,
        out uint queueSize,
        out uint itemsInQueue,
        out uint timeRemaining,
        out uint countDurationRemaining);
    '
,'
    public static DM_XINPUT_GAMEPAD NewGamepadInput() { return new DM_XINPUT_GAMEPAD(); }
    '
, '
    public static void SendGamepadInput(uint player, DM_XINPUT_GAMEPAD input) {
        int result = DmAutomationBindController(player - 1, 0);
        if (result != 0x2da0000) {
            string error = DmTranslateError(result);
            if (! String.IsNullOrEmpty(error)) {
                throw new Exception(error);
            }
        }
        result = DmAutomationConnectController(player - 1);
        if (result != 0x2da0000) {
            string error = DmTranslateError(result);
            if (! String.IsNullOrEmpty(error)) {
                throw new Exception(error);
            }
        }
        result = DmAutomationSetGamepadState(player -1, ref input);
        if (result != 0x2da0000) {
            string error = DmTranslateError(result);
            if (! String.IsNullOrEmpty(error)) {
                throw new Exception(error);
            }
        }
    }
    '
,'
    [DllImport("xbdm.dll")]
    static extern int DmTranslateError(int hr, [Out]StringBuilder buffer, out int BufferSize);
    '
,'
    public static string DmTranslateError(int hr) {
        int size = 0;
        StringBuilder buffer = new StringBuilder();
        DmTranslateError(hr, buffer, out size);
        return buffer.ToString();
    }
    '