using ComponentOrientedPlatform.Abstractions; using ComponentOrientedPlatform.Model; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks; namespace WinFormsComponentOrientedHost.Licensing; public sealed class LicenseProvider : ILicenseProvider { private readonly LicenseModel _model; private LicenseProvider(LicenseModel model) { _model = model; } public AccessLevel CurrentLevel => _model.Level; public bool IsExpired => _model.Expires.HasValue && _model.Expires.Value.Date < DateTime.UtcNow.Date; public string? Owner => _model.Owner; public static ILicenseProvider FromConfig(IConfiguration cfg, IAppLogger log) { var path = cfg["License:Path"]; if (string.IsNullOrWhiteSpace(path) || !File.Exists(path)) { log.Warn("License file not found, fallback to Minimal."); return new LicenseProvider(new LicenseModel { Level = AccessLevel.Minimal }); } try { var json = File.ReadAllText(path); var opts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; opts.Converters.Add(new JsonStringEnumConverter(allowIntegerValues: true)); var model = JsonSerializer.Deserialize(json, opts) ?? new LicenseModel { Level = AccessLevel.Minimal }; return new LicenseProvider(model); } catch (Exception ex) { log.Error("License read error", ex); return new LicenseProvider(new LicenseModel { Level = AccessLevel.Minimal }); } } private sealed class LicenseModel { public AccessLevel Level { get; set; } = AccessLevel.Minimal; public DateTime? Expires { get; set; } public string? Owner { get; set; } } }