Пример #1
0
        public static bool WriteWorldToFile(World world, FileInfo outputFile)
        {
            // TODO need to bring this back
#if false
            // Check for missing assembly references
            var unresolvedComponentTypes = GetAllUsedComponentTypes(world).Where(t => !DomainCache.IsIncludedInProject(project, t.GetManagedType())).ToArray();
            if (unresolvedComponentTypes.Length > 0)
            {
                foreach (var unresolvedComponentType in unresolvedComponentTypes)
                {
                    var type = unresolvedComponentType.GetManagedType();
                    Debug.LogError($"Could not resolve component type '{type.FullName}' while exporting {scenePath.ToHyperLink()}. Are you missing an assembly reference to '{type.Assembly.GetName().Name}' ?");
                }
                return(false);
            }
#endif

            var directoryName = Path.GetDirectoryName(outputFile.FullName);
            if (string.IsNullOrEmpty(directoryName))
            {
                throw new ArgumentException($"Invalid output file directory: {directoryName}", nameof(outputFile));
            }

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }

            // Merges the entities and shared component streams, (optionally) compresses them, and finally serializes to disk with a small header in front
            using (var fileStream = new StreamBinaryWriter(outputFile.FullName))
                using (var entitiesWriter = new MemoryBinaryWriter())
                {
                    var entityRemapInfos = new NativeArray <EntityRemapUtility.EntityRemapInfo>(world.EntityManager.EntityCapacity, Allocator.Temp);
                    SerializeUtility.SerializeWorldInternal(world.EntityManager, entitiesWriter, out var referencedObjects, entityRemapInfos, isDOTSRuntime: true);
                    entityRemapInfos.Dispose();

                    if (referencedObjects != null)
                    {
                        throw new ArgumentException("We are serializing a world that contains UnityEngine.Object references which are not supported in Dots Runtime.");
                    }

#if TINY_SCENE_DEP
                    unsafe
                    {
                        var worldHeader = new SceneHeader();
                        worldHeader.DecompressedSize = entitiesWriter.Length;
                        worldHeader.Codec            = Codec.LZ4;
                        worldHeader.SerializeHeader(fileStream);

                        if (worldHeader.Codec != Codec.None)
                        {
                            int compressedSize = CodecService.Compress(worldHeader.Codec, entitiesWriter.Data, entitiesWriter.Length, out var compressedData);
                            fileStream.WriteBytes(compressedData, compressedSize);
                        }
                        else
                        {
                            fileStream.WriteBytes(entitiesWriter.Data, entitiesWriter.Length);
                        }
                    }
#endif
                }

            return(true);
        }
Пример #2
0
        private static bool ExportWorld(FileInfo outputFile, Project project, string scenePath, World world, bool performConversion = true)
        {
            if (performConversion)
            {
                SceneConversion.Convert(world);
            }

            // Null out any references to avoid the SerializeUtility from trying to patch asset entities.
            world.GetOrCreateSystem <ClearRemappedEntityReferenceSystem>().Update();

            // Check for missing assembly references
            var unresolvedComponentTypes = GetAllUsedComponentTypes(world).Where(t => !DomainCache.IsIncludedInProject(project, t.GetManagedType())).ToArray();

            if (unresolvedComponentTypes.Length > 0)
            {
                foreach (var unresolvedComponentType in unresolvedComponentTypes)
                {
                    var type = unresolvedComponentType.GetManagedType();
                    Debug.LogError($"Could not resolve component type '{type.FullName}' while exporting {scenePath.HyperLink()}. Are you missing an assembly reference to '{type.Assembly.GetName().Name}' ?");
                }
                return(false);
            }

            // Remove non-exported components
            var nonExportedComponentTypes = UnityEditor.TypeCache.GetTypesWithAttribute <NonExportedAttribute>().Select(t => new ComponentType(t));

            world.EntityManager.RemoveComponent(world.EntityManager.UniversalQuery, new ComponentTypes(nonExportedComponentTypes.ToArray()));

            // Merges the entities and shared component streams, (optionally) compresses them, and finally serializes to disk with a small header in front
            using (var fileStream = new StreamBinaryWriter(outputFile.FullName))
                using (var entitiesWriter = new MemoryBinaryWriter())
                    using (var sharedComponentWriter = new MemoryBinaryWriter())
                        using (var combinedDataWriter = new MemoryBinaryWriter())
                        {
                            SerializeUtility.SerializeWorld(world.EntityManager, entitiesWriter, out var sharedComponentsToSerialize);
                            if (sharedComponentsToSerialize.Length > 0)
                            {
                                SerializeUtility.SerializeSharedComponents(world.EntityManager, sharedComponentWriter, sharedComponentsToSerialize);
                            }

                            unsafe
                            {
                                combinedDataWriter.WriteBytes(sharedComponentWriter.Data, sharedComponentWriter.Length);
                                combinedDataWriter.WriteBytes(entitiesWriter.Data, entitiesWriter.Length);

                                var worldHeader = new SceneHeader();
                                worldHeader.SharedComponentCount = sharedComponentsToSerialize.Length;
                                worldHeader.DecompressedSize     = entitiesWriter.Length + sharedComponentWriter.Length;
                                worldHeader.Codec = Codec.LZ4;
                                worldHeader.SerializeHeader(fileStream);

                                if (worldHeader.Codec != Codec.None)
                                {
                                    int compressedSize = CodecService.Compress(worldHeader.Codec, combinedDataWriter.Data, combinedDataWriter.Length, out var compressedData);
                                    fileStream.WriteBytes(compressedData, compressedSize);
                                }
                                else
                                {
                                    fileStream.WriteBytes(combinedDataWriter.Data, combinedDataWriter.Length);
                                }
                            }
                        }

            return(true);
        }