Extract out Skippers namespace

This commit is contained in:
Matt Nadareski
2020-12-07 13:02:42 -08:00
parent e7461370af
commit 8d593c585c
21 changed files with 234 additions and 70 deletions

2
.gitignore vendored
View File

@@ -12,4 +12,6 @@
/SabreTools.Data/obj/
/SabreTools.Library/bin/
/SabreTools.Library/obj/
/SabreTools.Skippers/bin/
/SabreTools.Skippers/obj/
/SabreTools.userprefs

View File

@@ -9,7 +9,7 @@ using SabreTools.Data;
using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes;
using SabreTools.Library.Logging;
using SabreTools.Library.Skippers;
using SabreTools.Library.Tools;
namespace SabreTools.Library.IO
{
@@ -370,7 +370,7 @@ namespace SabreTools.Library.IO
// Try to match the supplied header skipper
if (header != null)
{
SkipperRule rule = Transform.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header));
var rule = Transform.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header));
// If there's a match, transform the stream before getting info
if (rule.Tests != null && rule.Tests.Count != 0)

View File

@@ -4,11 +4,11 @@ using System.IO;
using SabreTools.Data;
using SabreTools.Library.DatFiles;
using SabreTools.Library.FileTypes;
using SabreTools.Library.IO;
using SabreTools.Library.Logging;
using SabreTools.Library.Skippers;
using SabreTools.Library.Tools;
namespace SabreTools.Library.Skippers
namespace SabreTools.Library.IO
{
/// <summary>
/// Class for wrapping general file transformations
@@ -32,6 +32,11 @@ namespace SabreTools.Library.Skippers
/// </summary>
private static List<SkipperFile> List;
/// <summary>
/// Local paths
/// </summary>
private static string LocalPath = Path.Combine(Globals.ExeDir, "Skippers") + Path.DirectorySeparatorChar;
#region Logging
/// <summary>
@@ -41,11 +46,6 @@ namespace SabreTools.Library.Skippers
#endregion
/// <summary>
/// Local paths
/// </summary>
public static string LocalPath = Path.Combine(Globals.ExeDir, "Skippers") + Path.DirectorySeparatorChar;
/// <summary>
/// Initialize static fields
/// </summary>

View File

@@ -13,48 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\SabreTools.Data\SabreTools.Data.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="Skippers\a7800.xml" />
<None Remove="Skippers\fds.xml" />
<None Remove="Skippers\lynx.xml" />
<None Remove="Skippers\n64.xml" />
<None Remove="Skippers\nes.xml" />
<None Remove="Skippers\pce.xml" />
<None Remove="Skippers\psid.xml" />
<None Remove="Skippers\snes.xml" />
<None Remove="Skippers\spc.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="Skippers\a7800.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\fds.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\lynx.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\n64.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\nes.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\pce.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\psid.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\snes.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Skippers\spc.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<ProjectReference Include="..\SabreTools.Skippers\SabreTools.Skippers.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,56 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;netcoreapp3.1;net5.0</TargetFrameworks>
<RuntimeIdentifiers>win10-x64;win7-x86</RuntimeIdentifiers>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net48'">
<DefineConstants>NET_FRAMEWORK</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Remove="a7800.xml" />
<None Remove="fds.xml" />
<None Remove="lynx.xml" />
<None Remove="n64.xml" />
<None Remove="nes.xml" />
<None Remove="pce.xml" />
<None Remove="psid.xml" />
<None Remove="snes.xml" />
<None Remove="spc.xml" />
</ItemGroup>
<ItemGroup>
<Content Include="a7800.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="fds.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="lynx.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="n64.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="nes.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="pce.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="psid.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="snes.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="spc.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -4,10 +4,14 @@ using System.Globalization;
using System.IO;
using System.Xml;
using SabreTools.Library.IO;
namespace SabreTools.Library.Skippers
{
/// <remarks>
/// It is well worth considering just moving the XML files to code, similar to how RV does it
/// if only because nobody really has any skippers outside of this. It would also make the
/// output directory cleaner and less prone to user error in case something didn't get copied
/// correctly. The contents of these files should still be added to the wiki, in that case.
/// </remarks>
public class SkipperFile
{
#region Fields
@@ -55,7 +59,7 @@ namespace SabreTools.Library.Skippers
Rules = new List<SkipperRule>();
SourceFile = Path.GetFileNameWithoutExtension(filename);
XmlReader xtr = filename.GetXmlTextReader();
XmlReader xtr = GetXmlTextReader(filename);
bool valid = Parse(xtr);
// If we somehow have an invalid file, zero out the fields
@@ -406,5 +410,34 @@ namespace SabreTools.Library.Skippers
}
#endregion
// TODO: Remove this region once IO namespace is separated out properly
#region TEMPORARY - REMOVEME
/// <summary>
/// Get the XmlTextReader associated with a file, if possible
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <returns>The XmlTextReader representing the (possibly converted) file, null otherwise</returns>
private static XmlReader GetXmlTextReader(string filename)
{
// Check if file exists
if (!File.Exists(filename))
return null;
XmlReader xtr = XmlReader.Create(filename, new XmlReaderSettings
{
CheckCharacters = false,
DtdProcessing = DtdProcessing.Ignore,
IgnoreComments = true,
IgnoreWhitespace = true,
ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
});
return xtr;
}
#endregion
}
}

View File

@@ -2,9 +2,6 @@
using System.Collections.Generic;
using System.IO;
using SabreTools.Library.IO;
using SabreTools.Library.Logging;
namespace SabreTools.Library.Skippers
{
public class SkipperRule
@@ -43,7 +40,7 @@ namespace SabreTools.Library.Skippers
/// <summary>
/// Logging object
/// </summary>
private readonly Logger logger;
//private readonly Logger logger; // TODO: Re-enable all logging once Logging namespace separated out
#endregion
@@ -54,7 +51,7 @@ namespace SabreTools.Library.Skippers
/// </summary>
public SkipperRule()
{
logger = new Logger(this);
//logger = new Logger(this);
}
#endregion
@@ -87,20 +84,20 @@ namespace SabreTools.Library.Skippers
// If the input file doesn't exist, fail
if (!File.Exists(input))
{
logger.Error($"I'm sorry but '{input}' doesn't exist!");
//logger.Error($"I'm sorry but '{input}' doesn't exist!");
return false;
}
// Create the output directory if it doesn't already
DirectoryExtensions.Ensure(Path.GetDirectoryName(output));
Ensure(Path.GetDirectoryName(output));
logger.User($"Attempting to apply rule to '{input}'");
bool success = TransformStream(FileExtensions.TryOpenRead(input), FileExtensions.TryCreate(output));
//logger.User($"Attempting to apply rule to '{input}'");
bool success = TransformStream(TryOpenRead(input), TryCreate(output));
// If the output file has size 0, delete it
if (new FileInfo(output).Length == 0)
{
FileExtensions.TryDelete(output);
TryDelete(output);
success = false;
}
@@ -125,7 +122,7 @@ namespace SabreTools.Library.Skippers
|| (Operation > HeaderSkipOperation.Byteswap && (extsize % 4) != 0)
|| (Operation > HeaderSkipOperation.Bitswap && (StartOffset == null || StartOffset % 2 == 0)))
{
logger.Error("The stream did not have the correct size to be transformed!");
//logger.Error("The stream did not have the correct size to be transformed!");
return false;
}
@@ -134,7 +131,7 @@ namespace SabreTools.Library.Skippers
BinaryReader br = null;
try
{
logger.User("Applying found rule to input stream");
//logger.User("Applying found rule to input stream");
bw = new BinaryWriter(output);
br = new BinaryReader(input);
@@ -222,7 +219,7 @@ namespace SabreTools.Library.Skippers
}
catch (Exception ex)
{
logger.Error(ex);
//logger.Error(ex);
return false;
}
finally
@@ -238,5 +235,113 @@ namespace SabreTools.Library.Skippers
return success;
}
// TODO: Remove this region once IO namespace is separated out properly
#region TEMPORARY - REMOVEME
/// <summary>
/// Ensure the output directory is a proper format and can be created
/// </summary>
/// <param name="dir">Directory to check</param>
/// <param name="create">True if the directory should be created, false otherwise (default)</param>
/// <param name="temp">True if this is a temp directory, false otherwise</param>
/// <returns>Full path to the directory</returns>
public static string Ensure(string dir, bool create = false, bool temp = false)
{
// If the output directory is invalid
if (string.IsNullOrWhiteSpace(dir))
{
if (temp)
dir = Path.GetTempPath();
else
dir = Environment.CurrentDirectory;
}
// Get the full path for the output directory
dir = Path.GetFullPath(dir);
// If we're creating the output folder, do so
if (create)
Directory.CreateDirectory(dir);
return dir;
}
/// <summary>
/// Try to create a file for write, optionally throwing the error
/// </summary>
/// <param name="file">Name of the file to create</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>An opened stream representing the file on success, null otherwise</returns>
public static FileStream TryCreate(string file, bool throwOnError = false)
{
// Now wrap opening the file
try
{
return File.Open(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
}
catch (Exception ex)
{
if (throwOnError)
throw ex;
else
return null;
}
}
/// <summary>
/// Try to safely delete a file, optionally throwing the error
/// </summary>
/// <param name="file">Name of the file to delete</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the file didn't exist or could be deleted, false otherwise</returns>
public static bool TryDelete(string file, bool throwOnError = false)
{
// Check if the file exists first
if (!File.Exists(file))
return true;
// Now wrap deleting the file
try
{
File.Delete(file);
return true;
}
catch (Exception ex)
{
if (throwOnError)
throw ex;
else
return false;
}
}
/// <summary>
/// Try to open a file for read, optionally throwing the error
/// </summary>
/// <param name="file">Name of the file to open</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>An opened stream representing the file on success, null otherwise</returns>
public static FileStream TryOpenRead(string file, bool throwOnError = false)
{
// Check if the file exists first
if (!File.Exists(file))
return null;
// Now wrap opening the file
try
{
return File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (Exception ex)
{
if (throwOnError)
throw ex;
else
return null;
}
}
#endregion
}
}

View File

@@ -16,7 +16,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.MD = README.MD
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Data", "SabreTools.Data\SabreTools.Data.csproj", "{66E2FB10-77C0-4589-9DCD-3CA48702C18A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SabreTools.Data", "SabreTools.Data\SabreTools.Data.csproj", "{66E2FB10-77C0-4589-9DCD-3CA48702C18A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Skippers", "SabreTools.Skippers\SabreTools.Skippers.csproj", "{D8665F27-75E6-4E3F-9F0A-286433831C69}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -58,6 +60,14 @@ Global
{66E2FB10-77C0-4589-9DCD-3CA48702C18A}.Release|Any CPU.Build.0 = Release|Any CPU
{66E2FB10-77C0-4589-9DCD-3CA48702C18A}.Release|x64.ActiveCfg = Release|Any CPU
{66E2FB10-77C0-4589-9DCD-3CA48702C18A}.Release|x64.Build.0 = Release|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Debug|x64.ActiveCfg = Debug|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Debug|x64.Build.0 = Debug|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Release|Any CPU.Build.0 = Release|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Release|x64.ActiveCfg = Release|Any CPU
{D8665F27-75E6-4E3F-9F0A-286433831C69}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -2,7 +2,7 @@
using SabreTools.Library.Help;
using SabreTools.Library.IO;
using SabreTools.Library.Skippers;
using SabreTools.Library.Tools;
namespace SabreTools.Features
{

View File

@@ -2,7 +2,6 @@
using SabreTools.Library.Help;
using SabreTools.Library.IO;
using SabreTools.Library.Skippers;
namespace SabreTools.Features
{