Skip to content

Commit

Permalink
Work:
Browse files Browse the repository at this point in the history
-Fix rtfbox centering broke again on Win10/new
-Fix FM cache wasn't cleared when re-scanning for readmes.
-Fix readme order could change when re-scanning for readmes. They're always sorted now.
-Fix readmes could get out of sync due to extra run of DisplaySelectedFM() on index changed
-Fix exception when choose readme listbox OK button was clicked when no item was selected (talk about a boneheaded bug...)
-DisplaySelectedFM() now no longer ever needs to be run when a readme is selected, no matter how it's done
  • Loading branch information
FenPhoenix committed May 3, 2019
1 parent 591f333 commit 0e7e855
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 80 deletions.
9 changes: 7 additions & 2 deletions AngelLoader/CustomControls/RichTextBoxCustom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,17 @@ private static string GLMLToRTF(string text)
else if (tag == "NL")
{
lastTagWasLineBreak = true;
sb.Append(@"\line ");
if (alignLeftOnNextLine)
{
sb.Append(@"\ql ");
// For newest rtfbox on Win10, we need to use \par instead of \line or
// else the \ql doesn't take. This works on Win7 too.
sb.Append(@"\par\ql ");
alignLeftOnNextLine = false;
}
else
{
sb.Append(@"\line ");
}
}
else if (tag == "LINE")
{
Expand Down
54 changes: 29 additions & 25 deletions AngelLoader/FMCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,39 @@ internal static async Task<CacheData> GetCacheableData(FanMission fm, ProgressPa
{
if (!fm.InstalledDir.IsEmpty())
{
var fmCachePath = Path.Combine(Paths.FMsCache, fm.InstalledDir);
if (!fmCachePath.TrimEnd('\\').EqualsI(Paths.FMsCache.TrimEnd('\\')) && Directory.Exists(fmCachePath))
{
try
{
foreach (var f in Directory.EnumerateFiles(fmCachePath, "*",
SearchOption.TopDirectoryOnly))
{
File.Delete(f);
}

foreach (var d in Directory.EnumerateDirectories(fmCachePath, "*",
SearchOption.TopDirectoryOnly))
{
Directory.Delete(d, recursive: true);
}
}
catch (Exception ex)
{
Log("Exception enumerating files or directories in cache for " + fm.Archive + " / " +
fm.InstalledDir, ex);
}
}
ClearCacheDir(fm);
}
return new CacheData();
}

return FMIsReallyInstalled(fm)
? FMCache.GetCacheableDataInFMInstalledDir(fm)
: await FMCache.GetCacheableDataInFMCacheDir(fm, ProgressBox);
? GetCacheableDataInFMInstalledDir(fm)
: await GetCacheableDataInFMCacheDir(fm, ProgressBox);
}

private static void ClearCacheDir(FanMission fm)
{
var fmCachePath = Path.Combine(Paths.FMsCache, fm.InstalledDir);
if (!fmCachePath.TrimEnd('\\').EqualsI(Paths.FMsCache.TrimEnd('\\')) && Directory.Exists(fmCachePath))
{
try
{
foreach (var f in Directory.EnumerateFiles(fmCachePath, "*", SearchOption.TopDirectoryOnly))
{
File.Delete(f);
}

foreach (var d in Directory.EnumerateDirectories(fmCachePath, "*", SearchOption.TopDirectoryOnly))
{
Directory.Delete(d, recursive: true);
}
}
catch (Exception ex)
{
Log("Exception enumerating files or directories in cache for " + fm.Archive + " / " +
fm.InstalledDir, ex);
}
}
}

private static void RemoveEmptyFiles(List<string> files)
Expand Down Expand Up @@ -153,6 +156,7 @@ internal static async Task<CacheData> GetCacheableDataInFMCacheDir(FanMission fm
}

readmes.Clear();
ClearCacheDir(fm);

var fmArchivePath = FindFMArchive(fm);

Expand Down
118 changes: 65 additions & 53 deletions AngelLoader/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2663,25 +2663,17 @@ private async Task DisplaySelectedFM(bool refreshReadme = false)
#region Readme

var readmeFiles = cacheData.Readmes;
readmeFiles.Sort();

if (!readmeFiles.ContainsI(fm.SelectedReadme)) fm.SelectedReadme = null;

using (new DisableEvents(this)) ChooseReadmeComboBox.ClearFullItems();

void FillAndSelectReadmeFromMulti(string readme)
{
using (new DisableEvents(this))
{
ChooseReadmeComboBox.AddRangeFull(readmeFiles);
ChooseReadmeComboBox.SelectBackingIndexOf(readme);
}
}

if (!fm.SelectedReadme.IsEmpty())
{
if (readmeFiles.Count > 1)
{
FillAndSelectReadmeFromMulti(fm.SelectedReadme);
ReadmeCB_FillAndSelect(readmeFiles, fm.SelectedReadme);
}
else
{
Expand All @@ -2708,7 +2700,7 @@ void FillAndSelectReadmeFromMulti(string readme)
if (!safeReadme.IsEmpty())
{
fm.SelectedReadme = safeReadme;
FillAndSelectReadmeFromMulti(safeReadme);
ReadmeCB_FillAndSelect(readmeFiles, safeReadme);
}
else
{
Expand All @@ -2735,21 +2727,61 @@ void FillAndSelectReadmeFromMulti(string readme)

ChooseReadmePanel.Hide();

LoadReadme(fm);

#endregion
}

private void ReadmeCB_FillAndSelect(List<string> readmeFiles, string readme)
{
using (new DisableEvents(this))
{
ChooseReadmeComboBox.AddRangeFull(readmeFiles);
ChooseReadmeComboBox.SelectBackingIndexOf(readme);
}
}

private void LoadReadme(FanMission fm)
{
try
{
var (path, type) = Core.GetReadmeFileAndType(fm);
ReadmeLoad(path, type);
#region Debug

// Tells me whether a readme got reloaded more than once, which should never be allowed to happen
// due to performance concerns.
#if !ReleasePublic
DebugLabel.Text = int.TryParse(DebugLabel.Text, out var result) ? (result + 1).ToString() : "1";
#endif

#endregion

if (type == ReadmeType.HTML)
{
ViewHTMLReadmeButton.Show();
ShowReadme(false);
// In case the cursor is over the scroll bar area
if (CursorOverReadmeArea()) ShowReadmeControls(true);
}
else
{
ShowReadme(true);
ViewHTMLReadmeButton.Hide();
var fileType = type == ReadmeType.PlainText
? RichTextBoxStreamType.PlainText
: RichTextBoxStreamType.RichText;

ReadmeRichTextBox.LoadContent(path, fileType);
}
}
catch (Exception ex)
{
Log(nameof(DisplaySelectedFM) + ": " + nameof(ReadmeLoad) + " failed.", ex);
Log(nameof(LoadReadme) + " failed.", ex);

ViewHTMLReadmeButton.Hide();
ShowReadme(true);
ReadmeRichTextBox.SetText(LText.ReadmeArea.UnableToLoadReadme);
}

#endregion
}

#region Progress window
Expand Down Expand Up @@ -3190,9 +3222,9 @@ private void AddTagMenuCustomItem_Click(object sender, EventArgs e)

#region Choose readme

private async void ChooseReadmeButton_Click(object sender, EventArgs e)
private void ChooseReadmeButton_Click(object sender, EventArgs e)
{
if (ChooseReadmeListBox.Items.Count == 0) return;
if (ChooseReadmeListBox.Items.Count == 0 || ChooseReadmeListBox.SelectedIndex == -1) return;

var fm = GetSelectedFM();
fm.SelectedReadme = ChooseReadmeListBox.SelectedBackingItem();
Expand All @@ -3207,15 +3239,28 @@ private async void ChooseReadmeButton_Click(object sender, EventArgs e)
ShowReadme(true);
}

await RefreshSelectedFM(refreshReadme: true);
if (ChooseReadmeListBox.Items.Count > 1)
{
ReadmeCB_FillAndSelect(ChooseReadmeListBox.BackingItems, fm.SelectedReadme);
ShowReadmeControls(CursorOverReadmeArea());
}
else
{
using (new DisableEvents(this)) ChooseReadmeComboBox.ClearFullItems();
ChooseReadmeComboBox.Hide();
}

LoadReadme(fm);
}

private async void ChooseReadmeComboBox_SelectedIndexChanged(object sender, EventArgs e)
private void ChooseReadmeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (EventsDisabled) return;
var fm = GetSelectedFM();
fm.SelectedReadme = ChooseReadmeComboBox.SelectedBackingItem();
await RefreshSelectedFM(refreshReadme: true);
// Just load the readme; don't call DisplaySelectedFM() because that will re-get readmes and screw
// things up
LoadReadme(fm);
}

private void ChooseReadmeComboBox_DropDownClosed(object sender, EventArgs e)
Expand All @@ -3233,39 +3278,6 @@ private void ChooseReadmeComboBox_DropDownClosed(object sender, EventArgs e)

private void ResetZoomButton_Click(object sender, EventArgs e) => ReadmeRichTextBox.ResetZoomFactor();

/// <exception cref="ArgumentException"></exception>
/// <exception cref="IOException"></exception>
private void ReadmeLoad(string path, ReadmeType readmeType)
{
#region Debug

// Tells me whether a readme got reloaded more than once, which should never be allowed to happen
// due to performance concerns.
#if !ReleasePublic
DebugLabel.Text = int.TryParse(DebugLabel.Text, out var result) ? (result + 1).ToString() : "1";
#endif

#endregion

if (readmeType == ReadmeType.HTML)
{
ViewHTMLReadmeButton.Show();
ShowReadme(false);
// In case the cursor is over the scroll bar area
if (CursorOverReadmeArea()) ShowReadmeControls(true);
}
else
{
ShowReadme(true);
ViewHTMLReadmeButton.Hide();
var fileType = readmeType == ReadmeType.PlainText
? RichTextBoxStreamType.PlainText
: RichTextBoxStreamType.RichText;

ReadmeRichTextBox.LoadContent(path, fileType);
}
}

private void ShowReadme(bool enabled)
{
ReadmeRichTextBox.Visible = enabled;
Expand Down

0 comments on commit 0e7e855

Please sign in to comment.