| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public interface IBIWPublishController : IBIWController |
| | 6 | | { |
| | 7 | | } |
| | 8 | |
|
| | 9 | | public class BIWPublishController : BIWController, IBIWPublishController |
| | 10 | | { |
| | 11 | | private IBIWEntityHandler entityHandler; |
| | 12 | | private IBIWCreatorController creatorController; |
| | 13 | |
|
| | 14 | | private BuilderInWorldBridge builderInWorldBridge; |
| | 15 | |
|
| | 16 | | private int checkerSceneLimitsOptimizationCounter = 0; |
| | 17 | |
|
| | 18 | | private const int FRAMES_BEETWEN_UPDATES = 10; |
| | 19 | | private const string FEEDBACK_MESSAGE_ENTITY_ERROR = "Some entities have errors (marked as pink cubes)."; |
| | 20 | | private const string FEEDBACK_MESSAGE_OUTSIDE_BOUNDARIES = "Some entities are outside of the Scene boundaries."; |
| | 21 | | private const string FEEDBACK_MESSAGE_TOO_MANY_ENTITIES = "Too many entities in the scene. Check scene limits."; |
| | 22 | |
|
| | 23 | | private float startPublishingTimestamp = 0; |
| | 24 | |
|
| | 25 | | public override void Initialize(BIWContext context) |
| | 26 | | { |
| 29 | 27 | | base.Initialize(context); |
| | 28 | |
|
| 29 | 29 | | entityHandler = context.entityHandler; |
| 29 | 30 | | creatorController = context.creatorController; |
| | 31 | |
|
| 29 | 32 | | if (HUDController.i?.builderInWorldMainHud != null) |
| | 33 | | { |
| 25 | 34 | | HUDController.i.builderInWorldMainHud.OnPublishAction += StartPublishFlow; |
| 25 | 35 | | HUDController.i.builderInWorldMainHud.OnConfirmPublishAction += StartPublishScene; |
| | 36 | | } |
| | 37 | |
|
| 29 | 38 | | builderInWorldBridge = context.sceneReferences.builderInWorldBridge; |
| | 39 | |
|
| 29 | 40 | | if (builderInWorldBridge != null) |
| 25 | 41 | | builderInWorldBridge.OnPublishEnd += PublishEnd; |
| 29 | 42 | | } |
| | 43 | |
|
| | 44 | | public override void Update() |
| | 45 | | { |
| 2 | 46 | | base.Update(); |
| 2 | 47 | | if (checkerSceneLimitsOptimizationCounter >= FRAMES_BEETWEN_UPDATES) |
| | 48 | | { |
| 0 | 49 | | checkerSceneLimitsOptimizationCounter = 0; |
| 0 | 50 | | CheckPublishConditions(); |
| 0 | 51 | | } |
| | 52 | | else |
| | 53 | | { |
| 2 | 54 | | checkerSceneLimitsOptimizationCounter++; |
| | 55 | | } |
| 2 | 56 | | } |
| | 57 | |
|
| | 58 | | public override void Dispose() |
| | 59 | | { |
| 29 | 60 | | base.Dispose(); |
| | 61 | |
|
| 29 | 62 | | if (HUDController.i.builderInWorldMainHud != null) |
| | 63 | | { |
| 25 | 64 | | HUDController.i.builderInWorldMainHud.OnPublishAction -= StartPublishFlow; |
| 25 | 65 | | HUDController.i.builderInWorldMainHud.OnConfirmPublishAction -= StartPublishScene; |
| | 66 | | } |
| | 67 | |
|
| 29 | 68 | | if (builderInWorldBridge != null) |
| 25 | 69 | | builderInWorldBridge.OnPublishEnd -= PublishEnd; |
| 29 | 70 | | } |
| | 71 | |
|
| | 72 | | public bool CanPublish() |
| | 73 | | { |
| 3 | 74 | | if (creatorController.IsAnyErrorOnEntities()) |
| 0 | 75 | | return false; |
| | 76 | |
|
| 3 | 77 | | if (!sceneToEdit.metricsCounter.IsInsideTheLimits()) |
| 1 | 78 | | return false; |
| | 79 | |
|
| 2 | 80 | | if (!entityHandler.AreAllEntitiesInsideBoundaries()) |
| 1 | 81 | | return false; |
| | 82 | |
|
| 1 | 83 | | return true; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// This function will check if you are able to publish the scene to the content server. If no error are present, an |
| | 88 | | /// </summary> |
| | 89 | | /// <returns>A message the with the reason telling you why you can't publish. If you can publish an empty message wi |
| | 90 | | public string CheckPublishConditions() |
| | 91 | | { |
| 1 | 92 | | string feedbackMessage = ""; |
| 1 | 93 | | if (creatorController.IsAnyErrorOnEntities()) |
| | 94 | | { |
| 0 | 95 | | feedbackMessage = FEEDBACK_MESSAGE_ENTITY_ERROR; |
| 0 | 96 | | } |
| 1 | 97 | | else if (!entityHandler.AreAllEntitiesInsideBoundaries()) |
| | 98 | | { |
| 0 | 99 | | feedbackMessage = FEEDBACK_MESSAGE_OUTSIDE_BOUNDARIES; |
| 0 | 100 | | } |
| 1 | 101 | | else if (!sceneToEdit.metricsCounter.IsInsideTheLimits()) |
| | 102 | | { |
| 0 | 103 | | feedbackMessage = FEEDBACK_MESSAGE_TOO_MANY_ENTITIES; |
| | 104 | | } |
| | 105 | |
|
| 1 | 106 | | if (HUDController.i.builderInWorldMainHud != null) |
| 0 | 107 | | HUDController.i.builderInWorldMainHud.SetPublishBtnAvailability(CanPublish(), feedbackMessage); |
| | 108 | |
|
| 1 | 109 | | return feedbackMessage; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private void StartPublishFlow() |
| | 113 | | { |
| 0 | 114 | | if (!CanPublish()) |
| 0 | 115 | | return; |
| | 116 | |
|
| 0 | 117 | | HUDController.i.builderInWorldMainHud.PublishStart(); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private void StartPublishScene(string sceneName, string sceneDescription, string sceneScreenshot) |
| | 121 | | { |
| 0 | 122 | | startPublishingTimestamp = Time.realtimeSinceStartup; |
| 0 | 123 | | BIWAnalytics.StartScenePublish(sceneToEdit.metricsCounter.GetModel()); |
| 0 | 124 | | builderInWorldBridge.PublishScene(sceneToEdit, sceneName, sceneDescription, sceneScreenshot); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | private void PublishEnd(bool isOk, string message) |
| | 128 | | { |
| 0 | 129 | | if (HUDController.i.builderInWorldMainHud != null) |
| 0 | 130 | | HUDController.i.builderInWorldMainHud.PublishEnd(isOk, message); |
| 0 | 131 | | string successString = isOk ? "Success" : message; |
| 0 | 132 | | BIWAnalytics.EndScenePublish(sceneToEdit.metricsCounter.GetModel(), successString, Time.realtimeSinceStartup - s |
| 0 | 133 | | } |
| | 134 | | } |