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>
|
2024-03-12 19:54:43 -04:00
|
|
|
|
internal 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
|
|
|
|
{
|
|
|
|
|
|
}
|
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/>
|
|
|
|
|
|
protected override List<string>? GetMissingRequiredFields(DatItem datItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
{
|
2024-10-30 10:59:04 -04:00
|
|
|
|
List<DatItem> datItems = Items.FilteredItems(key);
|
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
|
|
|
|
|
|
if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true || !ShouldIgnore(datItem, ignoreblanks))
|
|
|
|
|
|
WriteDatItem(sw, datItem, lastgame);
|
|
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
|
|
|
|
|
lastgame = datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var itemsDict = ItemsDB.GetItemsForBucket(key, filter: true);
|
2025-01-09 03:31:31 -05:00
|
|
|
|
if (itemsDict == null || !ContainsWritableDB(itemsDict))
|
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
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = ItemsDB.GetMachineForItem(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
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
|
|
|
|
|
|
|| !ShouldIgnore(datItem.Value, ignoreblanks))
|
|
|
|
|
|
{
|
2024-03-20 11:22:33 -04:00
|
|
|
|
WriteDatItemDB(sw, datItem, lastgame);
|
2024-12-06 23:16:09 -05:00
|
|
|
|
}
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
2024-12-06 23:16:09 -05:00
|
|
|
|
lastgame = machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Process the item name
|
|
|
|
|
|
ProcessItemName(datItem, false, forceRomName: false);
|
|
|
|
|
|
|
|
|
|
|
|
// Romba mode automatically uses item name
|
|
|
|
|
|
if (Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey)?.IsActive == true || Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
|
|
|
|
|
|
sw.Write($"{datItem.GetName() ?? string.Empty}\n");
|
|
|
|
|
|
else if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true && datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) != lastgame)
|
|
|
|
|
|
sw.Write($"{datItem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? string.Empty}\n");
|
|
|
|
|
|
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
|
|
// Get the machine for the item
|
2024-12-06 23:16:09 -05:00
|
|
|
|
var machine = ItemsDB.GetMachineForItem(datItem.Key);
|
2024-03-20 11:22:33 -04:00
|
|
|
|
|
|
|
|
|
|
// Process the item name
|
|
|
|
|
|
ProcessItemNameDB(datItem, false, forceRomName: false);
|
|
|
|
|
|
|
|
|
|
|
|
// Romba mode automatically uses item name
|
2024-12-06 23:16:09 -05:00
|
|
|
|
if (Header.GetFieldValue<DepotInformation?>(DatHeader.OutputDepotKey)?.IsActive == true
|
|
|
|
|
|
|| Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true)
|
|
|
|
|
|
{
|
|
|
|
|
|
sw.Write($"{datItem.Value.GetName() ?? string.Empty}\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!Header.GetBoolFieldValue(DatHeader.UseRomNameKey) == true
|
|
|
|
|
|
&& machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) != lastgame)
|
|
|
|
|
|
{
|
|
|
|
|
|
sw.Write($"{machine.Value!.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? 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
|
|
|
|
}
|