public DeviceManager() { Logger.Info("Creating Device Manager..."); var definitions = typeof(IControllerDefinition).FindImplementations() .Select(t => (IControllerDefinition)Activator.CreateInstance(t)) .ToList(); var devices = new List <IHidDeviceProxy>(); var controllers = new List <IControllerProxy>(); foreach (var definition in definitions) { Logger.Debug("Searching for \"{0}\" controllers", definition.Name); var detectedDevices = HidDevices.Enumerate(definition.VendorId, definition.ProductIds.ToArray()); var detectedCount = detectedDevices.Count(); if (detectedCount == 0) { continue; } if (detectedCount == 1) { Logger.Trace("Found 1 controller [{vid}, {pid}]", definition.VendorId, detectedDevices.Select(d => d.Attributes.ProductId).First()); } else { Logger.Trace("Found {count} controllers [{vid}, [{pids}]]", detectedCount, definition.VendorId, detectedDevices.Select(d => d.Attributes.ProductId)); } foreach (var device in detectedDevices) { var deviceProxy = new HidDeviceProxy(device); var controller = (IControllerProxy)Activator.CreateInstance(definition.ControllerProxyType, deviceProxy, definition); if (!controller.Init()) { Logger.Warn("Failed to initialize \"{0}\" controller! [{1}, {2}]", definition.Name, device.Attributes.VendorHexId, device.Attributes.ProductHexId); deviceProxy.Dispose(); continue; } Logger.Info("Initialized \"{0}\" controller [{1}, {2}]", definition.Name, device.Attributes.VendorHexId, device.Attributes.ProductHexId); devices.Add(deviceProxy); controllers.Add(controller); } } _deviceProxies = devices; _controllers = controllers; }
private void SearchForControllers() { var controllersCopy = new List <IControllerProxy>(_controllers); Logger.Info("Searching for controller changes"); foreach (var definition in _definitions) { Logger.Debug("Searching for \"{0}\" controllers", definition.Name); var detectedDevices = DeviceList.Local.GetHidDevices().Where(d => d.VendorID == definition.VendorId && definition.ProductIds.Contains(d.ProductID)); var detectedCount = detectedDevices.Count(); _ = controllersCopy.RemoveAll(controller => { if (detectedDevices.Any(d => d.VendorID == controller.VendorId && d.ProductID == controller.ProductId)) { return(false); } if (controller.VendorId != definition.VendorId || !definition.ProductIds.Contains(controller.ProductId)) { return(false); } Logger.Info("Removing missing \"{0}\" controller [{1}, {2}]", controller.Name, controller.ProductId, controller.VendorId); controller.Dispose(); return(true); }); if (detectedCount == 0) { continue; } if (detectedCount == 1) { Logger.Trace("Found 1 new controller [{vid}, {pid}]", definition.VendorId, detectedDevices.Select(d => d.ProductID).First()); } else { Logger.Trace("Found {count} new controllers [{vid}, [{pids}]]", detectedCount, definition.VendorId, detectedDevices.Select(d => d.ProductID)); } foreach (var device in detectedDevices) { if (controllersCopy.Any(c => c.ProductId == device.ProductID && c.VendorId == device.VendorID)) { continue; } var deviceProxy = new HidDeviceProxy(device); var controller = (IControllerProxy)Activator.CreateInstance(definition.ControllerProxyType, deviceProxy, definition); if (!controller.Init()) { Logger.Warn("Failed to initialize \"{0}\" controller! [{1}, {2}]", definition.Name, device.VendorID, device.ProductID); deviceProxy.Dispose(); continue; } Logger.Info("Initialized \"{0}\" controller [{1}, {2}], version: \"{3}\"", definition.Name, device.VendorID, device.ProductID, controller.Version); controllersCopy.Add(controller); } } if (!controllersCopy.ContentsEqual(_controllers)) { Interlocked.Exchange(ref _controllers, controllersCopy); } }