using System;
using System.IO;
using System.Linq;
using GeViSetEditor.Core.Models;
namespace GeViSetEditor.Core.Writers
{
///
/// Safe writer that preserves binary integrity
/// Only modifies action mappings, keeps everything else intact
///
public class SafeSetFileWriter
{
///
/// Write SafeSetFile back to binary format
/// Returns byte array that can be sent to GeViServer
///
public byte[] Write(SafeSetFile setFile)
{
if (setFile.OriginalData == null || setFile.OriginalData.Length == 0)
{
throw new InvalidOperationException("Cannot write: OriginalData is missing");
}
Console.WriteLine($"Writing .set file: {setFile.FileSize} bytes");
Console.WriteLine($"Action mappings: {setFile.ActionMappings.Count}");
// Start with exact copy of original data
byte[] output = setFile.GetDataForWriting();
Console.WriteLine($" Output data created: {output.Length} bytes");
// Verify header is intact
if (VerifyHeader(output))
{
Console.WriteLine(" Header verification: PASSED");
}
else
{
Console.WriteLine(" Header verification: FAILED (proceeding anyway for round-trip test)");
// Don't throw - allow round-trip test to see byte comparison
}
// TODO: If action mappings were modified, update them here
// For now, we just preserve everything as-is for safe round-trip
Console.WriteLine($"Write complete: {output.Length} bytes");
return output;
}
///
/// Write to file
///
public void WriteToFile(SafeSetFile setFile, string filePath)
{
byte[] data = Write(setFile);
// Create backup if file exists
if (File.Exists(filePath))
{
string backupPath = filePath + ".backup";
File.Copy(filePath, backupPath, true);
Console.WriteLine($"Created backup: {backupPath}");
}
File.WriteAllBytes(filePath, data);
Console.WriteLine($"Wrote {data.Length} bytes to {filePath}");
}
///
/// Verify the output file is valid before writing to server
///
public bool Verify(byte[] data)
{
if (data == null || data.Length < 100)
{
Console.WriteLine("Verification failed: File too small");
return false;
}
if (!VerifyHeader(data))
{
Console.WriteLine("Verification failed: Invalid header");
return false;
}
// Additional checks
if (data.Length > 10 * 1024 * 1024) // 10MB max
{
Console.WriteLine("Verification failed: File too large");
return false;
}
Console.WriteLine("Verification passed");
return true;
}
private bool VerifyHeader(byte[] data)
{
// Check for "GeViSoft Parameters" header
string expectedHeader = "GeViSoft Parameters";
int offset = (data[0] == 0x00) ? 1 : 0;
if (offset + 2 + expectedHeader.Length > data.Length)
return false;
if (data[offset] != 0x07) // Pascal string marker
return false;
byte headerLen = data[offset + 1];
if (headerLen < 10 || headerLen > 50)
return false;
string actualHeader = System.Text.Encoding.UTF8.GetString(data, offset + 2, Math.Min(headerLen, expectedHeader.Length));
return actualHeader.StartsWith("GeViSoft");
}
///
/// Compare two .set files for differences
/// Useful for testing round-trip conversion
///
public ComparisonResult Compare(byte[] original, byte[] modified)
{
var result = new ComparisonResult
{
OriginalSize = original.Length,
ModifiedSize = modified.Length
};
if (original.Length != modified.Length)
{
result.IsIdentical = false;
result.Differences.Add($"Size mismatch: {original.Length} vs {modified.Length}");
return result;
}
for (int i = 0; i < original.Length; i++)
{
if (original[i] != modified[i])
{
result.DifferentBytes++;
if (result.Differences.Count < 10) // Limit to first 10 differences
{
result.Differences.Add($"Byte {i} (0x{i:X}): {original[i]:X2} != {modified[i]:X2}");
}
}
}
result.IsIdentical = (result.DifferentBytes == 0);
if (result.IsIdentical)
{
Console.WriteLine("Files are identical!");
}
else
{
Console.WriteLine($"Files differ in {result.DifferentBytes} bytes");
}
return result;
}
}
public class ComparisonResult
{
public bool IsIdentical { get; set; }
public int OriginalSize { get; set; }
public int ModifiedSize { get; set; }
public int DifferentBytes { get; set; }
public List Differences { get; set; } = new();
}
}