private VJoyControllerManager() { var path = Process.GetCurrentProcess().MainModule.FileName; var filePath = $@"{path.Substring(0, path.LastIndexOf('\\'))}\{(_is64BitRuntime ? "x64" : "x86")}\vJoyInterfaceWrap.dll"; _vJoyAssemblyLoadContext = new VJoyAssemblyLoadContext(); _vJoyInterfaceWrapAssembly = _vJoyAssemblyLoadContext.LoadFromAssemblyPath(filePath); _joystick = Activator.CreateInstance(_vJoyInterfaceWrapAssembly.GetTypes().Single(t => t.Name == "vJoy")); _vJoyType = _joystick.GetType(); IsVJoyEnabled = (bool)_vJoyType.GetMethod("vJoyEnabled").Invoke(_joystick, null); if (!IsVJoyEnabled) { return; } _VjdStatEnumType = _vJoyInterfaceWrapAssembly.GetType("VjdStat"); _hidUsagesEnumType = _vJoyInterfaceWrapAssembly.GetType("HID_USAGES"); _axisEnumValues = new[] { Enum.Parse(_hidUsagesEnumType, "HID_USAGE_X"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_Y"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_Z"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_RX"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_RY"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_RZ"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_SL0"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_SL1"), Enum.Parse(_hidUsagesEnumType, "HID_USAGE_WHL") }; _controllers = new IVJoyController[16]; VJoyManufacturerString = (string)_vJoyType.GetMethod("GetvJoyManufacturerString").Invoke(_joystick, null); VJoyProductString = (string)_vJoyType.GetMethod("GetvJoyProductString").Invoke(_joystick, null); VJoySerialNumberString = (string)_vJoyType.GetMethod("GetvJoySerialNumberString").Invoke(_joystick, null); _getVJDButtonNumber = (Func <uint, int>)_vJoyType.GetMethod("GetVJDButtonNumber").CreateDelegate(typeof(Func <uint, int>), _joystick); _getVJDContPovNumber = (Func <uint, int>)_vJoyType.GetMethod("GetVJDContPovNumber").CreateDelegate(typeof(Func <uint, int>), _joystick); _getVJDDiscPovNumber = (Func <uint, int>)_vJoyType.GetMethod("GetVJDDiscPovNumber").CreateDelegate(typeof(Func <uint, int>), _joystick); _resetAll = (Func <bool>)_vJoyType.GetMethod("ResetAll").CreateDelegate(typeof(Func <bool>), _joystick); _acquireVJD = (Func <uint, bool>)_vJoyType.GetMethod("AcquireVJD").CreateDelegate(typeof(Func <uint, bool>), _joystick); _relinquishVJD = (Action <uint>)_vJoyType.GetMethod("RelinquishVJD").CreateDelegate(typeof(Action <uint>), _joystick); var funcType = typeof(Func <,>).MakeGenericType(new Type[] { typeof(uint), _VjdStatEnumType }); _getVJDStatusFunc = _vJoyType.GetMethod("GetVJDStatus").CreateDelegate(funcType, _joystick); funcType = typeof(Func <, ,>).MakeGenericType(new Type[] { typeof(uint), _hidUsagesEnumType, typeof(bool) }); _getVJDAxisExist = _vJoyType.GetMethod("GetVJDAxisExist").CreateDelegate(funcType, _joystick); var args = new object[] { 0u, 0u }; DriverMatch = (bool)_vJoyType.GetMethod("DriverMatch").Invoke(_joystick, args); DllVer = (uint)args[0]; DrvVer = (uint)args[1]; }
private void UnLoadContext() { _vJoyAssemblyLoadContext.Unload(); _vJoyAssemblyLoadContext = null; }