Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
drpetersonfernandes committed Feb 16, 2024
1 parent 92fbded commit 7f5b841
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 116 deletions.
28 changes: 25 additions & 3 deletions MAMEUtility/CopyImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public static async Task CopyImagesFromXmlAsync(string[] xmlFilePaths, string so
private static async Task ProcessXmlFileAsync(string xmlFilePath, string sourceDirectory, string destinationDirectory, IProgress<int> progress)
{
XDocument xmlDoc = XDocument.Load(xmlFilePath);

// Validate the XML document structure
if (!ValidateXmlStructure(xmlDoc))
{
Console.WriteLine($"The file {Path.GetFileName(xmlFilePath)} does not match the required XML structure. Operation cancelled.");
return; // Stop processing this XML file
}

var machineNames = xmlDoc.Descendants("Machine")
.Select(machine => machine.Element("MachineName")?.Value)
.Where(name => !string.IsNullOrEmpty(name))
Expand All @@ -39,16 +47,18 @@ private static async Task ProcessXmlFileAsync(string xmlFilePath, string sourceD

foreach (var machineName in machineNames)
{
await CopyImageFileAsync(sourceDirectory, destinationDirectory, machineName, "png");
await CopyImageFileAsync(sourceDirectory, destinationDirectory, machineName, "jpg");
await CopyImageFileAsync(sourceDirectory, destinationDirectory, machineName, "jpeg");
// Here, 'machineName' is enforced to be non-null by the previous checks, so the null-forgiving operator '!' is used.
await CopyImageFileAsync(sourceDirectory, destinationDirectory, machineName!, "png");
await CopyImageFileAsync(sourceDirectory, destinationDirectory, machineName!, "jpg");
await CopyImageFileAsync(sourceDirectory, destinationDirectory, machineName!, "jpeg");

imagesCopied++;
double progressPercentage = (double)imagesCopied / totalImages * 100;
progress.Report((int)progressPercentage);
}
}


private static Task CopyImageFileAsync(string sourceDirectory, string destinationDirectory, string? machineName, string extension)
{
if (machineName == null)
Expand All @@ -74,5 +84,17 @@ private static Task CopyImageFileAsync(string sourceDirectory, string destinatio
});
}

private static bool ValidateXmlStructure(XDocument xmlDoc)
{
// Check if the root element is "Machines" and if it contains at least one "Machine" element
// with both "MachineName" and "Description" child elements.
var isValid = xmlDoc.Root?.Name.LocalName == "Machines" &&
xmlDoc.Descendants("Machine").Any(machine =>
machine.Element("MachineName") != null &&
machine.Element("Description") != null);

return isValid;
}

}
}
67 changes: 37 additions & 30 deletions MAMEUtility/CopyRoms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace MAMEUtility
{
public static class CopyRoms
{
public static async Task CopyRomsFromXmlAsync(string[] xmlFilePaths, string sourceDirectory, IProgress<int> progress)
public static async Task CopyRomsFromXmlAsync(string[] xmlFilePaths, string sourceDirectory, string destinationDirectory, IProgress<int> progress)
{
int totalFiles = xmlFilePaths.Length;
int filesProcessed = 0;
Expand All @@ -14,7 +14,7 @@ public static async Task CopyRomsFromXmlAsync(string[] xmlFilePaths, string sour
{
try
{
await ProcessXmlFileAsync(xmlFilePath, sourceDirectory, progress);
await ProcessXmlFileAsync(xmlFilePath, sourceDirectory, destinationDirectory, progress);
filesProcessed++;
double progressPercentage = (double)filesProcessed / totalFiles * 100;
progress.Report((int)progressPercentage);
Expand All @@ -26,10 +26,17 @@ public static async Task CopyRomsFromXmlAsync(string[] xmlFilePaths, string sour
}
}

private static async Task ProcessXmlFileAsync(string xmlFilePath, string sourceDirectory, IProgress<int> progress)
private static async Task ProcessXmlFileAsync(string xmlFilePath, string sourceDirectory, string destinationDirectory, IProgress<int> progress)
{
XDocument xmlDoc = XDocument.Load(xmlFilePath);

// Validate the XML document structure
if (!ValidateXmlStructure(xmlDoc))
{
Console.WriteLine($"The file {Path.GetFileName(xmlFilePath)} does not match the required XML structure. Operation cancelled.");
return; // Stop processing this XML file
}

var machineNames = xmlDoc.Descendants("Machine")
.Select(machine => machine.Element("MachineName")?.Value)
.Where(name => !string.IsNullOrEmpty(name))
Expand All @@ -40,47 +47,35 @@ private static async Task ProcessXmlFileAsync(string xmlFilePath, string sourceD

foreach (var machineName in machineNames)
{
if (machineName != null)
{
await CopyRomAsync(sourceDirectory, machineName);
await CopyRomAsync(sourceDirectory, destinationDirectory, machineName!);

romsProcessed++;
double progressPercentage = (double)romsProcessed / totalRoms * 100;
progress.Report((int)progressPercentage);
}
else
{
Console.WriteLine("Invalid machine name: null");
}
romsProcessed++;
double progressPercentage = (double)romsProcessed / totalRoms * 100;
progress.Report((int)progressPercentage);
}
}

private static Task CopyRomAsync(string sourceDirectory, string machineName)
private static Task CopyRomAsync(string sourceDirectory, string destinationDirectory, string machineName)
{
return Task.Run(() =>
{
Console.WriteLine($"Attempting to copy ROM for machine: {machineName}");
try
{
if (machineName != null)
{
string sourceFile = Path.Combine(sourceDirectory, machineName + ".zip");
string sourceFile = Path.Combine(sourceDirectory, machineName + ".zip");
string destinationFile = Path.Combine(destinationDirectory, machineName + ".zip");

if (File.Exists(sourceFile))
{
string destinationDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Default destination directory
string destinationFile = Path.Combine(destinationDirectory, machineName + ".zip");
Console.WriteLine($"Source file path: {sourceFile}");
Console.WriteLine($"Destination file path: {destinationFile}");

File.Copy(sourceFile, destinationFile, overwrite: true);
Console.WriteLine($"Copied: {machineName}.zip to {destinationDirectory}");
}
else
{
Console.WriteLine($"File not found: {machineName}.zip");
}
if (File.Exists(sourceFile))
{
File.Copy(sourceFile, destinationFile, overwrite: true);
Console.WriteLine($"Successfully copied: {machineName}.zip to {destinationDirectory}");
}
else
{
Console.WriteLine($"Invalid machine name: {machineName}");
Console.WriteLine($"File not found: {sourceFile}");
}
}
catch (Exception ex)
Expand All @@ -90,5 +85,17 @@ private static Task CopyRomAsync(string sourceDirectory, string machineName)
});
}

private static bool ValidateXmlStructure(XDocument xmlDoc)
{
// Check if the root element is "Machines" and if it contains at least one "Machine" element
// with both "MachineName" and "Description" child elements.
var isValid = xmlDoc.Root?.Name.LocalName == "Machines" &&
xmlDoc.Descendants("Machine").Any(machine =>
machine.Element("MachineName") != null &&
machine.Element("Description") != null);

return isValid;
}

}
}
6 changes: 2 additions & 4 deletions MAMEUtility/MAMEFull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class MAMEFull
{
public static async Task CreateAndSaveMAMEFullAsync(XDocument inputDoc, string outputFilePathMAMEFull, BackgroundWorker worker)
{
Console.WriteLine($"Output folder for MAME Full: {outputFilePathMAMEFull}");

await Task.Run(() =>
{
int totalMachines = inputDoc.Descendants("machine").Count();
Expand All @@ -25,11 +27,7 @@ await Task.Run(() =>
Console.WriteLine($"Processing machine: {machineName}");

machinesProcessed++;

// Calculate progress percentage
progressPercentage = (int)((double)machinesProcessed / totalMachines * 100);

// Report progress
worker.ReportProgress(progressPercentage);

Console.WriteLine($"Progress: {machinesProcessed}/{totalMachines}");
Expand Down
20 changes: 6 additions & 14 deletions MAMEUtility/MAMEManufacturer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public static async Task CreateAndSaveMAMEManufacturerAsync(XDocument inputDoc,
.Trim())
.Replace("&amp;", "&"); // Replace &amp; with & in the filename.

if (safeManufacturerName.Contains("bootleg", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine($"Skipping {manufacturer} as it contains 'bootleg'.");
continue;
}

string outputFilePath = System.IO.Path.Combine(outputFolderMAMEManufacturer, $"{safeManufacturerName}.xml");
Console.WriteLine($"Attempting to create file for: {safeManufacturerName}.xml");

Expand Down Expand Up @@ -84,14 +78,14 @@ private static XDocument CreateFilteredDocument(XDocument inputDoc, string manuf
{
bool predicate(XElement machine) =>
(machine.Element("manufacturer")?.Value ?? "") == manufacturer &&
!(machine.Attribute("name")?.Value.Contains("bootleg", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
!(machine.Element("description")?.Value.Contains("bootleg", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
!(machine.Element("description")?.Value.Contains("prototype", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
!(machine.Element("description")?.Value.Contains("playchoice", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
//!(machine.Attribute("name")?.Value.Contains("bootleg", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
//!(machine.Element("description")?.Value.Contains("bootleg", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
//!(machine.Element("description")?.Value.Contains("prototype", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
//!(machine.Element("description")?.Value.Contains("playchoice", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
//machine.Attribute("cloneof") == null &&
!(machine.Attribute("name")?.Value.Contains("bios", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
!(machine.Element("description")?.Value.Contains("bios", StringComparison.InvariantCultureIgnoreCase) ?? false) &&
(machine.Element("driver")?.Attribute("emulation")?.Value ?? "") == "good" &&
machine.Attribute("cloneof") == null;
(machine.Element("driver")?.Attribute("emulation")?.Value ?? "") == "good";

// Retrieve the matched machines
var matchedMachines = inputDoc.Descendants("machine").Where(predicate).ToList();
Expand All @@ -103,11 +97,9 @@ from machine in matchedMachines
select new XElement("Machine",
new XElement("MachineName", RemoveExtraWhitespace(machine.Attribute("name")?.Value ?? "").Replace("&amp;", "&")),
new XElement("Description", RemoveExtraWhitespace(machine.Element("description")?.Value ?? "").Replace("&amp;", "&"))
// Apply RemoveExtraWhitespace to any other elements as needed
)
)
);

return filteredDoc;
}

Expand Down
5 changes: 2 additions & 3 deletions MAMEUtility/MAMEYear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public partial class MAMEYear
{
public static async Task CreateAndSaveMAMEYear(XDocument inputDoc, string outputFolderMAMEYear, IProgress<int> progress)
{
Console.WriteLine($"Output folder for MAME Year: {outputFolderMAMEYear}");

await Task.Run(() =>
{
Console.WriteLine($"Output folder for MAME Year: {outputFolderMAMEYear}");

try
{
// Extract unique years
Expand Down Expand Up @@ -55,7 +55,6 @@ from machine in machinesForYear
progress.Report((int)progressPercentage);
}
}

}
catch (Exception ex)
{
Expand Down
18 changes: 13 additions & 5 deletions MAMEUtility/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<!-- Add a row for the progress bar -->
</Grid.RowDefinitions>

<Menu Grid.Row="0">
Expand Down Expand Up @@ -81,13 +80,22 @@
Content="Copy Images" />
</StackPanel>

<!-- Progress bar at the bottom of the window -->
<ProgressBar Grid.Row="2" Name="ProgressBar" Height="25" Margin="10,0,10,10" Value="{Binding Path=OverallProgress, RelativeSource={RelativeSource AncestorType=Window}}">
<!-- Progress bar -->
<ProgressBar
Name="ProgressBar"
Grid.Row="2"
Height="25"
Margin="10,0,10,10"
Value="{Binding Path=OverallProgress, RelativeSource={RelativeSource AncestorType=Window}}">
<ProgressBar.Template>
<ControlTemplate TargetType="ProgressBar">
<Grid>
<ProgressBar Value="{TemplateBinding Value}" Background="{TemplateBinding Background}" />
<TextBlock Text="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}, StringFormat={}{0}%}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" />
<ProgressBar Background="{TemplateBinding Background}" Value="{TemplateBinding Value}" />
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Black"
Text="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}, StringFormat={}{0}%}" />
</Grid>
</ControlTemplate>
</ProgressBar.Template>
Expand Down
Loading

0 comments on commit 7f5b841

Please sign in to comment.