new extras system

This commit is contained in:
2025-12-19 14:07:59 +01:00
parent 32c4065940
commit 5947f81307
5 changed files with 6 additions and 94 deletions

View File

@@ -24,7 +24,6 @@ public partial class MainViewModel : ObservableObject
private readonly UpdateCheckOptions updateOptions = new();
private ModpackInfo modpackInfo = new();
private ModpackConfig updateConfig = new();
private ModpackFeatures? features;
private UpdateCheckResult? lastUpdateCheckResult;
[ObservableProperty] private string? minecraftProfileFolder;
@@ -99,7 +98,7 @@ public partial class MainViewModel : ObservableObject
private bool AllowExtras()
{
return features != null && features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(modpackInfo));
return !string.IsNullOrWhiteSpace(modpackInfo.ExtrasKey) && updateConfig.ExtrasKeys.Contains(modpackInfo.ExtrasKey);
}
public void LoadProfileToUi()
@@ -191,15 +190,14 @@ public partial class MainViewModel : ObservableObject
// Ignore
}
features = new(updateConfig);
modpackInfo.ExtrasKey = InstallKey?.Trim();
if (!features.IsInvalid() && !string.IsNullOrWhiteSpace(modpackInfo.ExtrasKey) && !AllowExtras())
if (!string.IsNullOrWhiteSpace(modpackInfo.ExtrasKey) && !AllowExtras())
{
SetStatus(GeneralLangRes.InstallationKeyNotValid, AppGlobals.Symbols.GetImageSource(AppSymbols.general_warning_sign));
return false;
}
CanUseExtrasKey = CanUseExtrasKey = !string.IsNullOrWhiteSpace(updateConfig.UnleashApiUrl);
CanUseExtrasKey = updateConfig.ExtrasKeys.Count > 0;
if (string.IsNullOrWhiteSpace(MinecraftProfileFolder) /*|| modpackInfo.Valid*/)
{

View File

@@ -39,13 +39,12 @@ public static class Program
{
var info = ModpackInfo.TryLoad(updateOptions.ProfileFolder);
var config = ModpackConfig.LoadFromUrl(CheckModpackConfigUrl(updateOptions.ModpackConfig!, info));
var features = new ModpackFeatures(config);
// Check features
if (!string.IsNullOrWhiteSpace(updateOptions.ExtrasKey))
info.ExtrasKey = updateOptions.ExtrasKey;
if (!string.IsNullOrWhiteSpace(info.ExtrasKey))
updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(info));
updateOptions.IncludeExtras = !string.IsNullOrWhiteSpace(info.ExtrasKey) && config.ExtrasKeys.Contains(info.ExtrasKey);
// Check for update
var installer = new ModpackInstaller(config, info)

View File

@@ -1,81 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using Unleash;
using Unleash.ClientFactory;
namespace ModpackUpdater.Manager;
public class ModpackFeatures(ModpackConfig modpackConfig)
{
private IUnleash? api;
private UnleashContext context;
private UnleashSettings settings;
public static string FeatureAllowExtas => "allow-extras";
~ModpackFeatures()
{
api?.Dispose();
}
public bool IsEnabled(string feature)
{
return IsEnabled(feature, null);
}
public bool IsEnabled(string feature, AppFeatureContext context)
{
return CheckFeature(feature, context);
}
public bool IsInvalid()
{
return string.IsNullOrWhiteSpace(modpackConfig.UnleashApiUrl) || string.IsNullOrWhiteSpace(modpackConfig.UnleashInstanceId);
}
[MemberNotNullWhen(true, nameof(api))]
private bool InitializeApi()
{
if (api != null
|| string.IsNullOrWhiteSpace(modpackConfig.UnleashApiUrl)
|| string.IsNullOrWhiteSpace(modpackConfig.UnleashInstanceId))
return api != null;
settings = new UnleashSettings
{
AppName = "Modpack Updater",
UnleashApi = new Uri(modpackConfig.UnleashApiUrl),
FetchTogglesInterval = TimeSpan.FromSeconds(0),
InstanceTag = modpackConfig.UnleashInstanceId,
};
api = new UnleashClientFactory().CreateClient(settings, synchronousInitialization: true);
return api != null;
}
private bool CheckFeature(string name, AppFeatureContext context)
{
return InitializeApi() && api.IsEnabled(name, GetContext(context));
}
private UnleashContext GetContext(AppFeatureContext ccontext)
{
context ??= new();
context.CurrentTime = DateTime.Now;
ccontext?.Apply(context);
return context;
}
}
public abstract class AppFeatureContext
{
public abstract void Apply(UnleashContext context);
}
public class AllowExtrasFeatureContext(ModpackInfo info) : AppFeatureContext
{
public override void Apply(UnleashContext context)
{
context.UserId = info.ExtrasKey;
}
}

View File

@@ -169,9 +169,7 @@ public class ModpackInstaller(ModpackConfig updateConfig, ModpackInfo modpackInf
foreach (InstallAction iaction in checkResult.Actions)
{
var destFilePath = iaction.GetDestPath(modpackInfo.LocalPath);
var sourceUrl = updateConfig.PreferDirectLinks && !string.IsNullOrWhiteSpace(iaction.SourceUrl)
? iaction.GetSourceUrl(checkResult.LatestVersion, overwriteVersion: OverwriteVersion)
: await factory.ResolveSourceUrl(iaction, targetVersion: checkResult.LatestVersion, overwriteVersion: OverwriteVersion);
var sourceUrl = iaction.GetSourceUrl(checkResult.LatestVersion, overwriteVersion: OverwriteVersion);
if (iaction is UpdateAction uaction)
{

View File

@@ -9,9 +9,7 @@ public class ModpackConfig
public string? Name { get; set; }
public string? UpdateUrl { get; set; }
public string? InstallUrl { get; set; }
public string? UnleashApiUrl { get; set; }
public string? UnleashInstanceId { get; set; }
public bool PreferDirectLinks { get; set; }
public List<string> ExtrasKeys { get; } = [];
public string? MinecraftVersion { get; set; }
public string? RefTag { get; set; }