123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- // Copyright (c) 2025 TerraByte Inc.
- //
- // A static class that persists the state of a multi-file reset operation
- // across script re-compilations, ensuring the process is not interrupted.
- using System.Linq;
- using UnityEditor;
- using UnityEngine.Scripting;
- namespace Terra.Arbitrator.Services
- {
- [Preserve]
- [InitializeOnLoad]
- public static class BetterGitStatePersistence
- {
- internal const string ResetQueueKey = "BetterGit.ResetQueue";
- // This static constructor runs automatically after every script compilation.
- static BetterGitStatePersistence()
- {
- // Use delayCall to ensure this runs after the editor is fully initialized.
- EditorApplication.delayCall += ContinueInterruptedReset;
- }
- public static void ContinueInterruptedReset()
- {
- while (true)
- {
- var queuedFiles = SessionState.GetString(ResetQueueKey, "");
- if (string.IsNullOrEmpty(queuedFiles))
- {
- return; // Nothing to do.
- }
- var fileList = queuedFiles.Split(';').ToList();
- if (!fileList.Any())
- {
- SessionState.EraseString(ResetQueueKey);
- return;
- }
- // Get the next file to process
- var fileToReset = fileList.First();
- fileList.RemoveAt(0);
- // Update the session state with the remaining files BEFORE starting the operation.
- if (fileList.Any())
- {
- SessionState.SetString(ResetQueueKey, string.Join(";", fileList));
- }
- else
- {
- // Last file was processed, clear the key.
- SessionState.EraseString(ResetQueueKey);
- }
- // Create a GitChange object for the file to be reset.
- // This is a small, synchronous operation to get the file's current status.
- var change = GitService.GetChangeForFile(fileToReset);
- if (change == null)
- {
- // The File might have already been reset or state is unusual.
- // We'll log it and attempt to continue.
- UnityEngine.Debug.LogWarning($"Could not find status for {fileToReset}, skipping reset. It may have already been processed.");
- continue;
- }
- // Trigger the reset. After this, Unity will recompile if it was a script,
- // and this whole static constructor will run again.
- GitService.ResetFileChanges(change);
- break;
- }
- }
- }
- }
|