manager: add proper support for options

This commit is contained in:
Pascal
2025-06-27 11:24:53 +02:00
parent d0e3d2fa61
commit 77a93b585a
8 changed files with 76 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
using Castle.Core.Logging;
using Newtonsoft.Json;
using Pilz.Extensions.Collections;
using System.IO.Compression;
using FileMode = System.IO.FileMode;
@@ -76,7 +77,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
if (installInfos is not null && installInfos.IsPublic && installInfos.Actions.Count != 0)
{
var actions = installInfos.Actions.Where(n => n.Side.IsSide(options.Side) && (!n.IsExtra || options.IncludeExtras));
var actions = installInfos.Actions.Where(n => n.Side.IsSide(options.Side) && (!n.IsExtra || options.IncludeExtras) && GetOptionValue(options, installInfos, n.TargetOption));
if (actions.Any())
{
result.Actions.AddRange(actions);
@@ -101,7 +102,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
var updatesOrderes = updateInfos.Updates.Where(n => n.IsPublic || options.IncludeNonPublic).OrderByDescending(n => n.Version);
var checkingVersionIndex = 0;
var checkingVersion = updatesOrderes.ElementAtOrDefault(checkingVersionIndex);
var actionsZeroIndex = result.Actions.Count; // Ensure we insert update actions behind install actions
var actionsZeroIndex = result.Actions.Count; // Ensure we insert update actions BEHIND install actions
while (checkingVersion is not null && checkingVersion.Version > result.CurrentVersion)
{
@@ -113,10 +114,8 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
ResolveInherit(action, installInfos);
// Check & add
if (action.Side.IsSide(options.Side) && (!action.IsExtra || options.IncludeExtras) && !result.Actions.Any(n => n.DestPath == action.DestPath))
{
if (action.Side.IsSide(options.Side) && (!action.IsExtra || options.IncludeExtras) && GetOptionValue(options, installInfos, action.TargetOption) && !result.Actions.Any(n => n.DestPath == action.DestPath))
actionsToAdd.Add(action);
}
}
result.Actions.InsertRange(actionsZeroIndex, actionsToAdd);
@@ -132,6 +131,22 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
result.HasError = true;
}
// Check option uninstall actions
result.OptionsAvailable.AddRange(installInfos.OptionSets); // Find used options
result.OptionsEnabled.AddRange(installInfos.OptionSets
.Where(set => set.Side.IsSide(options.Side))
.SelectMany(set => set.Options
.Where(opt => opt.Side.IsSide(options.Side) && options.Options.GetOptionValue(set, opt))
.Select(opt => $"{set.Id}.{opt.Id}"))); // Find used options
var uninstallOptionActions = installInfos.OptionSets
.Where(set => set.Side.IsSide(options.Side))
.SelectMany(set => set.Options
.Where(opt => opt.Side.IsSide(options.Side) && !result.OptionsEnabled.Contains($"{set.Id}.{opt.Id}") && modpackInfo.Options.ContainsKey($"{set.Id}.{opt.Id}"))
.SelectMany(opt => opt.UninstallActions))
.ForEach(action => ResolveInherit(action, installInfos))
.Where(action => !result.Actions.Any(n => n.DestPath == action.DestPath)); // Find actions that are not already there
result.Actions.InsertRange(0, uninstallOptionActions); // Add uninstall update actions BEFORE install actions
return result;
}
@@ -217,6 +232,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
}
// Save new modpack info
modpackInfo.Options.Clear();
modpackInfo.Version = checkResult.LatestVersion;
modpackInfo.ConfigUrl = updateConfig.ConfigUrl;
modpackInfo.Save();
@@ -293,4 +309,23 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
return true;
}
private static bool GetOptionValue(UpdateCheckOptions options, InstallInfos installInfos, string optionStr)
{
var optionsStr = optionStr.Split(',');
foreach (var set in installInfos.OptionSets)
{
if (!set.Side.IsSide(options.Side))
continue;
foreach (var opt in set.Options)
{
if (opt.Side.IsSide(options.Side) && optionsStr.Contains($"{set.Id}.{opt.Id}") && options.Options.GetOptionValue(set, opt))
return true;
}
}
return false;
}
}

View File

@@ -8,4 +8,5 @@ public class UpdateCheckOptions
public Side Side { get; set; } = Side.Client;
public bool IncludeExtras { get; set; }
public bool IgnoreInstalledVersion { get; set; }
public InstallOptionValueDictionary Options { get; } = [];
}

View File

@@ -5,6 +5,8 @@ public class UpdateCheckResult
public Version CurrentVersion { get; set; }
public Version LatestVersion { get; set; }
public List<InstallAction> Actions { get; private set; } = [];
public List<InstallOptionSet> OptionsAvailable { get; } = [];
public List<string> OptionsEnabled { get; } = [];
public bool IsInstalled { get; set; }
public bool HasError { get; set; }
public bool IsInMaintenance { get; set; }

View File

@@ -15,6 +15,9 @@ public class InstallOption : IActionSet
[JsonConverter(typeof(StringEnumConverter))]
public Side Side { get; set; } = Side.Both;
[DefaultValue(false)]
public bool Default { get; }
public List<UpdateAction> UninstallActions { get; } = [];
IEnumerable<InstallAction> IActionSet.Actions => UninstallActions.Cast<InstallAction>();

View File

@@ -18,7 +18,5 @@ public class InstallOptionSet
[DefaultValue(false)]
public bool Multiselect { get; set; }
public List<int> Defaults { get; } = [];
public List<InstallOption> Options { get; } = [];
}

View File

@@ -0,0 +1,28 @@
namespace ModpackUpdater;
public class InstallOptionValueDictionary : Dictionary<string, bool>
{
public void SetOption(InstallOptionSet set, InstallOption option, bool value)
{
if (!set.Options.Contains(option))
throw new KeyNotFoundException("Options seems to be not a part of the option set.");
if (option.Default == value)
UnsetOption(set, option);
else
this[$"{set.Id}.{option.Id}"] = value;
}
public void UnsetOption(InstallOptionSet set, InstallOption option)
{
if (!set.Options.Contains(option))
throw new KeyNotFoundException("Options seems to be not a part of the option set.");
Remove($"{set.Id}.{option.Id}");
}
public bool GetOptionValue(InstallOptionSet set, InstallOption option)
{
if (TryGetValue($"{set.Id}.{option.Id}", out var value))
return value;
return option.Default;
}
}

View File

@@ -12,6 +12,7 @@ public class ModpackInfo
public Version Version { get; set; }
public string ConfigUrl { get; set; }
public string ExtrasKey { get; set; }
public InstallOptionValueDictionary Options { get; } = [];
[JsonIgnore]
public string LocaLPath { get; private set; }

View File

@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Pilz.Cryptography" Version="2.1.2" />
<PackageReference Include="Pilz.Extensions" Version="2.1.1" />
</ItemGroup>
</Project>