Skip to content

Commit

Permalink
UI updating should be thread-safe now
Browse files Browse the repository at this point in the history
  • Loading branch information
Telanor committed Nov 8, 2014
1 parent 3570d2e commit 266d546
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 75 deletions.
106 changes: 60 additions & 46 deletions FolderLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ public static bool ValidDirectory(string sFolderPath)
}

//refresh infos before processing
public static void RefreshInfo(string sFolderPath, string formTitle, string formattedFolderStr, bool bUpdateLog)
public static void RefreshInfo(string sFolderPath, bool bUpdateLog, bool updateForm)
{
var sOldTitle = formTitle;
formTitle = formTitle + "-creating files lists"; //TODO: Broken in conversion to C#
Application.DoEvents();
if(updateForm)
{
MainForm.SetTitleAppend("-creating files lists");
Application.DoEvents();
}

fileList.Clear();
backupList.Clear();

//MBMs
var sT = Directory.GetFiles(sFolderPath, "*.mbm", SearchOption.AllDirectories);
var iMBMCount = sT.Length;
Expand All @@ -46,46 +50,56 @@ public static void RefreshInfo(string sFolderPath, string formTitle, string form
sT = Directory.GetFiles(sFolderPath, "*.ddsified", SearchOption.AllDirectories);
var iBackupCount = sT.Length;
backupList.AddRange(sT);

//refresh labels
var pathSplit = sFolderPath.Split('\\');
formattedFolderStr = pathSplit[pathSplit.Length - 2] + "\\" + pathSplit.Last(); //TODO: Broken in conversion to C#
if(updateForm)
{
var pathSplit = sFolderPath.Split('\\');
MainForm.DisplaySelectedFolder(pathSplit[pathSplit.Length - 2] + "\\" + pathSplit.Last());
}

if(bUpdateLog)
{
MainForm.Instance.Log_WriteLine("-----");
MainForm.Instance.Log_WriteLine("Folder infos : ");
MainForm.Instance.Log_WriteLine("MBM files count : " + iMBMCount);
MainForm.Instance.Log_WriteLine("TGA files count : " + iTGACount);
MainForm.Instance.Log_WriteLine("PNG files count : " + iPNGCount);
MainForm.Instance.Log_WriteLine("Backup files count : " + iBackupCount);
MainForm.Instance.Log_WriteLine("-----");
MainForm.Log_WriteLine("-----");
MainForm.Log_WriteLine("Folder infos : ");
MainForm.Log_WriteLine("MBM files count : " + iMBMCount);
MainForm.Log_WriteLine("TGA files count : " + iTGACount);
MainForm.Log_WriteLine("PNG files count : " + iPNGCount);
MainForm.Log_WriteLine("Backup files count : " + iBackupCount);
MainForm.Log_WriteLine("-----");
}
formTitle = sOldTitle;

if(updateForm)
MainForm.SetTitleAppend("");
}

//processing files lists
public static void ProcessFileLists(string sFolderPath, ProgressBar progressBar, Label lInfos, FolderProcessingParams cfg)
public static void ProcessFileLists(string sFolderPath, FolderProcessingParams cfg)
{
var sT = "";
RefreshInfo(sFolderPath, sT, sT, false);
RefreshInfo(sFolderPath, false, false);
FileSkipCount = 0;

var iExcludeCount = 0;
var argHandler = new ArgumentsHandler();
var lConvParms = new List<ImageManager.ConversionParameters>();

foreach(var s in fileList)
{
if(!argHandler.IsFileExcluded(s, cfg))
lConvParms.Add(argHandler.GetConversionParameters(s, cfg));
else
iExcludeCount += 1;
}

var iStep = 0;
MainForm.Instance.Log_WriteLine("-----");
MainForm.Instance.Log_WriteLine("Starting conversion of " + fileList.Count + " files.");
MainForm.Instance.Log_WriteLine(" MBM count : " + argHandler.Count_MBM + ".");
MainForm.Instance.Log_WriteLine(" TGA count : " + argHandler.Count_TGA + ".");
MainForm.Instance.Log_WriteLine(" PNG count : " + argHandler.Count_PNG + ".");
MainForm.Instance.Log_WriteLine(iExcludeCount + " files excluded, " + argHandler.Count_NoMipmaps + " files without mipmaps. " + (argHandler.Count_ForceNormal + argHandler.Count_ForceNotNormal) + " files will skip normal detection." + argHandler.Count_NoResize + " files will not be resized.");
MainForm.Instance.Log_WriteLine("-----");

MainForm.Log_WriteLine("-----");
MainForm.Log_WriteLine("Starting conversion of " + fileList.Count + " files.");
MainForm.Log_WriteLine(" MBM count : " + argHandler.Count_MBM + ".");
MainForm.Log_WriteLine(" TGA count : " + argHandler.Count_TGA + ".");
MainForm.Log_WriteLine(" PNG count : " + argHandler.Count_PNG + ".");
MainForm.Log_WriteLine(iExcludeCount + " files excluded, " + argHandler.Count_NoMipmaps + " files without mipmaps. " + (argHandler.Count_ForceNormal + argHandler.Count_ForceNotNormal) + " files will skip normal detection." + argHandler.Count_NoResize + " files will not be resized.");
MainForm.Log_WriteLine("-----");

var sw = Stopwatch.StartNew();
//processing
Expand All @@ -95,34 +109,35 @@ public static void ProcessFileLists(string sFolderPath, ProgressBar progressBar,
ImageManager.ConvertMBMtoDDS(c, cfg);
else
ImageManager.ConvertFileToDDS(c, cfg);

iStep += 1;
progressBar.Value = Convert.ToInt32((iStep / lConvParms.Count) * 100);
lInfos.Text = "Processing " + Path.GetFileName(c.FilePath) + ", file " + iStep + "\\" + lConvParms.Count;
MainForm.Instance.Log_WriteLine("---");

MainForm.ReportProgress(Convert.ToInt32((iStep / lConvParms.Count) * 100), "Processing " + Path.GetFileName(c.FilePath) + ", file " + iStep + "\\" + lConvParms.Count);
MainForm.Log_WriteLine("---");
Application.DoEvents();
}

sw.Stop();
MainForm.Instance.Log_WriteLine("-----");
MainForm.Instance.Log_WriteLine(String.Format("Conversion done! {0} files processed in {1}ms.", lConvParms.Count, sw.Elapsed.TotalMilliseconds));
MainForm.Log_WriteLine("-----");
MainForm.Log_WriteLine(String.Format("Conversion done! {0} files processed in {1}ms.", lConvParms.Count, sw.Elapsed.TotalMilliseconds));
if(FileSkipCount > 0)
MainForm.Instance.Log_WriteLine(FileSkipCount + " files skipped, check log.txt for more informations.");
MainForm.Instance.Log_WriteLine("-----");
MainForm.Log_WriteLine(FileSkipCount + " files skipped, check log.txt for more informations.");
MainForm.Log_WriteLine("-----");
}

//backup files
public static void BackupFiles(string sFolderPath, ProgressBar progressBar, Label lInfos)
public static void BackupFiles(string sFolderPath)
{
var st = "";
var dontAskAgain = false;
var yesNo = false;
var deleteBackups = false;
RefreshInfo(sFolderPath, st, st, false);
var iStep = 0;

RefreshInfo(sFolderPath, false, false);

MainForm.Instance.Log_WriteLine("-----");
MainForm.Instance.Log_WriteLine("Starting backup of " + backupList.Count + " files");
MainForm.Instance.Log_WriteLine("-----");
MainForm.Log_WriteLine("-----");
MainForm.Log_WriteLine("Starting backup of " + backupList.Count + " files");
MainForm.Log_WriteLine("-----");

foreach(var s in backupList)
{
Expand All @@ -146,33 +161,32 @@ public static void BackupFiles(string sFolderPath, ProgressBar progressBar, Labe
File.Delete(sBackupFile);
File.Move(s, sBackupFile);

MainForm.Instance.Log_WriteLine("Reverting " + sDDSToDelete + " to " + sBackupFile);
MainForm.Log_WriteLine("Reverting " + sDDSToDelete + " to " + sBackupFile);
}
else
{
if(deleteBackups)
File.Delete(s);

MainForm.Instance.Log_WriteLine("Skipping backup of " + sBackupFile);
MainForm.Log_WriteLine("Skipping backup of " + sBackupFile);
}
}
else
{
File.Move(s, sBackupFile);
MainForm.Instance.Log_WriteLine("Reverting " + sDDSToDelete + " to " + sBackupFile);
MainForm.Log_WriteLine("Reverting " + sDDSToDelete + " to " + sBackupFile);
}

iStep += 1;
progressBar.Value = Convert.ToInt32((iStep / backupList.Count) * 100);
lInfos.Text = "Reverting " + Path.GetFileName(sBackupFile) + ", file " + iStep + "\\" + backupList.Count;

MainForm.Instance.Log_WriteLine("---");
MainForm.ReportProgress(Convert.ToInt32((iStep / backupList.Count) * 100), "Reverting " + Path.GetFileName(sBackupFile) + ", file " + iStep + "\\" + backupList.Count);
MainForm.Log_WriteLine("---");
Application.DoEvents();
}

MainForm.Instance.Log_WriteLine("-----");
MainForm.Instance.Log_WriteLine("Backup done! " + backupList.Count + " files processed.");
MainForm.Instance.Log_WriteLine("-----");
MainForm.Log_WriteLine("-----");
MainForm.Log_WriteLine("Backup done! " + backupList.Count + " files processed.");
MainForm.Log_WriteLine("-----");
}

//novelty! a new, shiny class to handle arguments properly, instead of putting them in some lists (well, there are still lists, but with some better management)
Expand Down
26 changes: 13 additions & 13 deletions ImageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static bool DetectNormalFromTexturePattern(string filePath, out bool fai
catch(Exception)
{
failToLoad = true;
MainForm.Instance.Log_WriteLine("ERR : Can't read " + filePath + ", skipping file.");
MainForm.Log_WriteLine("ERR : Can't read " + filePath + ", skipping file.");
}

return bBool;
Expand Down Expand Up @@ -266,13 +266,13 @@ public static void ConvertFileToDDS(ConversionParameters convParams, FolderProce
}

//info for log
MainForm.Instance.Log_WriteLine("LOG : Converting " + convParams.FilePath + " to " + fpNew);
MainForm.Log_WriteLine("LOG : Converting " + convParams.FilePath + " to " + fpNew);

//keep small images uncompressed. This is independent from any parameters, the value of 64 is hard-coded
if(((iInfos.Width < MinHeightForCompressed) | (iInfos.Height < MinHeightForCompressed)))
{
if(((form == Format.Dxt1) | (form == Format.Dxt5)))
MainForm.Instance.Log_WriteLine("LOG : Resolution is considered too low for DXT compression. Switching to uncompressed format for better quality.");
MainForm.Log_WriteLine("LOG : Resolution is considered too low for DXT compression. Switching to uncompressed format for better quality.");
form = Format.A8R8G8B8;
mipmapLevel = 1;
}
Expand All @@ -281,15 +281,15 @@ public static void ConvertFileToDDS(ConversionParameters convParams, FolderProce
//skip file if the resolution is not correct
if(((iInfos.Width < cfg.MinRes_Process_Width) | (iInfos.Height < cfg.MinRes_Process_Width)))
{
MainForm.Instance.Log_WriteLine("LOG : Skipping " + convParams.FilePath + ", resolution is too low.");
MainForm.Log_WriteLine("LOG : Skipping " + convParams.FilePath + ", resolution is too low.");
FolderLoader.FileSkipCount += 1;
return;
}

//check if the file is large enough to be resized
if((((iInfos.Width < cfg.MinRes_Resize_Width) | (iInfos.Height < cfg.MinRes_Resize_Height)) & (Math.Abs(dRatio - 1) > 0.0001f)))
{
MainForm.Instance.Log_WriteLine("LOG : " + convParams.FilePath.Split('.').Last() + ", resolution is too low to be resized.");
MainForm.Log_WriteLine("LOG : " + convParams.FilePath.Split('.').Last() + ", resolution is too low to be resized.");
dRatio = 1;
}

Expand All @@ -315,13 +315,13 @@ public static void ConvertFileToDDS(ConversionParameters convParams, FolderProce
}
else
{
MainForm.Instance.Log_WriteLine("ERR : Unknown file format " + iInfos.Format + ", skipping conversion for " + convParams.FilePath);
MainForm.Log_WriteLine("ERR : Unknown file format " + iInfos.Format + ", skipping conversion for " + convParams.FilePath);
FolderLoader.FileSkipCount += 1;
return;
}

//info for log
MainForm.Instance.Log_WriteLine(" Format : " + form + ", normalmap : " + bNormal + ", res: " + (iCorWidth * dRatio) + "x" + (iCorHeight * dRatio));
MainForm.Log_WriteLine(" Format : " + form + ", normalmap : " + bNormal + ", res: " + (iCorWidth * dRatio) + "x" + (iCorHeight * dRatio));

//the output is saved in a graphicStream, but a memorystream could work just as well.
var gs = TextureLoader.SaveToStream(ImageFileFormat.Bmp, texture);
Expand All @@ -342,7 +342,7 @@ public static void ConvertFileToDDS(ConversionParameters convParams, FolderProce
texture.Dispose();
SwizzleImage(gs, iCorWidth, iCorHeight, Is32BPP(iInfos));
sw.Stop();
MainForm.Instance.Log_WriteLine(String.Format("SwizzleImage took {0}ms", sw2.Elapsed.TotalMilliseconds));
MainForm.Log_WriteLine(String.Format("SwizzleImage took {0}ms", sw2.Elapsed.TotalMilliseconds));
}

//another attempt to flush memory: the program tend to crash if too much large textures are converted
Expand Down Expand Up @@ -380,7 +380,7 @@ public static void ConvertFileToDDS(ConversionParameters convParams, FolderProce
}
sw.Stop();

MainForm.Instance.Log_WriteLine(String.Format("Image conversion took {0}ms", sw.Elapsed.TotalMilliseconds));
MainForm.Log_WriteLine(String.Format("Image conversion took {0}ms", sw.Elapsed.TotalMilliseconds));
}

//Convert a mbm file to dds
Expand Down Expand Up @@ -448,12 +448,12 @@ public static void ConvertMBMtoDDS(ConversionParameters convParams, FolderProces
{
mbmTemp.AsBitmap().Save(gs, ImageFormat.Bmp);

MainForm.Instance.Log_WriteLine("LOG : Converting " + convParams.FilePath + " to " + fpNew);
MainForm.Instance.Log_WriteLine(" Format : " + form + ", normalmap : " + bNormal + ", res: " + (mbmTemp.Width * dRatio) + "x" + (mbmTemp.Height * dRatio));
MainForm.Log_WriteLine("LOG : Converting " + convParams.FilePath + " to " + fpNew);
MainForm.Log_WriteLine(" Format : " + form + ", normalmap : " + bNormal + ", res: " + (mbmTemp.Width * dRatio) + "x" + (mbmTemp.Height * dRatio));

if((((mbmTemp.Width < cfg.MinRes_Resize_Width) | (mbmTemp.Height < cfg.MinRes_Resize_Height)) & (Math.Abs(dRatio - 1) > 0.0001f)))
{
MainForm.Instance.Log_WriteLine("LOG : " + convParams.FilePath.Split('.').Last() + ", resolution is too low to be resized.");
MainForm.Log_WriteLine("LOG : " + convParams.FilePath.Split('.').Last() + ", resolution is too low to be resized.");
dRatio = 1;
}
gs.Position = 0;
Expand Down Expand Up @@ -502,7 +502,7 @@ private static void FlipImage(GraphicsStream gs)

gs.Position = 0;
sw.Stop();
MainForm.Instance.Log_WriteLine(String.Format("FlipImage took {0}ms", sw.Elapsed.TotalMilliseconds));
MainForm.Log_WriteLine(String.Format("FlipImage took {0}ms", sw.Elapsed.TotalMilliseconds));
}

//Swizzlin'
Expand Down
Loading

0 comments on commit 266d546

Please sign in to comment.