Skip to content

Commit

Permalink
Work:
Browse files Browse the repository at this point in the history
-Fix import DL and NDL would add every FM twice and therefore scan twice as many as needed (the duplicates would then be removed by FindFMs, so in practice it wasn't catastrophic, but yeah)
-Disable selection change event during view list modify, because it will go to redraw from it in the middle of operations and anything could happen if we don't turn off events
  • Loading branch information
FenPhoenix committed Apr 30, 2019
1 parent 125c500 commit 5138824
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 219 deletions.
3 changes: 2 additions & 1 deletion AngelLoader/Common/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ internal enum Error
internal enum ImportError
{
None,
NoArchiveDirsFound
NoArchiveDirsFound,
Unknown
}

internal enum StubResponseError
Expand Down
4 changes: 4 additions & 0 deletions AngelLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ Task SortAndSetFilter(bool suppressRefresh = false, bool forceRefreshReadme = fa
bool forceSuppressSelectionChangedEvent = false, bool suppressSuspendResume = false);
void Init();
void SortFMsDGV(Column column, SortOrder sortDirection);
int GetRowCount();
void SetRowCount(int count);
void Show();
void ShowAlert(string message, string title);
object InvokeSync(Delegate method);
object InvokeSync(Delegate method, params object[] args);
object InvokeAsync(Delegate method);
object InvokeAsync(Delegate method, params object[] args);
void Block(bool block);
Task RefreshFMsList(bool refreshReadme, bool suppressSelectionChangedEvent = false,
bool suppressSuspendResume = false);
Task RefreshSelectedFM(bool refreshReadme, bool refreshGridRowOnly = false);
bool AskToContinue(string message, string title, bool noIcon = false);

Expand Down
183 changes: 94 additions & 89 deletions AngelLoader/FindFMs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,139 +21,144 @@ internal static void Find(List<FanMission> fmDataIniList, bool startup = false)
// Make sure we don't lose anything when we re-find!
if (!startup) Core.WriteFullFMDataIni();

// Init or reinit - must be deep-copied or changes propagate back because reference types
DeepCopyGlobalTags(PresetTags, GlobalTags);
// We're modifying the data that FMsDGV pulls from when it redraws. This will at least prevent a
// selection changed event from firing while we do it, as that could be really bad potentially.
using (new DisableEvents((IEventDisabler)Core.View))
{
// Init or reinit - must be deep-copied or changes propagate back because reference types
DeepCopyGlobalTags(PresetTags, GlobalTags);

#region Back up lists and read FM data file
#region Back up lists and read FM data file

// Copy FMs to backup lists before clearing, in case we can't read the ini file. We don't want to end
// up with a blank or incomplete list and then glibly save it out later.
var backupList = new List<FanMission>();
foreach (var fm in fmDataIniList) backupList.Add(fm);
// Copy FMs to backup lists before clearing, in case we can't read the ini file. We don't want to end
// up with a blank or incomplete list and then glibly save it out later.
var backupList = new List<FanMission>();
foreach (var fm in fmDataIniList) backupList.Add(fm);

var viewBackupList = new List<FanMission>();
foreach (var fm in Core.FMsViewList) viewBackupList.Add(fm);
var viewBackupList = new List<FanMission>();
foreach (var fm in Core.FMsViewList) viewBackupList.Add(fm);

fmDataIniList.Clear();
Core.FMsViewList.Clear();
fmDataIniList.Clear();
Core.FMsViewList.Clear();

var fmDataIniExists = File.Exists(Paths.FMDataIni);
var fmDataIniExists = File.Exists(Paths.FMDataIni);

if (fmDataIniExists)
{
try
{
ReadFMDataIni(Paths.FMDataIni, fmDataIniList);
}
catch (Exception ex)
if (fmDataIniExists)
{
Log("Exception reading FM data ini", ex);
if (startup)
try
{
MessageBox.Show("Exception reading FM data ini. Exiting. Please check " + Paths.LogFile,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
ReadFMDataIni(Paths.FMDataIni, fmDataIniList);
}
else
catch (Exception ex)
{
fmDataIniList.ClearAndAdd(backupList);
Core.FMsViewList.ClearAndAdd(viewBackupList);
return;
Log("Exception reading FM data ini", ex);
if (startup)
{
MessageBox.Show("Exception reading FM data ini. Exiting. Please check " + Paths.LogFile,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
else
{
fmDataIniList.ClearAndAdd(backupList);
Core.FMsViewList.ClearAndAdd(viewBackupList);
return;
}
}
}
}

#endregion

#region Get installed dirs from disk
#endregion

// Could check inside the folder for a .mis file to confirm it's really an FM folder, but that's
// horrendously expensive. Talking like eight seconds vs. < 4ms for the 1098 set. Weird.
var t1InstalledFMDirs = new List<string>();
var t2InstalledFMDirs = new List<string>();
var t3InstalledFMDirs = new List<string>();
#region Get installed dirs from disk

for (int i = 0; i < 3; i++)
{
var instFMDirs = i == 0 ? t1InstalledFMDirs : i == 1 ? t2InstalledFMDirs : t3InstalledFMDirs;
var instPath = i == 0 ? Config.T1FMInstallPath : i == 1 ? Config.T2FMInstallPath : Config.T3FMInstallPath;
// Could check inside the folder for a .mis file to confirm it's really an FM folder, but that's
// horrendously expensive. Talking like eight seconds vs. < 4ms for the 1098 set. Weird.
var t1InstalledFMDirs = new List<string>();
var t2InstalledFMDirs = new List<string>();
var t3InstalledFMDirs = new List<string>();

if (Directory.Exists(instPath))
for (int i = 0; i < 3; i++)
{
try
var instFMDirs = i == 0 ? t1InstalledFMDirs : i == 1 ? t2InstalledFMDirs : t3InstalledFMDirs;
var instPath = i == 0 ? Config.T1FMInstallPath : i == 1 ? Config.T2FMInstallPath : Config.T3FMInstallPath;

if (Directory.Exists(instPath))
{
foreach (var d in Directory.GetDirectories(instPath, "*", SearchOption.TopDirectoryOnly))
try
{
var dirName = d.GetTopmostDirName();
if (!dirName.EqualsI(".fmsel.cache")) instFMDirs.Add(dirName);
foreach (var d in Directory.GetDirectories(instPath, "*", SearchOption.TopDirectoryOnly))
{
var dirName = d.GetTopmostDirName();
if (!dirName.EqualsI(".fmsel.cache")) instFMDirs.Add(dirName);
}
}
catch (Exception ex)
{
Log("Exception getting directories in " + instPath, ex);
}
}
catch (Exception ex)
{
Log("Exception getting directories in " + instPath, ex);
}
}
}

#endregion
#endregion

#region Get archives from disk
#region Get archives from disk

var fmArchives = new List<string>();
var fmArchives = new List<string>();

foreach (var path in GetFMArchivePaths())
{
try
foreach (var path in GetFMArchivePaths())
{
var files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
foreach (var f in files)
try
{
if (!fmArchives.ContainsI(f.GetFileNameFast()) && f.ExtIsArchive() && !f.ContainsI(Paths.FMSelBak))
var files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
foreach (var f in files)
{
fmArchives.Add(f.GetFileNameFast());
if (!fmArchives.ContainsI(f.GetFileNameFast()) && f.ExtIsArchive() && !f.ContainsI(Paths.FMSelBak))
{
fmArchives.Add(f.GetFileNameFast());
}
}
}
catch (Exception ex)
{
Log("Exception getting files in " + path, ex);
}
}
catch (Exception ex)
{
Log("Exception getting files in " + path, ex);
}
}

#endregion

#region Build FanMission objects from installed dirs
#endregion

var t1List = new List<FanMission>();
var t2List = new List<FanMission>();
var t3List = new List<FanMission>();
#region Build FanMission objects from installed dirs

for (int i = 0; i < 3; i++)
{
var instFMDirs = i == 0 ? t1InstalledFMDirs : i == 1 ? t2InstalledFMDirs : t3InstalledFMDirs;
var list = i == 0 ? t1List : i == 1 ? t2List : t3List;
var game = i == 0 ? Game.Thief1 : i == 1 ? Game.Thief2 : Game.Thief3;
var t1List = new List<FanMission>();
var t2List = new List<FanMission>();
var t3List = new List<FanMission>();

foreach (var item in instFMDirs)
for (int i = 0; i < 3; i++)
{
list.Add(new FanMission { InstalledDir = item, Game = game, Installed = true });
var instFMDirs = i == 0 ? t1InstalledFMDirs : i == 1 ? t2InstalledFMDirs : t3InstalledFMDirs;
var list = i == 0 ? t1List : i == 1 ? t2List : t3List;
var game = i == 0 ? Game.Thief1 : i == 1 ? Game.Thief2 : Game.Thief3;

foreach (var item in instFMDirs)
{
list.Add(new FanMission { InstalledDir = item, Game = game, Installed = true });
}
}
}

#endregion
#endregion

MergeNewArchiveFMs(fmArchives, fmDataIniList);
MergeNewArchiveFMs(fmArchives, fmDataIniList);

int instInitCount = fmDataIniList.Count;
if (t1List.Count > 0) MergeNewInstalledFMs(t1List, fmDataIniList, instInitCount);
if (t2List.Count > 0) MergeNewInstalledFMs(t2List, fmDataIniList, instInitCount);
if (t3List.Count > 0) MergeNewInstalledFMs(t3List, fmDataIniList, instInitCount);
int instInitCount = fmDataIniList.Count;
if (t1List.Count > 0) MergeNewInstalledFMs(t1List, fmDataIniList, instInitCount);
if (t2List.Count > 0) MergeNewInstalledFMs(t2List, fmDataIniList, instInitCount);
if (t3List.Count > 0) MergeNewInstalledFMs(t3List, fmDataIniList, instInitCount);

SetArchiveNames(fmArchives, fmDataIniList);
SetArchiveNames(fmArchives, fmDataIniList);

SetInstalledNames(fmDataIniList);
SetInstalledNames(fmDataIniList);

BuildViewList(fmArchives, fmDataIniList, t1InstalledFMDirs, t2InstalledFMDirs, t3InstalledFMDirs);
BuildViewList(fmArchives, fmDataIniList, t1InstalledFMDirs, t2InstalledFMDirs, t3InstalledFMDirs);
}
}

private static void SetArchiveNames(List<string> fmArchives, List<FanMission> fmDataIniList)
Expand Down
36 changes: 27 additions & 9 deletions AngelLoader/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,10 @@ internal void SetDebugMessageText(string text)

#region FMsDGV-related

public int GetRowCount() => FMsDGV.RowCount;

public void SetRowCount(int count) => FMsDGV.RowCount = count;

private void ZoomFMsDGV(ZoomFMsDGVType type, float? zoomFontSize = null)
{
// We'll be changing widths all over the place here, so don't save them out while we do this
Expand Down Expand Up @@ -2238,7 +2242,7 @@ public async Task RefreshSelectedFM(bool refreshReadme, bool refreshGridRowOnly
await DisplaySelectedFM(refreshReadme);
}

internal async Task RefreshFMsList(bool refreshReadme, bool suppressSelectionChangedEvent = false,
public async Task RefreshFMsList(bool refreshReadme, bool suppressSelectionChangedEvent = false,
bool suppressSuspendResume = false)
{
using (suppressSelectionChangedEvent ? new DisableEvents(this) : null)
Expand Down Expand Up @@ -3772,9 +3776,16 @@ private async void ImportFromDarkLoaderMenuItem_Click(object sender, EventArgs e
return;
}

bool success = await Core.ImportFromDarkLoader(iniFile, importFMData, importSaves);
if (!success) return;
// We're modifying the data that FMsDGV pulls from when it redraws. This will at least prevent a
// selection changed event from firing while we do it, as that could be really bad potentially.
using (new DisableEvents(this))
{
bool success = await Core.ImportFromDarkLoader(iniFile, importFMData, importSaves);
//if (!success) return;
}

// Always do this, because if our selection changed without an event, we may be displaying the wrong
// data
await SortAndSetFilter(forceRefreshReadme: true, forceSuppressSelectionChangedEvent: true);
}

Expand All @@ -3797,19 +3808,26 @@ private async Task ImportFromNDLOrFMSel(ImportType importType)
return;
}

int successCount = 0;
//int successCount = 0;
foreach (var file in iniFiles)
{
if (file.IsWhiteSpace()) continue;

bool success = await (importType == ImportType.FMSel
? Core.ImportFromFMSel(file)
: Core.ImportFromNDL(file));
if (success) successCount++;
// We're modifying the data that FMsDGV pulls from when it redraws. This will at least prevent a
// selection changed event from firing while we do it, as that could be really bad potentially.
using (new DisableEvents(this))
{
bool success = await (importType == ImportType.FMSel
? Core.ImportFromFMSel(file)
: Core.ImportFromNDL(file));
//if (success) successCount++;
}
}

if (successCount == 0) return;
//if (successCount == 0) return;

// Always do this, because if our selection changed without an event, we may be displaying the wrong
// data
await SortAndSetFilter(forceRefreshReadme: true, forceSuppressSelectionChangedEvent: true);
}

Expand Down
Loading

0 comments on commit 5138824

Please sign in to comment.