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