81 lines
2.1 KiB
C#
81 lines
2.1 KiB
C#
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;
|
|
}
|
|
} |