2020-12-09 21:52:38 -08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using SabreTools.Core;
|
2020-12-10 23:24:09 -08:00
|
|
|
using SabreTools.DatFiles;
|
2020-12-09 21:52:38 -08:00
|
|
|
using SabreTools.DatItems;
|
2021-02-02 10:23:43 -08:00
|
|
|
using SabreTools.DatItems.Formats;
|
2020-12-09 21:52:38 -08:00
|
|
|
using SabreTools.FileTypes;
|
2020-12-10 22:31:23 -08:00
|
|
|
using SabreTools.FileTypes.Archives;
|
2020-12-09 21:52:38 -08:00
|
|
|
using SabreTools.IO;
|
2020-12-10 13:30:08 -08:00
|
|
|
using SabreTools.Logging;
|
2020-12-09 21:52:38 -08:00
|
|
|
|
2020-12-10 23:24:09 -08:00
|
|
|
namespace SabreTools.DatTools
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
2020-12-10 14:11:35 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// This file represents all methods related to populating a DatFile
|
|
|
|
|
/// from a set of files and directories
|
|
|
|
|
/// </summary>
|
2020-12-10 15:42:39 -08:00
|
|
|
public class DatFromDir
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
2020-12-10 13:30:08 -08:00
|
|
|
#region Logging
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logging object
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly Logger logger = new Logger();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new Dat from a directory
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="basePath">Base folder to be used in creating the DAT</param>
|
|
|
|
|
/// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param>
|
|
|
|
|
/// <param name="skipFileType">Type of files that should be skipped</param>
|
|
|
|
|
/// <param name="addBlanks">True if blank items should be created for empty folders, false otherwise</param>
|
|
|
|
|
/// <param name="hashes">Hashes to include in the information</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
public static bool PopulateFromDir(
|
2020-12-10 10:39:39 -08:00
|
|
|
DatFile datFile,
|
2020-12-09 21:52:38 -08:00
|
|
|
string basePath,
|
|
|
|
|
TreatAsFile asFiles = 0x00,
|
|
|
|
|
SkipFileType skipFileType = SkipFileType.None,
|
|
|
|
|
bool addBlanks = false,
|
|
|
|
|
Hash hashes = Hash.Standard)
|
|
|
|
|
{
|
|
|
|
|
// Set the progress variables
|
|
|
|
|
long totalSize = 0;
|
|
|
|
|
long currentSize = 0;
|
|
|
|
|
|
|
|
|
|
// Process the input
|
|
|
|
|
if (Directory.Exists(basePath))
|
|
|
|
|
{
|
|
|
|
|
logger.Verbose($"Folder found: {basePath}");
|
|
|
|
|
|
|
|
|
|
// Get a list of all files to process
|
|
|
|
|
List<string> files = Directory.EnumerateFiles(basePath, "*", SearchOption.AllDirectories).ToList();
|
|
|
|
|
|
|
|
|
|
// Loop through and add the file sizes
|
|
|
|
|
Parallel.ForEach(files, Globals.ParallelOptions, item =>
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Add(ref totalSize, new FileInfo(item).Length);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Process the files in the main folder or any subfolder
|
|
|
|
|
logger.User(totalSize, currentSize);
|
|
|
|
|
foreach (string item in files)
|
|
|
|
|
{
|
2020-12-10 10:39:39 -08:00
|
|
|
CheckFileForHashes(datFile, item, basePath, asFiles, skipFileType, addBlanks, hashes);
|
2020-12-09 21:52:38 -08:00
|
|
|
currentSize += new FileInfo(item).Length;
|
|
|
|
|
logger.User(totalSize, currentSize, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now find all folders that are empty, if we are supposed to
|
|
|
|
|
if (addBlanks)
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessDirectoryBlanks(datFile, basePath);
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
else if (File.Exists(basePath))
|
|
|
|
|
{
|
|
|
|
|
logger.Verbose($"File found: {basePath}");
|
|
|
|
|
|
|
|
|
|
totalSize = new FileInfo(basePath).Length;
|
|
|
|
|
logger.User(totalSize, currentSize);
|
|
|
|
|
|
|
|
|
|
string parentPath = Path.GetDirectoryName(Path.GetDirectoryName(basePath));
|
2020-12-10 10:39:39 -08:00
|
|
|
CheckFileForHashes(datFile, basePath, parentPath, asFiles, skipFileType, addBlanks, hashes);
|
2020-12-09 21:52:38 -08:00
|
|
|
logger.User(totalSize, totalSize, basePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check a given file for hashes, based on current settings
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="item">Filename of the item to be checked</param>
|
|
|
|
|
/// <param name="basePath">Base folder to be used in creating the DAT</param>
|
|
|
|
|
/// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param>
|
|
|
|
|
/// <param name="skipFileType">Type of files that should be skipped</param>
|
|
|
|
|
/// <param name="addBlanks">True if blank items should be created for empty folders, false otherwise</param>
|
|
|
|
|
/// <param name="hashes">Hashes to include in the information</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void CheckFileForHashes(
|
2020-12-10 10:39:39 -08:00
|
|
|
DatFile datFile,
|
|
|
|
|
string item,
|
|
|
|
|
string basePath,
|
|
|
|
|
TreatAsFile asFiles,
|
|
|
|
|
SkipFileType skipFileType,
|
|
|
|
|
bool addBlanks,
|
|
|
|
|
Hash hashes)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
// If we're in depot mode, process it separately
|
2020-12-10 10:39:39 -08:00
|
|
|
if (CheckDepotFile(datFile, item))
|
2020-12-09 21:52:38 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Initialize possible archive variables
|
|
|
|
|
BaseArchive archive = BaseArchive.Create(item);
|
|
|
|
|
|
|
|
|
|
// Process archives according to flags
|
|
|
|
|
if (archive != null)
|
|
|
|
|
{
|
|
|
|
|
// Set the archive flags
|
|
|
|
|
archive.AvailableHashes = hashes;
|
|
|
|
|
|
|
|
|
|
// Skip if we're treating archives as files and skipping files
|
|
|
|
|
if (asFiles.HasFlag(TreatAsFile.Archive) && skipFileType == SkipFileType.File)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip if we're skipping archives
|
|
|
|
|
else if (skipFileType == SkipFileType.Archive)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process as archive if we're not treating archives as files
|
|
|
|
|
else if (!asFiles.HasFlag(TreatAsFile.Archive))
|
|
|
|
|
{
|
|
|
|
|
var extracted = archive.GetChildren();
|
|
|
|
|
|
|
|
|
|
// If we have internal items to process, do so
|
|
|
|
|
if (extracted != null)
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessArchive(datFile, item, basePath, extracted);
|
2020-12-09 21:52:38 -08:00
|
|
|
|
|
|
|
|
// Now find all folders that are empty, if we are supposed to
|
|
|
|
|
if (addBlanks)
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessArchiveBlanks(datFile, item, basePath, archive);
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process as file if we're treating archives as files
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessFile(datFile, item, basePath, hashes, asFiles);
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process non-archives according to flags
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Skip if we're skipping files
|
|
|
|
|
if (skipFileType == SkipFileType.File)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Process as file
|
|
|
|
|
else
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessFile(datFile, item, basePath, hashes, asFiles);
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check an item as if it's supposed to be in a depot
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="item">Filename of the item to be checked</param>
|
|
|
|
|
/// <returns>True if we checked a depot file, false otherwise</returns>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static bool CheckDepotFile(DatFile datFile, string item)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
// If we're not in Depot mode, return false
|
2020-12-10 10:39:39 -08:00
|
|
|
if (datFile.Header.OutputDepot?.IsActive != true)
|
2020-12-09 21:52:38 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Check the file as if it were in a depot
|
|
|
|
|
GZipArchive gzarc = new GZipArchive(item);
|
|
|
|
|
BaseFile baseFile = gzarc.GetTorrentGZFileInfo();
|
|
|
|
|
|
|
|
|
|
// If the rom is valid, add it
|
|
|
|
|
if (baseFile != null && baseFile.Filename != null)
|
|
|
|
|
{
|
|
|
|
|
// Add the list if it doesn't exist already
|
|
|
|
|
Rom rom = new Rom(baseFile);
|
2020-12-14 15:43:01 -08:00
|
|
|
datFile.Items.Add(rom.GetKey(ItemKey.CRC), rom);
|
2020-12-09 21:52:38 -08:00
|
|
|
logger.Verbose($"File added: {Path.GetFileNameWithoutExtension(item)}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Verbose($"File not added: {Path.GetFileNameWithoutExtension(item)}");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process a single file as an archive
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="item">File to be added</param>
|
|
|
|
|
/// <param name="basePath">Path the represents the parent directory</param>
|
|
|
|
|
/// <param name="extracted">List of BaseFiles representing the internal files</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void ProcessArchive(DatFile datFile, string item, string basePath, List<BaseFile> extracted)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
// Get the parent path for all items
|
|
|
|
|
string parent = (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath.Length) + Path.GetFileNameWithoutExtension(item);
|
|
|
|
|
|
|
|
|
|
// First take care of the found items
|
|
|
|
|
Parallel.ForEach(extracted, Globals.ParallelOptions, baseFile =>
|
|
|
|
|
{
|
|
|
|
|
DatItem datItem = DatItem.Create(baseFile);
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessFileHelper(datFile, item, datItem, basePath, parent);
|
2020-12-09 21:52:38 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process blank folders in an archive
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="item">File containing the blanks</param>
|
|
|
|
|
/// <param name="basePath">Path the represents the parent directory</param>
|
|
|
|
|
/// <param name="archive">BaseArchive to get blanks from</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void ProcessArchiveBlanks(DatFile datFile, string item, string basePath, BaseArchive archive)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
List<string> empties = new List<string>();
|
|
|
|
|
|
|
|
|
|
// Get the parent path for all items
|
|
|
|
|
string parent = (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath.Length) + Path.GetFileNameWithoutExtension(item);
|
|
|
|
|
|
|
|
|
|
// Now get all blank folders from the archive
|
|
|
|
|
if (archive != null)
|
|
|
|
|
empties = archive.GetEmptyFolders();
|
|
|
|
|
|
|
|
|
|
// Add add all of the found empties to the DAT
|
|
|
|
|
Parallel.ForEach(empties, Globals.ParallelOptions, empty =>
|
|
|
|
|
{
|
|
|
|
|
Rom emptyRom = new Rom(Path.Combine(empty, "_"), item);
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessFileHelper(datFile, item, emptyRom, basePath, parent);
|
2020-12-09 21:52:38 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process blank folders in a directory
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="basePath">Path the represents the parent directory</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void ProcessDirectoryBlanks(DatFile datFile, string basePath)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
// If we're in depot mode, we don't process blanks
|
2020-12-10 10:39:39 -08:00
|
|
|
if (datFile.Header.OutputDepot?.IsActive == true)
|
2020-12-09 21:52:38 -08:00
|
|
|
return;
|
|
|
|
|
|
2020-12-10 22:16:53 -08:00
|
|
|
List<string> empties = basePath.ListEmpty();
|
2020-12-09 21:52:38 -08:00
|
|
|
Parallel.ForEach(empties, Globals.ParallelOptions, dir =>
|
|
|
|
|
{
|
|
|
|
|
// Get the full path for the directory
|
|
|
|
|
string fulldir = Path.GetFullPath(dir);
|
|
|
|
|
|
|
|
|
|
// Set the temporary variables
|
|
|
|
|
string gamename = string.Empty;
|
|
|
|
|
string romname = string.Empty;
|
|
|
|
|
|
|
|
|
|
// If we have a SuperDAT, we want anything that's not the base path as the game, and the file as the rom
|
2020-12-10 10:39:39 -08:00
|
|
|
if (datFile.Header.Type == "SuperDAT")
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
gamename = fulldir.Remove(0, basePath.Length + 1);
|
|
|
|
|
romname = "_";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, we want just the top level folder as the game, and the file as everything else
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gamename = fulldir.Remove(0, basePath.Length + 1).Split(Path.DirectorySeparatorChar)[0];
|
|
|
|
|
romname = Path.Combine(fulldir.Remove(0, basePath.Length + 1 + gamename.Length), "_");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sanitize the names
|
|
|
|
|
gamename = gamename.Trim(Path.DirectorySeparatorChar);
|
|
|
|
|
romname = romname.Trim(Path.DirectorySeparatorChar);
|
|
|
|
|
|
|
|
|
|
logger.Verbose($"Adding blank empty folder: {gamename}");
|
2020-12-10 10:39:39 -08:00
|
|
|
datFile.Items["null"].Add(new Rom(romname, gamename));
|
2020-12-09 21:52:38 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process a single file as a file
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="item">File to be added</param>
|
|
|
|
|
/// <param name="basePath">Path the represents the parent directory</param>
|
|
|
|
|
/// <param name="hashes">Hashes to include in the information</param>
|
|
|
|
|
/// <param name="asFiles">TreatAsFiles representing CHD and Archive scanning</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void ProcessFile(DatFile datFile, string item, string basePath, Hash hashes, TreatAsFile asFiles)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
logger.Verbose($"'{Path.GetFileName(item)}' treated like a file");
|
2020-12-10 10:39:39 -08:00
|
|
|
BaseFile baseFile = BaseFile.GetInfo(item, header: datFile.Header.HeaderSkipper, hashes: hashes, asFiles: asFiles);
|
2020-12-09 21:52:38 -08:00
|
|
|
DatItem datItem = DatItem.Create(baseFile);
|
2020-12-10 10:39:39 -08:00
|
|
|
ProcessFileHelper(datFile, item, datItem, basePath, string.Empty);
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process a single file as a file (with found Rom data)
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="item">File to be added</param>
|
|
|
|
|
/// <param name="item">Rom data to be used to write to file</param>
|
|
|
|
|
/// <param name="basepath">Path the represents the parent directory</param>
|
|
|
|
|
/// <param name="parent">Parent game to be used</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void ProcessFileHelper(DatFile datFile, string item, DatItem datItem, string basepath, string parent)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
// If we didn't get an accepted parsed type somehow, cancel out
|
|
|
|
|
List<ItemType> parsed = new List<ItemType> { ItemType.Disk, ItemType.Media, ItemType.Rom };
|
|
|
|
|
if (!parsed.Contains(datItem.ItemType))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// If the basepath doesn't end with a directory separator, add it
|
|
|
|
|
if (!basepath.EndsWith(Path.DirectorySeparatorChar.ToString()))
|
|
|
|
|
basepath += Path.DirectorySeparatorChar.ToString();
|
|
|
|
|
|
|
|
|
|
// Make sure we have the full item path
|
|
|
|
|
item = Path.GetFullPath(item);
|
|
|
|
|
|
|
|
|
|
// Process the item to sanitize names based on input
|
2020-12-10 10:39:39 -08:00
|
|
|
SetDatItemInfo(datFile, datItem, item, parent, basepath);
|
2020-12-09 21:52:38 -08:00
|
|
|
|
|
|
|
|
// Add the file information to the DAT
|
2020-12-14 15:31:28 -08:00
|
|
|
string key = datItem.GetKey(ItemKey.CRC);
|
2020-12-10 10:39:39 -08:00
|
|
|
datFile.Items.Add(key, datItem);
|
2020-12-09 21:52:38 -08:00
|
|
|
|
|
|
|
|
logger.Verbose($"File added: {datItem.GetName() ?? string.Empty}");
|
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
logger.Error(ex);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set proper Game and Rom names from user inputs
|
|
|
|
|
/// </summary>
|
2020-12-10 10:39:39 -08:00
|
|
|
/// <param name="datFile">Current DatFile object to add to</param>
|
2020-12-09 21:52:38 -08:00
|
|
|
/// <param name="datItem">DatItem representing the input file</param>
|
|
|
|
|
/// <param name="item">Item name to use</param>
|
|
|
|
|
/// <param name="parent">Parent name to use</param>
|
|
|
|
|
/// <param name="basepath">Base path to use</param>
|
2020-12-10 11:58:46 -08:00
|
|
|
private static void SetDatItemInfo(DatFile datFile, DatItem datItem, string item, string parent, string basepath)
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
// Get the data to be added as game and item names
|
|
|
|
|
string machineName, itemName;
|
|
|
|
|
|
|
|
|
|
// If the parent is blank, then we have a non-archive file
|
|
|
|
|
if (string.IsNullOrWhiteSpace(parent))
|
|
|
|
|
{
|
|
|
|
|
// If we have a SuperDAT, we want anything that's not the base path as the game, and the file as the rom
|
2020-12-10 10:39:39 -08:00
|
|
|
if (datFile.Header.Type == "SuperDAT")
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
machineName = Path.GetDirectoryName(item.Remove(0, basepath.Length));
|
|
|
|
|
itemName = Path.GetFileName(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, we want just the top level folder as the game, and the file as everything else
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
machineName = item.Remove(0, basepath.Length).Split(Path.DirectorySeparatorChar)[0];
|
|
|
|
|
itemName = item.Remove(0, (Path.Combine(basepath, machineName).Length));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, we assume that we have an archive
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If we have a SuperDAT, we want the archive name as the game, and the file as everything else (?)
|
2020-12-10 10:39:39 -08:00
|
|
|
if (datFile.Header.Type == "SuperDAT")
|
2020-12-09 21:52:38 -08:00
|
|
|
{
|
|
|
|
|
machineName = parent;
|
|
|
|
|
itemName = datItem.GetName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, we want the archive name as the game, and the file as everything else
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
machineName = parent;
|
|
|
|
|
itemName = datItem.GetName();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sanitize the names
|
|
|
|
|
machineName = machineName.Trim(Path.DirectorySeparatorChar);
|
|
|
|
|
itemName = itemName?.Trim(Path.DirectorySeparatorChar) ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(machineName) && string.IsNullOrWhiteSpace(itemName))
|
|
|
|
|
{
|
|
|
|
|
itemName = machineName;
|
|
|
|
|
machineName = "Default";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update machine information
|
|
|
|
|
datItem.Machine.Name = machineName;
|
|
|
|
|
datItem.Machine.Description = machineName;
|
|
|
|
|
|
|
|
|
|
// If we have a Disk, then the ".chd" extension needs to be removed
|
|
|
|
|
if (datItem.ItemType == ItemType.Disk && itemName.EndsWith(".chd"))
|
|
|
|
|
{
|
2020-12-14 16:01:28 -08:00
|
|
|
itemName = itemName[0..^4];
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have a Media, then the extension needs to be removed
|
|
|
|
|
else if (datItem.ItemType == ItemType.Media)
|
|
|
|
|
{
|
|
|
|
|
if (itemName.EndsWith(".dicf"))
|
2020-12-14 16:01:28 -08:00
|
|
|
itemName = itemName[0..^5];
|
2020-12-09 21:52:38 -08:00
|
|
|
else if (itemName.EndsWith(".aaru"))
|
2020-12-14 16:01:28 -08:00
|
|
|
itemName = itemName[0..^5];
|
2020-12-09 21:52:38 -08:00
|
|
|
else if (itemName.EndsWith(".aaruformat"))
|
2020-12-14 16:01:28 -08:00
|
|
|
itemName = itemName[0..^11];
|
2020-12-09 21:52:38 -08:00
|
|
|
else if (itemName.EndsWith(".aaruf"))
|
2020-12-14 16:01:28 -08:00
|
|
|
itemName = itemName[0..^6];
|
2020-12-09 21:52:38 -08:00
|
|
|
else if (itemName.EndsWith(".aif"))
|
2020-12-14 16:01:28 -08:00
|
|
|
itemName = itemName[0..^4];
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the item name back
|
2020-12-14 10:58:43 -08:00
|
|
|
datItem.SetName(itemName);
|
2020-12-09 21:52:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|