Use Aaru for media type retrival (.NET 6)

This commit is contained in:
Matt Nadareski
2022-04-19 16:53:13 -07:00
parent 772cefd700
commit d2d650cace
4 changed files with 194 additions and 25 deletions

View File

@@ -79,8 +79,11 @@ namespace MPF.Core.Converters
/// </summary>
/// <param name="type">Aaru.CommonTypes.MediaType value to check</param>
/// <returns>MediaType if possible, null on error</returns>
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

View File

@@ -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
/// <summary>
/// Create a list of active drives matched to their volume labels
/// </summary>
/// <param name="ignoreFixedDrives">Ture to ignore fixed drives from population, false otherwise</param>
/// <param name="ignoreFixedDrives">True to ignore fixed drives from population, false otherwise</param>
/// <returns>Active drives, matched to labels, if possible</returns>
/// <remarks>
/// 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
/// </summary>
public void RefreshDrive()
=> this.driveInfo = DriveInfo.GetDrives().FirstOrDefault(d => d?.Name == this.Name);
#if NET6_0_OR_GREATER
/// <summary>
/// Get all devices attached converted to Drive objects
/// </summary>
/// <param name="ignoreFixedDrives">True to ignore fixed drives from population, false otherwise</param>
/// <returns>List of drives, null on error</returns>
private static List<Drive> GetDriveList(bool ignoreFixedDrives)
{
DeviceInfo[] deviceInfos = Device.ListDevices();
if (deviceInfos == null)
return null;
var drives = new List<Drive>();
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;
}
/// <summary>
/// Generate a Drive object from a single device
/// </summary>
/// <param name="devicePath">Path to the device</param>
/// <param name="ignoreFixedDrives">True to ignore fixed drives from population, false otherwise</param>
/// <returns>Drive object for the device, null on error</returns>
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;
}
/// <summary>
/// Get the media type for a device path using the Aaru libraries
/// </summary>
/// <param name="devicePath">Path to the device</param>
/// <returns>Aaru MediaType, null on error</returns>
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
}
}

View File

@@ -51,6 +51,7 @@
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<ProjectReference Include="..\Aaru\Aaru.CommonTypes\Aaru.CommonTypes.csproj" />
<ProjectReference Include="..\Aaru\Aaru.Core\Aaru.Core.csproj" />
<ProjectReference Include="..\Aaru\Aaru.Devices\Aaru.Devices.csproj" />
</ItemGroup>

66
MPF.sln
View File

@@ -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