Files
SabreTools/SabreTools.DatFiles/Formats/Missfile.cs

120 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.Core;
2020-12-08 15:15:41 -08:00
using SabreTools.DatItems;
namespace SabreTools.DatFiles.Formats
{
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
{
}
/// <inheritdoc/>
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
// There is no consistent way to parse a missfile...
throw new NotImplementedException();
}
/// <inheritdoc/>
protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem)
{
// TODO: Check required fields
return null;
}
/// <inheritdoc/>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
try
{
2021-02-03 11:22:09 -08:00
logger.User($"Writing to '{outfile}'...");
2020-12-08 00:13:22 -08:00
FileStream fs = File.Create(outfile);
2019-01-11 13:43:15 -08:00
// If we get back null for some reason, just log and return
if (fs == null)
{
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(fs, new UTF8Encoding(false));
2019-01-11 13:43:15 -08:00
// 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
{
ConcurrentList<DatItem> datItems = Items.FilteredItems(key);
2019-01-11 13:43:15 -08:00
2020-09-25 20:25:29 -07:00
// If this machine doesn't contain any writable items, skip
if (!ContainsWritable(datItems))
continue;
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 using machine names or we're not ignoring
if (!Header.UseRomName || !ShouldIgnore(datItem, ignoreblanks))
2020-08-28 15:06:07 -07:00
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
}
}
2021-02-03 11:22:09 -08:00
logger.User($"'{outfile}' written!{Environment.NewLine}");
2019-01-11 13:43:15 -08:00
sw.Dispose();
fs.Dispose();
}
catch (Exception ex) when (!throwOnError)
2019-01-11 13:43:15 -08:00
{
logger.Error(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>
private void WriteDatItem(StreamWriter sw, DatItem datItem, string lastgame)
2019-01-11 13:43:15 -08:00
{
// Process the item name
ProcessItemName(datItem, false, forceRomName: false);
2019-01-11 13:43:15 -08:00
// Romba mode automatically uses item name
if (Header.OutputDepot?.IsActive == true || Header.UseRomName)
sw.Write($"{datItem.GetName() ?? string.Empty}\n");
else if (!Header.UseRomName && datItem.Machine.Name != lastgame)
sw.Write($"{datItem.Machine.Name}\n");
2019-01-11 13:43:15 -08:00
sw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}