From d2d650cace52a202b20906c5d631a1e73b6e0502 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 19 Apr 2022 16:53:13 -0700 Subject: [PATCH] Use Aaru for media type retrival (.NET 6) --- MPF.Core/Converters/EnumConverter.cs | 5 +- MPF.Core/Data/Drive.cs | 147 ++++++++++++++++++++++----- MPF.Core/MPF.Core.csproj | 1 + MPF.sln | 66 ++++++++++++ 4 files changed, 194 insertions(+), 25 deletions(-) diff --git a/MPF.Core/Converters/EnumConverter.cs b/MPF.Core/Converters/EnumConverter.cs index 8f8fa2c0..d0f2a176 100644 --- a/MPF.Core/Converters/EnumConverter.cs +++ b/MPF.Core/Converters/EnumConverter.cs @@ -79,8 +79,11 @@ namespace MPF.Core.Converters /// /// Aaru.CommonTypes.MediaType value to check /// MediaType if possible, null on error - public static MediaType? MediaTypeToMediaType(this Aaru.CommonTypes.MediaType type) + public static MediaType? MediaTypeToMediaType(this Aaru.CommonTypes.MediaType? type) { + if (type == null) + return null; + return (int)type switch { // Generics diff --git a/MPF.Core/Data/Drive.cs b/MPF.Core/Data/Drive.cs index dfb273a5..4740d337 100644 --- a/MPF.Core/Data/Drive.cs +++ b/MPF.Core/Data/Drive.cs @@ -10,7 +10,9 @@ using System.Management; using IMAPI2; #else using Aaru.CommonTypes.Enums; -using AaruDevices = Aaru.Devices; +using Aaru.Core.Media.Info; +using Aaru.Decoders.SCSI.SSC; +using Aaru.Devices; #endif namespace MPF.Core.Data @@ -106,7 +108,7 @@ namespace MPF.Core.Data /// /// Create a list of active drives matched to their volume labels /// - /// Ture to ignore fixed drives from population, false otherwise + /// True to ignore fixed drives from population, false otherwise /// Active drives, matched to labels, if possible /// /// https://stackoverflow.com/questions/3060796/how-to-distinguish-between-usb-and-floppy-devices?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa @@ -151,7 +153,8 @@ namespace MPF.Core.Data // No-op } #else - // TODO: Add implementation of finding all drives using Aaru.Devices + // TODO: Use this returned drive list once fixed + var driveList = GetDriveList(ignoreFixedDrives); #endif // Order the drives by drive letter @@ -234,27 +237,9 @@ namespace MPF.Core.Data #else try { - // Set a few rough sizes to check for - double cdMaxSize = 850 * Math.Pow(1024, 2); - double dvdMaxSize = 9 * Math.Pow(1024, 3); - - // Attempt to determine media type by size - if (this.TotalSize < cdMaxSize) - return (MediaType.CDROM, $"Detected filesystem: {this.DriveFormat}"); - else if (this.TotalSize < dvdMaxSize) - return (MediaType.DVD, $"Detected filesystem: {this.DriveFormat}"); - else - return (MediaType.BluRay, $"Detected filesystem: {this.DriveFormat}"); - - // Create an Aaru device from the current path - var device = AaruDevices.Device.Create(this.Name, out _); - if (device.Error) - return (null, "Could not open device"); - else if (device.Type != DeviceType.ATAPI && device.Type != DeviceType.SCSI) - return (null, "Device does not support media type detection"); - - // TODO: Use the current device and get the media type from it - + // TODO: Get the device type for devices with set media types + var aaruMediaType = GetMediaType(this.Name); + return (EnumConverter.MediaTypeToMediaType(aaruMediaType), null); } catch (Exception ex) { @@ -515,5 +500,119 @@ namespace MPF.Core.Data /// public void RefreshDrive() => this.driveInfo = DriveInfo.GetDrives().FirstOrDefault(d => d?.Name == this.Name); + +#if NET6_0_OR_GREATER + + /// + /// Get all devices attached converted to Drive objects + /// + /// True to ignore fixed drives from population, false otherwise + /// List of drives, null on error + private static List GetDriveList(bool ignoreFixedDrives) + { + DeviceInfo[] deviceInfos = Device.ListDevices(); + if (deviceInfos == null) + return null; + + var drives = new List(); + foreach (DeviceInfo deviceInfo in deviceInfos) + { + if (deviceInfo.Path == null) + continue; + + var drive = GetDriveFromDevice(deviceInfo.Path, ignoreFixedDrives); + if (drive == null) + continue; + + drives.Add(drive); + } + + return drives; + } + + /// + /// Generate a Drive object from a single device + /// + /// Path to the device + /// True to ignore fixed drives from population, false otherwise + /// Drive object for the device, null on error + private static Drive GetDriveFromDevice(string devicePath, bool ignoreFixedDrives) + { + if (devicePath.Length == 2 && + devicePath[1] == ':' && + devicePath[0] != '/' && + char.IsLetter(devicePath[0])) + devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':'; + + var dev = Device.Create(devicePath, out _); + if (dev == null || dev.Error) + return null; + + // TODO: Narrow down devices to "readable" ones + // TODO: Look at Prettify_0000 for optical disc drive support + + var devInfo = new Aaru.Core.Devices.Info.DeviceInfo(dev); + if (devInfo.MediumDensitySupport != null && devInfo.MediaTypeSupportHeader.HasValue) + { + foreach (DensitySupport.MediaTypeSupportDescriptor descriptor in devInfo.MediaTypeSupportHeader.Value.descriptors) + { + string mediumType = descriptor.name; + } + } + + if (!ignoreFixedDrives) + { + switch (dev.Type) + { + case DeviceType.MMC: + return new Drive(Data.InternalDriveType.Removable, new DriveInfo(devicePath)); + + case DeviceType.SecureDigital: + return new Drive(Data.InternalDriveType.Removable, new DriveInfo(devicePath)); + } + + if (dev.IsUsb) + return new Drive(Data.InternalDriveType.Removable, new DriveInfo(devicePath)); + + if (dev.IsFireWire) + return new Drive(Data.InternalDriveType.Removable, new DriveInfo(devicePath)); + + if (dev.IsPcmcia) + return new Drive(Data.InternalDriveType.Removable, new DriveInfo(devicePath)); + } + + dev.Close(); + return null; + } + + /// + /// Get the media type for a device path using the Aaru libraries + /// + /// Path to the device + /// Aaru MediaType, null on error + private static Aaru.CommonTypes.MediaType? GetMediaType(string devicePath) + { + if (devicePath.Length == 2 && + devicePath[1] == ':' && + devicePath[0] != '/' && + char.IsLetter(devicePath[0])) + devicePath = "\\\\.\\" + char.ToUpper(devicePath[0]) + ':'; + + var dev = Device.Create(devicePath, out _); + if (dev == null || dev.Error) + return null; + + switch (dev.Type) + { + case DeviceType.ATAPI: + case DeviceType.SCSI: + ScsiInfo scsiInfo = new ScsiInfo(dev); + return scsiInfo?.MediaType; + } + + return null; + } + +#endif } } diff --git a/MPF.Core/MPF.Core.csproj b/MPF.Core/MPF.Core.csproj index 35ac8bf5..e6fa0ecb 100644 --- a/MPF.Core/MPF.Core.csproj +++ b/MPF.Core/MPF.Core.csproj @@ -51,6 +51,7 @@ + diff --git a/MPF.sln b/MPF.sln index ca2488a2..00976948 100644 --- a/MPF.sln +++ b/MPF.sln @@ -42,6 +42,28 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Helpers", "Aaru\Aaru.H EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CICMMetadataEditor", "Aaru\CICMMetadata\CICMMetadataEditor\CICMMetadataEditor\CICMMetadataEditor.csproj", "{7BCE5DC7-BA1C-4B59-9AA9-1AB5F81A703C}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Core", "Aaru\Aaru.Core\Aaru.Core.csproj", "{679659B8-25D0-4279-B632-56EF8F94ADC0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Decryption", "Aaru\Aaru.Decryption\Aaru.Decryption.csproj", "{B38840FC-7A2F-4F68-9508-6DE313E04C6E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Archives", "Aaru\Aaru.Archives\Aaru.Archives.csproj", "{282271D0-CCC2-4ED7-BA38-EC06A84BB974}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Dto", "Aaru\Aaru.Dto\Aaru.Dto.csproj", "{F4399FF5-9BD0-475A-9EA7-3DAE45291FE2}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Database", "Aaru\Aaru.Database\Aaru.Database.csproj", "{EE2CE4AA-1422-4A44-8B46-086E01D9AC08}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Filesystems", "Aaru\Aaru.Filesystems\Aaru.Filesystems.csproj", "{D7016DF2-5A5E-4524-B40D-BA2D59576688}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Filters", "Aaru\Aaru.Filters\Aaru.Filters.csproj", "{D571B8EF-903D-4353-BDD5-B834F9F029EF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Images", "Aaru\Aaru.Images\Aaru.Images.csproj", "{74032CBC-339B-42F3-AF6F-E96C261F3E6A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Partitions", "Aaru\Aaru.Partitions\Aaru.Partitions.csproj", "{DA7AB65D-B5BA-4003-8893-A51BB071BA2F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Settings", "Aaru\Aaru.Settings\Aaru.Settings.csproj", "{5C4C7BAA-CF60-4233-84ED-39CB2312AF38}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aaru.Compression", "Aaru\Aaru.Compression\Aaru.Compression.csproj", "{858398D1-7321-4763-8BAB-56BBFEC74E29}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -112,6 +134,50 @@ Global {7BCE5DC7-BA1C-4B59-9AA9-1AB5F81A703C}.Debug|Any CPU.Build.0 = Debug|Any CPU {7BCE5DC7-BA1C-4B59-9AA9-1AB5F81A703C}.Release|Any CPU.ActiveCfg = Release|Any CPU {7BCE5DC7-BA1C-4B59-9AA9-1AB5F81A703C}.Release|Any CPU.Build.0 = Release|Any CPU + {679659B8-25D0-4279-B632-56EF8F94ADC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {679659B8-25D0-4279-B632-56EF8F94ADC0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {679659B8-25D0-4279-B632-56EF8F94ADC0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {679659B8-25D0-4279-B632-56EF8F94ADC0}.Release|Any CPU.Build.0 = Release|Any CPU + {B38840FC-7A2F-4F68-9508-6DE313E04C6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B38840FC-7A2F-4F68-9508-6DE313E04C6E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B38840FC-7A2F-4F68-9508-6DE313E04C6E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B38840FC-7A2F-4F68-9508-6DE313E04C6E}.Release|Any CPU.Build.0 = Release|Any CPU + {282271D0-CCC2-4ED7-BA38-EC06A84BB974}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {282271D0-CCC2-4ED7-BA38-EC06A84BB974}.Debug|Any CPU.Build.0 = Debug|Any CPU + {282271D0-CCC2-4ED7-BA38-EC06A84BB974}.Release|Any CPU.ActiveCfg = Release|Any CPU + {282271D0-CCC2-4ED7-BA38-EC06A84BB974}.Release|Any CPU.Build.0 = Release|Any CPU + {F4399FF5-9BD0-475A-9EA7-3DAE45291FE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4399FF5-9BD0-475A-9EA7-3DAE45291FE2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4399FF5-9BD0-475A-9EA7-3DAE45291FE2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4399FF5-9BD0-475A-9EA7-3DAE45291FE2}.Release|Any CPU.Build.0 = Release|Any CPU + {EE2CE4AA-1422-4A44-8B46-086E01D9AC08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE2CE4AA-1422-4A44-8B46-086E01D9AC08}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE2CE4AA-1422-4A44-8B46-086E01D9AC08}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE2CE4AA-1422-4A44-8B46-086E01D9AC08}.Release|Any CPU.Build.0 = Release|Any CPU + {D7016DF2-5A5E-4524-B40D-BA2D59576688}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7016DF2-5A5E-4524-B40D-BA2D59576688}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7016DF2-5A5E-4524-B40D-BA2D59576688}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7016DF2-5A5E-4524-B40D-BA2D59576688}.Release|Any CPU.Build.0 = Release|Any CPU + {D571B8EF-903D-4353-BDD5-B834F9F029EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D571B8EF-903D-4353-BDD5-B834F9F029EF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D571B8EF-903D-4353-BDD5-B834F9F029EF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D571B8EF-903D-4353-BDD5-B834F9F029EF}.Release|Any CPU.Build.0 = Release|Any CPU + {74032CBC-339B-42F3-AF6F-E96C261F3E6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {74032CBC-339B-42F3-AF6F-E96C261F3E6A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {74032CBC-339B-42F3-AF6F-E96C261F3E6A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74032CBC-339B-42F3-AF6F-E96C261F3E6A}.Release|Any CPU.Build.0 = Release|Any CPU + {DA7AB65D-B5BA-4003-8893-A51BB071BA2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA7AB65D-B5BA-4003-8893-A51BB071BA2F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA7AB65D-B5BA-4003-8893-A51BB071BA2F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA7AB65D-B5BA-4003-8893-A51BB071BA2F}.Release|Any CPU.Build.0 = Release|Any CPU + {5C4C7BAA-CF60-4233-84ED-39CB2312AF38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C4C7BAA-CF60-4233-84ED-39CB2312AF38}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C4C7BAA-CF60-4233-84ED-39CB2312AF38}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C4C7BAA-CF60-4233-84ED-39CB2312AF38}.Release|Any CPU.Build.0 = Release|Any CPU + {858398D1-7321-4763-8BAB-56BBFEC74E29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {858398D1-7321-4763-8BAB-56BBFEC74E29}.Debug|Any CPU.Build.0 = Debug|Any CPU + {858398D1-7321-4763-8BAB-56BBFEC74E29}.Release|Any CPU.ActiveCfg = Release|Any CPU + {858398D1-7321-4763-8BAB-56BBFEC74E29}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE