mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Cleanup and overhaul (#21)
* Syntax cleanup * More minor cleanup, use Linq * Fix broken features by using correct values * Feature flags the same * Features are modular * No AlphaFS, more .NET versions * Fix appveyor * Put back identifiers, for some reason * String interpolation, modernization * Better use of GetField * XmlTextWriter to remove possible issues * Fix header for OpenMSX
This commit is contained in:
@@ -1,19 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
using SabreTools.Library.Data;
|
||||
using SabreTools.Library.DatItems;
|
||||
using SabreTools.Library.Tools;
|
||||
|
||||
#if MONO
|
||||
using System.IO;
|
||||
#else
|
||||
using Alphaleonis.Win32.Filesystem;
|
||||
|
||||
using FileStream = System.IO.FileStream;
|
||||
using StreamWriter = System.IO.StreamWriter;
|
||||
#endif
|
||||
using NaturalSort;
|
||||
|
||||
namespace SabreTools.Library.DatFiles
|
||||
@@ -68,13 +61,13 @@ namespace SabreTools.Library.DatFiles
|
||||
{
|
||||
try
|
||||
{
|
||||
Globals.Logger.User("Opening file for writing: {0}", outfile);
|
||||
Globals.Logger.User($"Opening file for writing: {outfile}");
|
||||
FileStream fs = Utilities.TryCreate(outfile);
|
||||
|
||||
// If we get back null for some reason, just log and return
|
||||
if (fs == null)
|
||||
{
|
||||
Globals.Logger.Warning("File '{0}' could not be created for writing! Please check to see if the file is writable", outfile);
|
||||
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -112,22 +105,18 @@ namespace SabreTools.Library.DatFiles
|
||||
|
||||
// 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() != rom.MachineName.ToLowerInvariant())
|
||||
{
|
||||
WriteEndGame(sw, rom);
|
||||
}
|
||||
|
||||
// If we have a new game, output the beginning of the new item
|
||||
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
|
||||
{
|
||||
WriteStartGame(sw, rom);
|
||||
}
|
||||
|
||||
// If we have a "null" game (created by DATFromDir or something similar), log it to file
|
||||
if (rom.ItemType == ItemType.Rom
|
||||
&& ((Rom)rom).Size == -1
|
||||
&& ((Rom)rom).CRC == "null")
|
||||
{
|
||||
Globals.Logger.Verbose("Empty folder found: {0}", rom.MachineName);
|
||||
Globals.Logger.Verbose($"Empty folder found: {rom.MachineName}");
|
||||
|
||||
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
|
||||
((Rom)rom).Size = Constants.SizeZero;
|
||||
@@ -151,7 +140,7 @@ namespace SabreTools.Library.DatFiles
|
||||
// Write the file footer out
|
||||
WriteFooter(sw);
|
||||
|
||||
Globals.Logger.Verbose("File written!" + Environment.NewLine);
|
||||
Globals.Logger.Verbose($"File written!{Environment.NewLine}");
|
||||
sw.Dispose();
|
||||
fs.Dispose();
|
||||
}
|
||||
@@ -173,15 +162,15 @@ namespace SabreTools.Library.DatFiles
|
||||
{
|
||||
try
|
||||
{
|
||||
string header = "DOSCenter (\n" +
|
||||
"\tName: " + Name + "\n" +
|
||||
"\tDescription: " + Description + "\n" +
|
||||
"\tVersion: " + Version + "\n" +
|
||||
"\tDate: " + Date + "\n" +
|
||||
"\tAuthor: " + Author + "\n" +
|
||||
"\tHomepage: " + Homepage + "\n" +
|
||||
"\tComment: " + Comment + "\n" +
|
||||
")\n";
|
||||
string header = "DOSCenter (\n";
|
||||
header += $"\tName: {Name}\n";
|
||||
header += $"\tDescription: {Description}\n";
|
||||
header += $"\tVersion: {Version}\n";
|
||||
header += $"\tDate: {Date}\n";
|
||||
header += $"\tAuthor: {Author}\n";
|
||||
header += $"\tHomepage: {Homepage}\n";
|
||||
header += $"\tComment: {Comment}\n";
|
||||
header += ")\n";
|
||||
|
||||
// Write the header out
|
||||
sw.Write(header);
|
||||
@@ -200,19 +189,17 @@ namespace SabreTools.Library.DatFiles
|
||||
/// Write out Game start using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="sw">StreamWriter to output to</param>
|
||||
/// <param name="rom">DatItem object to be output</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
/// <returns>True if the data was written, false on error</returns>
|
||||
private bool WriteStartGame(StreamWriter sw, DatItem rom)
|
||||
private bool WriteStartGame(StreamWriter sw, DatItem datItem)
|
||||
{
|
||||
try
|
||||
{
|
||||
// No game should start with a path separator
|
||||
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
|
||||
{
|
||||
rom.MachineName = rom.MachineName.Substring(1);
|
||||
}
|
||||
datItem.MachineName = datItem.MachineName.TrimStart(Path.DirectorySeparatorChar);
|
||||
|
||||
string state = "game (\n\tname \"" + (!ExcludeFields[(int)Field.MachineName] ? rom.MachineName + ".zip" : "") + "\"\n";
|
||||
// Build the state based on excluded fields
|
||||
string state = $"game (\n\tname \"{datItem.GetField(Field.MachineName, ExcludeFields)}.zip\n";
|
||||
|
||||
sw.Write(state);
|
||||
sw.Flush();
|
||||
@@ -230,13 +217,19 @@ namespace SabreTools.Library.DatFiles
|
||||
/// Write out Game end using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="sw">StreamWriter to output to</param>
|
||||
/// <param name="rom">DatItem object to be output</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
/// <returns>True if the data was written, false on error</returns>
|
||||
private bool WriteEndGame(StreamWriter sw, DatItem rom)
|
||||
private bool WriteEndGame(StreamWriter sw, DatItem datItem)
|
||||
{
|
||||
try
|
||||
{
|
||||
string state = (!ExcludeFields[(int)Field.SampleOf] && String.IsNullOrWhiteSpace(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n") + ")\n";
|
||||
string state = string.Empty;
|
||||
|
||||
// Build the state based on excluded fields
|
||||
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SampleOf, ExcludeFields)))
|
||||
state += $"\tsampleof \"{datItem.SampleOf}\"\n";
|
||||
|
||||
state += ")\n";
|
||||
|
||||
sw.Write(state);
|
||||
sw.Flush();
|
||||
@@ -254,27 +247,24 @@ namespace SabreTools.Library.DatFiles
|
||||
/// Write out DatItem using the supplied StreamWriter
|
||||
/// </summary>
|
||||
/// <param name="sw">StreamWriter to output to</param>
|
||||
/// <param name="rom">DatItem object to be output</param>
|
||||
/// <param name="datItem">DatItem object to be output</param>
|
||||
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
||||
/// <returns>True if the data was written, false on error</returns>
|
||||
private bool WriteDatItem(StreamWriter sw, DatItem rom, bool ignoreblanks = false)
|
||||
private bool WriteDatItem(StreamWriter sw, DatItem datItem, bool ignoreblanks = false)
|
||||
{
|
||||
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
|
||||
if (ignoreblanks
|
||||
&& (rom.ItemType == ItemType.Rom
|
||||
&& (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1)))
|
||||
{
|
||||
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string state = "";
|
||||
string state = string.Empty;
|
||||
|
||||
// Pre-process the item name
|
||||
ProcessItemName(rom, true);
|
||||
ProcessItemName(datItem, true);
|
||||
|
||||
switch (rom.ItemType)
|
||||
// Build the state based on excluded fields
|
||||
switch (datItem.ItemType)
|
||||
{
|
||||
case ItemType.Archive:
|
||||
case ItemType.BiosSet:
|
||||
@@ -284,11 +274,15 @@ namespace SabreTools.Library.DatFiles
|
||||
// We don't output these at all for DosCenter
|
||||
break;
|
||||
case ItemType.Rom:
|
||||
state += "\tfile ( name " + (!ExcludeFields[(int)Field.Name] ? ((Rom)rom).Name : "")
|
||||
+ (!ExcludeFields[(int)Field.Size] && ((Rom)rom).Size != -1 ? " size " + ((Rom)rom).Size : "")
|
||||
+ (!ExcludeFields[(int)Field.Date] && !String.IsNullOrWhiteSpace(((Rom)rom).Date) ? " date " + ((Rom)rom).Date : "")
|
||||
+ (!ExcludeFields[(int)Field.CRC] && !String.IsNullOrWhiteSpace(((Rom)rom).CRC) ? " crc " + ((Rom)rom).CRC.ToLowerInvariant() : "")
|
||||
+ " )\n";
|
||||
var rom = datItem as Rom;
|
||||
state += $"\file ( name \"{datItem.GetField(Field.Name, ExcludeFields)}\"";
|
||||
if (!ExcludeFields[(int)Field.Size] && rom.Size != -1)
|
||||
state += $" size \"{rom.Size}\"";
|
||||
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Date, ExcludeFields)))
|
||||
state += $" date \"{rom.Date}\"";
|
||||
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.CRC, ExcludeFields)))
|
||||
state += $" crc \"{rom.CRC.ToLowerInvariant()}\"";
|
||||
state += " )\n";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user