Files
minecraft-modpack-updater/ModpackUpdater.Manager/ModpackFeatures.cs
2024-09-06 08:45:36 +02:00

72 lines
1.9 KiB
C#

using Unleash;
using Unleash.ClientFactory;
namespace ModpackUpdater;
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);
}
private bool InitializeApi()
{
if (api == null && !string.IsNullOrWhiteSpace(modpackConfig.UnleashApiUrl) && !string.IsNullOrWhiteSpace(modpackConfig.UnleashInstanceId))
{
settings = new UnleashSettings
{
AppName = "Modpack Updater",
UnleashApi = new Uri(modpackConfig.UnleashApiUrl),
FetchTogglesInterval = TimeSpan.FromSeconds(60 * 5),
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;
}
}