Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommendation suppression for modpacks #4147

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions GUI/Controls/EditModpack.resx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<data name="GameVersionLabel.Text" xml:space="preserve"><value>Game versions:</value></data>
<data name="LicenseLabel.Text" xml:space="preserve"><value>Licence:</value></data>
<data name="IncludeVersionsCheckbox.Text" xml:space="preserve"><value>Save mod versions</value></data>
<data name="IncludeOptRelsCheckbox.Text" xml:space="preserve"><value>Include optional relationships</value></data>
<data name="ModNameColumn.Text" xml:space="preserve"><value>Mod</value></data>
<data name="ModVersionColumn.Text" xml:space="preserve"><value>Version</value></data>
<data name="ModAbstractColumn.Text" xml:space="preserve"><value>Description</value></data>
Expand Down
Loading