private void ProcessQueue(Action <string> _ErrorMessageAction, ManualResetEvent ProcessQueueWait, ConcurrentQueue <Node> ProcessQueue, EDeflateCompression QueueCompressMode) { BTaskWrapper.Run(() => { try { _ErrorMessageAction?.Invoke($"[{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fffff")}] Start processing Queue"); Dictionary <ENodeType, StreamStruct> WriteStreams = CreateStreams(QueueCompressMode); using (XStreamWriter Writer = new XStreamWriter(WriteStreams)) { while (!QueueComplete || ProcessQueue.Count > 0) { if (ProcessQueue.TryDequeue(out Node WriteNode)) { if (WriteNode.GetNodeType() == ENodeType.Hierarchy) { HierarchyNode CurrentHierarchyNode = (HierarchyNode)WriteNode; //Need to check that lists are not null CheckHierarchyNode(CurrentHierarchyNode); Writer.Write(CurrentHierarchyNode); } if (WriteNode.GetNodeType() == ENodeType.Geometry) { GeometryNode CurrentGeometry = (GeometryNode)WriteNode; CheckGeometry(CurrentGeometry); Writer.Write((GeometryNode)WriteNode); } if (WriteNode.GetNodeType() == ENodeType.Metadata) { Writer.Write((MetadataNode)WriteNode); } } else { Thread.Sleep(10); } } _ErrorMessageAction?.Invoke($"[{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fffff")}] Closing Stream"); } _ErrorMessageAction?.Invoke($"[{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fffff")}] Closed Stream"); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"[{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fffff")}] {ex.Message}\n{ex.StackTrace}"); State = FAILED_STATE; } ProcessQueueWait.Set(); }); }
public static void SimpleReadWriteTest(bool Compress = true) { string[][] LodList = new string[][] { new string[] { "Content/Models/monkey.obj", "Content/Models/monkeyLod1.obj", "Content/Models/monkeyLod2.obj" }, new string[] { "Content/Models/squishymonkey.obj", "Content/Models/squishymonkeyLod1.obj" }, new string[] { "Content/Models/HDmonkey.obj", "Content/Models/HDMonkeyLod1.obj", "Content/Models/HDMonkeyLod2.obj", "Content/Models/HDMonkeyLod3.obj" } }; string[] MetadataStrings = new string[] { "Name:Monkey", "Name:Squishymonkey", "Name:HDmonkey" }; string Ext = ""; EDeflateCompression CompressEnum; if (Compress) { CompressEnum = EDeflateCompression.Compress; Ext = "x3_c_"; } else { CompressEnum = EDeflateCompression.DoNotCompress; Ext = "x3_p_"; } List <HierarchyNode> WriteHNodes = new List <HierarchyNode>(); List <GeometryNode> WritePolygons = new List <GeometryNode>(); List <MetadataNode> WriteMNodes = new List <MetadataNode>(); Random Rand = new Random(); for (int i = 0; i < 3; ++i) { HierarchyNode HNode = new HierarchyNode(); HNode.UniqueID = GetId(); MetadataNode Meta = new MetadataNode(); Meta.Metadata = MetadataStrings[i]; Meta.UniqueID = GetId(); var Polygon = new GeometryNode(); for (int f = 0; f < LodList[i].Length; ++f) { Obj ObjFile = new Obj(); ObjFile.LoadObj(LodList[i][f]); SetPolygonData(Polygon, ObjFile, f); } Polygon.UniqueID = GetId(); HierarchyNode.GeometryPart Part = CreateGeometryPart(Rand, i, Polygon); HNode.GeometryParts.Add(Part); HNode.MetadataID = Meta.UniqueID; if (i != 0) { HNode.ParentID = WriteHNodes[i - 1].UniqueID; WriteHNodes[i - 1].ChildNodes.Add(HNode.UniqueID); //HierarchyNode.GeometryPart InstancePart = CreateGeometryPart(Rand, i, WritePolygons[i - 1]); //HNode.GeometryParts.Add(InstancePart); } WriteHNodes.Add(HNode); WritePolygons.Add(Polygon); WriteMNodes.Add(Meta); } Dictionary <ENodeType, StreamStruct> Streams = new Dictionary <ENodeType, StreamStruct>(); FileStream WriteFileStreamH = new FileStream($"m.{Ext}h", FileMode.Create); StreamStruct StreamStructH = new StreamStruct(WriteFileStreamH, CompressEnum); FileStream WriteFileStreamG = new FileStream($"m.{Ext}g", FileMode.Create); StreamStruct StreamStructG = new StreamStruct(WriteFileStreamG, CompressEnum); FileStream FileStreamM = new FileStream($"m.{Ext}m", FileMode.Create); StreamStruct StreamStructM = new StreamStruct(FileStreamM, CompressEnum); Streams.Add(ENodeType.Hierarchy, StreamStructH); Streams.Add(ENodeType.Geometry, StreamStructG); Streams.Add(ENodeType.Metadata, StreamStructM); Console.WriteLine("Writing"); using (XStreamWriter writer = new XStreamWriter(Streams)) { for (int i = 0; i < WriteHNodes.Count; ++i) { writer.Write(WriteHNodes[i]); } for (int i = 0; i < WritePolygons.Count; ++i) { writer.Write(WritePolygons[i]); } for (int i = 0; i < WriteMNodes.Count; ++i) { writer.Write(WriteMNodes[i]); } } //Dispose of stream here Streams[ENodeType.Hierarchy].IOStream.Dispose(); Streams[ENodeType.Metadata].IOStream.Dispose(); Streams[ENodeType.Geometry].IOStream.Dispose(); Console.WriteLine("Done writing"); Console.WriteLine("Reading"); List <GeometryNode> ReadPolygons = new List <GeometryNode>(); List <HierarchyNode> ReadHNodes = new List <HierarchyNode>(); List <MetadataNode> ReadMNodes = new List <MetadataNode>(); ManualResetEvent WaitReads = new ManualResetEvent(false); int ReadsLeft = WriteHNodes.Count + WriteMNodes.Count + WritePolygons.Count; FileStream ReadFileStreamH = new FileStream($"m.{Ext}h", FileMode.Open); FileStream ReadFileStreamG = new FileStream($"m.{Ext}g", FileMode.Open); FileStream ReadFileStreamM = new FileStream($"m.{Ext}m", FileMode.Open); using (XStreamReader ReaderH = new XStreamReader(ENodeType.Hierarchy, ReadFileStreamH, (_Sdk) => { }, (_Node) => { Interlocked.Decrement(ref ReadsLeft); lock (ReadHNodes) { ReadHNodes.Add((HierarchyNode)_Node); } if (ReadsLeft == 0) { WaitReads.Set(); } }, CompressEnum)) { if (!ReaderH.Process(Console.WriteLine)) { throw new Exception("Failed"); } } using (XStreamReader ReaderM = new XStreamReader(ENodeType.Metadata, ReadFileStreamM, (_Sdk) => { }, (_Node) => { Interlocked.Decrement(ref ReadsLeft); lock (ReadMNodes) { ReadMNodes.Add((MetadataNode)_Node); } if (ReadsLeft == 0) { WaitReads.Set(); } }, CompressEnum)) { if (!ReaderM.Process(Console.WriteLine)) { throw new Exception("Failed"); } } using (XStreamReader ReaderG = new XStreamReader(ENodeType.Geometry, ReadFileStreamG, (_Sdk) => { }, (_Node) => { Interlocked.Decrement(ref ReadsLeft); lock (ReadPolygons) { ReadPolygons.Add((GeometryNode)_Node); } if (ReadsLeft == 0) { WaitReads.Set(); } }, CompressEnum)) { if (!ReaderG.Process(Console.WriteLine)) { throw new Exception("Failed"); } } WaitReads.WaitOne(); Console.WriteLine("Done Reading"); WritePolygons = WritePolygons.OrderBy(x => x.UniqueID).ToList(); ReadPolygons = ReadPolygons.OrderBy(x => x.UniqueID).ToList(); WriteMNodes = WriteMNodes.OrderBy(x => x.UniqueID).ToList(); ReadMNodes = ReadMNodes.OrderBy(x => x.UniqueID).ToList(); WriteHNodes = WriteHNodes.OrderBy(x => x.UniqueID).ToList(); ReadHNodes = ReadHNodes.OrderBy(x => x.UniqueID).ToList(); CheckInputVsOutput(WritePolygons, WriteHNodes, WriteMNodes, ReadPolygons, ReadHNodes, ReadMNodes); }