| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using DCL.Controllers.ParcelSceneDebug; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using MainScripts.DCL.WorldRuntime; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.Assertions; |
| | 11 | |
|
| | 12 | | namespace DCL.Controllers |
| | 13 | | { |
| | 14 | | public class ParcelScene : MonoBehaviour, IParcelScene, ISceneMessageProcessor |
| | 15 | | { |
| 508 | 16 | | public Dictionary<string, IDCLEntity> entities { get; private set; } = new Dictionary<string, IDCLEntity>(); |
| 850 | 17 | | public Dictionary<string, ISharedComponent> disposableComponents { get; private set; } = new Dictionary<string, |
| 2401 | 18 | | public LoadParcelScenesMessage.UnityParcelScene sceneData { get; protected set; } |
| | 19 | |
|
| 503 | 20 | | public HashSet<Vector2Int> parcels = new HashSet<Vector2Int>(); |
| 14 | 21 | | public ISceneMetricsCounter metricsCounter { get; set; } |
| | 22 | | public event System.Action<IDCLEntity> OnEntityAdded; |
| | 23 | | public event System.Action<IDCLEntity> OnEntityRemoved; |
| | 24 | | public event System.Action<IComponent> OnComponentAdded; |
| | 25 | | public event System.Action<IComponent> OnComponentRemoved; |
| | 26 | | public event System.Action OnChanged; |
| | 27 | | public event System.Action<LoadParcelScenesMessage.UnityParcelScene> OnSetData; |
| | 28 | | public event System.Action<string, ISharedComponent> OnAddSharedComponent; |
| | 29 | | public event System.Action<float> OnLoadingStateUpdated; |
| | 30 | |
|
| 144 | 31 | | public ContentProvider contentProvider { get; set; } |
| | 32 | |
|
| 0 | 33 | | public bool isTestScene { get; set; } = false; |
| 971 | 34 | | public bool isPersistent { get; set; } = false; |
| 0 | 35 | | public float loadingProgress { get; private set; } |
| | 36 | |
|
| | 37 | | [System.NonSerialized] |
| | 38 | | public string sceneName; |
| | 39 | |
|
| | 40 | | [System.NonSerialized] |
| 503 | 41 | | public bool unloadWithDistance = true; |
| | 42 | |
|
| | 43 | | SceneDebugPlane sceneDebugPlane = null; |
| | 44 | |
|
| | 45 | | public SceneLifecycleHandler sceneLifecycleHandler; |
| | 46 | |
|
| 0 | 47 | | public bool isReleased { get; private set; } |
| | 48 | |
|
| | 49 | | public void Awake() |
| | 50 | | { |
| 503 | 51 | | CommonScriptableObjects.worldOffset.OnChange += OnWorldReposition; |
| 503 | 52 | | sceneLifecycleHandler = new SceneLifecycleHandler(this); |
| 503 | 53 | | metricsCounter = new SceneMetricsCounter(DataStore.i.sceneWorldObjects); |
| 503 | 54 | | } |
| | 55 | |
|
| | 56 | | private void OnDestroy() |
| | 57 | | { |
| 503 | 58 | | CommonScriptableObjects.worldOffset.OnChange -= OnWorldReposition; |
| 503 | 59 | | metricsCounter?.Dispose(); |
| 503 | 60 | | } |
| | 61 | |
|
| 1006 | 62 | | void OnDisable() { metricsCounter?.Disable(); } |
| | 63 | |
|
| | 64 | | private void Update() |
| | 65 | | { |
| 6733 | 66 | | if (sceneLifecycleHandler.state == SceneLifecycleHandler.State.READY |
| | 67 | | && CommonScriptableObjects.rendererState.Get()) |
| 6693 | 68 | | SendMetricsEvent(); |
| 6733 | 69 | | } |
| | 70 | |
|
| 968 | 71 | | protected virtual string prettyName => sceneData.basePosition.ToString(); |
| | 72 | |
|
| | 73 | | public virtual void SetData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 74 | | { |
| 500 | 75 | | Assert.IsTrue( !string.IsNullOrEmpty(data.id), "Scene must have an ID!" ); |
| | 76 | |
|
| 500 | 77 | | this.sceneData = data; |
| | 78 | |
|
| 500 | 79 | | contentProvider = new ContentProvider(); |
| 500 | 80 | | contentProvider.baseUrl = data.baseUrl; |
| 500 | 81 | | contentProvider.contents = data.contents; |
| 500 | 82 | | contentProvider.BakeHashes(); |
| | 83 | |
|
| 500 | 84 | | parcels.Clear(); |
| | 85 | |
|
| 2032 | 86 | | for (int i = 0; i < sceneData.parcels.Length; i++) |
| | 87 | | { |
| 516 | 88 | | parcels.Add(sceneData.parcels[i]); |
| | 89 | | } |
| | 90 | |
|
| 500 | 91 | | if (DCLCharacterController.i != null) |
| | 92 | | { |
| 392 | 93 | | gameObject.transform.position = PositionUtils.WorldToUnityPosition(Utils.GridToWorldPosition(data.basePo |
| | 94 | | } |
| | 95 | |
|
| 500 | 96 | | DataStore.i.sceneWorldObjects.AddScene(sceneData.id); |
| | 97 | |
|
| 500 | 98 | | metricsCounter.Configure(sceneData.id, sceneData.basePosition, sceneData.parcels.Length); |
| 500 | 99 | | metricsCounter.Enable(); |
| | 100 | |
|
| 500 | 101 | | OnSetData?.Invoke(data); |
| 500 | 102 | | } |
| | 103 | |
|
| | 104 | | void OnWorldReposition(Vector3 current, Vector3 previous) |
| | 105 | | { |
| 2 | 106 | | Vector3 sceneWorldPos = Utils.GridToWorldPosition(sceneData.basePosition.x, sceneData.basePosition.y); |
| 2 | 107 | | gameObject.transform.position = PositionUtils.WorldToUnityPosition(sceneWorldPos); |
| 2 | 108 | | } |
| | 109 | |
|
| | 110 | | public virtual void SetUpdateData(LoadParcelScenesMessage.UnityParcelScene data) |
| | 111 | | { |
| 15 | 112 | | contentProvider = new ContentProvider(); |
| 15 | 113 | | contentProvider.baseUrl = data.baseUrl; |
| 15 | 114 | | contentProvider.contents = data.contents; |
| 15 | 115 | | contentProvider.BakeHashes(); |
| 15 | 116 | | } |
| | 117 | |
|
| | 118 | | public void InitializeDebugPlane() |
| | 119 | | { |
| 388 | 120 | | if (EnvironmentSettings.DEBUG && sceneData.parcels != null && sceneDebugPlane == null) |
| | 121 | | { |
| 386 | 122 | | sceneDebugPlane = new SceneDebugPlane(sceneData, gameObject.transform); |
| | 123 | | } |
| 388 | 124 | | } |
| | 125 | |
|
| | 126 | | public void RemoveDebugPlane() |
| | 127 | | { |
| 0 | 128 | | if (sceneDebugPlane != null) |
| | 129 | | { |
| 0 | 130 | | sceneDebugPlane.Dispose(); |
| 0 | 131 | | sceneDebugPlane = null; |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | public void Cleanup(bool immediate) |
| | 136 | | { |
| 428 | 137 | | if (isReleased) |
| 0 | 138 | | return; |
| | 139 | |
|
| 428 | 140 | | if (sceneDebugPlane != null) |
| | 141 | | { |
| 377 | 142 | | sceneDebugPlane.Dispose(); |
| 377 | 143 | | sceneDebugPlane = null; |
| | 144 | | } |
| | 145 | |
|
| 428 | 146 | | DisposeAllSceneComponents(); |
| | 147 | |
|
| 428 | 148 | | if (immediate) //!CommonScriptableObjects.rendererState.Get()) |
| | 149 | | { |
| 24 | 150 | | RemoveAllEntitiesImmediate(); |
| 24 | 151 | | PoolManager.i.Cleanup(true, true); |
| 24 | 152 | | DataStore.i.sceneWorldObjects.RemoveScene(sceneData.id); |
| 24 | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| 404 | 156 | | if (entities.Count > 0) |
| | 157 | | { |
| 208 | 158 | | this.gameObject.transform.position = EnvironmentSettings.MORDOR; |
| 208 | 159 | | this.gameObject.SetActive(false); |
| | 160 | |
|
| 208 | 161 | | RemoveAllEntities(); |
| 208 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 196 | 165 | | Destroy(this.gameObject); |
| 196 | 166 | | DataStore.i.sceneWorldObjects.RemoveScene(sceneData.id); |
| | 167 | | } |
| | 168 | | } |
| | 169 | |
|
| 428 | 170 | | isReleased = true; |
| 428 | 171 | | } |
| | 172 | |
|
| 0 | 173 | | public override string ToString() { return "Parcel Scene: " + base.ToString() + "\n" + sceneData; } |
| | 174 | |
|
| | 175 | | public string GetSceneName() |
| | 176 | | { |
| 0 | 177 | | return string.IsNullOrEmpty(sceneName) ? "Unnamed" : sceneName; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | public bool IsInsideSceneBoundaries(Bounds objectBounds) |
| | 181 | | { |
| 157 | 182 | | if (!IsInsideSceneBoundaries(objectBounds.min + CommonScriptableObjects.worldOffset, objectBounds.max.y)) |
| 116 | 183 | | return false; |
| 41 | 184 | | if (!IsInsideSceneBoundaries(objectBounds.max + CommonScriptableObjects.worldOffset, objectBounds.max.y)) |
| 14 | 185 | | return false; |
| | 186 | |
|
| 27 | 187 | | return true; |
| | 188 | | } |
| | 189 | |
|
| | 190 | | public virtual bool IsInsideSceneBoundaries(Vector2Int gridPosition, float height = 0f) |
| | 191 | | { |
| 84 | 192 | | if (parcels.Count == 0) |
| 0 | 193 | | return false; |
| | 194 | |
|
| 84 | 195 | | float heightLimit = metricsCounter.maxCount.sceneHeight; |
| | 196 | |
|
| 84 | 197 | | if (height > heightLimit) |
| 0 | 198 | | return false; |
| | 199 | |
|
| 84 | 200 | | return parcels.Contains(gridPosition); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | public virtual bool IsInsideSceneBoundaries(Vector3 worldPosition, float height = 0f) |
| | 204 | | { |
| 237 | 205 | | if (parcels.Count == 0) |
| 0 | 206 | | return false; |
| | 207 | |
|
| 237 | 208 | | float heightLimit = metricsCounter.maxCount.sceneHeight; |
| 237 | 209 | | if (height > heightLimit) |
| 24 | 210 | | return false; |
| | 211 | |
|
| 213 | 212 | | int noThresholdZCoordinate = Mathf.FloorToInt(worldPosition.z / ParcelSettings.PARCEL_SIZE); |
| 213 | 213 | | int noThresholdXCoordinate = Mathf.FloorToInt(worldPosition.x / ParcelSettings.PARCEL_SIZE); |
| | 214 | |
|
| | 215 | | // We check the target world position |
| 213 | 216 | | Vector2Int targetCoordinate = new Vector2Int(noThresholdXCoordinate, noThresholdZCoordinate); |
| 213 | 217 | | if (parcels.Contains(targetCoordinate)) |
| 97 | 218 | | return true; |
| | 219 | |
|
| | 220 | | // We need to check using a threshold from the target point, in order to cover correctly the parcel "border/ |
| 116 | 221 | | Vector2Int coordinateMin = new Vector2Int(); |
| 116 | 222 | | coordinateMin.x = Mathf.FloorToInt((worldPosition.x - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| 116 | 223 | | coordinateMin.y = Mathf.FloorToInt((worldPosition.z - ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| | 224 | |
|
| 116 | 225 | | Vector2Int coordinateMax = new Vector2Int(); |
| 116 | 226 | | coordinateMax.x = Mathf.FloorToInt((worldPosition.x + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| 116 | 227 | | coordinateMax.y = Mathf.FloorToInt((worldPosition.z + ParcelSettings.PARCEL_BOUNDARIES_THRESHOLD) / ParcelSe |
| | 228 | |
|
| | 229 | | // We check the east/north-threshold position |
| 116 | 230 | | targetCoordinate.Set(coordinateMax.x, coordinateMax.y); |
| 116 | 231 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 232 | | return true; |
| | 233 | |
|
| | 234 | | // We check the east/south-threshold position |
| 116 | 235 | | targetCoordinate.Set(coordinateMax.x, coordinateMin.y); |
| 116 | 236 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 237 | | return true; |
| | 238 | |
|
| | 239 | | // We check the west/north-threshold position |
| 116 | 240 | | targetCoordinate.Set(coordinateMin.x, coordinateMax.y); |
| 116 | 241 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 242 | | return true; |
| | 243 | |
|
| | 244 | | // We check the west/south-threshold position |
| 116 | 245 | | targetCoordinate.Set(coordinateMin.x, coordinateMin.y); |
| 116 | 246 | | if (parcels.Contains(targetCoordinate)) |
| 0 | 247 | | return true; |
| | 248 | |
|
| 116 | 249 | | return false; |
| | 250 | | } |
| | 251 | |
|
| 53 | 252 | | public Transform GetSceneTransform() { return transform; } |
| | 253 | |
|
| | 254 | | public IDCLEntity CreateEntity(string id) |
| | 255 | | { |
| 584 | 256 | | if (entities.ContainsKey(id)) |
| | 257 | | { |
| 1 | 258 | | return entities[id]; |
| | 259 | | } |
| | 260 | |
|
| 583 | 261 | | var newEntity = new DecentralandEntity(); |
| 583 | 262 | | newEntity.entityId = id; |
| | 263 | |
|
| 583 | 264 | | PoolManagerFactory.EnsureEntityPool(false); |
| | 265 | |
|
| | 266 | | // As we know that the pool already exists, we just get one gameobject from it |
| 583 | 267 | | PoolableObject po = PoolManager.i.Get(PoolManagerFactory.EMPTY_GO_POOL_NAME); |
| | 268 | |
|
| 583 | 269 | | newEntity.meshesInfo.innerGameObject = po.gameObject; |
| 583 | 270 | | newEntity.gameObject = po.gameObject; |
| | 271 | |
|
| | 272 | | #if UNITY_EDITOR |
| 583 | 273 | | newEntity.gameObject.name = "ENTITY_" + id; |
| | 274 | | #endif |
| 583 | 275 | | newEntity.gameObject.transform.SetParent(gameObject.transform, false); |
| 583 | 276 | | newEntity.gameObject.SetActive(true); |
| 583 | 277 | | newEntity.scene = this; |
| | 278 | |
|
| 583 | 279 | | newEntity.OnCleanupEvent += po.OnCleanup; |
| | 280 | |
|
| 583 | 281 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| 432 | 282 | | newEntity.OnShapeUpdated += Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; |
| | 283 | |
|
| 583 | 284 | | entities.Add(id, newEntity); |
| | 285 | |
|
| 583 | 286 | | DataStore.i.sceneWorldObjects.sceneData[sceneData.id].owners.Add(id); |
| | 287 | |
|
| 583 | 288 | | OnEntityAdded?.Invoke(newEntity); |
| | 289 | |
|
| 583 | 290 | | return newEntity; |
| | 291 | | } |
| | 292 | |
|
| | 293 | | public void RemoveEntity(string id, bool removeImmediatelyFromEntitiesList = true) |
| | 294 | | { |
| 69 | 295 | | if (entities.ContainsKey(id)) |
| | 296 | | { |
| 69 | 297 | | IDCLEntity entity = entities[id]; |
| | 298 | |
|
| 69 | 299 | | if (!entity.markedForCleanup) |
| | 300 | | { |
| | 301 | | // This will also cleanup its children |
| 69 | 302 | | CleanUpEntityRecursively(entity, removeImmediatelyFromEntitiesList); |
| | 303 | | } |
| | 304 | |
|
| 69 | 305 | | entities.Remove(id); |
| | 306 | |
|
| 69 | 307 | | var data = DataStore.i.sceneWorldObjects.sceneData; |
| | 308 | |
|
| 69 | 309 | | if (data.ContainsKey(sceneData.id)) |
| | 310 | | { |
| 69 | 311 | | data[sceneData.id].owners.Remove(id); |
| | 312 | | } |
| 69 | 313 | | } |
| | 314 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| | 315 | | else |
| | 316 | | { |
| 0 | 317 | | Debug.LogWarning($"Couldn't remove entity with ID: {id} as it doesn't exist."); |
| | 318 | | } |
| | 319 | | #endif |
| 0 | 320 | | } |
| | 321 | |
|
| | 322 | | void CleanUpEntityRecursively(IDCLEntity entity, bool removeImmediatelyFromEntitiesList) |
| | 323 | | { |
| | 324 | | // Iterate through all entity children |
| 70 | 325 | | using (var iterator = entity.children.GetEnumerator()) |
| | 326 | | { |
| 71 | 327 | | while (iterator.MoveNext()) |
| | 328 | | { |
| 1 | 329 | | CleanUpEntityRecursively(iterator.Current.Value, removeImmediatelyFromEntitiesList); |
| | 330 | | } |
| 70 | 331 | | } |
| | 332 | |
|
| 70 | 333 | | OnEntityRemoved?.Invoke(entity); |
| | 334 | |
|
| 70 | 335 | | if (Environment.i.world.sceneBoundsChecker.enabled) |
| | 336 | | { |
| 29 | 337 | | entity.OnShapeUpdated -= Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; |
| 29 | 338 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(entity); |
| | 339 | | } |
| | 340 | |
|
| 70 | 341 | | if (removeImmediatelyFromEntitiesList) |
| | 342 | | { |
| | 343 | | // Every entity ends up being removed through here |
| 69 | 344 | | entity.Cleanup(); |
| 69 | 345 | | entities.Remove(entity.entityId); |
| 69 | 346 | | } |
| | 347 | | else |
| | 348 | | { |
| 1 | 349 | | Environment.i.platform.parcelScenesCleaner.MarkForCleanup(entity); |
| | 350 | | } |
| 1 | 351 | | } |
| | 352 | |
|
| | 353 | | void RemoveAllEntities(bool instant = false) |
| | 354 | | { |
| | 355 | | //NOTE(Brian): We need to remove only the rootEntities. |
| | 356 | | // If we don't, duplicated entities will get removed when destroying |
| | 357 | | // recursively, making this more complicated than it should. |
| 232 | 358 | | List<IDCLEntity> rootEntities = new List<IDCLEntity>(); |
| | 359 | |
|
| 232 | 360 | | using (var iterator = entities.GetEnumerator()) |
| | 361 | | { |
| 718 | 362 | | while (iterator.MoveNext()) |
| | 363 | | { |
| 486 | 364 | | if (iterator.Current.Value.parent == null) |
| | 365 | | { |
| 479 | 366 | | if (instant) |
| 34 | 367 | | rootEntities.Add(iterator.Current.Value); |
| | 368 | | else |
| 445 | 369 | | Environment.i.platform.parcelScenesCleaner.MarkRootEntityForCleanup(this, iterator.Current.V |
| | 370 | | } |
| | 371 | | } |
| 232 | 372 | | } |
| | 373 | |
|
| 232 | 374 | | if (instant) |
| | 375 | | { |
| 24 | 376 | | int rootEntitiesCount = rootEntities.Count; |
| 116 | 377 | | for (int i = 0; i < rootEntitiesCount; i++) |
| | 378 | | { |
| 34 | 379 | | IDCLEntity entity = rootEntities[i]; |
| 34 | 380 | | RemoveEntity(entity.entityId, instant); |
| | 381 | | } |
| | 382 | |
|
| 24 | 383 | | entities.Clear(); |
| | 384 | |
|
| 24 | 385 | | Destroy(this.gameObject); |
| | 386 | | } |
| 232 | 387 | | } |
| | 388 | |
|
| 48 | 389 | | private void RemoveAllEntitiesImmediate() { RemoveAllEntities(instant: true); } |
| | 390 | |
|
| | 391 | | public void SetEntityParent(string entityId, string parentId) |
| | 392 | | { |
| 15 | 393 | | if (entityId == parentId) |
| | 394 | | { |
| 0 | 395 | | return; |
| | 396 | | } |
| | 397 | |
|
| 15 | 398 | | IDCLEntity me = GetEntityForUpdate(entityId); |
| | 399 | |
|
| 15 | 400 | | if (me == null) |
| 0 | 401 | | return; |
| | 402 | |
|
| 15 | 403 | | Environment.i.platform.cullingController.MarkDirty(); |
| 15 | 404 | | Environment.i.platform.physicsSyncController.MarkDirty(); |
| | 405 | |
|
| 15 | 406 | | if ( DCLCharacterController.i != null ) |
| | 407 | | { |
| 13 | 408 | | if (parentId == "FirstPersonCameraEntityReference" || parentId == "PlayerEntityReference") // PlayerEnti |
| | 409 | | { |
| | 410 | | // In this case, the entity will attached to the first person camera |
| | 411 | | // On first person mode, the entity will rotate with the camera. On third person mode, the entity wi |
| 1 | 412 | | me.SetParent(DCLCharacterController.i.firstPersonCameraReference); |
| 1 | 413 | | Environment.i.world.sceneBoundsChecker.AddPersistent(me); |
| 1 | 414 | | return; |
| | 415 | | } |
| | 416 | |
|
| 12 | 417 | | if (parentId == "AvatarEntityReference" || parentId == "AvatarPositionEntityReference") // AvatarPositio |
| | 418 | | { |
| | 419 | | // In this case, the entity will be attached to the avatar |
| | 420 | | // It will simply rotate with the avatar, regardless of where the camera is pointing |
| 1 | 421 | | me.SetParent(DCLCharacterController.i.avatarReference); |
| 1 | 422 | | Environment.i.world.sceneBoundsChecker.AddPersistent(me); |
| 1 | 423 | | return; |
| | 424 | | } |
| | 425 | |
|
| 11 | 426 | | if (me.parent == DCLCharacterController.i.firstPersonCameraReference || me.parent == DCLCharacterControl |
| | 427 | | { |
| 1 | 428 | | Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(me); |
| | 429 | | } |
| | 430 | | } |
| | 431 | |
|
| 13 | 432 | | if (parentId == "0") |
| | 433 | | { |
| | 434 | | // The entity will be child of the scene directly |
| 2 | 435 | | me.SetParent(null); |
| 2 | 436 | | me.gameObject.transform.SetParent(gameObject.transform, false); |
| 2 | 437 | | } |
| | 438 | | else |
| | 439 | | { |
| 11 | 440 | | IDCLEntity myParent = GetEntityForUpdate(parentId); |
| | 441 | |
|
| 11 | 442 | | if (myParent != null) |
| | 443 | | { |
| 11 | 444 | | me.SetParent(myParent); |
| | 445 | | } |
| | 446 | | } |
| 11 | 447 | | } |
| | 448 | |
|
| | 449 | | /** |
| | 450 | | * This method is called when we need to attach a disposable component to the entity |
| | 451 | | */ |
| | 452 | | public void SharedComponentAttach(string entityId, string componentId) |
| | 453 | | { |
| 454 | 454 | | IDCLEntity entity = GetEntityForUpdate(entityId); |
| | 455 | |
|
| 454 | 456 | | if (entity == null) |
| 2 | 457 | | return; |
| | 458 | |
|
| 452 | 459 | | if (disposableComponents.TryGetValue(componentId, out ISharedComponent sharedComponent)) |
| | 460 | | { |
| 450 | 461 | | sharedComponent.AttachTo(entity); |
| | 462 | | } |
| 452 | 463 | | } |
| | 464 | |
|
| | 465 | | public IEntityComponent EntityComponentCreateOrUpdateWithModel(string entityId, CLASS_ID_COMPONENT classId, obje |
| | 466 | | { |
| 381 | 467 | | IDCLEntity entity = GetEntityForUpdate(entityId); |
| | 468 | |
|
| 381 | 469 | | if (entity == null) |
| | 470 | | { |
| 0 | 471 | | Debug.LogError($"scene '{sceneData.id}': Can't create entity component if the entity {entityId} doesn't |
| 0 | 472 | | return null; |
| | 473 | | } |
| | 474 | |
|
| 381 | 475 | | IEntityComponent newComponent = null; |
| | 476 | |
|
| 381 | 477 | | if (classId == CLASS_ID_COMPONENT.UUID_CALLBACK) |
| | 478 | | { |
| 69 | 479 | | OnPointerEvent.Model model = JsonUtility.FromJson<OnPointerEvent.Model>(data as string); |
| 69 | 480 | | classId = model.GetClassIdFromType(); |
| 69 | 481 | | } |
| | 482 | | // NOTE: TRANSFORM and AVATAR_ATTACH can't be used in the same Entity at the same time. |
| | 483 | | // so we remove AVATAR_ATTACH (if exists) when a TRANSFORM is created. |
| 312 | 484 | | else if (classId == CLASS_ID_COMPONENT.TRANSFORM |
| | 485 | | && entity.TryGetBaseComponent(CLASS_ID_COMPONENT.AVATAR_ATTACH, out IEntityComponent component)) |
| | 486 | | { |
| 0 | 487 | | component.Cleanup(); |
| 0 | 488 | | entity.components.Remove( CLASS_ID_COMPONENT.AVATAR_ATTACH ); |
| | 489 | | } |
| | 490 | |
|
| 381 | 491 | | if (!entity.components.ContainsKey(classId)) |
| | 492 | | { |
| 304 | 493 | | var factory = Environment.i.world.componentFactory; |
| 304 | 494 | | newComponent = factory.CreateComponent((int) classId) as IEntityComponent; |
| | 495 | |
|
| 304 | 496 | | if (newComponent != null) |
| | 497 | | { |
| 304 | 498 | | entity.components.Add(classId, newComponent); |
| 304 | 499 | | OnComponentAdded?.Invoke(newComponent); |
| | 500 | |
|
| 304 | 501 | | newComponent.Initialize(this, entity); |
| | 502 | |
|
| 304 | 503 | | if (data is string json) |
| | 504 | | { |
| 269 | 505 | | newComponent.UpdateFromJSON(json); |
| 269 | 506 | | } |
| | 507 | | else |
| | 508 | | { |
| 35 | 509 | | newComponent.UpdateFromModel(data as BaseModel); |
| | 510 | | } |
| | 511 | | } |
| 35 | 512 | | } |
| | 513 | | else |
| | 514 | | { |
| 77 | 515 | | newComponent = EntityComponentUpdate(entity, classId, data as string); |
| | 516 | | } |
| | 517 | |
|
| 381 | 518 | | if (newComponent != null && newComponent is IOutOfSceneBoundariesHandler) |
| 15 | 519 | | Environment.i.world.sceneBoundsChecker?.AddEntityToBeChecked(entity); |
| | 520 | |
|
| 381 | 521 | | OnChanged?.Invoke(); |
| 381 | 522 | | Environment.i.platform.physicsSyncController.MarkDirty(); |
| 381 | 523 | | Environment.i.platform.cullingController.MarkDirty(); |
| 381 | 524 | | return newComponent; |
| | 525 | | } |
| | 526 | |
|
| 346 | 527 | | public IEntityComponent EntityComponentCreateOrUpdate(string entityId, CLASS_ID_COMPONENT classId, string data) |
| | 528 | |
|
| | 529 | | // The EntityComponentUpdate() parameters differ from other similar methods because there is no EntityComponentU |
| | 530 | | public IEntityComponent EntityComponentUpdate(IDCLEntity entity, CLASS_ID_COMPONENT classId, |
| | 531 | | string componentJson) |
| | 532 | | { |
| 99 | 533 | | if (entity == null) |
| | 534 | | { |
| 0 | 535 | | Debug.LogError($"Can't update the {classId} component of a nonexistent entity!", this); |
| 0 | 536 | | return null; |
| | 537 | | } |
| | 538 | |
|
| 99 | 539 | | if (!entity.components.ContainsKey(classId)) |
| | 540 | | { |
| 0 | 541 | | Debug.LogError($"Entity {entity.entityId} doesn't have a {classId} component to update!", this); |
| 0 | 542 | | return null; |
| | 543 | | } |
| | 544 | |
|
| 99 | 545 | | IComponent targetComponent = entity.components[classId]; |
| 99 | 546 | | targetComponent.UpdateFromJSON(componentJson); |
| | 547 | |
|
| 99 | 548 | | return targetComponent as IEntityComponent; |
| | 549 | | } |
| | 550 | |
|
| | 551 | | public ISharedComponent SharedComponentCreate(string id, int classId) |
| | 552 | | { |
| 634 | 553 | | if (disposableComponents.TryGetValue(id, out ISharedComponent component)) |
| 4 | 554 | | return component; |
| | 555 | |
|
| 630 | 556 | | if (classId == (int) CLASS_ID.UI_SCREEN_SPACE_SHAPE || classId == (int) CLASS_ID.UI_FULLSCREEN_SHAPE) |
| | 557 | | { |
| 44 | 558 | | if (GetSharedComponent<UIScreenSpace>() != null) |
| 0 | 559 | | return null; |
| | 560 | | } |
| | 561 | |
|
| 630 | 562 | | var factory = Environment.i.world.componentFactory; |
| 630 | 563 | | ISharedComponent newComponent = factory.CreateComponent(classId) as ISharedComponent; |
| | 564 | |
|
| 630 | 565 | | if (newComponent == null) |
| 0 | 566 | | return null; |
| | 567 | |
|
| 630 | 568 | | disposableComponents.Add(id, newComponent); |
| 630 | 569 | | OnAddSharedComponent?.Invoke(id, newComponent); |
| | 570 | |
|
| 630 | 571 | | newComponent.Initialize(this, id); |
| | 572 | |
|
| 630 | 573 | | return newComponent; |
| | 574 | | } |
| | 575 | |
|
| | 576 | | public void SharedComponentDispose(string id) |
| | 577 | | { |
| 88 | 578 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 579 | | { |
| 88 | 580 | | sharedComponent?.Dispose(); |
| 88 | 581 | | disposableComponents.Remove(id); |
| 88 | 582 | | OnComponentRemoved?.Invoke(sharedComponent); |
| | 583 | | } |
| 0 | 584 | | } |
| | 585 | |
|
| | 586 | | public void EntityComponentRemove(string entityId, string name) |
| | 587 | | { |
| 20 | 588 | | IDCLEntity decentralandEntity = GetEntityForUpdate(entityId); |
| | 589 | |
|
| 20 | 590 | | if (decentralandEntity == null) |
| | 591 | | { |
| 0 | 592 | | return; |
| | 593 | | } |
| | 594 | |
|
| 20 | 595 | | RemoveEntityComponent(decentralandEntity, name); |
| 20 | 596 | | } |
| | 597 | |
|
| | 598 | | public T GetSharedComponent<T>() |
| | 599 | | where T : class |
| | 600 | | { |
| 433 | 601 | | return disposableComponents.Values.FirstOrDefault(x => x is T) as T; |
| | 602 | | } |
| | 603 | |
|
| | 604 | | private void RemoveComponentType<T>(IDCLEntity entity, CLASS_ID_COMPONENT classId) |
| | 605 | | where T : MonoBehaviour |
| | 606 | | { |
| 0 | 607 | | var component = entity.components[classId] as IEntityComponent; |
| | 608 | |
|
| 0 | 609 | | if (component == null) |
| 0 | 610 | | return; |
| | 611 | |
|
| 0 | 612 | | var monoBehaviour = component.GetTransform().GetComponent<T>(); |
| | 613 | |
|
| 0 | 614 | | if (monoBehaviour != null) |
| | 615 | | { |
| 0 | 616 | | Utils.SafeDestroy(monoBehaviour); |
| | 617 | | } |
| 0 | 618 | | } |
| | 619 | |
|
| | 620 | | private void RemoveEntityComponent(IDCLEntity entity, string componentName) |
| | 621 | | { |
| | 622 | | switch (componentName) |
| | 623 | | { |
| | 624 | | case "shape": |
| 0 | 625 | | if (entity.meshesInfo.currentShape is BaseShape baseShape) |
| | 626 | | { |
| 0 | 627 | | baseShape.DetachFrom(entity); |
| | 628 | | } |
| | 629 | |
|
| 0 | 630 | | return; |
| | 631 | |
|
| | 632 | | case OnClick.NAME: |
| | 633 | | { |
| 1 | 634 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_CLICK, out IEntityComponent component |
| | 635 | | { |
| 1 | 636 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 637 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_CLICK ); |
| | 638 | | } |
| | 639 | |
|
| 1 | 640 | | return; |
| | 641 | | } |
| | 642 | | case OnPointerDown.NAME: |
| | 643 | | { |
| 1 | 644 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_DOWN, out IEntityComponent component |
| | 645 | | { |
| 1 | 646 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 647 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_DOWN ); |
| | 648 | | } |
| | 649 | | } |
| 1 | 650 | | return; |
| | 651 | | case OnPointerUp.NAME: |
| | 652 | | { |
| 1 | 653 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_UP, out IEntityComponent component )) |
| | 654 | | { |
| 1 | 655 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 656 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_UP ); |
| | 657 | | } |
| | 658 | | } |
| 1 | 659 | | return; |
| | 660 | | case OnPointerHoverEnter.NAME: |
| | 661 | | { |
| 1 | 662 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_HOVER_ENTER, out IEntityComponent com |
| | 663 | | { |
| 1 | 664 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 665 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_HOVER_ENTER ); |
| | 666 | | } |
| | 667 | | } |
| 1 | 668 | | return; |
| | 669 | | case OnPointerHoverExit.NAME: |
| | 670 | | { |
| 1 | 671 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.UUID_ON_HOVER_EXIT, out IEntityComponent comp |
| | 672 | | { |
| 1 | 673 | | Utils.SafeDestroy(component.GetTransform().gameObject); |
| 1 | 674 | | entity.components.Remove( CLASS_ID_COMPONENT.UUID_ON_HOVER_EXIT ); |
| | 675 | | } |
| | 676 | | } |
| 1 | 677 | | return; |
| | 678 | | case "transform": |
| | 679 | | { |
| 0 | 680 | | if ( entity.TryGetBaseComponent(CLASS_ID_COMPONENT.AVATAR_ATTACH, out IEntityComponent component |
| | 681 | | { |
| 0 | 682 | | component.Cleanup(); |
| 0 | 683 | | entity.components.Remove( CLASS_ID_COMPONENT.AVATAR_ATTACH ); |
| | 684 | | } |
| | 685 | | } |
| 0 | 686 | | return; |
| | 687 | | } |
| 0 | 688 | | } |
| | 689 | |
|
| | 690 | | public ISharedComponent SharedComponentUpdate(string id, BaseModel model) |
| | 691 | | { |
| 27 | 692 | | if (disposableComponents.TryGetValue(id, out ISharedComponent sharedComponent)) |
| | 693 | | { |
| 27 | 694 | | sharedComponent.UpdateFromModel(model); |
| 27 | 695 | | return sharedComponent; |
| | 696 | | } |
| | 697 | |
|
| 0 | 698 | | if (gameObject == null) |
| | 699 | | { |
| 0 | 700 | | Debug.LogError($"Unknown disposableComponent {id} -- scene has been destroyed?"); |
| 0 | 701 | | } |
| | 702 | | else |
| | 703 | | { |
| 0 | 704 | | Debug.LogError($"Unknown disposableComponent {id}", gameObject); |
| | 705 | | } |
| | 706 | |
|
| 0 | 707 | | return null; |
| | 708 | | } |
| | 709 | |
|
| | 710 | | public ISharedComponent SharedComponentUpdate(string id, string json) |
| | 711 | | { |
| 673 | 712 | | if (disposableComponents.TryGetValue(id, out ISharedComponent disposableComponent)) |
| | 713 | | { |
| 673 | 714 | | disposableComponent.UpdateFromJSON(json); |
| 673 | 715 | | return disposableComponent; |
| | 716 | | } |
| | 717 | |
|
| 0 | 718 | | if (gameObject == null) |
| | 719 | | { |
| 0 | 720 | | Debug.LogError($"Unknown disposableComponent {id} -- scene has been destroyed?"); |
| 0 | 721 | | } |
| | 722 | | else |
| | 723 | | { |
| 0 | 724 | | Debug.LogError($"Unknown disposableComponent {id}", gameObject); |
| | 725 | | } |
| | 726 | |
|
| 0 | 727 | | return null; |
| | 728 | | } |
| | 729 | |
|
| | 730 | | protected virtual void SendMetricsEvent() |
| | 731 | | { |
| 6693 | 732 | | if (Time.frameCount % 10 == 0) |
| 669 | 733 | | metricsCounter.SendEvent(); |
| 6693 | 734 | | } |
| | 735 | |
|
| | 736 | | public ISharedComponent GetSharedComponent(string componentId) |
| | 737 | | { |
| 571 | 738 | | if (!disposableComponents.TryGetValue(componentId, out ISharedComponent result)) |
| | 739 | | { |
| 526 | 740 | | return null; |
| | 741 | | } |
| | 742 | |
|
| 45 | 743 | | return result; |
| | 744 | | } |
| | 745 | |
|
| | 746 | | private IDCLEntity GetEntityForUpdate(string entityId) |
| | 747 | | { |
| 881 | 748 | | if (string.IsNullOrEmpty(entityId)) |
| | 749 | | { |
| 0 | 750 | | Debug.LogError("Null or empty entityId"); |
| 0 | 751 | | return null; |
| | 752 | | } |
| | 753 | |
|
| 881 | 754 | | if (!entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 755 | | { |
| 2 | 756 | | return null; |
| | 757 | | } |
| | 758 | |
|
| | 759 | | //NOTE(Brian): This is for removing stray null references? This should never happen. |
| | 760 | | // Maybe move to a different 'clean-up' method to make this method have a single responsibility? |
| 879 | 761 | | if (entity == null || entity.gameObject == null) |
| | 762 | | { |
| 0 | 763 | | entities.Remove(entityId); |
| 0 | 764 | | return null; |
| | 765 | | } |
| | 766 | |
|
| 879 | 767 | | return entity; |
| | 768 | | } |
| | 769 | |
|
| | 770 | | private void DisposeAllSceneComponents() |
| | 771 | | { |
| 923 | 772 | | List<string> allDisposableComponents = disposableComponents.Select(x => x.Key).ToList(); |
| 1846 | 773 | | foreach (string id in allDisposableComponents) |
| | 774 | | { |
| 495 | 775 | | Environment.i.platform.parcelScenesCleaner.MarkDisposableComponentForCleanup(this, id); |
| | 776 | | } |
| 428 | 777 | | } |
| | 778 | |
|
| | 779 | | public string GetStateString() |
| | 780 | | { |
| 968 | 781 | | string baseState = isPersistent ? "global-scene" : "scene"; |
| 968 | 782 | | switch (sceneLifecycleHandler.state) |
| | 783 | | { |
| | 784 | | case SceneLifecycleHandler.State.NOT_READY: |
| 0 | 785 | | return $"{baseState}:{prettyName} - not ready..."; |
| | 786 | | case SceneLifecycleHandler.State.WAITING_FOR_INIT_MESSAGES: |
| 500 | 787 | | return $"{baseState}:{prettyName} - waiting for init messages..."; |
| | 788 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| 0 | 789 | | if (disposableComponents != null && disposableComponents.Count > 0) |
| 0 | 790 | | return $"{baseState}:{prettyName} - left to ready:{disposableComponents.Count - sceneLifecycleHa |
| | 791 | | else |
| 0 | 792 | | return $"{baseState}:{prettyName} - no components. waiting..."; |
| | 793 | | case SceneLifecycleHandler.State.READY: |
| 468 | 794 | | return $"{baseState}:{prettyName} - ready!"; |
| | 795 | | } |
| | 796 | |
|
| 0 | 797 | | return $"scene:{prettyName} - no state?"; |
| | 798 | | } |
| | 799 | |
|
| | 800 | | public void RefreshLoadingState() |
| | 801 | | { |
| | 802 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 968 | 803 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 804 | | return; |
| | 805 | | #endif |
| | 806 | |
|
| 968 | 807 | | CalculateSceneLoadingState(); |
| | 808 | |
|
| | 809 | | #if UNITY_EDITOR |
| 968 | 810 | | gameObject.name = GetStateString(); |
| | 811 | | #endif |
| 968 | 812 | | } |
| | 813 | |
|
| | 814 | | [ContextMenu("Get Waiting Components Debug Info")] |
| | 815 | | public void GetWaitingComponentsDebugInfo() |
| | 816 | | { |
| 0 | 817 | | switch (sceneLifecycleHandler.state) |
| | 818 | | { |
| | 819 | | case SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS: |
| | 820 | |
|
| 0 | 821 | | foreach (string componentId in sceneLifecycleHandler.disposableNotReady) |
| | 822 | | { |
| 0 | 823 | | if (disposableComponents.ContainsKey(componentId)) |
| | 824 | | { |
| 0 | 825 | | var component = disposableComponents[componentId]; |
| | 826 | |
|
| 0 | 827 | | Debug.Log($"Waiting for: {component.ToString()}"); |
| | 828 | |
|
| 0 | 829 | | foreach (var entity in component.GetAttachedEntities()) |
| | 830 | | { |
| 0 | 831 | | var loader = LoadableShape.GetLoaderForEntity(entity); |
| | 832 | |
|
| 0 | 833 | | string loadInfo = "No loader"; |
| | 834 | |
|
| 0 | 835 | | if (loader != null) |
| | 836 | | { |
| 0 | 837 | | loadInfo = loader.ToString(); |
| | 838 | | } |
| | 839 | |
|
| 0 | 840 | | Debug.Log($"This shape is attached to {entity.entityId} entity. Click here for highlight |
| | 841 | | } |
| | 842 | | } |
| | 843 | | else |
| | 844 | | { |
| 0 | 845 | | Debug.Log($"Waiting for missing component? id: {componentId}"); |
| | 846 | | } |
| | 847 | | } |
| | 848 | |
|
| | 849 | | break; |
| | 850 | |
|
| | 851 | | default: |
| 0 | 852 | | Debug.Log("This scene is not waiting for any components. Its current state is " + sceneLifecycleHand |
| | 853 | | break; |
| | 854 | | } |
| 0 | 855 | | } |
| | 856 | |
|
| | 857 | | /// <summary> |
| | 858 | | /// Calculates the current loading progress of the scene and raise the event OnLoadingStateUpdated with the perc |
| | 859 | | /// </summary> |
| | 860 | | public void CalculateSceneLoadingState() |
| | 861 | | { |
| 968 | 862 | | loadingProgress = 0f; |
| | 863 | |
|
| 968 | 864 | | if (sceneLifecycleHandler.state == SceneLifecycleHandler.State.WAITING_FOR_COMPONENTS || |
| | 865 | | sceneLifecycleHandler.state == SceneLifecycleHandler.State.READY) |
| | 866 | | { |
| 468 | 867 | | loadingProgress = disposableComponents != null && disposableComponents.Count > 0 ? (disposableComponents |
| | 868 | | } |
| | 869 | |
|
| 968 | 870 | | OnLoadingStateUpdated?.Invoke(loadingProgress); |
| 0 | 871 | | } |
| | 872 | | } |
| | 873 | | } |