Editor/Core/AutoSaveRules.cs

Rules for deciding when an editor idle autosave should run. Defines input record IdleSaveInput and static checks including minimum idle and minimum interval, and ensures saves are allowed and not during peer edit or play mode.

using System;

namespace TeamCreate;

public sealed record IdleSaveInput(bool Enabled, bool HasUnsavedChanges, bool PeerEditing, bool InPlayMode, DateTime LastLocalEdit, DateTime LastAutoSave, DateTime Now, Func<bool> CanSave);

public static class AutoSaveRules
{
    public const int MinIntervalSeconds = 30;

    public const int IdleSeconds = 30;

    public static bool CheckpointEligible(bool hasUnsavedChanges, Func<bool> canSave)
    {
        if (!hasUnsavedChanges)
        {
            return false;
        }
        return canSave?.Invoke() ?? false;
    }

    public static bool Eligible(IdleSaveInput input)
    {
        if (!input.Enabled)
        {
            return false;
        }
        if (input.PeerEditing || input.InPlayMode)
        {
            return false;
        }
        if (input.LastLocalEdit == default(DateTime))
        {
            return false;
        }
        if (input.Now - input.LastLocalEdit < TimeSpan.FromSeconds(30L))
        {
            return false;
        }
        if (input.LastAutoSave != default(DateTime) && input.Now - input.LastAutoSave < TimeSpan.FromSeconds(30L))
        {
            return false;
        }
        return CheckpointEligible(input.HasUnsavedChanges, input.CanSave);
    }
}