private static SerializedObjectReader ReadSceneFile(string path, out SerializedObjectView headerInformations) { var config = new SerializedObjectReaderConfiguration { UseReadAsync = false, BlockBufferSize = 256 << 10, // 256 KB TokenBufferSize = 1024, NodeBufferSize = JsonFrontEnd.EntityBatchSize, ValidationType = JsonValidationType.Standard, OutputBufferSize = 1024 << 10 // 1 MB }; var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, config.BlockBufferSize); var reader = new SerializedObjectReader(stream, config, leaveInputOpen: false); try { if (reader.Step(out var root) != NodeType.BeginObject) { throw new Exception(); } // 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; } } // Unpack header information from whats been read so far. headerInformations = root.AsObjectView(); return(reader); } catch { reader.Dispose(); throw; } }
public void SetUp() { m_Stream = new MemoryStream(); m_ConfigWithNoValidation = SerializedObjectReaderConfiguration.Default; m_ConfigWithNoValidation.ValidationType = JsonValidationType.None; }
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); }