Repository URL to install this package:
Version:
1.3.1 ▾
|
using System;
using System.Threading;
using Fluctio.FluctioSim.EditorUtils.EditorGeneral;
using Fluctio.FluctioSim.EditorUtils.Gui;
using Fluctio.FluctioSim.Utils.General;
using Fluctio.FluctioSim.Utils.SerializableClasses;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.SettingsManagement;
using static Fluctio.FluctioSim.EditorUtils.SettingsManagement.SettingsManager;
namespace Fluctio.FluctioSim.EditorCore.AccountManagement
{
internal static class License
{
public static readonly TimeSpan TrialDuration = TimeSpan.FromDays(14);
[InitializeOnLoadMethod] private static void InitSettings() => AddExecutingAssembly();
private static readonly UserSetting<SerializableDateTime> FirstInstallDateSetting = Preference("License.FirstInstallDate", DateTime.UtcNow.MakeSerializable());
private static CancellationTokenSource _scheduledUpdateCancellation;
internal static LicenseType Type { get; private set; } //TODO: add 'update' event
internal static DateTime ValidTill { get; private set; }
[CanBeNull] internal static string GiftedBy { get; private set; }
[InitializeOnLoadMethod]
private static void Init()
{
Account.InfoChanged += UpdateLicenseInfo;
UpdateLicenseInfo();
}
private static void UpdateLicenseInfo()
{
SetNewLicenseInfo();
ScheduleNextUpdate();
ShowNoLicenseBanner();
}
private static void SetNewLicenseInfo()
{
var useAccountLicense =
Account.IsLoggedIn &&
Account.Info.LicenseType > LicenseType.None &&
Account.Info.LicenseValidTill > DateTime.UtcNow;
var trialValidTill = FirstInstallDateSetting.value.DateTime + TrialDuration;
var useMachineTrialLicense = trialValidTill > DateTime.UtcNow;
if (useAccountLicense)
{
Type = Account.Info.LicenseType;
ValidTill = Account.Info.LicenseValidTill;
GiftedBy = Account.Info.LicenseGiftedBy.NullIfWhiteSpace();
}
else if (useMachineTrialLicense)
{
Type = LicenseType.Trial;
ValidTill = trialValidTill;
}
else
{
Type = LicenseType.None;
ValidTill = DateTime.MaxValue;
}
}
private static void ScheduleNextUpdate()
{
_scheduledUpdateCancellation?.Cancel();
_scheduledUpdateCancellation = new CancellationTokenSource();
Util.ExecuteAfter(UpdateLicenseInfo, ValidTill, cancellationToken: _scheduledUpdateCancellation.Token);
}
private static void ShowNoLicenseBanner()
{
var didEditorJustOpen = EditorUtil.DidEditorJustOpen;
EditorApplication.delayCall += () =>
{
// if trial, open once
if (Type == LicenseType.Trial && didEditorJustOpen)
{
EditorWindow.GetWindow<AccountWindow>();
}
// if no license, show banner
ConfigurableEditorWindow.ToggleBanner<AccountWindow>(Type == LicenseType.None);
};
}
}
}