Skip to content

Commit 37df692

Browse files
committed
Remove troublesome progressbox timer
Just use the ol' "show it if 7z, not if not" method
1 parent ed19050 commit 37df692

File tree

1 file changed

+146
-153
lines changed

1 file changed

+146
-153
lines changed

AngelLoader/BusinessLogic.cs

Lines changed: 146 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,17 @@ void ReportProgress(ProgressReport pr)
758758
if (scanningOne)
759759
{
760760
Log(nameof(ScanFMs) + ": Scanning one", methodName: false);
761-
View.BeginInvoke(new Action(() => View.Block()));
762-
ProgressBox.ProgressTask = ProgressPanel.ProgressTasks.ScanAllFMs;
763-
ProgressBox.ShowProgressWindow(ProgressBox.ProgressTask, suppressShow: true);
761+
// Just use a cheap check and throw up the progress box for .7z files, otherwise not. Not as nice
762+
// as the timer method, but that can cause race conditions I don't know how to fix, so whatever.
763+
if (fmsToScan[0].Archive.ExtEqualsI(".7z"))
764+
{
765+
ProgressBox.ShowScanningAllFMs();
766+
}
767+
else
768+
{
769+
ProgressBox.ProgressTask = ProgressPanel.ProgressTasks.ScanAllFMs;
770+
ProgressBox.ShowProgressWindow(ProgressBox.ProgressTask, suppressShow: true);
771+
}
764772
}
765773
else
766774
{
@@ -772,190 +780,177 @@ void ReportProgress(ProgressReport pr)
772780
// TODO: This is pretty hairy, try and organize this better
773781
try
774782
{
775-
using (var timeOut = new Timer(500) { AutoReset = false })
783+
ScanCts = new CancellationTokenSource();
784+
785+
var fms = new List<string>();
786+
// Get archive paths list only once and cache it - in case of "include subfolders" being true,
787+
// cause then it will hit the actual disk rather than just going through a list of paths in
788+
// memory
789+
Log(nameof(ScanFMs) + ": about to call " + nameof(GetFMArchivePaths) + " with subfolders=" +
790+
Config.FMArchivePathsIncludeSubfolders);
791+
var archivePaths = await Task.Run(GetFMArchivePaths);
792+
foreach (var fm in fmsToScan)
776793
{
777-
timeOut.Elapsed += (sender, e) =>
794+
var fmArchivePath = await Task.Run(() => FindFMArchive(fm, archivePaths));
795+
if (!fm.Archive.IsEmpty() && !fmArchivePath.IsEmpty())
778796
{
779-
if (scanningOne)
780-
{
781-
Log(nameof(ScanFMs) + ": timeOut.Elapsed: showing ProgressBox");
782-
View.BeginInvoke(new Action(ProgressBox.ShowThis));
783-
View.BeginInvoke(new Action(View.Unblock));
784-
}
785-
};
786-
timeOut.Start();
787-
788-
ScanCts = new CancellationTokenSource();
789-
790-
var fms = new List<string>();
791-
// Get archive paths list only once and cache it - in case of "include subfolders" being true,
792-
// cause then it will hit the actual disk rather than just going through a list of paths in
793-
// memory
794-
Log(nameof(ScanFMs) + ": about to call " + nameof(GetFMArchivePaths) + " with subfolders=" + Config.FMArchivePathsIncludeSubfolders);
795-
var archivePaths = await Task.Run(GetFMArchivePaths);
796-
foreach (var fm in fmsToScan)
797-
{
798-
var fmArchivePath = await Task.Run(() => FindFMArchive(fm, archivePaths));
799-
if (!fm.Archive.IsEmpty() && !fmArchivePath.IsEmpty())
800-
{
801-
fms.Add(fmArchivePath);
802-
}
803-
else if (GameIsKnownAndSupported(fm))
804-
{
805-
var fmInstalledPath = GetFMInstallsBasePath(fm.Game);
806-
if (!fmInstalledPath.IsEmpty())
807-
{
808-
fms.Add(Path.Combine(fmInstalledPath, fm.InstalledDir));
809-
}
810-
}
811-
else
812-
{
813-
continue;
814-
}
815-
816-
if (ScanCts.IsCancellationRequested)
817-
{
818-
ScanCts?.Dispose();
819-
return false;
820-
}
797+
fms.Add(fmArchivePath);
821798
}
822-
823-
List<ScannedFMData> fmDataList;
824-
try
799+
else if (GameIsKnownAndSupported(fm))
825800
{
826-
var progress = new Progress<ProgressReport>(ReportProgress);
827-
828-
using (var scanner = new Scanner())
801+
var fmInstalledPath = GetFMInstallsBasePath(fm.Game);
802+
if (!fmInstalledPath.IsEmpty())
829803
{
830-
scanner.LogFile = Paths.ScannerLogFile;
831-
scanner.ZipEntryNameEncoding = Encoding.UTF8;
832-
Paths.PrepareTempPath(Paths.FMScannerTemp);
833-
fmDataList = await scanner.ScanAsync(fms, Paths.FMScannerTemp, scanOptions, progress,
834-
ScanCts.Token);
804+
fms.Add(Path.Combine(fmInstalledPath, fm.InstalledDir));
835805
}
836806
}
837-
catch (OperationCanceledException)
807+
else
838808
{
839-
return false;
809+
continue;
840810
}
841-
finally
811+
812+
if (ScanCts.IsCancellationRequested)
842813
{
843814
ScanCts?.Dispose();
815+
return false;
844816
}
817+
}
845818

846-
foreach (var fm in fmDataList)
819+
List<ScannedFMData> fmDataList;
820+
try
821+
{
822+
var progress = new Progress<ProgressReport>(ReportProgress);
823+
824+
using (var scanner = new Scanner())
847825
{
848-
if (fm == null)
849-
{
850-
// We need to return fail for scanning one, else we get into an infinite loop because
851-
// of a refresh that gets called in that case
852-
if (scanningOne)
853-
{
854-
Log(nameof(ScanFMs) + " (one) scanned FM was null. FM was:\r\n" +
855-
"Archive: " + fmsToScan[0].Archive + "\r\n" +
856-
"InstalledDir: " + fmsToScan[0].InstalledDir,
857-
methodName: false);
858-
return false;
859-
}
860-
continue;
861-
}
826+
scanner.LogFile = Paths.ScannerLogFile;
827+
scanner.ZipEntryNameEncoding = Encoding.UTF8;
828+
Paths.PrepareTempPath(Paths.FMScannerTemp);
829+
fmDataList = await scanner.ScanAsync(fms, Paths.FMScannerTemp, scanOptions, progress,
830+
ScanCts.Token);
831+
}
832+
}
833+
catch (OperationCanceledException)
834+
{
835+
return false;
836+
}
837+
finally
838+
{
839+
ScanCts?.Dispose();
840+
}
862841

863-
FanMission sel;
842+
foreach (var fm in fmDataList)
843+
{
844+
if (fm == null)
845+
{
846+
// We need to return fail for scanning one, else we get into an infinite loop because
847+
// of a refresh that gets called in that case
864848
if (scanningOne)
865849
{
866-
sel = fmsToScan[0];
867-
}
868-
else
869-
{
870-
sel = fmsToScan.FirstOrDefault(x =>
871-
x.Archive.RemoveExtension().EqualsI(fm.ArchiveName.RemoveExtension()) ||
872-
x.InstalledDir.EqualsI(fm.ArchiveName.RemoveExtension()));
850+
Log(nameof(ScanFMs) + " (one) scanned FM was null. FM was:\r\n" +
851+
"Archive: " + fmsToScan[0].Archive + "\r\n" +
852+
"InstalledDir: " + fmsToScan[0].InstalledDir,
853+
methodName: false);
854+
return false;
873855
}
856+
continue;
857+
}
874858

875-
if (sel == null)
876-
{
877-
// Same as above (this should never happen now, but hey)
878-
if (scanningOne) return false;
879-
continue;
880-
}
859+
FanMission sel;
860+
if (scanningOne)
861+
{
862+
sel = fmsToScan[0];
863+
}
864+
else
865+
{
866+
sel = fmsToScan.FirstOrDefault(x =>
867+
x.Archive.RemoveExtension().EqualsI(fm.ArchiveName.RemoveExtension()) ||
868+
x.InstalledDir.EqualsI(fm.ArchiveName.RemoveExtension()));
869+
}
881870

882-
var gameSup = fm.Game != Games.Unsupported;
871+
if (sel == null)
872+
{
873+
// Same as above (this should never happen now, but hey)
874+
if (scanningOne) return false;
875+
continue;
876+
}
883877

884-
if (overwriteUnscannedFields || scanOptions.ScanTitle)
885-
{
886-
sel.Title = !fm.Title.IsEmpty() ? fm.Title : fm.ArchiveName.RemoveExtension();
878+
var gameSup = fm.Game != Games.Unsupported;
887879

888-
if (gameSup)
889-
{
890-
sel.AltTitles = new List<string> { fm.Title };
891-
sel.AltTitles.AddRange(fm.AlternateTitles);
892-
}
893-
else
894-
{
895-
sel.AltTitles = new List<string>();
896-
}
897-
}
880+
if (overwriteUnscannedFields || scanOptions.ScanTitle)
881+
{
882+
sel.Title = !fm.Title.IsEmpty() ? fm.Title : fm.ArchiveName.RemoveExtension();
898883

899-
if (overwriteUnscannedFields || scanOptions.ScanSize)
900-
{
901-
sel.SizeString = gameSup ? fm.Size.ConvertSize() : "";
902-
sel.SizeBytes = (ulong)(gameSup ? fm.Size ?? 0 : 0);
903-
}
904-
if (overwriteUnscannedFields || scanOptions.ScanReleaseDate)
884+
if (gameSup)
905885
{
906-
sel.ReleaseDate = gameSup ? fm.LastUpdateDate : null;
886+
sel.AltTitles = new List<string> { fm.Title };
887+
sel.AltTitles.AddRange(fm.AlternateTitles);
907888
}
908-
if (overwriteUnscannedFields || scanOptions.ScanCustomResources)
889+
else
909890
{
910-
sel.HasMap = gameSup ? fm.HasMap : null;
911-
sel.HasAutomap = gameSup ? fm.HasAutomap : null;
912-
sel.HasScripts = gameSup ? fm.HasCustomScripts : null;
913-
sel.HasTextures = gameSup ? fm.HasCustomTextures : null;
914-
sel.HasSounds = gameSup ? fm.HasCustomSounds : null;
915-
sel.HasObjects = gameSup ? fm.HasCustomObjects : null;
916-
sel.HasCreatures = gameSup ? fm.HasCustomCreatures : null;
917-
sel.HasMotions = gameSup ? fm.HasCustomMotions : null;
918-
sel.HasMovies = gameSup ? fm.HasMovies : null;
919-
sel.HasSubtitles = gameSup ? fm.HasCustomSubtitles : null;
891+
sel.AltTitles = new List<string>();
920892
}
893+
}
921894

922-
if (overwriteUnscannedFields || scanOptions.ScanAuthor)
923-
{
924-
sel.Author = gameSup ? fm.Author : "";
925-
}
895+
if (overwriteUnscannedFields || scanOptions.ScanSize)
896+
{
897+
sel.SizeString = gameSup ? fm.Size.ConvertSize() : "";
898+
sel.SizeBytes = (ulong)(gameSup ? fm.Size ?? 0 : 0);
899+
}
900+
if (overwriteUnscannedFields || scanOptions.ScanReleaseDate)
901+
{
902+
sel.ReleaseDate = gameSup ? fm.LastUpdateDate : null;
903+
}
904+
if (overwriteUnscannedFields || scanOptions.ScanCustomResources)
905+
{
906+
sel.HasMap = gameSup ? fm.HasMap : null;
907+
sel.HasAutomap = gameSup ? fm.HasAutomap : null;
908+
sel.HasScripts = gameSup ? fm.HasCustomScripts : null;
909+
sel.HasTextures = gameSup ? fm.HasCustomTextures : null;
910+
sel.HasSounds = gameSup ? fm.HasCustomSounds : null;
911+
sel.HasObjects = gameSup ? fm.HasCustomObjects : null;
912+
sel.HasCreatures = gameSup ? fm.HasCustomCreatures : null;
913+
sel.HasMotions = gameSup ? fm.HasCustomMotions : null;
914+
sel.HasMovies = gameSup ? fm.HasMovies : null;
915+
sel.HasSubtitles = gameSup ? fm.HasCustomSubtitles : null;
916+
}
926917

927-
if (overwriteUnscannedFields || scanOptions.ScanGameType)
928-
{
929-
sel.Game =
930-
fm.Game == Games.Unsupported ? Game.Unsupported :
931-
fm.Game == Games.TDP ? Game.Thief1 :
932-
fm.Game == Games.TMA ? Game.Thief2 :
933-
fm.Game == Games.TDS ? Game.Thief3 :
934-
(Game?)null;
935-
}
918+
if (overwriteUnscannedFields || scanOptions.ScanAuthor)
919+
{
920+
sel.Author = gameSup ? fm.Author : "";
921+
}
936922

937-
if (overwriteUnscannedFields || scanOptions.ScanLanguages)
938-
{
939-
sel.Languages = gameSup ? fm.Languages : new string[0];
940-
sel.LanguagesString = gameSup
941-
? fm.Languages != null ? string.Join(", ", fm.Languages) : ""
942-
: "";
943-
}
923+
if (overwriteUnscannedFields || scanOptions.ScanGameType)
924+
{
925+
sel.Game =
926+
fm.Game == Games.Unsupported ? Game.Unsupported :
927+
fm.Game == Games.TDP ? Game.Thief1 :
928+
fm.Game == Games.TMA ? Game.Thief2 :
929+
fm.Game == Games.TDS ? Game.Thief3 :
930+
(Game?)null;
931+
}
944932

945-
if (overwriteUnscannedFields || scanOptions.ScanTags)
946-
{
947-
sel.TagsString = gameSup ? fm.TagsString : "";
933+
if (overwriteUnscannedFields || scanOptions.ScanLanguages)
934+
{
935+
sel.Languages = gameSup ? fm.Languages : new string[0];
936+
sel.LanguagesString = gameSup
937+
? fm.Languages != null ? string.Join(", ", fm.Languages) : ""
938+
: "";
939+
}
948940

949-
// Don't clear the tags, because the user could have added a bunch and we should only
950-
// add to those, not overwrite them
951-
if (gameSup) AddTagsToFMAndGlobalList(sel.TagsString, sel.Tags);
952-
}
941+
if (overwriteUnscannedFields || scanOptions.ScanTags)
942+
{
943+
sel.TagsString = gameSup ? fm.TagsString : "";
953944

954-
sel.MarkedScanned = markAsScanned;
945+
// Don't clear the tags, because the user could have added a bunch and we should only
946+
// add to those, not overwrite them
947+
if (gameSup) AddTagsToFMAndGlobalList(sel.TagsString, sel.Tags);
955948
}
956949

957-
WriteFMDataIni(FMDataIniList, Paths.FMDataIni);
950+
sel.MarkedScanned = markAsScanned;
958951
}
952+
953+
WriteFMDataIni(FMDataIniList, Paths.FMDataIni);
959954
}
960955
catch (Exception ex)
961956
{
@@ -968,9 +963,7 @@ void ReportProgress(ProgressReport pr)
968963
}
969964
finally
970965
{
971-
// Always, always, always run this! Try block must enclose everything or we can get a race condition
972966
View.BeginInvoke(new Action(() => ProgressBox.HideThis()));
973-
View.BeginInvoke(new Action(() => View.Unblock()));
974967
}
975968

976969
return true;

0 commit comments

Comments
 (0)