mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Create and use base class for readers/writers
This commit is contained in:
@@ -15,6 +15,7 @@ using SabreTools.DatItems.Formats;
|
||||
using SabreTools.Filter;
|
||||
using SabreTools.Hashing;
|
||||
using SabreTools.Logging;
|
||||
using SabreTools.Serialization.Interfaces;
|
||||
|
||||
namespace SabreTools.DatFiles
|
||||
{
|
||||
@@ -661,4 +662,63 @@ namespace SabreTools.DatFiles
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a DAT that can be serialized
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Base internal model for the DAT type</typeparam>
|
||||
/// <typeparam name="U">IFileSerializer type to use for conversion</typeparam>
|
||||
/// <typeparam name="V">IModelSerializer for cross-model serialization</typeparam>
|
||||
public abstract class SerializableDatFile<T, U, V> : DatFile
|
||||
where U : IFileSerializer<T>
|
||||
where V : IModelSerializer<T, Models.Metadata.MetadataFile>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected SerializableDatFile(DatFile? datFile) : base(datFile) { }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Deserialize the input file in two steps
|
||||
var specificFormat = Activator.CreateInstance<U>().Deserialize(filename);
|
||||
var internalFormat = Activator.CreateInstance<V>().Serialize(specificFormat);
|
||||
|
||||
// Convert to the internal format
|
||||
ConvertMetadata(internalFormat, filename, indexId, keep, statsOnly);
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
string message = $"'{filename}' - An error occurred during parsing";
|
||||
logger.Error(ex, message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.User($"Writing to '{outfile}'...");
|
||||
|
||||
// Serialize the input file in two steps
|
||||
var internalFormat = ConvertMetadata(ignoreblanks);
|
||||
var specificFormat = Activator.CreateInstance<V>().Deserialize(internalFormat);
|
||||
if (!Activator.CreateInstance<U>().Serialize(specificFormat, outfile))
|
||||
{
|
||||
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
logger.Error(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.User($"'{outfile}' written!{Environment.NewLine}");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user