//TODO: We should deprecate this method, it is unused internally
        public static SceneSectionData[] WriteEntityScene(EntityManager entityManager, Hash128 sceneGUID,
                                                          string sceneName, AssetImportContext importContext, int framesToRetainBlobAssets = 0,
                                                          List <ReferencedUnityObjects> sectionRefObjs = null)
        {
            ConversionJournalData journalData = new ConversionJournalData();

            return(WriteEntitySceneInternal(entityManager, sceneGUID, sceneName, importContext, framesToRetainBlobAssets, sectionRefObjs, new WriteEntitySceneSettings(), ref journalData));
        }
 public void Hook()
 {
     if (m_HookedLogger != null)
     {
         throw new InvalidOperationException($"{nameof(ExportConfigurationLogHandler)} has already been hooked into the logger.");
     }
     m_JournalData = new ConversionJournalData();
     m_JournalData.Init();
     m_HookedLogger = UnityDebug.unityLogger.logHandler;
     UnityDebug.unityLogger.logHandler = this;
 }
        internal static SceneSectionData[] WriteEntitySceneInternal(EntityManager entityManager, Hash128 sceneGUID,
                                                                    string sceneName, AssetImportContext importContext, int framesToRetainBlobAssets,
                                                                    List <ReferencedUnityObjects> sectionRefObjs, WriteEntitySceneSettings writeEntitySceneSettings, ref ConversionJournalData journalData)
        {
            using (var allTypes = new NativeHashMap <ComponentType, int>(100, Allocator.Temp))
                using (var archetypes = new NativeList <EntityArchetype>(Allocator.Temp))
                {
                    entityManager.GetAllArchetypes(archetypes);
                    for (int i = 0; i < archetypes.Length; i++)
                    {
                        var archetype = archetypes[i];
                        unsafe
                        {
                            if (archetype.Archetype->EntityCount == 0)
                            {
                                continue;
                            }
                        }

                        using (var componentTypes = archetype.GetComponentTypes())
                            foreach (var componentType in componentTypes)
                            {
                                if (allTypes.TryAdd(componentType, 0))
                                {
                                    if (importContext != null)
                                    {
                                        TypeDependencyCache.AddDependency(importContext, componentType);
                                    }
                                }
                            }
                    }
                    //Add exported types and assets to the journal data
                    using (var types = allTypes.GetKeyArray(Allocator.Temp))
                    {
                        CheckExportedTypes(writeEntitySceneSettings, true, types.Select(t => TypeManager.GetTypeInfo(t.TypeIndex)), ref journalData);
                    }
                }
            if (importContext != null)
            {
                TypeDependencyCache.AddAllSystemsDependency(importContext);
            }

            var sceneSections = new List <SceneSectionData>();

            var subSectionList = new List <SceneSection>();

            entityManager.GetAllUniqueSharedComponentData(subSectionList);
            //Order sections by section id
            subSectionList.Sort(Comparer <SceneSection> .Create((a, b) => a.Section.CompareTo(b.Section)));

            var extRefInfoEntities = new NativeArray <Entity>(subSectionList.Count, Allocator.Temp);

            NativeArray <Entity> entitiesInMainSection;

            var sectionQuery = entityManager.CreateEntityQuery(
                new EntityQueryDesc
            {
                All     = new[] { ComponentType.ReadWrite <SceneSection>() },
                Options = EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabled
            }
                );

            var sectionBoundsQuery = entityManager.CreateEntityQuery(
                new EntityQueryDesc
            {
                All     = new[] { ComponentType.ReadWrite <SceneBoundingVolume>(), ComponentType.ReadWrite <SceneSection>() },
                Options = EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabled
            }
                );

            {
                var section = new SceneSection {
                    SceneGUID = sceneGUID, Section = 0
                };
                sectionQuery.SetSharedComponentFilter(new SceneSection {
                    SceneGUID = sceneGUID, Section = 0
                });
                sectionBoundsQuery.SetSharedComponentFilter(new SceneSection {
                    SceneGUID = sceneGUID, Section = 0
                });
                entitiesInMainSection = sectionQuery.ToEntityArray(Allocator.TempJob);

                var bounds = GetBoundsAndRemove(entityManager, sectionBoundsQuery);

                // Each section will be serialized in its own world, entities that don't have a section are part of the main scene.
                // An entity that holds the array of external references to the main scene is required for each section.
                // We need to create them all before we start moving entities to section scenes,
                // otherwise they would reuse entities that have been moved and mess up the remapping tables.
                for (int sectionIndex = 1; sectionIndex < subSectionList.Count; ++sectionIndex)
                {
                    if (subSectionList[sectionIndex].Section == 0)
                    {
                        // Main section, the only one that doesn't need an external ref array
                        continue;
                    }

                    var extRefInfoEntity = entityManager.CreateEntity();
                    entityManager.AddSharedComponentData(extRefInfoEntity, subSectionList[sectionIndex]);
                    extRefInfoEntities[sectionIndex] = extRefInfoEntity;
                }

                // Public references array, only on the main section.
                var refInfoEntity = entityManager.CreateEntity();
                entityManager.AddBuffer <PublicEntityRef>(refInfoEntity);
                entityManager.AddSharedComponentData(refInfoEntity, section);
                var publicRefs = entityManager.GetBuffer <PublicEntityRef>(refInfoEntity);

//                entityManager.Debug.CheckInternalConsistency();

                //@TODO do we need to keep this index? doesn't carry any additional info
                for (int i = 0; i < entitiesInMainSection.Length; ++i)
                {
                    PublicEntityRef.Add(ref publicRefs,
                                        new PublicEntityRef {
                        entityIndex = i, targetEntity = entitiesInMainSection[i]
                    });
                }

                UnityEngine.Debug.Assert(publicRefs.Length == entitiesInMainSection.Length);

                // Save main section
                var sectionWorld   = new World("SectionWorld");
                var sectionManager = sectionWorld.EntityManager;

                var entityRemapping = entityManager.CreateEntityRemapArray(Allocator.TempJob);
                sectionManager.MoveEntitiesFrom(entityManager, sectionQuery, entityRemapping);

                AddRetainBlobAssetsEntity(sectionManager, framesToRetainBlobAssets);

                // The section component is only there to break the conversion world into different sections
                // We don't want to store that on the disk
                //@TODO: Component should be removed but currently leads to corrupt data file. Figure out why.
                //sectionManager.RemoveComponent(sectionManager.UniversalQuery, typeof(SceneSection));

                var(decompressedSectionFileSize, compressedSectionFileSize) = WriteEntitySceneSection(sectionManager, sceneGUID, "0",
                                                                                                      importContext, writeEntitySceneSettings, out var objectRefCount, out var objRefs, default);
        internal static void CheckExportedTypes(WriteEntitySceneSettings writeEntitySceneSettings, bool record, IEnumerable <TypeManager.TypeInfo> typeInfos, ref ConversionJournalData journalData)
        {
            if (!writeEntitySceneSettings.IsDotsRuntime)
            {
                return;
            }

            if (typeInfos.Any())
            {
                if (record)
                {
                    journalData.RecordLogEvent(null, LogType.Log, "::Exported Types (by stable hash)::");
                }
                foreach (var typeInfo in typeInfos)
                {
                    // TODO: We need to define what the assembly cache should look like for hybrid. Right now BuildAssemblyCache is defined from a root assembly and a build target and has only being used by DotsRuntime
                    if (writeEntitySceneSettings.BuildAssemblyCache != null)
                    {
                        var type = typeInfo.Type;
                        if (!writeEntitySceneSettings.BuildAssemblyCache.HasType(type))
                        {
                            journalData.RecordExceptionEvent(null, new ArgumentException($"The {type.Name} component is defined in the {type.Assembly.GetName().Name} assembly, but that assembly is not referenced by the current build configuration. Either add it as a reference, or ensure that the conversion process that is adding that component does not run."));
                        }
                    }

                    // Record exported types in the conversion log file for debug purposes
                    if (record)
                    {
                        journalData.RecordLogEvent(null, LogType.Log, $"0x{typeInfo.StableTypeHash:x16} - {typeInfo.StableTypeHash,22} - {typeInfo.Type.FullName}");
                    }
                }
            }
        }