mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-09-22 06:45:11 +00:00
Add FDS image writing
This commit is contained in:
@@ -48,7 +48,7 @@ namespace SabreTools.Wrappers
|
||||
/// <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 (includeDebug) Console.WriteLine("Attempting to write header data");
|
||||
|
||||
if (Header is null)
|
||||
{
|
||||
@@ -151,7 +151,7 @@ namespace SabreTools.Wrappers
|
||||
/// <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");
|
||||
if (includeDebug) Console.WriteLine("Attempting to write ROM data");
|
||||
|
||||
// Try to write the data
|
||||
try
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace SabreTools.Wrappers
|
||||
/// <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 (includeDebug) Console.WriteLine("Attempting to write header data");
|
||||
|
||||
if (Header is null)
|
||||
{
|
||||
@@ -109,7 +109,7 @@ namespace SabreTools.Wrappers
|
||||
/// <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");
|
||||
if (includeDebug) Console.WriteLine("Attempting to write ROM data");
|
||||
|
||||
// Try to write the data
|
||||
try
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SabreTools.Numerics.Extensions;
|
||||
|
||||
namespace SabreTools.Wrappers
|
||||
{
|
||||
public partial class FDS : IExtractable
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
/// TODO: Convert to QD as well?
|
||||
public bool Extract(string outputDirectory, bool includeDebug)
|
||||
{
|
||||
// Get the base path
|
||||
@@ -27,23 +27,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.IdentificationString);
|
||||
fs.Flush();
|
||||
|
||||
// Byte 4
|
||||
fs.Write(Header.DiskSides);
|
||||
fs.Flush();
|
||||
|
||||
// Byte 5-15
|
||||
fs.Write(Header.Padding);
|
||||
fs.Flush();
|
||||
|
||||
// Header extracted
|
||||
success = true;
|
||||
success |= WriteHeader(fs, includeDebug);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -51,23 +36,17 @@ namespace SabreTools.Wrappers
|
||||
}
|
||||
}
|
||||
|
||||
// Disk data
|
||||
// TODO: Convert to QD format?
|
||||
// ROM data
|
||||
if (Model.Data.Length > 0)
|
||||
{
|
||||
string diskPath = $"{basePath}.unh";
|
||||
if (includeDebug) Console.WriteLine($"Attempting to extract disk data to {diskPath}");
|
||||
string romPath = $"{basePath}.bin";
|
||||
if (includeDebug) Console.WriteLine($"Attempting to extract ROM data to {romPath}");
|
||||
|
||||
// Try to write the data
|
||||
try
|
||||
{
|
||||
// Open the output file for writing
|
||||
using var fs = File.Open(diskPath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
fs.Write(Model.Data, 0, Model.Data.Length);
|
||||
fs.Flush();
|
||||
|
||||
// PRG-ROM extracted
|
||||
success = true;
|
||||
using var fs = File.Open(romPath, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
success |= WriteRom(fs, includeDebug);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
107
SabreTools.Wrappers/FDS.Writing.cs
Normal file
107
SabreTools.Wrappers/FDS.Writing.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SabreTools.Numerics.Extensions;
|
||||
|
||||
namespace SabreTools.Wrappers
|
||||
{
|
||||
public partial class FDS : IWritable
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public bool Write(string outputPath, bool includeDebug)
|
||||
{
|
||||
// Ensure an output path
|
||||
if (string.IsNullOrEmpty(outputPath))
|
||||
{
|
||||
string outputFilename = Filename is null
|
||||
? (Guid.NewGuid().ToString() + ".fds")
|
||||
: (Filename + ".new");
|
||||
outputPath = Path.GetFullPath(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 write 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.IdentificationString);
|
||||
stream.Flush();
|
||||
|
||||
// Byte 4
|
||||
stream.Write(Header.DiskSides);
|
||||
stream.Flush();
|
||||
|
||||
// Byte 5-15
|
||||
stream.Write(Header.Padding);
|
||||
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 write 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