Files
SabreTools/SabreTools.DatFiles/Formats/AttractMode.cs

276 lines
10 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
2020-12-08 15:15:41 -08:00
using SabreTools.DatItems;
2020-12-07 15:08:57 -08:00
using SabreTools.IO;
2020-12-09 23:11:10 -08:00
using SabreTools.IO.Readers;
using SabreTools.IO.Writers;
namespace SabreTools.DatFiles.Formats
{
2019-01-11 13:43:15 -08:00
/// <summary>
/// Represents parsing and writing of an AttractMode DAT
/// </summary>
internal class AttractMode : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public AttractMode(DatFile datFile)
: base(datFile)
2019-01-11 13:43:15 -08:00
{
}
/// <summary>
/// Parse an AttractMode DAT and return all found games 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>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
2020-12-10 13:30:08 -08:00
public override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
// Open a file reader
2020-12-10 22:16:53 -08:00
Encoding enc = filename.GetEncoding();
2020-12-08 00:13:22 -08:00
SeparatedValueReader svr = new SeparatedValueReader(File.OpenRead(filename), enc)
2020-09-07 12:58:55 -07:00
{
Header = true,
Quotes = false,
Separator = ';',
VerifyFieldCount = true
};
// If we're somehow at the end of the stream already, we can't do anything
if (svr.EndOfStream)
return;
// Read in the header
svr.ReadHeader();
// Header values should match
// #Name;Title;Emulator;CloneOf;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons
2019-01-11 13:43:15 -08:00
2020-09-07 12:58:55 -07:00
// Loop through all of the data lines
while (!svr.EndOfStream)
2019-01-11 13:43:15 -08:00
{
2020-09-07 12:58:55 -07:00
try
{
// Get the current line, split and parse
svr.ReadNextLine();
2019-01-11 13:43:15 -08:00
Rom rom = new Rom
2020-08-20 13:17:14 -07:00
{
Name = "-",
Size = Constants.SizeZero,
CRC = Constants.CRCZero,
MD5 = Constants.MD5Zero,
SHA1 = Constants.SHA1Zero,
ItemStatus = ItemStatus.None,
2020-08-20 13:17:14 -07:00
Machine = new Machine
{
Name = svr.Line[0], // #Name
Description = svr.Line[1], // Title
CloneOf = svr.Line[3], // CloneOf
Year = svr.Line[4], // Year
Manufacturer = svr.Line[5], // Manufacturer
Category = svr.Line[6], // Category
Players = svr.Line[7], // Players
Rotation = svr.Line[8], // Rotation
Control = svr.Line[9], // Control
Status = svr.Line[10], // Status
DisplayCount = svr.Line[11], // DisplayCount
DisplayType = svr.Line[12], // DisplayType
Comment = svr.Line[15], // Extra
Buttons = svr.Line[16], // Buttons
},
2020-08-20 21:15:37 -07:00
AltName = svr.Line[13], // AltRomname
AltTitle = svr.Line[14], // AltTitle
2019-01-11 13:43:15 -08:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
// Now process and add the rom
ParseAddHelper(rom);
}
catch (Exception ex)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
}
2019-01-11 13:43:15 -08:00
}
2020-09-07 12:58:55 -07:00
svr.Dispose();
2019-01-11 13:43:15 -08:00
}
2020-09-18 17:12:31 -07:00
/// <inheritdoc/>
protected override ItemType[] GetSupportedTypes()
{
return new ItemType[] { ItemType.Rom };
}
2019-01-11 13:43:15 -08:00
/// <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>
2019-02-08 15:21:25 -08:00
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
2019-01-11 13:43:15 -08:00
/// <returns>True if the DAT was written correctly, false otherwise</returns>
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
2019-01-11 13:43:15 -08:00
{
try
{
logger.User($"Opening file for writing: {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;
}
SeparatedValueWriter svw = new SeparatedValueWriter(fs, new UTF8Encoding(false))
{
Quotes = false,
Separator = ';',
VerifyFieldCount = true
};
2019-01-11 13:43:15 -08:00
// Write out the header
WriteHeader(svw);
2019-01-11 13:43:15 -08:00
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
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];
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Check for a "null" item
datItem = ProcessNullifiedItem(datItem);
2019-01-11 13:43:15 -08:00
2020-08-28 15:06:07 -07:00
// Write out the item if we're not ignoring
if (!ShouldIgnore(datItem, ignoreblanks))
WriteDatItem(svw, datItem);
2019-01-11 13:43:15 -08:00
}
}
logger.Verbose($"File written!{Environment.NewLine}");
svw.Dispose();
2019-01-11 13:43:15 -08:00
fs.Dispose();
}
catch (Exception ex)
{
logger.Error(ex);
if (throwOnError) throw ex;
2019-01-11 13:43:15 -08:00
return false;
}
return true;
}
/// <summary>
/// Write out DAT header using the supplied StreamWriter
/// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param>
private void WriteHeader(SeparatedValueWriter svw)
2019-01-11 13:43:15 -08:00
{
string[] headers = new string[]
2019-01-11 13:43:15 -08:00
{
"#Name",
"Title",
"Emulator",
"CloneOf",
"Year",
"Manufacturer",
"Category",
"Players",
"Rotation",
"Control",
"Status",
"DisplayCount",
"DisplayType",
"AltRomname",
"AltTitle",
"Extra",
"Buttons",
};
2020-09-15 12:12:13 -07:00
svw.WriteHeader(headers);
2019-01-11 13:43:15 -08:00
svw.Flush();
2019-01-11 13:43:15 -08:00
}
/// <summary>
/// Write out Game start using the supplied StreamWriter
/// </summary>
/// <param name="svw">SeparatedValueWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
2019-01-11 13:43:15 -08:00
{
// No game should start with a path separator
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
// Pre-process the item name
ProcessItemName(datItem, true);
// Build the state
switch (datItem.ItemType)
{
case ItemType.Rom:
var rom = datItem as Rom;
string[] fields = new string[]
{
2020-08-23 22:23:55 -07:00
rom.Machine.Name,
rom.Machine.Description,
Header.FileName,
rom.Machine.CloneOf,
rom.Machine.Year,
rom.Machine.Manufacturer,
rom.Machine.Category,
rom.Machine.Players,
rom.Machine.Rotation,
rom.Machine.Control,
rom.ItemStatus.ToString(),
rom.Machine.DisplayCount,
rom.Machine.DisplayType,
rom.AltName,
rom.AltTitle,
rom.Machine.Comment,
rom.Machine.Buttons,
};
svw.WriteValues(fields);
break;
2019-01-11 13:43:15 -08:00
}
2020-09-15 12:12:13 -07:00
svw.Flush();
2019-01-11 13:43:15 -08:00
}
}
}