Files
SabreTools/SabreTools.Library/DatFiles/Missfile.cs

144 lines
5.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2020-08-28 15:06:07 -07:00
using System.Linq;
using System.Text;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
2020-08-01 23:04:11 -07:00
using SabreTools.Library.IO;
namespace SabreTools.Library.DatFiles
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of a Missfile
/// </summary>
internal class Missfile : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public Missfile(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse a Missfile and return all found games and roms within
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
2019-01-11 13:43:15 -08:00
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
2020-08-28 15:06:07 -07:00
protected override void ParseFile(string filename, int indexId, bool keep)
2019-01-11 13:43:15 -08:00
{
// There is no consistent way to parse a missfile...
throw new NotImplementedException();
}
/// <summary>
/// Create and open an output file for writing direct from a dictionary
/// </summary>
/// <param name="outfile">Name of the file to write to</param>
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
2019-01-11 13:43:15 -08:00
// 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");
2019-01-11 13:43:15 -08:00
return false;
}
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
// Write out each of the machines and roms
string lastgame = null;
2020-07-26 21:00:30 -07:00
// Use a sorted list of games to output
2020-07-26 22:34:45 -07:00
foreach (string key in Items.SortedKeys)
2019-01-11 13:43:15 -08:00
{
2020-08-28 15:06:07 -07:00
List<DatItem> datItems = Items.FilteredItems(key);
2019-01-11 13:43:15 -08:00
// Resolve the names in the block
2020-08-28 15:06:07 -07:00
datItems = DatItem.ResolveNames(datItems);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
for (int index = 0; index < datItems.Count; index++)
2019-01-11 13:43:15 -08:00
{
2020-08-28 15:06:07 -07:00
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);
2019-01-11 13:43:15 -08:00
// Set the new data to compare against
2020-08-28 15:06:07 -07:00
lastgame = datItem.Machine.Name;
2019-01-11 13:43:15 -08:00
}
}
Globals.Logger.Verbose("File written!" + Environment.NewLine);
sw.Dispose();
fs.Dispose();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
2020-09-15 12:12:13 -07:00
if (Globals.ThrowOnError)
throw ex;
2019-01-11 13:43:15 -08:00
return false;
}
return true;
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="sw">StreamWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
2019-01-11 13:43:15 -08:00
/// <param name="lastgame">The name of the last game to be output</param>
/// <returns>True if the data was written, false on error</returns>
2020-08-28 15:06:07 -07:00
private bool WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame)
2019-01-11 13:43:15 -08:00
{
try
{
// Process the item name
ProcessItemName(datItem, false, forceRomName: false);
2019-01-11 13:43:15 -08:00
2020-08-14 20:28:54 -07:00
// Romba mode automatically uses item name
2020-08-28 01:13:55 -07:00
if (Header.OutputDepot?.IsActive == true || Header.UseRomName)
2019-01-11 13:43:15 -08:00
{
2020-09-02 12:32:10 -07:00
sw.Write($"{datItem.GetName() ?? string.Empty}\n");
2019-01-11 13:43:15 -08:00
}
2020-08-20 13:17:14 -07:00
else if (!Header.UseRomName && datItem.Machine.Name != lastgame)
2019-01-11 13:43:15 -08:00
{
2020-08-23 22:23:55 -07:00
sw.Write($"{datItem.Machine.Name}\n");
2020-08-20 13:17:14 -07:00
lastgame = datItem.Machine.Name;
2019-01-11 13:43:15 -08:00
}
sw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
2020-09-15 12:12:13 -07:00
if (Globals.ThrowOnError)
throw ex;
2019-01-11 13:43:15 -08:00
return false;
}
return true;
}
}
}