Move all image logic to factory

This commit is contained in:
Matt Nadareski
2021-07-05 00:31:19 -07:00
parent 65ab29f29f
commit 59bfd405a6
2 changed files with 38 additions and 25 deletions

View File

@@ -1,10 +1,44 @@
using System.IO;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Metadata;
using Aaru.DiscImages;
using Aaru.Filters;
namespace RedBookPlayer.Discs
{
public static class OpticalDiscFactory
{
/// <summary>
/// Generate an OpticalDisc from an input path
/// </summary>
/// <param name="path">Path to load the image from</param>
/// <param name="autoPlay">True if the image should be playable immediately, false otherwise</param>
/// <returns>Instantiated OpticalDisc, if possible</returns>
public static OpticalDisc GenerateFromPath(string path, bool autoPlay)
{
try
{
// Validate the image exists
if(string.IsNullOrWhiteSpace(path) || !File.Exists(path))
return null;
// Load the disc image to memory
// TODO: Assumes Aaruformat right now for all
var image = new AaruFormat();
var filter = new ZZZNoFilter();
filter.Open(path);
image.Open(filter);
// Generate and instantiate the disc
return GenerateFromImage(image, autoPlay);
}
catch
{
// All errors mean an invalid image in some way
return null;
}
}
/// <summary>
/// Generate an OpticalDisc from an input IOpticalMediaImage
/// </summary>

View File

@@ -1,9 +1,6 @@
using System;
using System.ComponentModel;
using System.IO;
using Aaru.CommonTypes.Enums;
using Aaru.DiscImages;
using Aaru.Filters;
using ReactiveUI;
using RedBookPlayer.Discs;
@@ -188,32 +185,14 @@ namespace RedBookPlayer.Hardware
Initialized = false;
_soundOutput = new SoundOutput();
_soundOutput.SetDeEmphasis(false);
_opticalDisc = null;
try
{
// Validate the image exists
if(string.IsNullOrWhiteSpace(path) || !File.Exists(path))
return;
// Load the disc image to memory
var image = new AaruFormat();
var filter = new ZZZNoFilter();
filter.Open(path);
image.Open(filter);
// Generate and instantiate the disc
_opticalDisc = OpticalDiscFactory.GenerateFromImage(image, autoPlay);
}
catch
{
// All errors mean an invalid image in some way
// Initalize the disc
_opticalDisc = OpticalDiscFactory.GenerateFromPath(path, autoPlay);
if(_opticalDisc == null || !_opticalDisc.Initialized)
return;
}
// Add event handling for the optical disc
if(_opticalDisc != null)
_opticalDisc.PropertyChanged += OpticalDiscStateChanged;
_opticalDisc.PropertyChanged += OpticalDiscStateChanged;
// Initialize the sound output
_soundOutput.Init(_opticalDisc, autoPlay, defaultVolume);