Esempio n. 1
0
        void InitializeIncrementalConversion()
        {
            var settings = new GameObjectConversionSettings(DestinationWorld, ConversionFlags)
            {
                Systems = TestWorldSetup.GetDefaultInitSystemsFromEntitiesPackage(WorldSystemFilterFlags.GameObjectConversion).ToList()
            };

            ConversionWorld = GameObjectConversionUtility.InitializeIncrementalConversion(SceneManager.GetActiveScene(), settings);
        }
        void InitializeIncrementalConversion()
        {
            // TODO - if missing these conversion flags, it will cause an exception in ConvertIncrementalInitialize
            // Need better exception
            var settings = new GameObjectConversionSettings(DestinationWorld, ConversionFlags)
            {
                Systems = TestWorldSetup.GetDefaultInitSystemsFromEntitiesPackage(WorldSystemFilterFlags.GameObjectConversion).ToList()
            };

            settings.Systems.Add(typeof(InterceptConvertedGameObjects));
            ConversionWorld = GameObjectConversionUtility.InitializeIncrementalConversion(SceneManager.GetActiveScene(), settings);
        }
        public void IncrementalConversionLinkedGroup()
        {
            var conversionFlags = GameObjectConversionUtility.ConversionFlags.GameViewLiveLink | GameObjectConversionUtility.ConversionFlags.AssignName;
            // Parent (LinkedEntityGroup) (2 additional entities)
            // - Child (2 additional entities)
            // All reference parent game object

            var parent = CreateGameObject().AddComponent <EntityRefTestDataAuthoring>();
            var child  = CreateGameObject().AddComponent <EntityRefTestDataAuthoring>();

            child.transform.parent = parent.transform;

            child.name = "child";
            child.AdditionalEntityCount    = 2;
            child.DeclareLinkedEntityGroup = false;
            child.Value = parent.gameObject;

            parent.name = "parent";
            parent.AdditionalEntityCount    = 2;
            parent.DeclareLinkedEntityGroup = true;
            parent.Value = parent.gameObject;

            var settings = new GameObjectConversionSettings(World, conversionFlags)
            {
                Systems = TestWorldSetup.GetDefaultInitSystemsFromEntitiesPackage(WorldSystemFilterFlags.GameObjectConversion).ToList()
            };

            using (var conversionWorld = GameObjectConversionUtility.InitializeIncrementalConversion(SceneManager.GetActiveScene(), settings))
            {
                Entities.ForEach((ref EntityRefTestData data) =>
                                 StringAssert.StartsWith("parent", m_Manager.GetName(data.Value)));

                var entity = EmptySystem.GetSingletonEntity <LinkedEntityGroup>();

                // Parent (LinkedEntityGroup) (2 additional entities)
                // - Child (1 additional entities)
                // All reference child game object
                child.Value = child.gameObject;
                child.AdditionalEntityCount = 1;
                parent.Value = child.gameObject;
#pragma warning disable 618
                GameObjectConversionUtility.ConvertIncremental(conversionWorld, new[] { child.gameObject }, conversionFlags);
#pragma warning restore 618

                EntitiesAssert.ContainsOnly(m_Manager,
                                            EntityMatch.Exact <EntityRefTestData>(entity, k_CommonComponents,
                                                                                  EntityMatch.Component((LinkedEntityGroup[] group) => group.Length == 5)),
                                            EntityMatch.Exact <EntityRefTestData>(k_ChildComponents),
                                            EntityMatch.Exact <EntityRefTestData>(),
                                            EntityMatch.Exact <EntityRefTestData>(),
                                            EntityMatch.Exact <EntityRefTestData>());

                // We expect there to still only be one linked entity group and it should be the same entity as before
                // since it is attached to the primary entity which is not getting destroyed.
                Assert.AreEqual(entity, EmptySystem.GetSingletonEntity <LinkedEntityGroup>());

                Entities.ForEach((ref EntityRefTestData data) =>
                                 StringAssert.StartsWith("child", m_Manager.GetName(data.Value)));

                foreach (var e in m_Manager.GetBuffer <LinkedEntityGroup>(entity).AsNativeArray())
                {
                    Assert.IsTrue(m_Manager.Exists(e.Value));
                }
            }
        }
        internal void DebugIncrementalConversion()
        {
            if (!_IncrementalConversionDebug.NeedsUpdate)
            {
                return;
            }
            _IncrementalConversionDebug.NeedsUpdate = false;
            var flags = _IncrementalConversionDebug.LastConversionFlags;

            using (DebugConversionMarker.Auto())
            {
                // use this to compare the results of incremental conversion with the results of a clean conversion.
                var settings = PrepareConversion(_IncrementalConversionDebug.World, flags, _buildConfigurationGUID, _buildConfiguration);
                GameObjectConversionUtility.InitializeIncrementalConversion(_Scene, settings).Dispose();
                AddMissingData(_IncrementalConversionDebug.World, _IncrementalConversionDebug.MissingSceneQuery,
                               _IncrementalConversionDebug.MissingRenderDataQuery);
                const EntityManagerDifferOptions options =
                    EntityManagerDifferOptions.IncludeForwardChangeSet |
                    EntityManagerDifferOptions.ValidateUniqueEntityGuid |
                    EntityManagerDifferOptions.UseReferentialEquality;

                unsafe
                {
                    if (_IncrementalConversionDebug.BlobAssets.BlobAssetBatch != null)
                    {
                        _IncrementalConversionDebug.BlobAssets.Dispose();
                    }
                }

                _IncrementalConversionDebug.BlobAssets = new BlobAssetCache(Allocator.Persistent);
                EntityDiffer.PrecomputeBlobAssetCache(_ConvertedWorld.EntityManager,
                                                      EntityManagerDiffer.EntityGuidQueryDesc, _IncrementalConversionDebug.BlobAssets);

                var changes = EntityDiffer.GetChanges(
                    _IncrementalConversionDebug.World.EntityManager,
                    _ConvertedWorld.EntityManager,
                    options,
                    EntityManagerDiffer.EntityGuidQueryDesc,
                    _IncrementalConversionDebug.BlobAssets,
                    Allocator.TempJob
                    );
                using (changes)
                {
                    if (!changes.AnyChanges)
                    {
                        return;
                    }

                    var fwdChanges = changes.ForwardChangeSet;
#if !UNITY_DISABLE_MANAGED_COMPONENTS
                    {
                        // Remove all companion link object changes.
                        // Companion objects will always be different between different conversions, so this is
                        // absolutely expected.
                        // It is unlikely that a diff will ever only consist of changes to hybrid components, and even
                        // in that case pointing out that the companion link changed is not exactly helpful for the user
                        // either.
                        var managedComponents       = fwdChanges.SetManagedComponents;
                        int numCompanionLinkObjects = 0;
                        var types = fwdChanges.TypeHashes;
                        var companionLinkIndex = TypeManager.GetTypeIndex <CompanionLink>();
                        int last = managedComponents.Length - 1;
                        for (int i = last; i >= 0; i--)
                        {
                            // We need to go through the type index to correctly handle null Companion Links
                            int packedTypeIdx = managedComponents[i].Component.PackedTypeIndex;
                            var idx           = TypeManager.GetTypeIndexFromStableTypeHash(types[packedTypeIdx].StableTypeHash);
                            if (idx == companionLinkIndex)
                            {
                                managedComponents[i]     = managedComponents[last - numCompanionLinkObjects];
                                numCompanionLinkObjects += 1;
                            }
                        }

                        if (numCompanionLinkObjects > 0)
                        {
                            // throw away the companion link changes
                            Array.Resize(ref managedComponents, last + 1 - numCompanionLinkObjects);
                            fwdChanges = new EntityChangeSet(fwdChanges.CreatedEntityCount,
                                                             fwdChanges.DestroyedEntityCount, fwdChanges.Entities, fwdChanges.TypeHashes,
                                                             fwdChanges.Names, fwdChanges.AddComponents, fwdChanges.RemoveComponents,
                                                             fwdChanges.SetComponents, fwdChanges.ComponentData, fwdChanges.EntityReferenceChanges,
                                                             fwdChanges.BlobAssetReferenceChanges,
                                                             managedComponents, // <- this changes
                                                             fwdChanges.SetSharedComponents, fwdChanges.LinkedEntityGroupAdditions,
                                                             fwdChanges.LinkedEntityGroupRemovals, fwdChanges.CreatedBlobAssets,
                                                             fwdChanges.DestroyedBlobAssets, fwdChanges.BlobAssetData);
                            if (!fwdChanges.HasChanges)
                            {
                                return;
                            }
                        }
                    }
#endif
                    _RequestCleanConversion = true;
                    var sb = new StringBuilder();
                    fwdChanges.PrintSummary(sb);
                    var errorString =
                        "The result of incrementally converting changes and a clean conversion didn't match, are you missing some dependencies?\n" +
                        "This is what was added/removed/changed by the clean conversion relative to the incremental conversion:\n" +
                        sb;
                    if (LiveConversionSettings.TreatIncrementalConversionFailureAsError)
                    {
                        throw new Exception(errorString);
                    }
                    Debug.LogWarning(errorString);
                }
            }
        }
        void Convert(GameObjectConversionUtility.ConversionFlags flags)
        {
            using (LiveLinkConvertMarker.Auto())
            {
                var mode = LiveConversionSettings.Mode;
#if UNITY_2020_2_OR_NEWER
                if (mode == LiveConversionSettings.ConversionMode.AlwaysCleanConvert)
                {
                    _RequestCleanConversion = true;
                }
                _IncrementalConversionDebug.LastConversionFlags = flags;
#endif

                // Try incremental conversion
                if (!_RequestCleanConversion)
                {
                    try
                    {
                        using (IncrementalConversionMarker.Auto())
                        {
#if UNITY_2020_2_OR_NEWER
                            _IncrementalConversionDebug.NeedsUpdate = true;
                            var batch = new IncrementalConversionBatch();
                            _IncrementalConversionChangeTracker.FillBatch(ref batch);
                            GameObjectConversionUtility.ConvertIncremental(_GameObjectWorld, flags, ref batch);
#else
                            GameObjectConversionUtility.ConvertIncremental(_GameObjectWorld, m_ChangedGameObjects, _IncrementalConversionChangeTracker.ChangedAssets, flags);
#endif
                            AddMissingData(_ConvertedWorld, _MissingSceneQuery, _MissingRenderDataQuery);
                        }
                    }
#if UNITY_2020_2_OR_NEWER
                    catch (Exception e)
                    {
                        _RequestCleanConversion = true;
                        if (LiveConversionSettings.TreatIncrementalConversionFailureAsError)
                        {
                            throw;
                        }
                        if (mode != LiveConversionSettings.ConversionMode.AlwaysCleanConvert)
                        {
                            Debug.Log("Incremental conversion failed. Performing full conversion instead\n" + e);
                        }
                    }
#else
                    catch (Exception)

                    {
                        _RequestCleanConversion = true;
                    }
#endif
                }

                // If anything failed, fall back to clean conversion
                if (_RequestCleanConversion)
                {
#if UNITY_2020_2_OR_NEWER
                    _IncrementalConversionDebug.NeedsUpdate = false;
#endif
                    using (CleanConversionMarker.Auto())
                    {
                        if (_GameObjectWorld != null && _GameObjectWorld.IsCreated)
                        {
                            _GameObjectWorld.Dispose();
                            _GameObjectWorld = null;
                        }

                        var settings = PrepareConversion(_ConvertedWorld, flags, _buildConfigurationGUID, _buildConfiguration);
                        _GameObjectWorld = GameObjectConversionUtility.InitializeIncrementalConversion(_Scene, settings);
                        _MappingSystem   = _GameObjectWorld.GetExistingSystem <GameObjectConversionMappingSystem>();
                        AddMissingData(_ConvertedWorld, _MissingSceneQuery, _MissingRenderDataQuery);
                    }
                }

                _IncrementalConversionChangeTracker.Clear();
                _RequestCleanConversion = false;
            }
        }