| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Builder; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class BIWPublishController : BIWController, IBIWPublishController |
| | 7 | | { |
| | 8 | | private IBIWEntityHandler entityHandler; |
| | 9 | | private IBIWCreatorController creatorController; |
| | 10 | | private IBIWActionController actionController; |
| | 11 | |
|
| | 12 | | private int checkerSceneLimitsOptimizationCounter = 0; |
| | 13 | | private bool hasUnpublishedChanges = false; |
| | 14 | |
|
| | 15 | | private const int FRAMES_BEETWEN_UPDATES = 10; |
| | 16 | | private const string FEEDBACK_MESSAGE_ENTITY_ERROR = "Some entities have errors (marked as pink cubes)."; |
| | 17 | | private const string FEEDBACK_MESSAGE_OUTSIDE_BOUNDARIES = "Some entities are outside of the Scene boundaries."; |
| | 18 | | private const string FEEDBACK_MESSAGE_TOO_MANY_ENTITIES = "Too many entities in the scene. Check scene limits."; |
| | 19 | |
|
| | 20 | | internal float publishTimeStamp = 0; |
| | 21 | |
|
| | 22 | | public override void Initialize(IContext context) |
| | 23 | | { |
| 4 | 24 | | base.Initialize(context); |
| | 25 | |
|
| 4 | 26 | | entityHandler = context.editorContext.entityHandler; |
| 4 | 27 | | creatorController = context.editorContext.creatorController; |
| 4 | 28 | | actionController = context.editorContext.actionController; |
| | 29 | |
|
| 4 | 30 | | if (context.editorContext.editorHUD != null) |
| 4 | 31 | | context.editorContext.editorHUD.OnPublishAction += StartPublishFlow; |
| | 32 | |
|
| 4 | 33 | | context.publisher.OnPublishFinish += PublishFinish; |
| 4 | 34 | | } |
| | 35 | |
|
| | 36 | | public override void Update() |
| | 37 | | { |
| 0 | 38 | | base.Update(); |
| 0 | 39 | | if (checkerSceneLimitsOptimizationCounter >= FRAMES_BEETWEN_UPDATES) |
| | 40 | | { |
| 0 | 41 | | checkerSceneLimitsOptimizationCounter = 0; |
| 0 | 42 | | CheckPublishConditions(); |
| 0 | 43 | | } |
| | 44 | | else |
| | 45 | | { |
| 0 | 46 | | checkerSceneLimitsOptimizationCounter++; |
| | 47 | | } |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public override void Dispose() |
| | 51 | | { |
| 4 | 52 | | base.Dispose(); |
| | 53 | |
|
| 4 | 54 | | context.publisher.OnPublishFinish -= PublishFinish; |
| 4 | 55 | | if ( context.editorContext.editorHUD != null) |
| 4 | 56 | | context.editorContext.editorHUD.OnPublishAction -= StartPublishFlow; |
| 4 | 57 | | } |
| | 58 | |
|
| | 59 | | public override void EnterEditMode(IBuilderScene scene) |
| | 60 | | { |
| 4 | 61 | | base.EnterEditMode(scene); |
| 4 | 62 | | publishTimeStamp = 0; |
| 4 | 63 | | } |
| | 64 | |
|
| | 65 | | public override void ExitEditMode() |
| | 66 | | { |
| 0 | 67 | | base.ExitEditMode(); |
| 0 | 68 | | CheckIfThereAreUnpublishChanges(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public bool HasUnpublishChanges() |
| | 72 | | { |
| 0 | 73 | | CheckIfThereAreUnpublishChanges(); |
| 0 | 74 | | return hasUnpublishedChanges; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | internal void CheckIfThereAreUnpublishChanges() |
| | 78 | | { |
| 0 | 79 | | hasUnpublishedChanges = actionController.HasApplyAnyActionThisSession(); |
| 0 | 80 | | if (hasUnpublishedChanges) |
| | 81 | | { |
| 0 | 82 | | hasUnpublishedChanges = actionController.GetLastActionTimestamp() > publishTimeStamp; |
| | 83 | | } |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public bool CanPublish() |
| | 87 | | { |
| 4 | 88 | | if (creatorController.IsAnyErrorOnEntities()) |
| 0 | 89 | | return false; |
| | 90 | |
|
| 4 | 91 | | if (!sceneToEdit.metricsCounter.IsInsideTheLimits()) |
| 1 | 92 | | return false; |
| | 93 | |
|
| 3 | 94 | | if (!entityHandler.AreAllEntitiesInsideBoundaries()) |
| 1 | 95 | | return false; |
| | 96 | |
|
| 2 | 97 | | return true; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// This function will check if you are able to publish the scene to the content server. If no error are present, an |
| | 102 | | /// </summary> |
| | 103 | | /// <returns>A message the with the reason telling you why you can't publish. If you can publish an empty message wi |
| | 104 | | public string CheckPublishConditions() |
| | 105 | | { |
| 1 | 106 | | string feedbackMessage = ""; |
| 1 | 107 | | if (creatorController.IsAnyErrorOnEntities()) |
| | 108 | | { |
| 0 | 109 | | feedbackMessage = FEEDBACK_MESSAGE_ENTITY_ERROR; |
| 0 | 110 | | } |
| 1 | 111 | | else if (!entityHandler.AreAllEntitiesInsideBoundaries()) |
| | 112 | | { |
| 0 | 113 | | feedbackMessage = FEEDBACK_MESSAGE_OUTSIDE_BOUNDARIES; |
| 0 | 114 | | } |
| 1 | 115 | | else if (!sceneToEdit.metricsCounter.IsInsideTheLimits()) |
| | 116 | | { |
| 0 | 117 | | feedbackMessage = FEEDBACK_MESSAGE_TOO_MANY_ENTITIES; |
| | 118 | | } |
| | 119 | |
|
| 1 | 120 | | if ( context.editorContext.editorHUD != null) |
| 1 | 121 | | context.editorContext.editorHUD.SetPublishBtnAvailability(CanPublish(), feedbackMessage); |
| | 122 | |
|
| 1 | 123 | | return feedbackMessage; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | private void StartPublishFlow() |
| | 127 | | { |
| 0 | 128 | | if (!CanPublish()) |
| 0 | 129 | | return; |
| | 130 | |
|
| | 131 | | // We update the manifest with the current scene to send the last state to publish |
| 0 | 132 | | builderScene.UpdateManifestFromScene(); |
| | 133 | |
|
| 0 | 134 | | context.cameraController.TakeSceneScreenshot((sceneSnapshot) => |
| | 135 | | { |
| 0 | 136 | | builderScene.sceneScreenshotTexture = sceneSnapshot; |
| 0 | 137 | | if (builderScene.sceneType == IBuilderScene.SceneType.PROJECT) |
| | 138 | | { |
| | 139 | | //If it is a project, we took an aerial view of the scene too for the rotation of the scene |
| 0 | 140 | | context.cameraController.TakeSceneAerialScreenshot( sceneToEdit, (aerialSceenshot) => |
| | 141 | | { |
| 0 | 142 | | builderScene.aerialScreenshotTexture = aerialSceenshot; |
| 0 | 143 | | context.publisher.StartPublish(builderScene); |
| 0 | 144 | | }); |
| 0 | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 0 | 148 | | context.publisher.StartPublish(builderScene); |
| | 149 | | } |
| 0 | 150 | | }); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | private void PublishFinish(bool isOk) |
| | 154 | | { |
| 0 | 155 | | if(!isEditModeActive) |
| 0 | 156 | | return; |
| | 157 | |
|
| 0 | 158 | | hasUnpublishedChanges = false; |
| 0 | 159 | | publishTimeStamp = Time.unscaledTime; |
| 0 | 160 | | } |
| | 161 | | } |