diff --git a/SabreTools.Wrappers/AtariLynxCart.Extraction.cs b/SabreTools.Wrappers/AtariLynxCart.Extraction.cs index 93dc8648..46f70214 100644 --- a/SabreTools.Wrappers/AtariLynxCart.Extraction.cs +++ b/SabreTools.Wrappers/AtariLynxCart.Extraction.cs @@ -1,7 +1,5 @@ using System; using System.IO; -using SabreTools.Data.Models.AtariLynx; -using SabreTools.Numerics.Extensions; namespace SabreTools.Wrappers { @@ -28,43 +26,8 @@ namespace SabreTools.Wrappers // Try to write the data try { - // Open the output file for writing using var fs = File.Open(headerPath, FileMode.Create, FileAccess.Write, FileShare.None); - - // Bytes 0-3 - fs.Write(Header.Magic); - fs.Flush(); - - // Bytes 4-5 - fs.Write(Header.Bank0PageSize); - fs.Flush(); - - // Bytes 6-7 - fs.Write(Header.Bank0PageSize); - fs.Flush(); - - // Bytes 8-9 - fs.Write(Header.Version); - fs.Flush(); - - // Bytes 10-41 - fs.Write(Header.CartName); - fs.Flush(); - - // Bytes 42-57 - fs.Write(Header.Manufacturer); - fs.Flush(); - - // Byte 58 - fs.Write((byte)Header.Rotation); - fs.Flush(); - - // Bytes 59-63 - fs.Write(Header.Spare); - fs.Flush(); - - // Header extracted - success = true; + success |= WriteHeader(fs, includeDebug); } catch (Exception ex) { @@ -81,13 +44,8 @@ namespace SabreTools.Wrappers // Try to write the data try { - // Open the output file for writing using var fs = File.Open(romPath, FileMode.Create, FileAccess.Write, FileShare.None); - fs.Write(Model.Data, 0, Model.Data.Length); - fs.Flush(); - - // ROM extracted - success = true; + success |= WriteRom(fs, includeDebug); } catch (Exception ex) { diff --git a/SabreTools.Wrappers/AtariLynxCart.Writing.cs b/SabreTools.Wrappers/AtariLynxCart.Writing.cs new file mode 100644 index 00000000..c1086fa2 --- /dev/null +++ b/SabreTools.Wrappers/AtariLynxCart.Writing.cs @@ -0,0 +1,126 @@ +using System; +using System.IO; +using SabreTools.Data.Models.AtariLynx; +using SabreTools.Numerics.Extensions; + +namespace SabreTools.Wrappers +{ + public partial class AtariLynxCart : IWritable + { + /// + public bool Write(string outputDirectory, bool includeDebug) + { + // Get the base path + string outputFilename = Filename is null + ? Guid.NewGuid().ToString() + : Path.GetFileName(Filename); + outputFilename += ".lnx"; + string outputPath = Path.Combine(outputDirectory, outputFilename); + + // Check for invalid data + if (Header is null || Model.Data is null || Model.Data.Length == 0) + { + if (includeDebug) Console.WriteLine("Model was invalid, cannot write!"); + return false; + } + + // Open the output file for writing + using var fs = File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None); + + // Header data + if (!WriteHeader(fs, includeDebug)) + return false; + + // ROM data + if (!WriteRom(fs, includeDebug)) + return false; + + return true; + } + + /// + /// Write header data to the stream + /// + /// Stream to write to + /// True to include debug data, false otherwise + /// True if the writing was successful, false otherwise + private bool WriteHeader(Stream stream, bool includeDebug) + { + if (includeDebug) Console.WriteLine("Attempting to extract header data"); + + if (Header is null) + { + if (includeDebug) Console.WriteLine("Header was invalid!"); + return false; + } + + // Try to write the data + try + { + // Bytes 0-3 + stream.Write(Header.Magic); + stream.Flush(); + + // Bytes 4-5 + stream.Write(Header.Bank0PageSize); + stream.Flush(); + + // Bytes 6-7 + stream.Write(Header.Bank0PageSize); + stream.Flush(); + + // Bytes 8-9 + stream.Write(Header.Version); + stream.Flush(); + + // Bytes 10-41 + stream.Write(Header.CartName); + stream.Flush(); + + // Bytes 42-57 + stream.Write(Header.Manufacturer); + stream.Flush(); + + // Byte 58 + stream.Write((byte)Header.Rotation); + stream.Flush(); + + // Bytes 59-63 + stream.Write(Header.Spare); + stream.Flush(); + + // Header extracted + return true; + } + catch (Exception ex) + { + if (includeDebug) Console.Error.WriteLine(ex); + return false; + } + } + + /// + /// Write rom data to the stream + /// + /// Stream to write to + /// True to include debug data, false otherwise + /// True if the writing was successful, false otherwise + private bool WriteRom(Stream stream, bool includeDebug) + { + if (includeDebug) Console.WriteLine("Attempting to extract ROM data"); + + // Try to write the data + try + { + stream.Write(Model.Data, 0, Model.Data.Length); + stream.Flush(); + return true; + } + catch (Exception ex) + { + if (includeDebug) Console.Error.WriteLine(ex); + return false; + } + } + } +}