2024-03-12 19:54:43 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using SabreTools.DatItems;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.DatFiles.Formats
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
2023-07-30 21:32:49 -04:00
|
|
|
|
/// Represents a Missfile
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// </summary>
|
2025-02-13 14:22:51 -05:00
|
|
|
|
public sealed class Missfile : DatFile
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2025-01-09 06:14:01 -05:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override ItemType[] SupportedTypes
|
|
|
|
|
|
=> Enum.GetValues(typeof(ItemType)) as ItemType[] ?? [];
|
|
|
|
|
|
|
2019-01-11 13:43:15 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
2024-10-24 04:48:21 -04:00
|
|
|
|
public Missfile(DatFile? datFile) : base(datFile)
|
2019-01-11 13:43:15 -08:00
|
|
|
|
{
|
2025-04-14 12:10:09 -04:00
|
|
|
|
Header.SetFieldValue<DatFormat>(DatHeader.DatFormatKey, DatFormat.MissFile);
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
2024-03-12 19:54:43 -04:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
/// <remarks>There is no consistent way to parse a missfile</remarks>
|
|
|
|
|
|
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-01-09 16:25:53 -05:00
|
|
|
|
protected internal override List<string>? GetMissingRequiredFields(DatItem datItem)
|
2024-03-12 19:54:43 -04:00
|
|
|
|
{
|
|
|
|
|
|
// TODO: Check required fields
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.User($"Writing to '{outfile}'...");
|
2024-03-12 19:54:43 -04:00
|
|
|
|
FileStream fs = File.Create(outfile);
|
|
|
|
|
|
|
|
|
|
|
|
// If we get back null for some reason, just log and return
|
|
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2024-03-12 19:54:43 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StreamWriter sw = new(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)
|
|
|
|
|
|
{
|
2025-01-12 23:15:30 -05:00
|
|
|
|
List<DatItem> datItems = GetItemsForBucket(key, filter: true);
|
2024-03-12 19:54:43 -04:00
|
|
|
|
|
|
|
|
|
|
// If this machine doesn't contain any writable items, skip
|
|
|
|
|
|
if (!ContainsWritable(datItems))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve the names in the block
|
2025-01-07 15:28:01 -05:00
|
|
|
|
datItems = ResolveNames(datItems);
|
2024-03-12 19:54:43 -04:00
|
|
|
|
|
|
|
|
|
|
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 using machine names or we're not ignoring
|
2025-01-29 22:51:30 -05:00
|
|
|
|
if (!Modifiers.UseRomName || !ShouldIgnore(datItem, ignoreblanks))
|
2024-03-12 19:54:43 -04:00
|
|
|
|
WriteDatItem(sw, datItem, lastgame);
|
|
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
2025-05-02 16:46:20 -04:00
|
|
|
|
lastgame = datItem.GetMachine()!.GetName();
|
2024-03-12 19:54:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.User($"'{outfile}' written!{Environment.NewLine}");
|
2024-03-12 19:54:43 -04:00
|
|
|
|
sw.Dispose();
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Error(ex);
|
2024-03-12 19:54:43 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-20 11:22:33 -04:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override bool WriteToFileDB(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.User($"Writing to '{outfile}'...");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
FileStream fs = File.Create(outfile);
|
|
|
|
|
|
|
|
|
|
|
|
// If we get back null for some reason, just log and return
|
|
|
|
|
|
if (fs == null)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StreamWriter sw = new(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 ItemsDB.SortedKeys)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If this machine doesn't contain any writable items, skip
|
2025-01-12 23:15:30 -05:00
|
|
|
|
var itemsDict = GetItemsForBucketDB(key, filter: true);
|
2025-01-09 10:16:39 -05:00
|
|
|
|
if (itemsDict == null || !ContainsWritable([.. itemsDict.Values]))
|
2024-03-20 11:22:33 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve the names in the block
|
2025-01-07 15:28:01 -05:00
|
|
|
|
var items = ResolveNamesDB([.. itemsDict]);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
2024-12-06 23:16:09 -05:00
|
|
|
|
foreach (var kvp in items)
|
2024-03-20 11:22:33 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Check for a "null" item
|
2025-01-09 10:01:56 -05:00
|
|
|
|
var datItem = new KeyValuePair<long, DatItem>(kvp.Key, ProcessNullifiedItem(kvp.Value));
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Get the machine for the item
|
2025-02-24 09:20:46 -05:00
|
|
|
|
var machine = GetMachineForItemDB(datItem.Key);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Write out the item if we're using machine names or we're not ignoring
|
2025-01-29 22:51:30 -05:00
|
|
|
|
if (!Modifiers.UseRomName || !ShouldIgnore(datItem.Value, ignoreblanks))
|
2024-03-20 11:22:33 -04:00
|
|
|
|
WriteDatItemDB(sw, datItem, lastgame);
|
|
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
2025-05-02 16:05:08 -04:00
|
|
|
|
lastgame = machine.Value!.GetName();
|
2024-03-20 11:22:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.User($"'{outfile}' written!{Environment.NewLine}");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
sw.Dispose();
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex) when (!throwOnError)
|
|
|
|
|
|
{
|
2025-01-08 16:59:44 -05:00
|
|
|
|
_logger.Error(ex);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-12 19:54:43 -04:00
|
|
|
|
/// <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>
|
|
|
|
|
|
/// <param name="lastgame">The name of the last game to be output</param>
|
|
|
|
|
|
private void WriteDatItem(StreamWriter sw, DatItem datItem, string? lastgame)
|
|
|
|
|
|
{
|
2025-05-02 16:46:20 -04:00
|
|
|
|
var machine = datItem.GetMachine();
|
2025-02-14 15:28:22 -05:00
|
|
|
|
WriteDatItemImpl(sw, datItem, machine!, lastgame);
|
2024-03-12 19:54:43 -04:00
|
|
|
|
}
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
/// <param name="lastgame">The name of the last game to be output</param>
|
2024-12-06 23:16:09 -05:00
|
|
|
|
private void WriteDatItemDB(StreamWriter sw, KeyValuePair<long, DatItem> datItem, string? lastgame)
|
2024-03-20 11:22:33 -04:00
|
|
|
|
{
|
2025-02-24 09:20:46 -05:00
|
|
|
|
var machine = GetMachineForItemDB(datItem.Key).Value;
|
2025-02-14 15:28:22 -05:00
|
|
|
|
WriteDatItemImpl(sw, datItem.Value, machine!, lastgame);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
/// <param name="machine">Machine object representing the set the item is in</param>
|
|
|
|
|
|
/// <param name="lastgame">The name of the last game to be output</param>
|
|
|
|
|
|
private void WriteDatItemImpl(StreamWriter sw, DatItem datItem, Machine machine, string? lastgame)
|
|
|
|
|
|
{
|
2024-03-20 11:22:33 -04:00
|
|
|
|
// Process the item name
|
2025-02-14 15:28:22 -05:00
|
|
|
|
ProcessItemName(datItem, machine, forceRemoveQuotes: false, forceRomName: false);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Romba mode automatically uses item name
|
2025-01-29 22:51:30 -05:00
|
|
|
|
if (Modifiers.OutputDepot?.IsActive == true || Modifiers.UseRomName)
|
2025-02-14 15:28:22 -05:00
|
|
|
|
sw.Write($"{datItem.GetName() ?? string.Empty}\n");
|
2025-05-02 16:05:08 -04:00
|
|
|
|
else if (!Modifiers.UseRomName && machine!.GetName() != lastgame)
|
|
|
|
|
|
sw.Write($"{machine!.GetName() ?? string.Empty}\n");
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
2019-01-11 13:43:15 -08:00
|
|
|
|
}
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|