| | 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 numberOfSaves { get; set; } = 0; |
| | 13 | |
|
| | 14 | | private BuilderInWorldBridge bridge; |
| | 15 | | private Manifest sceneManifest; |
| | 16 | |
|
| | 17 | | private float nextTimeToSave; |
| 0 | 18 | | private bool canActivateSave = true; |
| 0 | 19 | | public int GetSaveTimes() { return numberOfSaves; } |
| | 20 | |
|
| | 21 | | public override void Initialize(IContext context) |
| | 22 | | { |
| 3 | 23 | | base.Initialize(context); |
| | 24 | |
|
| 3 | 25 | | bridge = context.sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>(); |
| 3 | 26 | | if (bridge != null) |
| 3 | 27 | | bridge.OnKernelUpdated += TryToSave; |
| | 28 | |
|
| 3 | 29 | | if ( context.editorContext.editorHUD != null) |
| | 30 | | { |
| 3 | 31 | | context.editorContext.editorHUD.OnSaveSceneInfoAction += SaveSceneInfo; |
| 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.OnSaveSceneInfoAction -= SaveSceneInfo; |
| 0 | 46 | | context.editorContext.editorHUD.OnConfirmPublishAction -= ConfirmPublishScene; |
| | 47 | | } |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | public void ResetSaveTime() { nextTimeToSave = 0; } |
| | 51 | |
|
| 0 | 52 | | public void ResetNumberOfSaves() { numberOfSaves = 0; } |
| | 53 | |
|
| | 54 | | public override void EnterEditMode(IParcelScene scene) |
| | 55 | | { |
| 3 | 56 | | base.EnterEditMode(scene); |
| 3 | 57 | | nextTimeToSave = DCLTime.realtimeSinceStartup + MS_BETWEEN_SAVES / 1000f; |
| 3 | 58 | | ResetNumberOfSaves(); |
| 3 | 59 | | } |
| | 60 | |
|
| | 61 | | public override void ExitEditMode() |
| | 62 | | { |
| 0 | 63 | | if (numberOfSaves > 0) |
| | 64 | | { |
| 0 | 65 | | ForceSave(); |
| 0 | 66 | | ResetNumberOfSaves(); |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | base.ExitEditMode(); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public void SetSaveActivation(bool isActive, bool tryToSave = false) |
| | 73 | | { |
| 0 | 74 | | canActivateSave = isActive; |
| 0 | 75 | | if (tryToSave) |
| 0 | 76 | | TryToSave(); |
| 0 | 77 | | } |
| | 78 | |
|
| 4 | 79 | | public bool CanSave() { return DCLTime.realtimeSinceStartup >= nextTimeToSave && isEditModeActive && canActivateSave |
| | 80 | |
|
| | 81 | | public void TryToSave() |
| | 82 | | { |
| 1 | 83 | | if (CanSave()) |
| 1 | 84 | | ForceSave(); |
| 1 | 85 | | } |
| | 86 | |
|
| | 87 | | public void ForceSave() |
| | 88 | | { |
| 2 | 89 | | if (!isEditModeActive || !canActivateSave) |
| 0 | 90 | | return; |
| | 91 | |
|
| 2 | 92 | | if (DataStore.i.builderInWorld.isDevBuild.Get() && sceneManifest != null) |
| | 93 | | { |
| 0 | 94 | | sceneManifest.scene = ManifestTranslator.TranslateSceneToManifest(sceneToEdit); |
| 0 | 95 | | context.builderAPIController.SetManifest(sceneManifest); |
| 0 | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| 2 | 99 | | bridge.SaveSceneState(sceneToEdit); |
| | 100 | | } |
| | 101 | |
|
| 2 | 102 | | nextTimeToSave = DCLTime.realtimeSinceStartup + MS_BETWEEN_SAVES / 1000f; |
| 2 | 103 | | context.editorContext.editorHUD?.SceneSaved(); |
| 2 | 104 | | numberOfSaves++; |
| 2 | 105 | | } |
| | 106 | |
|
| | 107 | | public void SetManifest(Manifest manifest) |
| | 108 | | { |
| 0 | 109 | | sceneManifest = manifest; |
| 0 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | public void SaveSceneInfo(string sceneName, string sceneDescription, string sceneScreenshot) { bridge.SaveSceneInfo( |
| | 113 | |
|
| 0 | 114 | | internal void ConfirmPublishScene(string sceneName, string sceneDescription, string sceneScreenshot) { ResetNumberOf |
| | 115 | | } |