Ejemplo n.º 1
0
        public unsafe NativeArray <BlobAssetReference <MeshSourceData> > LoadSources(int surfaceId)
        {
            string path;

            if (surfaceId == -1)
            {
                path = GetGlobalPath(typeof(MeshSourceData).Name);
            }
            else
            {
                path = GetPath(surfaceId, typeof(MeshSourceData).Name, null);
            }

            using (StreamBinaryReader reader = new StreamBinaryReader(path))
            {
                int sourceCount = reader.ReadInt();
                NativeArray <BlobAssetReference <MeshSourceData> > sources = new NativeArray <BlobAssetReference <MeshSourceData> >(sourceCount, Allocator.Temp);

                for (int i = 0; i < sourceCount; i++)
                {
                    int sizeInBytes           = reader.ReadInt();
                    NativeArray <int> indices = new NativeArray <int>(sizeInBytes, Allocator.Temp);
                    reader.ReadBytes(indices.GetUnsafePtr(), sizeInBytes);

                    sizeInBytes = reader.ReadInt();
                    NativeArray <float3> vertices = new NativeArray <float3>(sizeInBytes, Allocator.Temp);
                    reader.ReadBytes(vertices.GetUnsafePtr(), sizeInBytes);

                    sources[i] = MeshSourceData.Create(indices, vertices);
                    indices.Dispose();
                    vertices.Dispose();
                }
                return(sources);
            }
        }
Ejemplo n.º 2
0
    protected override void OnUpdate()
    {
        if (Input.GetKeyDown(KeyCode.H))
        {
            Debug.Log($"Saving world...");
            using (var writer = new StreamBinaryWriter(FullPath))
            {
                EntityManager.CompleteAllJobs();
                var q = EntityManager.CreateEntityQuery(typeof(Terminal));
                EntityManager.DestroyEntity(q);
                SerializeUtilityHybrid.Serialize(EntityManager, writer, out _refs);
                int refCount = _refs == null ? 0 : _refs.Array.Length;
                Debug.Log($"RefCount: {refCount}");
            }
        }

        if (Input.GetKeyDown(KeyCode.L))
        {
            Debug.Log("Loading World...");

            var loadWorld = new World("Loading World");
            var em        = loadWorld.EntityManager;
            using (var reader = new StreamBinaryReader(FullPath))
            {
                SerializeUtilityHybrid.Deserialize(em, reader, _refs);
            }

            EntityManager.CompleteAllJobs();
            EntityManager.DestroyEntity(EntityManager.UniversalQuery);

            EntityManager.MoveEntitiesFrom(em);
            loadWorld.Dispose();
        }
    }
Ejemplo n.º 3
0
        public unsafe bool LoadTiles(int surfaceId, Dictionary <int2, NavMeshTile> tiles)
        {
            string path = GetPath(surfaceId, typeof(NavMeshTile).Name, null);

            if (!File.Exists(path))
            {
                //Debug.LogFormat("Tiles not found id:{0}", surfaceId);
                return(false);
            }

            using (StreamBinaryReader reader = new StreamBinaryReader(path))
            {
                int tileCount = reader.ReadInt();
                for (int i = 0; i < tileCount; i++)
                {
                    int    tileSize = reader.ReadInt();
                    byte[] data     = new byte[tileSize];
                    fixed(byte *dataPtr = data)
                    {
                        reader.ReadBytes(dataPtr, tileSize);
                    }

                    NavMeshTile tile = new NavMeshTile();
                    tile.Data         = data;
                    tiles[tile.Coord] = tile;
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
 static unsafe EntityScenesPaths.SceneWithBuildConfigurationGUIDs ReadSceneWithBuildConfiguration(string path)
 {
     EntityScenesPaths.SceneWithBuildConfigurationGUIDs sceneWithBuildConfiguration = default;
     using (var reader = new StreamBinaryReader(path, sizeof(EntityScenesPaths.SceneWithBuildConfigurationGUIDs)))
     {
         reader.ReadBytes(&sceneWithBuildConfiguration, sizeof(EntityScenesPaths.SceneWithBuildConfigurationGUIDs));
     }
     return(sceneWithBuildConfiguration);
 }
 static unsafe EntityScenesPaths.SceneWithBuildSettingsGUIDs ReadSceneWithBuildSettings(string path)
 {
     EntityScenesPaths.SceneWithBuildSettingsGUIDs sceneWithBuildSettings = default;
     using (var reader = new StreamBinaryReader(path))
     {
         reader.ReadBytes(&sceneWithBuildSettings, sizeof(EntityScenesPaths.SceneWithBuildSettingsGUIDs));
     }
     return(sceneWithBuildSettings);
 }
        public static unsafe SceneWithBuildConfigurationGUIDs ReadFromFile(string path)
        {
            SceneWithBuildConfigurationGUIDs sceneWithBuildConfiguration = default;

            using (var reader = new StreamBinaryReader(path))
            {
                reader.ReadBytes(&sceneWithBuildConfiguration, sizeof(SceneWithBuildConfigurationGUIDs));
            }
            return(sceneWithBuildConfiguration);
        }
        unsafe void ReceiveResponseSubSceneTargetHash(MessageEventArgs args)
        {
            using (var subSceneAssets = args.ReceiveArray <ResolvedSubSceneID>())
            {
                foreach (var subSceneAsset in subSceneAssets)
                {
                    if (_WaitingForSubScenes.ContainsKey(subSceneAsset.SubSceneGUID))
                    {
                        return;
                    }

                    var headerPath = EntityScenesPaths.GetLiveLinkCachePath(subSceneAsset.TargetHash, EntityScenesPaths.PathType.EntitiesHeader, -1);

                    if (File.Exists(headerPath))
                    {
                        LiveLinkMsg.LogInfo($"ReceiveResponseSubSceneTargetHash => {subSceneAsset.SubSceneGUID} | {subSceneAsset.TargetHash}, File.Exists => 'True', {Path.GetFullPath(headerPath)}");

                        //TODO: This is a hack to make sure assets are managed by asset manifest when loading from cache for first run
                        if (!BlobAssetReference <SceneMetaData> .TryRead(headerPath, SceneMetaDataSerializeUtility.CurrentFileFormatVersion, out var sceneMetaDataRef))
                        {
                            Debug.LogError("Loading Entity Scene failed because the entity header file was an old version: " + subSceneAsset.SubSceneGUID);
                            return;
                        }

                        ref var sceneMetaData = ref sceneMetaDataRef.Value;

                        var waitingSubScene = new WaitingSubScene
                        {
                            TargetHash      = subSceneAsset.TargetHash,
                            SubSections     = new List <NativeArray <RuntimeGlobalObjectId> >(),
                            SubSectionCount = sceneMetaData.Sections.Length
                        };

                        for (int i = 0; i < sceneMetaData.Sections.Length; i++)
                        {
                            if (sceneMetaData.Sections[i].ObjectReferenceCount != 0)
                            {
                                var refObjGUIDsPath = EntityScenesPaths.GetLiveLinkCachePath(subSceneAsset.TargetHash, EntityScenesPaths.PathType.EntitiesUnityObjectReferences, i);
                                using (var reader = new StreamBinaryReader(refObjGUIDsPath))
                                {
                                    var numObjRefGUIDs = reader.ReadInt();
                                    NativeArray <RuntimeGlobalObjectId> objRefGUIDs = new NativeArray <RuntimeGlobalObjectId>(numObjRefGUIDs, Allocator.Persistent);
                                    reader.ReadArray(objRefGUIDs, numObjRefGUIDs);
                                    waitingSubScene.SubSections.Add(objRefGUIDs);
                                }
                            }
                            else
                            {
                                waitingSubScene.SubSections.Add(new NativeArray <RuntimeGlobalObjectId>(0, Allocator.Persistent));
                            }
                        }

                        _WaitingForSubScenes[subSceneAsset.SubSceneGUID] = waitingSubScene;
                    }
Ejemplo n.º 8
0
    static void LoadHybrid(EntityManager entityManager, ReferencedUnityObjects objectTable)
    {
        string filePath = Application.persistentDataPath + "/testHyb.bin";

        using (var binaryReader = new StreamBinaryReader(filePath))
        {
            //ExclusiveEntityTransaction transaction = entityManager.BeginExclusiveEntityTransaction();

            SerializeUtilityHybrid.Deserialize(entityManager, binaryReader, objectTable);

            //entityManager.EndExclusiveEntityTransaction();
        }
    }
Ejemplo n.º 9
0
    static void LoadWorld(EntityManager entityManager, object[] objectTable)
    {
        string filePath = Application.persistentDataPath + "/test.bin";

        using (var binaryReader = new StreamBinaryReader(filePath))
        {
            ExclusiveEntityTransaction transaction = entityManager.BeginExclusiveEntityTransaction();

            SerializeUtility.DeserializeWorld(transaction, binaryReader, objectTable);

            entityManager.EndExclusiveEntityTransaction();
        }
    }
        public static unsafe void ReadPlayerLiveLinkCacheGUID()
        {
            var cacheGUIDPath = $"{EntityScenesPaths.GetLiveLinkCacheDirPath()}/livelinkcacheguid";

            if (File.Exists(cacheGUIDPath))
            {
                using (var rdr = new StreamBinaryReader(cacheGUIDPath))
                {
                    Hash128 guid = default;
                    rdr.ReadBytes(&guid, sizeof(Hash128));
                    LiveLinkCacheGUID = guid;
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Reads bytes from a fileName, validates the expected serialized version, and deserializes them into a new blob asset.
        /// </summary>
        /// <param name="path">The path of the blob data to read.</param>
        /// <param name="version">Expected version number of the blob data.</param>
        /// <param name="result">The resulting BlobAssetReference if the data was read successful.</param>
        /// <returns>A bool if the read was successful or not.</returns>
        public static bool TryRead(string path, int version, out BlobAssetReference <T> result)
        {
            using (var binaryReader = new StreamBinaryReader(path))
            {
                var storedVersion = binaryReader.ReadInt();
                if (storedVersion != version)
                {
                    result = default;
                    return(false);
                }

                result = binaryReader.Read <T>();
                return(true);
            }
        }
 static unsafe void ReadEditorLiveLinkCacheGUID()
 {
     if (File.Exists(k_LiveLinkEditorCacheGUIDPath))
     {
         using (var rdr = new StreamBinaryReader(k_LiveLinkEditorCacheGUIDPath))
         {
             Hash128 guid = default;
             rdr.ReadBytes(&guid, sizeof(Hash128));
             s_CacheGUID = guid;
         }
     }
     else
     {
         GenerateNewEditorLiveLinkCacheGUID();
     }
 }
        static unsafe void ReadBootstrap()
        {
            var path = bootstrapPath;

            using (var rdr = new StreamBinaryReader(path))
            {
                Hash128 guid = default;
                long    id   = 0;

                rdr.ReadBytes(&guid, sizeof(Hash128));
                rdr.ReadBytes(&id, sizeof(long));

                BuildConfigurationGUID = guid;
                LiveLinkId             = id;
            }
        }
Ejemplo n.º 14
0
        protected override string GetFileSignature(FileSystemPath targetFilePath)
        {
            if (!targetFilePath.ExistsFile)
            {
                Console.Error.WriteLine("PDB file does not exists " + targetFilePath);
                return(null);
            }

            if (targetFilePath.GetFileLength() == 0)
            {
                Console.Error.WriteLine("Empty PDB file " + targetFilePath);
                return(null);
            }

            if (PdbUtils.GetPdbType(targetFilePath) == DebugInfoType.Windows)
            {
                using (var pdbStream = targetFilePath.OpenFileForReading())
                {
                    var pdbFile    = new WindowsPdbFile(pdbStream);
                    var dbiStream  = pdbFile.GetDbiStream();
                    var ageFromDbi = 1;
                    if (dbiStream.Length > 0)
                    {
                        var binaryStream = new StreamBinaryReader(dbiStream);
                        var dbiHeader    = new DbiHeader(binaryStream);
                        ageFromDbi = dbiHeader.PdbAge;
                    }
                    var root      = pdbFile.GetRoot();
                    var signature = root.PdbSignature;
                    return(string.Format("{0}{1:X}", signature.ToString("N").ToUpperInvariant(), ageFromDbi));
                }
            }

            var debugInfo = PdbUtils.TryGetPdbDebugInfo(targetFilePath);

            if (debugInfo == null)
            {
                Console.Error.WriteLine("Unsupport PDB file " + targetFilePath);
                return(null);
            }

            return(string.Format("{0}{1:X}", debugInfo.Signature.ToString("N").ToUpperInvariant(), debugInfo.AgeOrTimestamp));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Reads bytes from a fileName, validates the expected serialized version, and deserializes them into a new blob asset.
        /// </summary>
        /// <param name="path">The path of the blob data to read.</param>
        /// <param name="version">Expected version number of the blob data.</param>
        /// <param name="result">The resulting BlobAssetReference if the data was read successful.</param>
        /// <returns>A bool if the read was successful or not.</returns>
        public static bool TryRead(string path, int version, out BlobAssetReference <T> result)
        {
            if (string.IsNullOrEmpty(path))
            {
                result = default;
                return(false);
            }

            using (var binaryReader = new StreamBinaryReader(path, UnsafeUtility.SizeOf <T>() + sizeof(int)))
            {
                var storedVersion = binaryReader.ReadInt();
                if (storedVersion != version)
                {
                    result = default;
                    return(false);
                }

                result = binaryReader.Read <T>();
                return(true);
            }
        }
Ejemplo n.º 16
0
        public static void Load()
        {
            if (World.All.Count < 1)
            {
                return;
            }

            // need an empty world to do this
            var world = new World("LoadingWorld");

            using (var reader = new StreamBinaryReader(WorldLocation)) //GetFullPathByName(fileName))
            {
                var objectRefAsset = AssetDatabase.LoadAssetAtPath <ReferencedUnityObjects>(WorldReferencesLocation);
                // Load objects as binary file
                SerializeUtilityHybrid.Deserialize(world.EntityManager, reader, objectRefAsset);
            }

            World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(World.DefaultGameObjectInjectionWorld.EntityManager.UniversalQuery);
            World.DefaultGameObjectInjectionWorld.EntityManager.MoveEntitiesFrom(world.EntityManager);
            Debug.Log("Loaded");
        }
        public bool Init(Stream stream, ShaderEffectSamplerAnisotropy anisotropyOverride)
        {
            // read shader effect desc
            var desc = new ShaderEffectDesc();            // TODO: read/create ShaderEffectDesc.

            if (anisotropyOverride != ShaderEffectSamplerAnisotropy.Default && desc.samplers != null)
            {
                for (int i = 0; i != desc.samplers.Length; ++i)
                {
                    desc.samplers[i].anisotropy = anisotropyOverride;
                }
            }

            // read shaders
            var reader      = new StreamBinaryReader(stream);
            int shaderCount = stream.ReadByte();

            for (int i = 0; i != shaderCount; ++i)
            {
                // read shader type
                var type = (ShaderType)stream.ReadByte();

                // read shader data
                int shaderSize = reader.ReadInt32();
                var shaderData = new byte[shaderSize];
                int read       = stream.Read(shaderData, 0, shaderSize);
                if (read < shaderSize)
                {
                    throw new Exception("End of file reached");
                }

                // create shader
                if (!CreateShader(shaderData, type))
                {
                    return(false);
                }
            }

            return(InitFinish(ref desc));
        }
Ejemplo n.º 18
0
    private void LoadData()
    {
        if (!File.Exists("Saves/Save.bin"))
        {
            return;
        }
        if (World.All.Count < 1)
        {
            return;
        }

        var formatter          = new BinaryFormatter();
        var saveFile           = File.Open("Saves/Save.bin", FileMode.Open);
        var deserializedObject = formatter.Deserialize(saveFile);
        var objects            = deserializedObject as List <Object>;
        var refObjects         = objects[0] as ReferencedUnityObjects;

        // To generate the file we'll test against
        var binaryPath = _FileLocation + "\\" + "DefaultWorld.world";

        // need an empty world to do this
        var loadingWorld = new World("SavingWorld");
        var em           = loadingWorld.EntityManager;

        using (var reader = new StreamBinaryReader(binaryPath))                     //GetFullPathByName(fileName))
        {
            var referencedObjectsPath = "Assets/ReferencedUnityWorldObjects.asset"; // path forward slash for asset access
            var objectRefAsset        = AssetDatabase.LoadAssetAtPath <ReferencedUnityObjects>(referencedObjectsPath);
            // Load objects as binary file
            SerializeUtilityHybrid.Deserialize(em, reader, refObjects);
        }


        World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(World.DefaultGameObjectInjectionWorld.EntityManager.UniversalQuery);
        World.DefaultGameObjectInjectionWorld.EntityManager.MoveEntitiesFrom(em);

        Debug.Log("Loaded");
    }
Ejemplo n.º 19
0
        // index -1 means 'use current index'
        public static int ReadContainerFromDisk(this PersistentSceneSystem persistentSceneSystem, Hash128 containerIdentifier, int index = -1)
        {
            // Path
            string path = CalculateStreamingAssetsPath(containerIdentifier);

            Debug.Assert(File.Exists(path));

            // Read From Disk
            using (var fileStream = new StreamBinaryReader(path))
            {
                PersistentDataStorage dataStorage = persistentSceneSystem.PersistentDataStorage;
                int amountEntitiesInFile          = fileStream.ReadInt();
                if (!dataStorage.IsInitialized(containerIdentifier))
                {
                    // Read before the scene has been loaded once this session
                    int amountBytesOfRawData   = fileStream.ReadInt();
                    NativeArray <byte> rawData = new NativeArray <byte>(amountBytesOfRawData, Allocator.Persistent);
                    fileStream.ReadArray(rawData, rawData.Length);
                    dataStorage.AddUninitializedSceneContainer(containerIdentifier, rawData);
                }
                else
                {
                    int currentEntityCapacity = dataStorage.GetEntityCapacity(containerIdentifier, out bool isPool);

                    if (amountEntitiesInFile != currentEntityCapacity)
                    {
                        if (isPool)
                        {
                            persistentSceneSystem.InstantChangePoolCapacity(containerIdentifier, amountEntitiesInFile);
                        }
                        else
                        {
                            throw new InvalidDataException($"The data in \"{path}\" is invalid. (.sav data only stays valid as long as the subscene doesn't change)");
                        }
                    }

                    // Container (Important that it happens after potential capacity change!)
                    PersistentDataContainer container;
                    if (index == -1)
                    {
                        container = dataStorage.GetWriteContainerForCurrentIndex(containerIdentifier);
                    }
                    else
                    {
                        int oldIndex = dataStorage.NonWrappedIndex;
                        dataStorage.ToIndex(index);
                        container = dataStorage.GetWriteContainerForCurrentIndex(containerIdentifier);
                        dataStorage.ToIndex(oldIndex);
                    }

                    // Read Data From Disk
                    NativeArray <byte> rawData = container.GetRawData();

                    int amountBytesOfRawData = fileStream.ReadInt();
                    if (amountBytesOfRawData != rawData.Length)
                    {
                        // Extra integrity Check
                        throw new InvalidDataException($"The data in \"{path}\" is invalid. (maybe component data structs changed?)");
                    }

                    fileStream.ReadArray(rawData, rawData.Length);
                }
                return(amountEntitiesInFile);
            }
        }