private static Scene LoadFromFile(EntityManager entityManager, string path, IAssetManager assetManager) { using (var reader = ReadSceneFile(path, out var sceneHeader)) { var guid = sceneHeader["Guid"].AsStringView().ToString(); if (sceneHeader.TryGetValue(AssetReferencesPropertyName, out var assetReferences)) { LoadAssetReferences(assetReferences.AsArrayView(), assetManager); } JsonFrontEnd.Accept(entityManager, reader); var scene = SceneManager.Create(new Guid(guid)); using (var entities = entityManager.GetAllEntities()) { for (var i = 0; i < entities.Length; i++) { scene.AddEntityReference(entityManager, entities[i]); } } return(scene); } }
private static Project RestoreState(FileInfo file) { var world = new World(nameof(DomainReload)); var config = new SerializedObjectReaderConfiguration { UseReadAsync = false, BlockBufferSize = 256 << 10, // 256 KB TokenBufferSize = 1024, NodeBufferSize = JsonFrontEnd.EntityBatchSize, ValidationType = JsonValidationType.Standard, OutputBufferSize = 1024 << 10 // 1 MB }; Project project; using (var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, config.BlockBufferSize, FileOptions.Asynchronous)) using (var reader = new SerializedObjectReader(stream, config)) { if (reader.Step(out var root) != NodeType.BeginObject) { Debug.LogWarning("Temp file was not in the correct format. Domain was not reloaded."); return(null); } // ASSUMPTION: All members are written BEFORE the blob of entities. // Read until we hit "Entities": NodeType node; while ((node = reader.Step(out var view)) != NodeType.None) { if ((node & NodeType.ObjectKey) == 0) { continue; } if (view.AsStringView().Equals("Entities")) { break; } } var sessionState = root.AsObjectView(); if (!sessionState.TryGetValue("SerializedVersion", out var versionView) || versionView.AsInt64() != k_TempVersion) { Debug.LogWarning($"Temp file version has changed. Domain was not reloaded"); return(null); } var projectAssetGuid = sessionState[nameof(SessionState.ProjectAssetGuid)].AsStringView().ToString(); var projectPath = AssetDatabase.GUIDToAssetPath(projectAssetGuid); if (!File.Exists(projectPath)) { return(null); } project = Project.Open(new FileInfo(projectPath)); try { JsonFrontEnd.Accept(world.EntityManager, reader); project.Session.GetManager <WorldManager>().EntityManager.MoveEntitiesFrom(world.EntityManager); } finally { world.Dispose(); } var worldManager = project.Session.GetManager <IWorldManager>(); worldManager.EntityManager.World.GetOrCreateSystem <EntityReferenceRemapSystem>().Update(); worldManager.EntityManager.World.GetOrCreateSystem <RemoveRemapInformationSystem>().Update(); } return(project); }