mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Use ClrMamePro serializer in current parser
This also starts splitting up the current parser structures to make them a bit more modular.
This commit is contained in:
635
SabreTools.DatFiles/Formats/ClrMamePro.Reader.cs
Normal file
635
SabreTools.DatFiles/Formats/ClrMamePro.Reader.cs
Normal file
@@ -0,0 +1,635 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
|
||||
namespace SabreTools.DatFiles.Formats
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents parsing of a ClrMamePro DAT
|
||||
/// </summary>
|
||||
internal partial class ClrMamePro : DatFile
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Deserialize the input file
|
||||
var metadataFile = Serialization.ClrMamePro.Deserialize(filename, this.Quotes);
|
||||
|
||||
// Convert the header to the internal format
|
||||
ConvertHeader(metadataFile?.ClrMamePro, keep);
|
||||
|
||||
// Convert the game data to the internal format
|
||||
ConvertGames(metadataFile?.Game, filename, indexId, statsOnly);
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
string message = $"'{filename}' - An error occurred during parsing";
|
||||
logger.Error(ex, message);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#region Converters
|
||||
|
||||
/// <summary>
|
||||
/// Convert header information
|
||||
/// </summary>
|
||||
/// <param name="cmp">Deserialized model to convert</param>
|
||||
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
||||
private void ConvertHeader(Models.ClrMamePro.ClrMamePro? cmp, bool keep)
|
||||
{
|
||||
// If the header is missing, we can't do anything
|
||||
if (cmp == null)
|
||||
return;
|
||||
|
||||
Header.Name ??= cmp.Name;
|
||||
Header.Description ??= cmp.Description;
|
||||
Header.RootDir ??= cmp.RootDir;
|
||||
Header.Category ??= cmp.Category;
|
||||
Header.Version ??= cmp.Version;
|
||||
Header.Date ??= cmp.Date;
|
||||
Header.Author ??= cmp.Author;
|
||||
Header.Homepage ??= cmp.Homepage;
|
||||
Header.Url ??= cmp.Url;
|
||||
Header.Comment ??= cmp.Comment;
|
||||
Header.HeaderSkipper ??= cmp.Header;
|
||||
Header.Type ??= cmp.Type;
|
||||
if (Header.ForceMerging == MergingFlag.None)
|
||||
Header.ForceMerging = cmp.ForceMerging.AsMergingFlag();
|
||||
if (Header.ForcePacking == PackingFlag.None)
|
||||
Header.ForcePacking = cmp.ForceZipping.AsPackingFlag();
|
||||
if (Header.ForcePacking == PackingFlag.None)
|
||||
Header.ForcePacking = cmp.ForcePacking.AsPackingFlag();
|
||||
|
||||
// Handle implied SuperDAT
|
||||
if (cmp.Name.Contains(" - SuperDAT") && keep)
|
||||
Header.Type ??= "SuperDAT";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert games information
|
||||
/// </summary>
|
||||
/// <param name="games">Array of deserialized models to convert</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
private void ConvertGames(Models.ClrMamePro.GameBase[]? games, string filename, int indexId, bool statsOnly)
|
||||
{
|
||||
// If the game array is missing, we can't do anything
|
||||
if (games == null || !games.Any())
|
||||
return;
|
||||
|
||||
// Loop through the games and add
|
||||
foreach (var game in games)
|
||||
{
|
||||
ConvertGame(game, filename, indexId, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert game information
|
||||
/// </summary>
|
||||
/// <param name="game">Deserialized model to convert</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
private void ConvertGame(Models.ClrMamePro.GameBase game, string filename, int indexId, bool statsOnly)
|
||||
{
|
||||
// If the game is missing, we can't do anything
|
||||
if (game == null)
|
||||
return;
|
||||
|
||||
// Create the machine for copying information
|
||||
var machine = new Machine
|
||||
{
|
||||
Name = game.Name,
|
||||
Description = game.Description,
|
||||
Year = game.Year,
|
||||
Manufacturer = game.Manufacturer,
|
||||
Category = game.Category,
|
||||
CloneOf = game.CloneOf,
|
||||
RomOf = game.RomOf,
|
||||
SampleOf = game.SampleOf,
|
||||
MachineType = (game is Models.ClrMamePro.Resource ? MachineType.Bios : MachineType.None),
|
||||
};
|
||||
|
||||
// Check if there are any items
|
||||
bool containsItems = false;
|
||||
|
||||
// Loop through each type of item
|
||||
ConvertReleases(game.Release, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertBiosSets(game.BiosSet, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertRoms(game.Rom, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertDisks(game.Disk, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertMedia(game.Media, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertArchives(game.Archive, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertChips(game.Chip, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertVideo(game.Video, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertSound(game.Sound, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertInput(game.Input, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertDipSwitches(game.DipSwitch, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
ConvertDriver(game.Driver, machine, filename, indexId, statsOnly, ref containsItems);
|
||||
|
||||
// If we had no items, create a Blank placeholder
|
||||
if (!containsItems)
|
||||
{
|
||||
var blank = new Blank
|
||||
{
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
blank.CopyMachineInformation(machine);
|
||||
ParseAddHelper(blank, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Release information
|
||||
/// </summary>
|
||||
/// <param name="releases">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertReleases(Models.ClrMamePro.Release[]? releases, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the release array is missing, we can't do anything
|
||||
if (releases == null || !releases.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var release in releases)
|
||||
{
|
||||
var item = new Release
|
||||
{
|
||||
Name = release.Name,
|
||||
Region = release.Region,
|
||||
Language = release.Language,
|
||||
Date = release.Date,
|
||||
Default = release.Default?.AsYesNo(),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert BiosSet information
|
||||
/// </summary>
|
||||
/// <param name="biossets">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertBiosSets(Models.ClrMamePro.BiosSet[]? biossets, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the biosset array is missing, we can't do anything
|
||||
if (biossets == null || !biossets.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var biosset in biossets)
|
||||
{
|
||||
var item = new BiosSet
|
||||
{
|
||||
Name = biosset.Name,
|
||||
Description = biosset.Description,
|
||||
Default = biosset.Default?.AsYesNo(),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Rom information
|
||||
/// </summary>
|
||||
/// <param name="roms">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertRoms(Models.ClrMamePro.Rom[]? roms, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the rom array is missing, we can't do anything
|
||||
if (roms == null || !roms.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var rom in roms)
|
||||
{
|
||||
var item = new Rom
|
||||
{
|
||||
Name = rom.Name,
|
||||
Size = Utilities.CleanLong(rom.Size),
|
||||
CRC = rom.CRC,
|
||||
MD5 = rom.MD5,
|
||||
SHA1 = rom.SHA1,
|
||||
SHA256 = rom.SHA256,
|
||||
SHA384 = rom.SHA384,
|
||||
SHA512 = rom.SHA512,
|
||||
SpamSum = rom.SpamSum,
|
||||
//xxHash364 = rom.xxHash364, // TODO: Add to internal model
|
||||
//xxHash3128 = rom.xxHash3128, // TODO: Add to internal model
|
||||
MergeTag = rom.Merge,
|
||||
ItemStatus = rom.Status?.AsItemStatus() ?? ItemStatus.NULL,
|
||||
Region = rom.Region,
|
||||
//Flags = rom.Flags, // TODO: Add to internal model
|
||||
Offset = rom.Offs,
|
||||
//Serial = rom.Serial, // TODO: Add to internal model
|
||||
//Header = rom.Header, // TODO: Add to internal model
|
||||
Date = rom.Date,
|
||||
Inverted = rom.Inverted?.AsYesNo(),
|
||||
MIA = rom.MIA?.AsYesNo(),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Disk information
|
||||
/// </summary>
|
||||
/// <param name="disks">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertDisks(Models.ClrMamePro.Disk[]? disks, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the disk array is missing, we can't do anything
|
||||
if (disks == null || !disks.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var disk in disks)
|
||||
{
|
||||
var item = new Disk
|
||||
{
|
||||
Name = disk.Name,
|
||||
MD5 = disk.MD5,
|
||||
SHA1 = disk.SHA1,
|
||||
MergeTag = disk.Merge,
|
||||
ItemStatus = disk.Status?.AsItemStatus() ?? ItemStatus.NULL,
|
||||
//Flags = disk.Flags, // TODO: Add to internal model
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Media information
|
||||
/// </summary>
|
||||
/// <param name="media">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertMedia(Models.ClrMamePro.Media[]? media, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the media array is missing, we can't do anything
|
||||
if (media == null || !media.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var medium in media)
|
||||
{
|
||||
var item = new Media
|
||||
{
|
||||
Name = medium.Name,
|
||||
MD5 = medium.MD5,
|
||||
SHA1 = medium.SHA1,
|
||||
SHA256 = medium.SHA256,
|
||||
SpamSum = medium.SpamSum,
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Archive information
|
||||
/// </summary>
|
||||
/// <param name="archives">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertArchives(Models.ClrMamePro.Archive[]? archives, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the archive array is missing, we can't do anything
|
||||
if (archives == null || !archives.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var archive in archives)
|
||||
{
|
||||
var item = new Archive
|
||||
{
|
||||
Name = archive.Name,
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Chip information
|
||||
/// </summary>
|
||||
/// <param name="chips">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertChips(Models.ClrMamePro.Chip[]? chips, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the chip array is missing, we can't do anything
|
||||
if (chips == null || !chips.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var chip in chips)
|
||||
{
|
||||
var item = new Chip
|
||||
{
|
||||
ChipType = chip.Type?.AsChipType() ?? ChipType.NULL,
|
||||
Name = chip.Name,
|
||||
//Flags = chip.Flags, // TODO: Add to internal model
|
||||
Clock = Utilities.CleanLong(chip.Clock),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Video information
|
||||
/// </summary>
|
||||
/// <param name="video">Deserialized model to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertVideo(Models.ClrMamePro.Video? video, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the video is missing, we can't do anything
|
||||
if (video == null)
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
var item = new Display
|
||||
{
|
||||
DisplayType = video.Screen?.AsDisplayType() ?? DisplayType.NULL,
|
||||
Width = Utilities.CleanLong(video.X),
|
||||
Height = Utilities.CleanLong(video.Y),
|
||||
//AspectX = video.AspectX, // TODO: Add to internal model or find mapping
|
||||
//AspectY = video.AspectY, // TODO: Add to internal model or find mapping
|
||||
Refresh = Utilities.CleanDouble(video.Freq),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
switch (video.Orientation)
|
||||
{
|
||||
case "vertical":
|
||||
item.Rotate = 0;
|
||||
break;
|
||||
case "horizontal":
|
||||
item.Rotate = 90;
|
||||
break;
|
||||
}
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Sound information
|
||||
/// </summary>
|
||||
/// <param name="sound">Deserialized model to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertSound(Models.ClrMamePro.Sound? sound, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the sound is missing, we can't do anything
|
||||
if (sound == null)
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
var item = new Sound
|
||||
{
|
||||
Channels = Utilities.CleanLong(sound.Channels),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Input information
|
||||
/// </summary>
|
||||
/// <param name="input">Deserialized model to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertInput(Models.ClrMamePro.Input? input, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the input is missing, we can't do anything
|
||||
if (input == null)
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
var item = new Input
|
||||
{
|
||||
Players = Utilities.CleanLong(input.Players),
|
||||
//Control = input.Control, // TODO: Add to internal model or find mapping
|
||||
Controls = new List<Control>
|
||||
{
|
||||
new Control
|
||||
{
|
||||
Buttons = Utilities.CleanLong(input.Buttons),
|
||||
},
|
||||
},
|
||||
Coins = Utilities.CleanLong(input.Coins),
|
||||
Tilt = input.Tilt?.AsYesNo(),
|
||||
Service = input.Service?.AsYesNo(),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert DipSwitch information
|
||||
/// </summary>
|
||||
/// <param name="dipswitches">Array of deserialized models to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertDipSwitches(Models.ClrMamePro.DipSwitch[]? dipswitches, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the dipswitch array is missing, we can't do anything
|
||||
if (dipswitches == null || !dipswitches.Any())
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
foreach (var dipswitch in dipswitches)
|
||||
{
|
||||
var item = new DipSwitch
|
||||
{
|
||||
Name = dipswitch.Name,
|
||||
Values = new List<Setting>(),
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
foreach (string entry in dipswitch.Entry ?? Array.Empty<string>())
|
||||
{
|
||||
var setting = new Setting
|
||||
{
|
||||
Name = dipswitch.Name,
|
||||
Value = entry,
|
||||
Default = entry == dipswitch.Default,
|
||||
};
|
||||
item.Values.Add(setting);
|
||||
}
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Driver information
|
||||
/// </summary>
|
||||
/// <param name="driver">Deserialized model to convert</param>
|
||||
/// <param name="machine">Prefilled machine to use</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="containsItems">True if there were any items in the array, false otherwise</param>
|
||||
private void ConvertDriver(Models.ClrMamePro.Driver? driver, Machine machine, string filename, int indexId, bool statsOnly, ref bool containsItems)
|
||||
{
|
||||
// If the driver is missing, we can't do anything
|
||||
if (driver == null)
|
||||
return;
|
||||
|
||||
containsItems = true;
|
||||
var item = new Driver
|
||||
{
|
||||
Status = driver.Status?.AsSupportStatus() ?? SupportStatus.NULL,
|
||||
//Color = driver.Color, // TODO: Add to internal model or find mapping
|
||||
//Sound = driver.Sound, // TODO: Add to internal model or find mapping
|
||||
//PaletteSize = driver.PaletteSize, // TODO: Add to internal model or find mapping
|
||||
//Blit = driver.Blit, // TODO: Add to internal model or find mapping
|
||||
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
item.CopyMachineInformation(machine);
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
297
SabreTools.DatFiles/Formats/ClrMamePro.Writer.cs
Normal file
297
SabreTools.DatFiles/Formats/ClrMamePro.Writer.cs
Normal file
@@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.IO.Writers;
|
||||
|
||||
namespace SabreTools.DatFiles.Formats
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents writing of a ClrMamePro DAT
|
||||
/// </summary>
|
||||
internal partial class ClrMamePro : DatFile
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected override ItemType[] GetSupportedTypes()
|
||||
{
|
||||
return new ItemType[]
|
||||
{
|
||||
ItemType.Archive,
|
||||
ItemType.BiosSet,
|
||||
//ItemType.Chip,
|
||||
//ItemType.DipSwitch,
|
||||
ItemType.Disk,
|
||||
//ItemType.Display,
|
||||
//ItemType.Driver,
|
||||
//ItemType.Input,
|
||||
ItemType.Media,
|
||||
ItemType.Release,
|
||||
ItemType.Rom,
|
||||
ItemType.Sample,
|
||||
//ItemType.Sound,
|
||||
};
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.User($"Writing to '{outfile}'...");
|
||||
FileStream fs = System.IO.File.Create(outfile);
|
||||
|
||||
// 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");
|
||||
return false;
|
||||
}
|
||||
|
||||
ClrMameProWriter cmpw = new(fs, new UTF8Encoding(false))
|
||||
{
|
||||
Quotes = Quotes
|
||||
};
|
||||
|
||||
// Write out the header
|
||||
WriteHeader(cmpw);
|
||||
|
||||
// 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)
|
||||
{
|
||||
ConcurrentList<DatItem> datItems = Items.FilteredItems(key);
|
||||
|
||||
// If this machine doesn't contain any writable items, skip
|
||||
if (!ContainsWritable(datItems))
|
||||
continue;
|
||||
|
||||
// Resolve the names in the block
|
||||
datItems = DatItem.ResolveNames(datItems);
|
||||
|
||||
for (int index = 0; index < datItems.Count; index++)
|
||||
{
|
||||
DatItem datItem = datItems[index];
|
||||
|
||||
// If we have a different game and we're not at the start of the list, output the end of last item
|
||||
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
|
||||
WriteEndGame(cmpw, datItem);
|
||||
|
||||
// If we have a new game, output the beginning of the new item
|
||||
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
|
||||
WriteStartGame(cmpw, datItem);
|
||||
|
||||
// Check for a "null" item
|
||||
datItem = ProcessNullifiedItem(datItem);
|
||||
|
||||
// Write out the item if we're not ignoring
|
||||
if (!ShouldIgnore(datItem, ignoreblanks))
|
||||
WriteDatItem(cmpw, datItem);
|
||||
|
||||
// Set the new data to compare against
|
||||
lastgame = datItem.Machine.Name;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the file footer out
|
||||
WriteFooter(cmpw);
|
||||
|
||||
logger.User($"'{outfile}' written!{Environment.NewLine}");
|
||||
cmpw.Dispose();
|
||||
fs.Dispose();
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
logger.Error(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out DAT header using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
private void WriteHeader(ClrMameProWriter cmpw)
|
||||
{
|
||||
cmpw.WriteStartElement("clrmamepro");
|
||||
|
||||
cmpw.WriteRequiredStandalone("name", Header.Name);
|
||||
cmpw.WriteRequiredStandalone("description", Header.Description);
|
||||
cmpw.WriteOptionalStandalone("category", Header.Category);
|
||||
cmpw.WriteRequiredStandalone("version", Header.Version);
|
||||
cmpw.WriteOptionalStandalone("date", Header.Date);
|
||||
cmpw.WriteRequiredStandalone("author", Header.Author);
|
||||
cmpw.WriteOptionalStandalone("email", Header.Email);
|
||||
cmpw.WriteOptionalStandalone("homepage", Header.Homepage);
|
||||
cmpw.WriteOptionalStandalone("url", Header.Url);
|
||||
cmpw.WriteOptionalStandalone("comment", Header.Comment);
|
||||
if (Header.ForcePacking != PackingFlag.None)
|
||||
cmpw.WriteOptionalStandalone("forcezipping", Header.ForcePacking.FromPackingFlag(true), false);
|
||||
if (Header.ForceMerging != MergingFlag.None)
|
||||
cmpw.WriteOptionalStandalone("forcemerging", Header.ForceMerging.FromMergingFlag(false), false);
|
||||
|
||||
// End clrmamepro
|
||||
cmpw.WriteEndElement();
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out Game start using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
private static void WriteStartGame(ClrMameProWriter cmpw, DatItem datItem)
|
||||
{
|
||||
// No game should start with a path separator
|
||||
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
|
||||
|
||||
// Build the state
|
||||
cmpw.WriteStartElement(datItem.Machine.MachineType == MachineType.Bios ? "resource" : "game");
|
||||
|
||||
cmpw.WriteRequiredStandalone("name", datItem.Machine.Name);
|
||||
cmpw.WriteOptionalStandalone("romof", datItem.Machine.RomOf);
|
||||
cmpw.WriteOptionalStandalone("cloneof", datItem.Machine.CloneOf);
|
||||
cmpw.WriteOptionalStandalone("description", datItem.Machine.Description ?? datItem.Machine.Name);
|
||||
cmpw.WriteOptionalStandalone("year", datItem.Machine.Year);
|
||||
cmpw.WriteOptionalStandalone("manufacturer", datItem.Machine.Manufacturer);
|
||||
cmpw.WriteOptionalStandalone("category", datItem.Machine.Category);
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out Game end using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
private static void WriteEndGame(ClrMameProWriter cmpw, DatItem datItem)
|
||||
{
|
||||
// Build the state
|
||||
cmpw.WriteOptionalStandalone("sampleof", datItem.Machine.SampleOf);
|
||||
|
||||
// End game
|
||||
cmpw.WriteEndElement();
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out DatItem using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="datFile">DatFile to write out from</param>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
private void WriteDatItem(ClrMameProWriter cmpw, DatItem datItem)
|
||||
{
|
||||
// Pre-process the item name
|
||||
ProcessItemName(datItem, true);
|
||||
|
||||
// Build the state
|
||||
switch (datItem.ItemType)
|
||||
{
|
||||
case ItemType.Archive:
|
||||
var archive = datItem as Archive;
|
||||
cmpw.WriteStartElement("archive");
|
||||
cmpw.WriteRequiredAttributeString("name", archive.Name);
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.BiosSet:
|
||||
var biosSet = datItem as BiosSet;
|
||||
cmpw.WriteStartElement("biosset");
|
||||
cmpw.WriteRequiredAttributeString("name", biosSet.Name);
|
||||
cmpw.WriteOptionalAttributeString("description", biosSet.Description);
|
||||
cmpw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant());
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Disk:
|
||||
var disk = datItem as Disk;
|
||||
cmpw.WriteStartElement("disk");
|
||||
cmpw.WriteRequiredAttributeString("name", disk.Name);
|
||||
cmpw.WriteOptionalAttributeString("md5", disk.MD5?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant(), quoteOverride: false);
|
||||
if (disk.ItemStatus != ItemStatus.None)
|
||||
cmpw.WriteOptionalAttributeString("flags", disk.ItemStatus.FromItemStatus(false));
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Media:
|
||||
var media = datItem as Media;
|
||||
cmpw.WriteStartElement("media");
|
||||
cmpw.WriteRequiredAttributeString("name", media.Name);
|
||||
cmpw.WriteOptionalAttributeString("md5", media.MD5?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha1", media.SHA1?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha256", media.SHA256?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("spamsum", media.SpamSum?.ToLowerInvariant());
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Release:
|
||||
var release = datItem as Release;
|
||||
cmpw.WriteStartElement("release");
|
||||
cmpw.WriteRequiredAttributeString("name", release.Name);
|
||||
cmpw.WriteOptionalAttributeString("region", release.Region);
|
||||
cmpw.WriteOptionalAttributeString("language", release.Language);
|
||||
cmpw.WriteOptionalAttributeString("date", release.Date);
|
||||
cmpw.WriteOptionalAttributeString("default", release.Default?.ToString().ToLowerInvariant());
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Rom:
|
||||
var rom = datItem as Rom;
|
||||
cmpw.WriteStartElement("rom");
|
||||
cmpw.WriteRequiredAttributeString("name", rom.Name);
|
||||
cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha256", rom.SHA256?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha384", rom.SHA384?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha512", rom.SHA512?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("spamsum", rom.SpamSum?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("date", rom.Date);
|
||||
if (rom.ItemStatus != ItemStatus.None)
|
||||
cmpw.WriteOptionalAttributeString("flags", rom.ItemStatus.FromItemStatus(false));
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Sample:
|
||||
var sample = datItem as Sample;
|
||||
cmpw.WriteStartElement("sample");
|
||||
cmpw.WriteRequiredAttributeString("name", sample.Name);
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
}
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out DAT footer using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
private static void WriteFooter(ClrMameProWriter cmpw)
|
||||
{
|
||||
// End game
|
||||
cmpw.WriteEndElement();
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.IO;
|
||||
using SabreTools.IO.Readers;
|
||||
using SabreTools.IO.Writers;
|
||||
|
||||
namespace SabreTools.DatFiles.Formats
|
||||
namespace SabreTools.DatFiles.Formats
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents parsing and writing of a ClrMamePro DAT
|
||||
/// Represents a ClrMamePro DAT
|
||||
/// </summary>
|
||||
internal class ClrMamePro : DatFile
|
||||
internal partial class ClrMamePro : DatFile
|
||||
{
|
||||
#region Fields
|
||||
|
||||
@@ -36,662 +24,5 @@ namespace SabreTools.DatFiles.Formats
|
||||
{
|
||||
Quotes = quotes;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
||||
{
|
||||
// Open a file reader
|
||||
Encoding enc = filename.GetEncoding();
|
||||
ClrMameProReader cmpr = new(System.IO.File.OpenRead(filename), enc)
|
||||
{
|
||||
DosCenter = false,
|
||||
Quotes = Quotes,
|
||||
};
|
||||
|
||||
while (!cmpr.EndOfStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
cmpr.ReadNextLine();
|
||||
|
||||
// Ignore everything not top-level
|
||||
if (cmpr.RowType != CmpRowType.TopLevel)
|
||||
continue;
|
||||
|
||||
// Switch on the top-level name
|
||||
switch (cmpr.TopLevel.ToLowerInvariant())
|
||||
{
|
||||
// Header values
|
||||
case "clrmamepro":
|
||||
case "romvault":
|
||||
ReadHeader(cmpr, keep);
|
||||
break;
|
||||
|
||||
// Sets
|
||||
case "set": // Used by the most ancient DATs
|
||||
case "game": // Used by most CMP DATs
|
||||
case "machine": // Possibly used by MAME CMP DATs
|
||||
ReadSet(cmpr, false, statsOnly, filename, indexId);
|
||||
break;
|
||||
case "resource": // Used by some other DATs to denote a BIOS set
|
||||
ReadSet(cmpr, true, statsOnly, filename, indexId);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
string message = $"'{filename}' - There was an error parsing line {cmpr.LineNumber} '{cmpr.CurrentLine}'";
|
||||
logger.Error(ex, message);
|
||||
}
|
||||
}
|
||||
|
||||
cmpr.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read header information
|
||||
/// </summary>
|
||||
/// <param name="cmpr">ClrMameProReader to use to parse the header</param>
|
||||
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
||||
private void ReadHeader(ClrMameProReader cmpr, bool keep)
|
||||
{
|
||||
bool superdat = false;
|
||||
|
||||
// If there's no subtree to the header, skip it
|
||||
if (cmpr == null || cmpr.EndOfStream)
|
||||
return;
|
||||
|
||||
// While we don't hit an end element or end of stream
|
||||
while (!cmpr.EndOfStream)
|
||||
{
|
||||
cmpr.ReadNextLine();
|
||||
|
||||
// Ignore comments, internal items, and nothingness
|
||||
if (cmpr.RowType == CmpRowType.None || cmpr.RowType == CmpRowType.Comment || cmpr.RowType == CmpRowType.Internal)
|
||||
continue;
|
||||
|
||||
// If we reached the end of a section, break
|
||||
if (cmpr.RowType == CmpRowType.EndTopLevel)
|
||||
break;
|
||||
|
||||
// If the standalone value is null, we skip
|
||||
if (cmpr.Standalone == null)
|
||||
continue;
|
||||
|
||||
string itemKey = cmpr.Standalone?.Key.ToLowerInvariant();
|
||||
string itemVal = cmpr.Standalone?.Value;
|
||||
|
||||
// For all other cases
|
||||
switch (itemKey)
|
||||
{
|
||||
case "name":
|
||||
Header.Name ??= itemVal;
|
||||
superdat |= itemVal.Contains(" - SuperDAT");
|
||||
|
||||
if (keep && superdat)
|
||||
Header.Type ??= "SuperDAT";
|
||||
|
||||
break;
|
||||
case "description":
|
||||
Header.Description ??= itemVal;
|
||||
break;
|
||||
case "rootdir":
|
||||
Header.RootDir ??= itemVal;
|
||||
break;
|
||||
case "category":
|
||||
Header.Category ??= itemVal;
|
||||
break;
|
||||
case "version":
|
||||
Header.Version ??= itemVal;
|
||||
break;
|
||||
case "date":
|
||||
Header.Date ??= itemVal;
|
||||
break;
|
||||
case "author":
|
||||
Header.Author ??= itemVal;
|
||||
break;
|
||||
case "email":
|
||||
Header.Email ??= itemVal;
|
||||
break;
|
||||
case "homepage":
|
||||
Header.Homepage ??= itemVal;
|
||||
break;
|
||||
case "url":
|
||||
Header.Url ??= itemVal;
|
||||
break;
|
||||
case "comment":
|
||||
Header.Comment ??= itemVal;
|
||||
break;
|
||||
case "header":
|
||||
Header.HeaderSkipper ??= itemVal;
|
||||
break;
|
||||
case "type":
|
||||
Header.Type ??= itemVal;
|
||||
superdat |= itemVal.Contains("SuperDAT");
|
||||
break;
|
||||
case "forcemerging":
|
||||
if (Header.ForceMerging == MergingFlag.None)
|
||||
Header.ForceMerging = itemVal.AsMergingFlag();
|
||||
|
||||
break;
|
||||
case "forcezipping":
|
||||
if (Header.ForcePacking == PackingFlag.None)
|
||||
Header.ForcePacking = itemVal.AsPackingFlag();
|
||||
|
||||
break;
|
||||
case "forcepacking":
|
||||
if (Header.ForcePacking == PackingFlag.None)
|
||||
Header.ForcePacking = itemVal.AsPackingFlag();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read set information
|
||||
/// </summary>
|
||||
/// <param name="cmpr">ClrMameProReader to use to parse the header</param>
|
||||
/// <param name="resource">True if the item is a resource (bios), false otherwise</param>
|
||||
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||
/// <param name="filename">Name of the file to be parsed</param>
|
||||
/// <param name="indexId">Index ID for the DAT</param>
|
||||
private void ReadSet(
|
||||
ClrMameProReader cmpr,
|
||||
bool resource,
|
||||
bool statsOnly,
|
||||
|
||||
// Standard Dat parsing
|
||||
string filename,
|
||||
int indexId)
|
||||
{
|
||||
// Prepare all internal variables
|
||||
bool containsItems = false;
|
||||
Machine machine = new()
|
||||
{
|
||||
MachineType = (resource ? MachineType.Bios : MachineType.None),
|
||||
};
|
||||
|
||||
// If there's no subtree to the header, skip it
|
||||
if (cmpr == null || cmpr.EndOfStream)
|
||||
return;
|
||||
|
||||
// While we don't hit an end element or end of stream
|
||||
while (!cmpr.EndOfStream)
|
||||
{
|
||||
cmpr.ReadNextLine();
|
||||
|
||||
// Ignore comments and nothingness
|
||||
if (cmpr.RowType == CmpRowType.None || cmpr.RowType == CmpRowType.Comment)
|
||||
continue;
|
||||
|
||||
// If we reached the end of a section, break
|
||||
if (cmpr.RowType == CmpRowType.EndTopLevel)
|
||||
break;
|
||||
|
||||
// Handle any standalone items
|
||||
if (cmpr.RowType == CmpRowType.Standalone && cmpr.Standalone != null)
|
||||
{
|
||||
string itemKey = cmpr.Standalone?.Key.ToLowerInvariant();
|
||||
string itemVal = cmpr.Standalone?.Value;
|
||||
|
||||
switch (itemKey)
|
||||
{
|
||||
case "name":
|
||||
machine.Name = itemVal;
|
||||
break;
|
||||
case "description":
|
||||
machine.Description = itemVal;
|
||||
break;
|
||||
case "year":
|
||||
machine.Year = itemVal;
|
||||
break;
|
||||
case "manufacturer":
|
||||
machine.Manufacturer = itemVal;
|
||||
break;
|
||||
case "category":
|
||||
machine.Category = itemVal;
|
||||
break;
|
||||
case "cloneof":
|
||||
machine.CloneOf = itemVal;
|
||||
break;
|
||||
case "romof":
|
||||
machine.RomOf = itemVal;
|
||||
break;
|
||||
case "sampleof":
|
||||
machine.SampleOf = itemVal;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle any internal items
|
||||
else if (cmpr.RowType == CmpRowType.Internal
|
||||
&& !string.IsNullOrWhiteSpace(cmpr.InternalName)
|
||||
&& cmpr.Internal != null)
|
||||
{
|
||||
containsItems = true;
|
||||
string itemKey = cmpr.InternalName;
|
||||
|
||||
// Create the proper DatItem based on the type
|
||||
ItemType itemType = itemKey.AsItemType();
|
||||
if (itemType == ItemType.NULL)
|
||||
itemType = ItemType.Rom;
|
||||
|
||||
DatItem item = DatItem.Create(itemType);
|
||||
|
||||
// Then populate it with information
|
||||
item.CopyMachineInformation(machine);
|
||||
|
||||
item.Source.Index = indexId;
|
||||
item.Source.Name = filename;
|
||||
|
||||
// Loop through all of the attributes
|
||||
foreach (var kvp in cmpr.Internal)
|
||||
{
|
||||
string attrKey = kvp.Key;
|
||||
string attrVal = kvp.Value;
|
||||
|
||||
switch (attrKey)
|
||||
{
|
||||
//If the item is empty, we automatically skip it because it's a fluke
|
||||
case "":
|
||||
continue;
|
||||
|
||||
// Regular attributes
|
||||
case "name":
|
||||
item.SetName(attrVal);
|
||||
break;
|
||||
|
||||
case "size":
|
||||
if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).Size = Utilities.CleanLong(attrVal);
|
||||
|
||||
break;
|
||||
case "crc":
|
||||
if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).CRC = attrVal;
|
||||
|
||||
break;
|
||||
case "md5":
|
||||
if (item.ItemType == ItemType.Disk)
|
||||
(item as Disk).MD5 = attrVal;
|
||||
else if (item.ItemType == ItemType.Media)
|
||||
(item as Media).MD5 = attrVal;
|
||||
else if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).MD5 = attrVal;
|
||||
|
||||
break;
|
||||
case "sha1":
|
||||
if (item.ItemType == ItemType.Disk)
|
||||
(item as Disk).SHA1 = attrVal;
|
||||
else if (item.ItemType == ItemType.Media)
|
||||
(item as Media).SHA1 = attrVal;
|
||||
else if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).SHA1 = attrVal;
|
||||
|
||||
break;
|
||||
case "sha256":
|
||||
if (item.ItemType == ItemType.Media)
|
||||
(item as Media).SHA256 = attrVal;
|
||||
else if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).SHA256 = attrVal;
|
||||
|
||||
break;
|
||||
case "sha384":
|
||||
if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).SHA384 = attrVal;
|
||||
|
||||
break;
|
||||
case "sha512":
|
||||
if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).SHA512 = attrVal;
|
||||
|
||||
break;
|
||||
case "spamsum":
|
||||
if (item.ItemType == ItemType.Media)
|
||||
(item as Media).SpamSum = attrVal;
|
||||
else if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).SpamSum = attrVal;
|
||||
|
||||
break;
|
||||
case "status":
|
||||
ItemStatus tempFlagStatus = attrVal.AsItemStatus();
|
||||
if (item.ItemType == ItemType.Disk)
|
||||
(item as Disk).ItemStatus = tempFlagStatus;
|
||||
else if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).ItemStatus = tempFlagStatus;
|
||||
|
||||
break;
|
||||
case "date":
|
||||
if (item.ItemType == ItemType.Release)
|
||||
(item as Release).Date = attrVal;
|
||||
else if (item.ItemType == ItemType.Rom)
|
||||
(item as Rom).Date = attrVal;
|
||||
|
||||
break;
|
||||
case "default":
|
||||
if (item.ItemType == ItemType.BiosSet)
|
||||
(item as BiosSet).Default = attrVal.AsYesNo();
|
||||
else if (item.ItemType == ItemType.Release)
|
||||
(item as Release).Default = attrVal.AsYesNo();
|
||||
|
||||
break;
|
||||
case "description":
|
||||
if (item.ItemType == ItemType.BiosSet)
|
||||
(item as BiosSet).Description = attrVal;
|
||||
|
||||
break;
|
||||
case "region":
|
||||
if (item.ItemType == ItemType.Release)
|
||||
(item as Release).Region = attrVal;
|
||||
|
||||
break;
|
||||
case "language":
|
||||
if (item.ItemType == ItemType.Release)
|
||||
(item as Release).Language = attrVal;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Now process and add the rom
|
||||
ParseAddHelper(item, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
// If no items were found for this machine, add a Blank placeholder
|
||||
if (!containsItems)
|
||||
{
|
||||
Blank blank = new()
|
||||
{
|
||||
Source = new Source
|
||||
{
|
||||
Index = indexId,
|
||||
Name = filename,
|
||||
},
|
||||
};
|
||||
|
||||
blank.CopyMachineInformation(machine);
|
||||
|
||||
// Now process and add the rom
|
||||
ParseAddHelper(blank, statsOnly);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override ItemType[] GetSupportedTypes()
|
||||
{
|
||||
return new ItemType[]
|
||||
{
|
||||
ItemType.Archive,
|
||||
ItemType.BiosSet,
|
||||
ItemType.Disk,
|
||||
ItemType.Media,
|
||||
ItemType.Release,
|
||||
ItemType.Rom,
|
||||
ItemType.Sample,
|
||||
};
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.User($"Writing to '{outfile}'...");
|
||||
FileStream fs = System.IO.File.Create(outfile);
|
||||
|
||||
// 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");
|
||||
return false;
|
||||
}
|
||||
|
||||
ClrMameProWriter cmpw = new(fs, new UTF8Encoding(false))
|
||||
{
|
||||
Quotes = Quotes
|
||||
};
|
||||
|
||||
// Write out the header
|
||||
WriteHeader(cmpw);
|
||||
|
||||
// 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)
|
||||
{
|
||||
ConcurrentList<DatItem> datItems = Items.FilteredItems(key);
|
||||
|
||||
// If this machine doesn't contain any writable items, skip
|
||||
if (!ContainsWritable(datItems))
|
||||
continue;
|
||||
|
||||
// Resolve the names in the block
|
||||
datItems = DatItem.ResolveNames(datItems);
|
||||
|
||||
for (int index = 0; index < datItems.Count; index++)
|
||||
{
|
||||
DatItem datItem = datItems[index];
|
||||
|
||||
// If we have a different game and we're not at the start of the list, output the end of last item
|
||||
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
|
||||
WriteEndGame(cmpw, datItem);
|
||||
|
||||
// If we have a new game, output the beginning of the new item
|
||||
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
|
||||
WriteStartGame(cmpw, datItem);
|
||||
|
||||
// Check for a "null" item
|
||||
datItem = ProcessNullifiedItem(datItem);
|
||||
|
||||
// Write out the item if we're not ignoring
|
||||
if (!ShouldIgnore(datItem, ignoreblanks))
|
||||
WriteDatItem(cmpw, datItem);
|
||||
|
||||
// Set the new data to compare against
|
||||
lastgame = datItem.Machine.Name;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the file footer out
|
||||
WriteFooter(cmpw);
|
||||
|
||||
logger.User($"'{outfile}' written!{Environment.NewLine}");
|
||||
cmpw.Dispose();
|
||||
fs.Dispose();
|
||||
}
|
||||
catch (Exception ex) when (!throwOnError)
|
||||
{
|
||||
logger.Error(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out DAT header using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
private void WriteHeader(ClrMameProWriter cmpw)
|
||||
{
|
||||
cmpw.WriteStartElement("clrmamepro");
|
||||
|
||||
cmpw.WriteRequiredStandalone("name", Header.Name);
|
||||
cmpw.WriteRequiredStandalone("description", Header.Description);
|
||||
cmpw.WriteOptionalStandalone("category", Header.Category);
|
||||
cmpw.WriteRequiredStandalone("version", Header.Version);
|
||||
cmpw.WriteOptionalStandalone("date", Header.Date);
|
||||
cmpw.WriteRequiredStandalone("author", Header.Author);
|
||||
cmpw.WriteOptionalStandalone("email", Header.Email);
|
||||
cmpw.WriteOptionalStandalone("homepage", Header.Homepage);
|
||||
cmpw.WriteOptionalStandalone("url", Header.Url);
|
||||
cmpw.WriteOptionalStandalone("comment", Header.Comment);
|
||||
if (Header.ForcePacking != PackingFlag.None)
|
||||
cmpw.WriteOptionalStandalone("forcezipping", Header.ForcePacking.FromPackingFlag(true), false);
|
||||
if (Header.ForceMerging != MergingFlag.None)
|
||||
cmpw.WriteOptionalStandalone("forcemerging", Header.ForceMerging.FromMergingFlag(false), false);
|
||||
|
||||
// End clrmamepro
|
||||
cmpw.WriteEndElement();
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out Game start using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
private void WriteStartGame(ClrMameProWriter cmpw, DatItem datItem)
|
||||
{
|
||||
// No game should start with a path separator
|
||||
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
|
||||
|
||||
// Build the state
|
||||
cmpw.WriteStartElement(datItem.Machine.MachineType == MachineType.Bios ? "resource" : "game");
|
||||
|
||||
cmpw.WriteRequiredStandalone("name", datItem.Machine.Name);
|
||||
cmpw.WriteOptionalStandalone("romof", datItem.Machine.RomOf);
|
||||
cmpw.WriteOptionalStandalone("cloneof", datItem.Machine.CloneOf);
|
||||
cmpw.WriteOptionalStandalone("description", datItem.Machine.Description ?? datItem.Machine.Name);
|
||||
cmpw.WriteOptionalStandalone("year", datItem.Machine.Year);
|
||||
cmpw.WriteOptionalStandalone("manufacturer", datItem.Machine.Manufacturer);
|
||||
cmpw.WriteOptionalStandalone("category", datItem.Machine.Category);
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out Game end using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
private void WriteEndGame(ClrMameProWriter cmpw, DatItem datItem)
|
||||
{
|
||||
// Build the state
|
||||
cmpw.WriteOptionalStandalone("sampleof", datItem.Machine.SampleOf);
|
||||
|
||||
// End game
|
||||
cmpw.WriteEndElement();
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out DatItem using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="datFile">DatFile to write out from</param>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
private void WriteDatItem(ClrMameProWriter cmpw, DatItem datItem)
|
||||
{
|
||||
// Pre-process the item name
|
||||
ProcessItemName(datItem, true);
|
||||
|
||||
// Build the state
|
||||
switch (datItem.ItemType)
|
||||
{
|
||||
case ItemType.Archive:
|
||||
var archive = datItem as Archive;
|
||||
cmpw.WriteStartElement("archive");
|
||||
cmpw.WriteRequiredAttributeString("name", archive.Name);
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.BiosSet:
|
||||
var biosSet = datItem as BiosSet;
|
||||
cmpw.WriteStartElement("biosset");
|
||||
cmpw.WriteRequiredAttributeString("name", biosSet.Name);
|
||||
cmpw.WriteOptionalAttributeString("description", biosSet.Description);
|
||||
cmpw.WriteOptionalAttributeString("default", biosSet.Default?.ToString().ToLowerInvariant());
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Disk:
|
||||
var disk = datItem as Disk;
|
||||
cmpw.WriteStartElement("disk");
|
||||
cmpw.WriteRequiredAttributeString("name", disk.Name);
|
||||
cmpw.WriteOptionalAttributeString("md5", disk.MD5?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha1", disk.SHA1?.ToLowerInvariant(), quoteOverride: false);
|
||||
if (disk.ItemStatus != ItemStatus.None)
|
||||
cmpw.WriteOptionalAttributeString("flags", disk.ItemStatus.FromItemStatus(false));
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Media:
|
||||
var media = datItem as Media;
|
||||
cmpw.WriteStartElement("media");
|
||||
cmpw.WriteRequiredAttributeString("name", media.Name);
|
||||
cmpw.WriteOptionalAttributeString("md5", media.MD5?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha1", media.SHA1?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha256", media.SHA256?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("spamsum", media.SpamSum?.ToLowerInvariant());
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Release:
|
||||
var release = datItem as Release;
|
||||
cmpw.WriteStartElement("release");
|
||||
cmpw.WriteRequiredAttributeString("name", release.Name);
|
||||
cmpw.WriteOptionalAttributeString("region", release.Region);
|
||||
cmpw.WriteOptionalAttributeString("language", release.Language);
|
||||
cmpw.WriteOptionalAttributeString("date", release.Date);
|
||||
cmpw.WriteOptionalAttributeString("default", release.Default?.ToString().ToLowerInvariant());
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Rom:
|
||||
var rom = datItem as Rom;
|
||||
cmpw.WriteStartElement("rom");
|
||||
cmpw.WriteRequiredAttributeString("name", rom.Name);
|
||||
cmpw.WriteOptionalAttributeString("size", rom.Size?.ToString(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("crc", rom.CRC?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("md5", rom.MD5?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha1", rom.SHA1?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha256", rom.SHA256?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha384", rom.SHA384?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("sha512", rom.SHA512?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("spamsum", rom.SpamSum?.ToLowerInvariant(), quoteOverride: false);
|
||||
cmpw.WriteOptionalAttributeString("date", rom.Date);
|
||||
if (rom.ItemStatus != ItemStatus.None)
|
||||
cmpw.WriteOptionalAttributeString("flags", rom.ItemStatus.FromItemStatus(false));
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
|
||||
case ItemType.Sample:
|
||||
var sample = datItem as Sample;
|
||||
cmpw.WriteStartElement("sample");
|
||||
cmpw.WriteRequiredAttributeString("name", sample.Name);
|
||||
cmpw.WriteEndElement();
|
||||
break;
|
||||
}
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write out DAT footer using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="cmpw">ClrMameProWriter to output to</param>
|
||||
private void WriteFooter(ClrMameProWriter cmpw)
|
||||
{
|
||||
// End game
|
||||
cmpw.WriteEndElement();
|
||||
|
||||
cmpw.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
<ProjectReference Include="..\SabreTools.DatItems\SabreTools.DatItems.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.IO\SabreTools.IO.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.Logging\SabreTools.Logging.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.Models\SabreTools.Models.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.Serialization\SabreTools.Serialization.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace SabreTools.DatItems.Formats
|
||||
public long? Clock { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public bool ClockTypeSpecified { get { return Clock != null; } }
|
||||
public bool ClockSpecified { get { return Clock != null; } }
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace SabreTools.Models.ClrMamePro
|
||||
/// <remarks>flags</remarks>
|
||||
public string? Flags { get; set; }
|
||||
|
||||
/// <remarks>clock</remarks>
|
||||
/// <remarks>clock, Numeric?</remarks>
|
||||
public string? Clock { get; set; }
|
||||
|
||||
#region DO NOT USE IN PRODUCTION
|
||||
|
||||
@@ -15,10 +15,10 @@ namespace SabreTools.Models.ClrMamePro
|
||||
/// <remarks>coins, Numeric?</remarks>
|
||||
public string? Coins { get; set; }
|
||||
|
||||
/// <remarks>tilt</remarks>
|
||||
/// <remarks>tilt, Boolean?</remarks>
|
||||
public string? Tilt { get; set; }
|
||||
|
||||
/// <remarks>service</remarks>
|
||||
/// <remarks>service, Boolean?</remarks>
|
||||
public string? Service { get; set; }
|
||||
|
||||
#region DO NOT USE IN PRODUCTION
|
||||
|
||||
@@ -3,8 +3,8 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Readers;
|
||||
using SabreTools.IO.Writers;
|
||||
using SabreTools.Models.ClrMamePro;
|
||||
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
@@ -12,17 +12,20 @@ namespace SabreTools.Serialization
|
||||
/// </summary>
|
||||
public class ClrMamePro
|
||||
{
|
||||
#region Deserialization
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a ClrMamePro metadata file to the defined type
|
||||
/// </summary>
|
||||
/// <param name="path">Path to the file to deserialize</param>
|
||||
/// <param name="quotes">Enable quotes on read and write, false otherwise</param>
|
||||
/// <returns>Deserialized data on success, null on failure</returns>
|
||||
public static MetadataFile? Deserialize(string path)
|
||||
public static MetadataFile? Deserialize(string path, bool quotes)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = PathProcessor.OpenStream(path);
|
||||
return Deserialize(stream);
|
||||
return Deserialize(stream, quotes);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -35,8 +38,9 @@ namespace SabreTools.Serialization
|
||||
/// Deserializes a ClrMamePro metadata file in a stream to the defined type
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream to deserialize</param>
|
||||
/// <param name="quotes">Enable quotes on read and write, false otherwise</param>
|
||||
/// <returns>Deserialized data on success, null on failure</returns>
|
||||
public static MetadataFile? Deserialize(Stream? stream)
|
||||
public static MetadataFile? Deserialize(Stream? stream, bool quotes)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -45,7 +49,7 @@ namespace SabreTools.Serialization
|
||||
return default;
|
||||
|
||||
// Setup the reader and output
|
||||
var reader = new ClrMameProReader(stream, Encoding.UTF8);
|
||||
var reader = new ClrMameProReader(stream, Encoding.UTF8) { Quotes = quotes };
|
||||
var dat = new MetadataFile();
|
||||
|
||||
// Loop through and parse out the values
|
||||
@@ -737,5 +741,59 @@ namespace SabreTools.Serialization
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Serialization
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the defined type to a ClrMamePro metadata file
|
||||
/// </summary>
|
||||
/// <param name="metadataFile">Data to serialize</param>
|
||||
/// <param name="path">Path to the file to serialize to</param>
|
||||
/// <returns>True on successful serialization, false otherwise</returns>
|
||||
public static bool SerializeToFile(MetadataFile? metadataFile, string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = SerializeToStream(metadataFile);
|
||||
if (stream == null)
|
||||
return false;
|
||||
|
||||
using var fs = File.OpenWrite(path);
|
||||
stream.CopyTo(fs);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// TODO: Handle logging the exception
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the defined type to a stream
|
||||
/// </summary>
|
||||
/// <param name="metadataFile">Data to serialize</param>
|
||||
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
||||
public static Stream? SerializeToStream(MetadataFile? metadataFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
// If the metadata file is null
|
||||
if (metadataFile == null)
|
||||
return null;
|
||||
|
||||
// TODO: Implement writing
|
||||
return null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// TODO: Handle logging the exception
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ namespace SabreTools.Serialization
|
||||
/// </summary>
|
||||
public abstract class XmlSerializer<T>
|
||||
{
|
||||
#region Deserialization
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes an XML file to the defined type
|
||||
/// </summary>
|
||||
@@ -61,6 +63,10 @@ namespace SabreTools.Serialization
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Serialization
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the defined type to an XML file
|
||||
/// </summary>
|
||||
@@ -119,5 +125,7 @@ namespace SabreTools.Serialization
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ namespace SabreTools.Test.Parser
|
||||
string filename = System.IO.Path.Combine(Environment.CurrentDirectory, "TestData", path);
|
||||
|
||||
// Deserialize the file
|
||||
var dat = Serialization.ClrMamePro.Deserialize(filename);
|
||||
var dat = Serialization.ClrMamePro.Deserialize(filename, quotes: true);
|
||||
|
||||
// Validate the values
|
||||
if (expectHeader)
|
||||
|
||||
Reference in New Issue
Block a user