Create initlal version of Filter program

This program allows users to filter a DAT or set of DATs based on various criteria including names, sizes, and hashes.
This commit is contained in:
Matt Nadareski
2016-05-24 12:59:58 -07:00
parent 0db337476f
commit 7c6dcbc0dd
7 changed files with 542 additions and 0 deletions

3
.gitignore vendored
View File

@@ -11,6 +11,9 @@
/Deheader/obj
/Deheader/obj/Debug
/Deheader/obj/Release
/Filter/obj
/Filter/obj/Debug
/Filter/obj/Release
/OfflineMerge/obj
/OfflineMerge/obj/Debug
/OfflineMerge/obj/Release

6
Filter/App.config Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>

375
Filter/Filter.cs Normal file
View File

@@ -0,0 +1,375 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SabreTools.Helper;
namespace SabreTools
{
/*
Create new tool: Filter, with the following filters available
Game name, Rom name, CRC, MD5, SHA-1 use asterisks as follows(case insensitive):
-crc=*00 (means ends with "00")
-crc=00* (means starts with "00")
-crc=*00* (means contains "00")
-crc=00 (means is "00")
*/
public class Filter
{
// Private instance variables
private string _filename;
private string _gamename;
private string _romname;
private string _romtype;
private long _sgt;
private long _slt;
private long _seq;
private string _crc;
private string _md5;
private string _sha1;
private bool? _nodump;
private Logger _logger;
/// <summary>
/// Create a Filter object
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="gamename">Name of the game to match (can use asterisk-partials)</param>
/// <param name="romname">Name of the rom to match (can use asterisk-partials)</param>
/// <param name="romtype">Type of the rom to match</param>
/// <param name="sgt">Find roms greater than or equal to this size</param>
/// <param name="slt">Find roms less than or equal to this size</param>
/// <param name="seq">Find roms equal to this size</param>
/// <param name="crc">CRC of the rom to match (can use asterisk-partials)</param>
/// <param name="md5">MD5 of the rom to match (can use asterisk-partials)</param>
/// <param name="sha1">SHA-1 of the rom to match (can use asterisk-partials)</param>
/// <param name="nodump">Select roms with nodump status as follows: null (match all), true (match Nodump only), false (exclude Nodump)</param>
/// <param name="logger">Logging object for file and console output</param>
public Filter(string filename, string gamename, string romname, string romtype, long sgt, long slt, long seq, string crc, string md5, string sha1, bool? nodump, Logger logger)
{
_filename = filename;
_gamename = gamename;
_romname = romname;
_romtype = romtype;
_sgt = sgt;
_slt = slt;
_seq = seq;
_crc = crc;
_md5 = md5;
_sha1 = sha1;
_nodump = nodump;
_logger = logger;
}
/// <summary>
/// Start help or use supplied parameters
/// </summary>
/// <param name="args">String array representing command line parameters</param>
public static void Main(string[] args)
{
Console.Clear();
// Credits take precidence over all
if ((new List<string>(args)).Contains("--credits"))
{
Build.Credits();
return;
}
Logger logger = new Logger(true, "filter.log");
logger.Start();
// First things first, take care of all of the arguments that this could have
bool? nodump = null;
string gamename = "", romname = "", romtype = "", crc = "", md5 = "", sha1= "";
long sgt = -1, slt = -1, seq = -1;
List<string> inputs = new List<string>();
foreach (string arg in args)
{
switch (arg)
{
case "-h":
case "-?":
case "--help":
Build.Help();
logger.Close();
return;
case "-nd":
case "--nodump":
nodump = true;
break;
case "-nnd":
case "--not-nodump":
nodump = false;
break;
default:
// Numerical inputs
if (arg.StartsWith("-seq=") || arg.StartsWith("--equal="))
{
if (!Int64.TryParse(arg.Split('=')[1], out seq))
{
seq = -1;
}
}
else if (arg.StartsWith("-sgt=") || arg.StartsWith("--greater="))
{
if (!Int64.TryParse(arg.Split('=')[1], out sgt))
{
sgt = -1;
}
}
else if (arg.StartsWith("-slt=") || arg.StartsWith("--less="))
{
if (!Int64.TryParse(arg.Split('=')[1], out slt))
{
slt = -1;
}
}
// String inputs
else if (arg.StartsWith("-crc=") || arg.StartsWith("--crc="))
{
crc = arg.Split('=')[1];
}
else if (arg.StartsWith("-gn=") || arg.StartsWith("--game-name="))
{
gamename = arg.Split('=')[1];
}
else if (arg.StartsWith("-md5=") || arg.StartsWith("--md5="))
{
md5 = arg.Split('=')[1];
}
else if (arg.StartsWith("-rn=") || arg.StartsWith("--rom-name="))
{
romname = arg.Split('=')[1];
}
else if (arg.StartsWith("-rt=") || arg.StartsWith("--rom-type="))
{
romtype = arg.Split('=')[1];
}
else if (arg.StartsWith("-sha1=") || arg.StartsWith("--sha1="))
{
sha1 = arg.Split('=')[1];
}
else
{
inputs.Add(arg);
}
break;
}
}
// If there's no inputs, show the help
if (inputs.Count == 0)
{
Build.Help();
logger.Close();
return;
}
// Output the title
Build.Start("Filter");
// If any of the inputs are not valid, show the help
foreach (string input in inputs)
{
if (!File.Exists(input) && !Directory.Exists(input))
{
logger.Error(input + " is not a valid input!");
Console.WriteLine();
Build.Help();
return;
}
}
// Create new Filter objects for each input
Filter filter;
bool success = true;
foreach (string input in inputs)
{
string newinput = Path.GetFullPath(input.Replace("\"", ""));
if (File.Exists(newinput))
{
filter = new Filter(newinput, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger);
success &= filter.Process();
}
if (Directory.Exists(newinput))
{
foreach (string file in Directory.EnumerateFiles(newinput, "*", SearchOption.AllDirectories))
{
filter = new Filter(file, gamename, romname, romtype, sgt, slt, seq, crc, md5, sha1, nodump, logger);
success &= filter.Process();
}
}
}
// If we failed, show the help
if (!success)
{
Console.WriteLine();
Build.Help();
}
logger.Close();
}
/// <summary>
/// Process an individual DAT with the given information
/// </summary>
/// <returns>True if the DAT was output, false otherwise</returns>
public bool Process()
{
_logger.User("Processing file: '" + _filename + "'");
// Populated the DAT information
DatData datdata = new DatData();
datdata = RomManipulation.Parse(_filename, 0, 0, datdata, _logger);
// Now loop through and create a new Rom dictionary using filtered values
Dictionary<string, List<RomData>> dict = new Dictionary<string, List<RomData>>();
List<string> keys = datdata.Roms.Keys.ToList();
foreach (string key in keys)
{
List<RomData> roms = datdata.Roms[key];
foreach (RomData rom in roms)
{
// Filter on game name
if (_gamename != "")
{
if (!(_gamename.StartsWith("*") && _gamename.EndsWith("*") && rom.Game.Contains(_gamename.Replace("*", ""))))
{
continue;
}
else if (!(_gamename.StartsWith("*") && rom.Game.EndsWith(_gamename.Replace("*", ""))))
{
continue;
}
else if (!(_gamename.EndsWith("*") && rom.Game.StartsWith(_gamename.Replace("*", ""))))
{
continue;
}
}
// Filter on rom name
if (_romname != "")
{
if (!(_romname.StartsWith("*") && _romname.EndsWith("*") && rom.Game.Contains(_romname.Replace("*", ""))))
{
continue;
}
else if (!(_romname.StartsWith("*") && rom.Game.EndsWith(_romname.Replace("*", ""))))
{
continue;
}
else if (!(_romname.EndsWith("*") && rom.Game.StartsWith(_romname.Replace("*", ""))))
{
continue;
}
}
// Filter on rom type
if (_romtype != "" && rom.Type != _romtype)
{
continue;
}
// Filter on rom size
if (_seq != -1 && rom.Size != _seq)
{
continue;
}
else
{
if (_sgt != -1 && rom.Size < _sgt)
{
continue;
}
if (_slt != -1 && rom.Size > _slt)
{
continue;
}
}
// Filter on crc
if (_crc != "")
{
if (!(_crc.StartsWith("*") && _crc.EndsWith("*") && rom.Game.Contains(_crc.Replace("*", ""))))
{
continue;
}
else if (!(_crc.StartsWith("*") && rom.Game.EndsWith(_crc.Replace("*", ""))))
{
continue;
}
else if (!(_crc.EndsWith("*") && rom.Game.StartsWith(_crc.Replace("*", ""))))
{
continue;
}
}
// Filter on md5
if (_md5 != "")
{
if (!(_md5.StartsWith("*") && _md5.EndsWith("*") && rom.Game.Contains(_md5.Replace("*", ""))))
{
continue;
}
else if (!(_md5.StartsWith("*") && rom.Game.EndsWith(_md5.Replace("*", ""))))
{
continue;
}
else if (!(_md5.EndsWith("*") && rom.Game.StartsWith(_md5.Replace("*", ""))))
{
continue;
}
}
// Filter on sha1
if (_sha1 != "")
{
if (!(_sha1.StartsWith("*") && _sha1.EndsWith("*") && rom.Game.Contains(_sha1.Replace("*", ""))))
{
continue;
}
else if (!(_sha1.StartsWith("*") && rom.Game.EndsWith(_sha1.Replace("*", ""))))
{
continue;
}
else if (!(_sha1.EndsWith("*") && rom.Game.StartsWith(_sha1.Replace("*", ""))))
{
continue;
}
}
// If it made it this far, add the rom to the output dictionary
if (dict.ContainsKey(key))
{
dict[key].Add(rom);
}
else
{
List<RomData> temp = new List<RomData>();
temp.Add(rom);
dict.Add(key, temp);
}
}
// Now clean up by removing the old list
datdata.Roms[key] = null;
}
// Get the correct output values
datdata.Name += " (Filtered)";
datdata.Description += " (Filtered)";
datdata.Roms = dict;
// Now write the file out and return
return Output.WriteDatfile(datdata, Path.GetDirectoryName(_filename), _logger);
}
}
}

86
Filter/Filter.csproj Normal file
View File

@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{136AA0D0-9234-4680-B593-A32C0762972E}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SabreTools</RootNamespace>
<AssemblyName>Filter</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\Debug-x64\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>..\..\Release-x64\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Filter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SabreHelper\SabreHelper.csproj">
<Project>{225a1afd-0890-44e8-b779-7502665c23a5}</Project>
<Name>SabreHelper</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Filter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Filter")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("136aa0d0-9234-4680-b593-a32c0762972e")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -208,6 +208,32 @@ Options:
-sys=, --system= System ID to generate from
");
break;
case "Filter":
Console.WriteLine(@"Filter - Filter DATs by inputted criteria
-----------------------------------------
Usage: Filter [options] [inputs]
Options:
-h, -?, --help Show this help dialog
-gn=, --game-name= Game name to be filtered on
-rn=, --rom-name= Rom name to be filtered on
-rt=, --rom-type= Rom type to be filtered on
-sgt=, --greater= Size greater than or equal to
-slt=, --less= Size less than or equal to
-seq=, --equal= Size equal to
-crc=, --crc= CRC to be filtered on
-md5=, --md5= MD5 to be filtered on
-sha1=, --sha1= SHA-1 to be filtered on
-nd, --nodump Only match nodump roms
-nnd, --not-nodump Exclude all nodump roms
Game name, Rom name, CRC, MD5, SHA-1 can do partial matches
using asterisks as follows (case insensitive):
*00 means ends with '00'
00* means starts with '00'
*00* means contains '00'
00 means exactly equals '00'");
break;
default:
Console.Write("This is the default help output");
break;

View File

@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UncompressedSize", "Uncompr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DATabaseTwo", "DATabaseTwo\DATabaseTwo.csproj", "{C7732A05-1F96-43ED-AC8C-0E388F37EBC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Filter", "Filter\Filter.csproj", "{136AA0D0-9234-4680-B593-A32C0762972E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -92,6 +94,14 @@ Global
{C7732A05-1F96-43ED-AC8C-0E388F37EBC1}.Release|Any CPU.Build.0 = Release|Any CPU
{C7732A05-1F96-43ED-AC8C-0E388F37EBC1}.Release|x64.ActiveCfg = Release|x64
{C7732A05-1F96-43ED-AC8C-0E388F37EBC1}.Release|x64.Build.0 = Release|x64
{136AA0D0-9234-4680-B593-A32C0762972E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{136AA0D0-9234-4680-B593-A32C0762972E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{136AA0D0-9234-4680-B593-A32C0762972E}.Debug|x64.ActiveCfg = Debug|x64
{136AA0D0-9234-4680-B593-A32C0762972E}.Debug|x64.Build.0 = Debug|x64
{136AA0D0-9234-4680-B593-A32C0762972E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{136AA0D0-9234-4680-B593-A32C0762972E}.Release|Any CPU.Build.0 = Release|Any CPU
{136AA0D0-9234-4680-B593-A32C0762972E}.Release|x64.ActiveCfg = Release|Any CPU
{136AA0D0-9234-4680-B593-A32C0762972E}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE