Skip to content

Commit

Permalink
Merge #4147 Recommendation suppression for modpacks
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Aug 6, 2024
2 parents 8c06ebd + f8785f9 commit 81ec4c2
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Chinese translation fixes (#4115, #4116, #4131 by: zhouyiqing0304; reviewed: HebaruSan)
- [Multiple] Visually indicate to users that they should click Refresh (#4133 by: HebaruSan)
- [Multiple] Option to clone smaller instances with junction points (Windows) or symbolic links (Unix) (#4129, #4144 by: HebaruSan)
- [Multiple] Recommendation suppression for modpacks (#4147 by: HebaruSan; reviewed: JonnyOThan)

### Bugfixes

Expand Down
22 changes: 18 additions & 4 deletions Core/Relationships/RelationshipResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ private void ResolveStanza(List<RelationshipDescriptor> stanza,
if (options.get_recommenders && descriptor.suppress_recommendations)
{
log.DebugFormat("Skipping {0} because get_recommenders option is set", descriptor.ToString());
suppressedRecommenders.Add(descriptor);
continue;
}
options = orig_options.OptionsFor(descriptor);
Expand Down Expand Up @@ -575,18 +576,25 @@ public IEnumerable<CkanModule> Dependencies()

public IEnumerable<CkanModule> Recommendations(HashSet<CkanModule> dependencies)
=> modlist.Values.Except(dependencies)
.Where(m => ReasonsFor(m).Any(r => r is SelectionReason.Recommended
&& dependencies.Contains(r.Parent)))
.Where(m => ValidRecSugReasons(dependencies,
ReasonsFor(m).Where(r => r is SelectionReason.Recommended)
.ToList()))
.OrderByDescending(totalDependers);

public IEnumerable<CkanModule> Suggestions(HashSet<CkanModule> dependencies,
List<CkanModule> recommendations)
=> modlist.Values.Except(dependencies)
.Except(recommendations)
.Where(m => ReasonsFor(m).Any(r => r is SelectionReason.Suggested
&& dependencies.Contains(r.Parent)))
.Where(m => ValidRecSugReasons(dependencies,
ReasonsFor(m).Where(r => r is SelectionReason.Suggested)
.ToList()))
.OrderByDescending(totalDependers);

private bool ValidRecSugReasons(HashSet<CkanModule> dependencies,
List<SelectionReason> recSugReasons)
=> recSugReasons.Any(r => dependencies.Contains(r.Parent))
&& !suppressedRecommenders.Any(rel => recSugReasons.Any(r => rel.WithinBounds(r.Parent)));

public ParallelQuery<KeyValuePair<CkanModule, HashSet<string>>> Supporters(
HashSet<CkanModule> supported,
IEnumerable<CkanModule> toExclude)
Expand Down Expand Up @@ -682,6 +690,12 @@ private void AddReason(CkanModule module, SelectionReason reason)
private readonly Dictionary<CkanModule, List<SelectionReason>> reasons =
new Dictionary<CkanModule, List<SelectionReason>>();

/// <summary>
/// Depends relationships with suppress_recommendations=true,
/// to be applied to all recommendations and suggestions
/// </summary>
private HashSet<RelationshipDescriptor> suppressedRecommenders = new HashSet<RelationshipDescriptor>();

private readonly IRegistryQuerier registry;
private readonly GameVersionCriteria versionCrit;
private readonly RelationshipResolverOptions options;
Expand Down
47 changes: 31 additions & 16 deletions GUI/Controls/EditModpack.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 40 additions & 5 deletions GUI/Controls/EditModpack.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -32,6 +33,7 @@ public EditModpack()
ToolTip.SetToolTip(GameVersionMaxComboBox, Properties.Resources.EditModpackTooltipGameVersionMax);
ToolTip.SetToolTip(LicenseComboBox, Properties.Resources.EditModpackTooltipLicense);
ToolTip.SetToolTip(IncludeVersionsCheckbox, Properties.Resources.EditModpackTooltipIncludeVersions);
ToolTip.SetToolTip(IncludeOptRelsCheckbox, Properties.Resources.EditModpackTooltipIncludeOptRels);
ToolTip.SetToolTip(DependsRadioButton, Properties.Resources.EditModpackTooltipDepends);
ToolTip.SetToolTip(RecommendsRadioButton, Properties.Resources.EditModpackTooltipRecommends);
ToolTip.SetToolTip(SuggestsRadioButton, Properties.Resources.EditModpackTooltipSuggests);
Expand Down Expand Up @@ -322,7 +324,7 @@ private void ExportModpackButton_Click(object sender, EventArgs e)
badField.Focus();
user.RaiseError(error);
}
else if (TrySavePrompt(modpackExportOptions, out _, out string filename))
else if (TrySavePrompt(modpackExportOptions, out string filename))
{
if (module.depends.Count == 0)
{
Expand All @@ -336,11 +338,15 @@ private void ExportModpackButton_Click(object sender, EventArgs e)
{
module.suggests = null;
}
CkanModule.ToFile(ApplyVersionsCheckbox(module), filename);
CkanModule.ToFile(ApplyCheckboxes(module), filename);
OpenFileBrowser(filename);
task?.SetResult(true);
}
}

private CkanModule ApplyCheckboxes(CkanModule input)
=> ApplyIncludeOptRelsCheckbox(ApplyVersionsCheckbox(input));

private CkanModule ApplyVersionsCheckbox(CkanModule input)
{
if (IncludeVersionsCheckbox.Checked)
Expand Down Expand Up @@ -375,7 +381,38 @@ private CkanModule ApplyVersionsCheckbox(CkanModule input)
}
}

private bool TrySavePrompt(List<ExportOption> exportOptions, out ExportOption selectedOption, out string filename)
private CkanModule ApplyIncludeOptRelsCheckbox(CkanModule input)
{
if (IncludeOptRelsCheckbox.Checked)
{
return input;
}
else
{
var newMod = CkanModule.FromJson(CkanModule.ToJson(input));
foreach (var rel in newMod.depends)
{
rel.suppress_recommendations = true;
}
return newMod;
}
}

private void OpenFileBrowser(string location)
{
if (File.Exists(location))
{
// We need the folder of the file
// Otherwise the OS would try to open the file in its default application
location = Path.GetDirectoryName(location);
}
if (Directory.Exists(location))
{
Utilities.ProcessStartURL(location);
}
}

private bool TrySavePrompt(List<ExportOption> exportOptions, out string filename)
{
var dlg = new SaveFileDialog()
{
Expand All @@ -384,13 +421,11 @@ private bool TrySavePrompt(List<ExportOption> exportOptions, out ExportOption se
};
if (dlg.ShowDialog(ParentForm) == DialogResult.OK)
{
selectedOption = exportOptions[dlg.FilterIndex - 1];
filename = dlg.FileName;
return true;
}
else
{
selectedOption = null;
filename = null;
return false;
}
Expand Down
Loading

0 comments on commit 81ec4c2

Please sign in to comment.