using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.IO;
namespace SabreTools.Library.DatFiles
{
///
/// Represents parsing and writing of a Missfile
///
internal class Missfile : DatFile
{
///
/// Constructor designed for casting a base DatFile
///
/// Parent DatFile to copy from
public Missfile(DatFile datFile)
: base(datFile)
{
}
///
/// Parse a Missfile and return all found games and roms within
///
/// Name of the file to be parsed
/// Index ID for the DAT
/// True if full pathnames are to be kept, false otherwise (default)
protected override void ParseFile(string filename, int indexId, bool keep)
{
// There is no consistent way to parse a missfile...
throw new NotImplementedException();
}
///
/// Create and open an output file for writing direct from a dictionary
///
/// Name of the file to write to
/// True if blank roms should be skipped on output, false otherwise (default)
/// True if the DAT was written correctly, false otherwise
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
// Write out each of the machines and roms
string lastgame = null;
// Use a sorted list of games to output
foreach (string key in Items.SortedKeys)
{
List datItems = Items.FilteredItems(key);
// Resolve the names in the block
datItems = DatItem.ResolveNames(datItems);
for (int index = 0; index < datItems.Count; index++)
{
DatItem datItem = datItems[index];
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(sw, datItem, lastgame);
// Set the new data to compare against
lastgame = datItem.Machine.Name;
}
}
Globals.Logger.Verbose("File written!" + Environment.NewLine);
sw.Dispose();
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
///
/// Write out DatItem using the supplied StreamWriter
///
/// StreamWriter to output to
/// DatItem object to be output
/// The name of the last game to be output
/// True if the data was written, false on error
private bool WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame)
{
try
{
// Process the item name
ProcessItemName(datItem, false, forceRomName: false);
// Romba mode automatically uses item name
if (Header.OutputDepot?.IsActive == true || Header.UseRomName)
{
sw.Write($"{datItem.Name}\n");
}
else if (!Header.UseRomName && datItem.Machine.Name != lastgame)
{
sw.Write($"{datItem.Machine.Name}\n");
lastgame = datItem.Machine.Name;
}
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}