using System; using System.Reflection; using UnityEngine; namespace DanroJump.Hardware.Com { /// /// Получает список COM-портов через reflection, чтобы проект не падал в Unity-профилях без System.IO.Ports. /// public sealed class ReflectionComPortProvider : IComPortProvider { private static readonly Type SerialPortType = Type.GetType("System.IO.Ports.SerialPort, System"); /// /// Возвращает имена доступных serial-портов или пустой список, если API недоступен. /// public string[] GetPortNames() { if (SerialPortType == null) { Debug.LogWarning("[ComSystem] System.IO.Ports.SerialPort is not available in this Unity profile."); return Array.Empty(); } try { return (string[])SerialPortType.GetMethod("GetPortNames", BindingFlags.Public | BindingFlags.Static) ?.Invoke(null, null) ?? Array.Empty(); } catch (Exception exception) { Debug.LogWarning($"[ComSystem] Failed to list COM ports: {Unwrap(exception).Message}"); return Array.Empty(); } } /// /// Возвращает исходную ошибку reflection-вызова без TargetInvocationException-обертки. /// private static Exception Unwrap(Exception exception) { return exception is TargetInvocationException { InnerException: not null } ? exception.InnerException : exception; } } }