2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-09-05 17:37:31 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Filters.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Filters.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Enumerates all filters and instantiates the correct one.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-09-05 17:37:31 +01:00
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-01-28 17:30:22 +00:00
|
|
|
using System.IO;
|
2017-12-26 06:05:12 +00:00
|
|
|
using System.Linq;
|
2016-09-05 17:37:31 +01:00
|
|
|
using System.Reflection;
|
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filters
|
|
|
|
|
{
|
|
|
|
|
public class FiltersList
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
public SortedDictionary<string, IFilter> Filters;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-23 04:06:02 +00:00
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
/// Fills the list of all known filters
|
2017-12-23 04:06:02 +00:00
|
|
|
/// </summary>
|
2016-09-05 17:37:31 +01:00
|
|
|
public FiltersList()
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
Assembly assembly = Assembly.GetAssembly(typeof(IFilter));
|
2018-06-22 08:08:38 +01:00
|
|
|
Filters = new SortedDictionary<string, IFilter>();
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
foreach(Type type in assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IFilter))))
|
2016-09-05 17:37:31 +01:00
|
|
|
try
|
|
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
IFilter filter = (IFilter)type.GetConstructor(Type.EmptyTypes)?.Invoke(new object[] { });
|
2017-12-24 02:43:49 +00:00
|
|
|
if(filter != null && !Filters.ContainsKey(filter.Name.ToLower()))
|
|
|
|
|
Filters.Add(filter.Name.ToLower(), filter);
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
catch(Exception exception) { DicConsole.ErrorWriteLine("Exception {0}", exception); }
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-23 04:06:02 +00:00
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
/// Gets the filter that allows to read the specified path
|
2017-12-23 04:06:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Path</param>
|
|
|
|
|
/// <returns>The filter that allows reading the specified path</returns>
|
2017-12-26 06:05:12 +00:00
|
|
|
public IFilter GetFilter(string path)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2018-01-28 17:30:22 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IFilter noFilter = null;
|
|
|
|
|
foreach(IFilter filter in Filters.Values)
|
|
|
|
|
if(filter.Id != new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
|
|
|
|
|
{
|
|
|
|
|
if(!filter.Identify(path)) continue;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-01-28 17:30:22 +00:00
|
|
|
IFilter foundFilter =
|
|
|
|
|
(IFilter)filter.GetType().GetConstructor(Type.EmptyTypes)?.Invoke(new object[] { });
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2018-01-28 17:30:22 +00:00
|
|
|
foundFilter?.Open(path);
|
2017-12-21 16:59:15 +00:00
|
|
|
|
2018-01-28 17:30:22 +00:00
|
|
|
if(foundFilter?.IsOpened() == true) return foundFilter;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
noFilter = filter;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2018-01-28 17:30:22 +00:00
|
|
|
if(!noFilter?.Identify(path) == true) return noFilter;
|
2016-09-13 14:52:31 +01:00
|
|
|
|
2018-01-28 17:30:22 +00:00
|
|
|
noFilter?.Open(path);
|
2016-09-13 14:52:31 +01:00
|
|
|
|
2018-01-28 17:30:22 +00:00
|
|
|
return noFilter;
|
|
|
|
|
}
|
|
|
|
|
catch(IOException) { return null; }
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-23 04:06:02 +00:00
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
/// Gets all known filters
|
2017-12-23 04:06:02 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Known filters</returns>
|
2017-12-26 06:05:12 +00:00
|
|
|
public SortedDictionary<string, IFilter> GetFiltersList()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2017-12-22 16:17:40 +00:00
|
|
|
return Filters;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|