Skip to content

Commit

Permalink
Add Image Deduplicator option for #2
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrider3774 committed Sep 4, 2021
1 parent fc51487 commit 071eceb
Show file tree
Hide file tree
Showing 46 changed files with 135 additions and 26 deletions.
51 changes: 51 additions & 0 deletions FileDeDuplicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HtmlExporterPlugin
{
class FileDeDuplicator
{
private Dictionary<string, string> DuplicateDictionary = new Dictionary<string, string>();

private static string CalculateMD5(string filename)
{
using (var md5 = MD5.Create())
{
using (var stream = new BufferedStream(File.OpenRead(filename), 1048576))
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}

public void clear()
{
DuplicateDictionary.Clear();
}

public string GetUniqueFile(string fullfilename, string storedfilename, bool DoCheck)
{
if (!DoCheck || !File.Exists(fullfilename))
{
return storedfilename;
}

string md5 = CalculateMD5(fullfilename);
if (DuplicateDictionary.ContainsKey(md5))
{
return DuplicateDictionary[md5];
}
else
{
DuplicateDictionary[md5] = storedfilename;
return storedfilename;
}
}
}
}
22 changes: 15 additions & 7 deletions HtmlExporterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading;
using System.Web;
using System.Windows.Controls;
using System.Security.Cryptography;
//using ZetaProducerHtmlCompressor.Internal;

namespace HtmlExporterPlugin
Expand Down Expand Up @@ -121,6 +122,7 @@ public void DoExportToHtml()
Dictionary<string, string> GameMediaHashtable = new Dictionary<string, string>();
Dictionary<string, bool> GameMediaCopyDoneDict = new Dictionary<string, bool>();
Dictionary<string, string> FirstGroupFieldFileNames = new Dictionary<string, string>();
FileDeDuplicator DeDuplicator = new FileDeDuplicator();
int pagecount = Settings.Pages.Count;
int PageNr = 0;
int Errors = 0;
Expand Down Expand Up @@ -1134,6 +1136,7 @@ public void DoExportToHtml()
gameicon = realgame.Platform.Icon.Replace("\\", "/");
}
}
gameicon = DeDuplicator.GetUniqueFile(PlayniteApi.Database.GetFullFilePath(gameicon), gameicon, Settings.ConvertImageOptions.DetectDuplicates);
}
GameMediaHashtable[Constants.MediaIconText + realgame.Id.ToString()] = gameicon;
}
Expand Down Expand Up @@ -1169,6 +1172,8 @@ public void DoExportToHtml()
coverimage = realgame.Platform.Cover.Replace("\\", "/");
}
}

coverimage = DeDuplicator.GetUniqueFile(PlayniteApi.Database.GetFullFilePath(coverimage), coverimage, Settings.ConvertImageOptions.DetectDuplicates);
}
GameMediaHashtable[Constants.MediaCoverText + realgame.Id.ToString()] = coverimage;
}
Expand All @@ -1190,17 +1195,20 @@ public void DoExportToHtml()
{
backgroundimage = filespathbackground;
}
else
if ((filespathbackground != String.Empty) && File.Exists(PlayniteApi.Database.GetFullFilePath(filespathbackground)))
{
backgroundimage = realgame.BackgroundImage.Replace("\\", "/");
}
else
{
if (!String.IsNullOrEmpty(realgame.Platform?.Background))
if ((filespathbackground != String.Empty) && File.Exists(PlayniteApi.Database.GetFullFilePath(filespathbackground)))
{
backgroundimage = realgame.Platform.Background.Replace("\\", "/");
backgroundimage = realgame.BackgroundImage.Replace("\\", "/");
}
else
{
if (!String.IsNullOrEmpty(realgame.Platform?.Background))
{
backgroundimage = realgame.Platform.Background.Replace("\\", "/");
}
}
backgroundimage = DeDuplicator.GetUniqueFile(PlayniteApi.Database.GetFullFilePath(backgroundimage), backgroundimage, Settings.ConvertImageOptions.DetectDuplicates);
}
GameMediaHashtable[Constants.MediaBackgroundText + realgame.Id.ToString()] = backgroundimage;
}
Expand Down
1 change: 1 addition & 0 deletions HtmlExporterPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileDeDuplicator.cs" />
<Compile Include="OriginalImageDataFile.cs" />
<Compile Include="OriginalGameImageData.cs" />
<Compile Include="ImageProcessRunner.cs" />
Expand Down
2 changes: 1 addition & 1 deletion HtmlExporterPluginSettingsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private void ButImageOptions_Click(object sender, RoutedEventArgs e)
});


window.Height = 450;
window.Height = 475;
window.Width = 800;
window.Title = Constants.ImageOptionsText;

Expand Down
3 changes: 2 additions & 1 deletion ImageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public class ImageOptions

public bool AlwaysProcess { get; set; } = false;

public bool DetectDuplicates { get; set; } = false;

public string GetUniqueString(bool includeMaxTasks = false, bool includeAlwaysProcess = false)
{
return ConvertToJpg.ToString() + '_' + ConvertToPng.ToString() + '_' + JpgQuality.ToString() + '_' +
ResizeCoverImage.ToString() + '_' + CoverImageWidth + '_' + CoverImageHeight + '_' +
ResizeBackgroundImage.ToString() + '_' + BackgroundImageWidth + '_' + BackgroundImageHeight + '_' +
ResizeIconImage.ToString() + '_' + IconImageWidth + '_' + IconImageHeight + '_' +
ForceConversion.ToString() + (includeMaxTasks ? '_' + MaxTasks.ToString() : String.Empty) +
ForceConversion.ToString() + '_' + DetectDuplicates.ToString() + (includeMaxTasks ? '_' + MaxTasks.ToString() : String.Empty) +
(includeAlwaysProcess ? '_' + AlwaysProcess.ToString() : String.Empty);
}

Expand Down
3 changes: 3 additions & 0 deletions ImageOptionsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<StackPanel Orientation="Horizontal" Margin="0,0,5,5">
<CheckBox x:Name="ChkAwalysProcess" IsChecked="{Binding AlwaysProcess}" Content="{DynamicResource LOC_HTMLEXPORTER_AlwaysProcess}" VerticalAlignment="Center" Margin="0,0,5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,5,5">
<CheckBox x:Name="ChkDetectDuplicates" IsChecked="{Binding DetectDuplicates}" Content="{DynamicResource LOC_HTMLEXPORTER_DetectDuplicates}" VerticalAlignment="Center" Margin="0,0,5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,5,5">
<CheckBox x:Name="ChkResizeCoverImage" IsChecked="{Binding ResizeCoverImage}" Content="{DynamicResource LOC_HTMLEXPORTER_ResizeCoverImage}" VerticalAlignment="Center" Margin="0,0,5,0"/>
<TextBox Height="23" PreviewTextInput="NumberValidationTextBox" Text="{Binding CoverImageWidth}" x:Name="tbCoverImageWidth" HorizontalContentAlignment="Stretch" Width="50">
Expand Down
39 changes: 23 additions & 16 deletions ImageProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ImageProcessDef(ImageProcessDef Source)
public int FailedConverts = 0;
public List<string> FailedFiles = new List<string>();

private readonly List<ImageProcessDef> ImagesProcessDefList = new List<ImageProcessDef>();
private readonly Dictionary<string, ImageProcessDef> ImagesProcessDefDict = new Dictionary<string, ImageProcessDef>();
private CancellationTokenSource cancellationTokenSource = null;

private static int StartProcessWait(string path, string arguments, string workDir, bool noWindow = false)
Expand Down Expand Up @@ -77,22 +77,25 @@ private static int StartProcessWait(string path, string arguments, string workDi

public void addImageProcess(bool copyonly, string source, string dest, string destconverted, string exepath, string arguments, string workDir, bool noWindow = false)
{
ImageProcessDef ImageProcess = new ImageProcessDef();
ImageProcess.path = exepath;
ImageProcess.arguments = arguments;
ImageProcess.workDir = workDir;
ImageProcess.noWindow = noWindow;
ImageProcess.copyonly = copyonly;
ImageProcess.source = source;
ImageProcess.dest = dest;
ImageProcess.destconverted = destconverted;
ImagesProcessDefList.Add(ImageProcess);
if (!ImagesProcessDefDict.ContainsKey(source))
{
ImageProcessDef ImageProcess = new ImageProcessDef();
ImageProcess.path = exepath;
ImageProcess.arguments = arguments;
ImageProcess.workDir = workDir;
ImageProcess.noWindow = noWindow;
ImageProcess.copyonly = copyonly;
ImageProcess.source = source;
ImageProcess.dest = dest;
ImageProcess.destconverted = destconverted;
ImagesProcessDefDict.Add(source, ImageProcess);
}

}

public void Clear()
{
ImagesProcessDefList.Clear();
ImagesProcessDefDict.Clear();
}

public void Stop()
Expand All @@ -105,14 +108,14 @@ public void Stop()

public int Count()
{
return ImagesProcessDefList.Count;
return ImagesProcessDefDict.Count;
}

public void Start(int maxConcurrency, DelCallback CallBack)
{
using (SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(maxConcurrency))
{
if (ImagesProcessDefList.Count == 0)
if (ImagesProcessDefDict.Count == 0)
{
return;
}
Expand All @@ -121,9 +124,9 @@ public void Start(int maxConcurrency, DelCallback CallBack)
FailedConverts = 0;
SuccesFullConverts = 0;
FailedFiles.Clear();
int max = ImagesProcessDefList.Count();
int max = ImagesProcessDefDict.Count();
List<Task<bool>> tasks = new List<Task<bool>>();
foreach (ImageProcessDef ImageProcess in ImagesProcessDefList)
foreach (ImageProcessDef ImageProcess in ImagesProcessDefDict.Values)
{
concurrencySemaphore.Wait();
Task<bool> t = Task.Factory.StartNew((object o) =>
Expand Down Expand Up @@ -186,6 +189,10 @@ public void Start(int maxConcurrency, DelCallback CallBack)
return File.Exists(ImageProcessDefTask.destconverted);
}
}
catch
{
return false;
}
finally
{
_ = concurrencySemaphore.Release();
Expand Down
1 change: 1 addition & 0 deletions Localization/LocSource.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/af_ZA.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/ar_SA.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/ca_ES.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/cs_CZ.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/da_DK.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/de_DE.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Gesamte zu verarbeitende Bilder:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Bilder immer verarbeiten, auch wenn sie nicht geändert wurden</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimierungen</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Doppelte Bilder erkennen</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/el_GR.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/en_US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/eo_UY.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/es_ES.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total de imágenes para procesar:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Siempre procesar imágenes, incluso si no han cambiado</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizaciones</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detectar imágenes duplicadas</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/et_EE.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/fa_IR.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/fi_FI.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/fr_FR.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total des images à traiter:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Toujours traiter les images, même si elles n'ont pas changé</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Détecter les doublons d'images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/he_IL.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/hr_HR.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
1 change: 1 addition & 0 deletions Localization/hu_HU.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<sys:String x:Key="LOC_HTMLEXPORTER_TotalImagesProcess">Total images to process:</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_AlwaysProcess">Always process images, even if they have not changed</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_Optimizations">optimizations</sys:String>
<sys:String x:Key="LOC_HTMLEXPORTER_DetectDuplicates">Detect Duplicate Images</sys:String>



Expand Down
Loading

0 comments on commit 071eceb

Please sign in to comment.