unleash api via modpack config

This commit is contained in:
2024-07-10 18:24:49 +02:00
parent 1bd0e87211
commit d1f35676ee
7 changed files with 83 additions and 84 deletions

View File

@@ -1,81 +0,0 @@
using ModpackUpdater.Model;
using Unleash;
namespace ModpackUpdater;
public enum AppFeatures
{
AllowExtras
}
public static class AppFeaturesExtensions
{
private const string apiUrl = "https://git.pilzinsel64.de/api/v4/feature_flags/unleash/2";
private const string instanceId = "glffct-3vCzJXChAnxjsgvoHijR";
private static IUnleash api;
private static UnleashContext context;
private static UnleashSettings settings;
public static bool IsEnabled(this AppFeatures feature, AppFeatureContext context)
{
return feature switch
{
AppFeatures.AllowExtras => CheckFeature("allow-extras", false, context),
_ => throw new NotSupportedException(),
};
}
public static bool IsEnabled(this AppFeatures feature)
{
return feature switch
{
_ => throw new NotSupportedException(),
};
}
private static bool InitializeApi()
{
if (api == null)
{
settings = new UnleashSettings
{
AppName = "Modpack Updater",
UnleashApi = new Uri(apiUrl),
FetchTogglesInterval = TimeSpan.FromSeconds(60 * 5),
InstanceTag = instanceId,
};
api = new DefaultUnleash(settings);
}
return api != null;
}
private static bool CheckFeature(string name, bool defaultValue, AppFeatureContext context)
{
return InitializeApi() && api.IsEnabled(name, GetContext(context), defaultValue);
}
private static 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, ModpackConfig config) : AppFeatureContext
{
public override void Apply(UnleashContext context)
{
context.UserId = info.ExtrasKey;
context.Environment = config.Key;
}
}

View File

@@ -13,6 +13,7 @@ public partial class Form1
{
private ModpackInfo modpackInfo = new();
private ModpackConfig updateConfig = new();
private ModpackFeatures features;
private bool currentUpdating = false;
private UpdateCheckResult lastUpdateCheckResult = null;
private readonly UpdateCheckOptionsAdv updateOptions;
@@ -101,6 +102,9 @@ public partial class Form1
{
}
if (modpackInfo != null)
features = new(updateConfig);
if (modpackInfo == null || string.IsNullOrWhiteSpace(RadTextBoxControl_MinecraftProfileFolder.Text) /*|| modpackInfo.Valid*/)
{
SetStatus(LangRes.StatusText_MinecraftProfileWarning, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.general_warning_sign, SvgImageSize.Small));
@@ -168,7 +172,7 @@ public partial class Form1
SetStatus(LangRes.StatusText_CheckingForUpdates, AppSymbolFactory.Instance.GetSvgImage(AppSymbols.update_done, SvgImageSize.Small));
// Check for extras once again
updateOptions.IncludeExtras = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(modpackInfo, updateConfig));
updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(modpackInfo, updateConfig));
try
{

View File

@@ -54,7 +54,6 @@
<PackageReference Include="Pilz.UI.Telerik.SymbolFactory" Version="2.0.3" />
<PackageReference Include="Pilz.Win32" Version="2.0.0" />
<PackageReference Include="UI.for.WinForms.Common" Version="2024.2.514" />
<PackageReference Include="Unleash.Client" Version="4.1.9" />
</ItemGroup>
<ItemGroup>

View File

@@ -81,12 +81,13 @@ 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 = AppFeatures.AllowExtras.IsEnabled(new AllowExtrasFeatureContext(info, config));
updateOptions.IncludeExtras = features.IsEnabled(ModpackFeatures.FeatureAllowExtas, new AllowExtrasFeatureContext(info, config));
// Check for update
var installer = new ModpackInstaller(config, info);