diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index 0981b88b..99a9977d 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -13,7 +13,6 @@ using SabreTools.DatItems; using SabreTools.DatItems.Formats; using SabreTools.Hashing; using SabreTools.Logging; -using SabreTools.Serialization.Interfaces; namespace SabreTools.DatFiles { @@ -1050,63 +1049,4 @@ namespace SabreTools.DatFiles #endregion } - - /// - /// Represents a DAT that can be serialized - /// - /// Base internal model for the DAT type - /// IFileSerializer type to use for conversion - /// IModelSerializer for cross-model serialization - public abstract class SerializableDatFile : DatFile - where U : IFileSerializer - where V : IModelSerializer - { - /// - protected SerializableDatFile(DatFile? datFile) : base(datFile) { } - - /// - 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().Deserialize(filename); - var internalFormat = Activator.CreateInstance().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); - } - } - - /// - 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().Deserialize(internalFormat); - if (!Activator.CreateInstance().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; - } - } } diff --git a/SabreTools.DatFiles/SerializableDatFile.cs b/SabreTools.DatFiles/SerializableDatFile.cs new file mode 100644 index 00000000..c8b2e539 --- /dev/null +++ b/SabreTools.DatFiles/SerializableDatFile.cs @@ -0,0 +1,64 @@ +using System; +using SabreTools.Serialization.Interfaces; + +namespace SabreTools.DatFiles +{ + /// + /// Represents a DAT that can be serialized + /// + /// Base internal model for the DAT type + /// IFileSerializer type to use for conversion + /// IModelSerializer for cross-model serialization + public abstract class SerializableDatFile : DatFile + where U : IFileSerializer + where V : IModelSerializer + { + /// + protected SerializableDatFile(DatFile? datFile) : base(datFile) { } + + /// + 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().Deserialize(filename); + var internalFormat = Activator.CreateInstance().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); + } + } + + /// + 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().Deserialize(internalFormat); + if (!Activator.CreateInstance().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; + } + } +}