| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Builder; |
| | 4 | | using DCL.Builder.Manifest; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class BIWSaveController : BIWController, IBIWSaveController |
| | 9 | | { |
| | 10 | | private const float MS_BETWEEN_SAVES = 5000f; |
| | 11 | |
|
| 0 | 12 | | public int saveAttemptsSinceLastSave { get; set; } = 0; |
| 0 | 13 | | public int timesSaved { get; set; } = 0; |
| | 14 | |
|
| | 15 | | private BuilderInWorldBridge bridge; |
| | 16 | |
|
| | 17 | | private float nextTimeToSave; |
| 0 | 18 | | private bool canActivateSave = true; |
| 0 | 19 | | public int GetSaveAttempsSinceLastSave() { return saveAttemptsSinceLastSave; } |
| 0 | 20 | | public int GetTimesSaved() { return timesSaved; } |
| | 21 | |
|
| | 22 | | public override void Initialize(IContext context) |
| | 23 | | { |
| 3 | 24 | | base.Initialize(context); |
| | 25 | |
|
| 3 | 26 | | bridge = context.sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>(); |
| 3 | 27 | | if (bridge != null) |
| 3 | 28 | | bridge.OnKernelUpdated += TryToSave; |
| | 29 | |
|
| 3 | 30 | | if ( context.editorContext.editorHUD != null) |
| | 31 | | { |
| 3 | 32 | | context.editorContext.editorHUD.OnConfirmPublishAction += ConfirmPublishScene; |
| | 33 | | } |
| 3 | 34 | | } |
| | 35 | |
|
| | 36 | | public override void Dispose() |
| | 37 | | { |
| 0 | 38 | | base.Dispose(); |
| | 39 | |
|
| 0 | 40 | | if (bridge != null) |
| 0 | 41 | | bridge.OnKernelUpdated -= TryToSave; |
| | 42 | |
|
| 0 | 43 | | if ( context.editorContext.editorHUD != null) |
| | 44 | | { |
| 0 | 45 | | context.editorContext.editorHUD.OnConfirmPublishAction -= ConfirmPublishScene; |
| | 46 | | } |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | public void ResetSaveTime() { nextTimeToSave = 0; } |
| | 50 | |
|
| | 51 | | public void ResetNumberOfSaves() |
| | 52 | | { |
| 0 | 53 | | saveAttemptsSinceLastSave = 0; |
| 0 | 54 | | timesSaved = 0; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public override void EnterEditMode(IBuilderScene scene) |
| | 58 | | { |
| 3 | 59 | | base.EnterEditMode(scene); |
| 3 | 60 | | nextTimeToSave = DCLTime.realtimeSinceStartup + MS_BETWEEN_SAVES / 1000f; |
| 3 | 61 | | ResetNumberOfSaves(); |
| 3 | 62 | | } |
| | 63 | |
|
| | 64 | | public override void ExitEditMode() |
| | 65 | | { |
| 0 | 66 | | if (saveAttemptsSinceLastSave > 0 && context.editorContext.publishController.HasUnpublishChanges()) |
| | 67 | | { |
| 0 | 68 | | ForceSave(); |
| 0 | 69 | | ResetNumberOfSaves(); |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | base.ExitEditMode(); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void SetSaveActivation(bool isActive, bool tryToSave = false) |
| | 76 | | { |
| 0 | 77 | | canActivateSave = isActive; |
| 0 | 78 | | if (tryToSave) |
| 0 | 79 | | TryToSave(); |
| 0 | 80 | | } |
| | 81 | |
|
| 4 | 82 | | public bool CanSave() { return DCLTime.realtimeSinceStartup >= nextTimeToSave && isEditModeActive && canActivateSave |
| | 83 | |
|
| | 84 | | public void TryToSave() |
| | 85 | | { |
| 1 | 86 | | if(!isEditModeActive) |
| 0 | 87 | | return; |
| | 88 | |
|
| 1 | 89 | | saveAttemptsSinceLastSave++; |
| | 90 | |
|
| 1 | 91 | | if (CanSave()) |
| 1 | 92 | | ForceSave(); |
| 1 | 93 | | } |
| | 94 | |
|
| | 95 | | public void ForceSave() |
| | 96 | | { |
| 2 | 97 | | if (!isEditModeActive || !canActivateSave) |
| 0 | 98 | | return; |
| | 99 | |
|
| | 100 | | // We update the manifest with the current state |
| 2 | 101 | | builderScene.UpdateManifestFromScene(); |
| | 102 | |
|
| | 103 | | //We set the manifest on the builder server |
| 2 | 104 | | context.builderAPIController.SetManifest(builderScene.manifest); |
| | 105 | |
|
| 2 | 106 | | nextTimeToSave = DCLTime.realtimeSinceStartup + MS_BETWEEN_SAVES / 1000f; |
| 2 | 107 | | context.editorContext.editorHUD?.SceneSaved(); |
| 2 | 108 | | saveAttemptsSinceLastSave = 0; |
| 2 | 109 | | timesSaved++; |
| 2 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | internal void ConfirmPublishScene(string sceneName, string sceneDescription, string sceneScreenshot) { ResetNumberOf |
| | 113 | | } |