2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2020-08-28 15:06:07 -07:00
|
|
|
|
using System.Linq;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using System.Text;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2017-10-09 18:04:49 -07:00
|
|
|
|
using SabreTools.Library.Data;
|
2017-11-02 15:44:15 -07:00
|
|
|
|
using SabreTools.Library.DatItems;
|
2020-08-01 23:04:11 -07:00
|
|
|
|
using SabreTools.Library.IO;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
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)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
: 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>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <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
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
Globals.Logger.User($"Opening file for writing: {outfile}");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2020-06-10 22:37:19 -07:00
|
|
|
|
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());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DatItem using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <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
|
2020-06-10 22:37:19 -07:00
|
|
|
|
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-08-23 22:23:55 -07:00
|
|
|
|
sw.Write($"{datItem.Name}\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());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|