mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-09-22 06:45:11 +00:00
Add LNX cart writing
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
126
SabreTools.Wrappers/AtariLynxCart.Writing.cs
Normal file
126
SabreTools.Wrappers/AtariLynxCart.Writing.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write header data to the stream
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream to write to</param>
|
||||
/// <param name="includeDebug">True to include debug data, false otherwise</param>
|
||||
/// <returns>True if the writing was successful, false otherwise</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write rom data to the stream
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream to write to</param>
|
||||
/// <param name="includeDebug">True to include debug data, false otherwise</param>
|
||||
/// <returns>True if the writing was successful, false otherwise</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user