private void InternalRead(BinaryReader reader) { _header = reader.ReadStructure<CVMDirectoryListingHeader>(CVMDirectoryListingHeader.SIZE); _subEntries = new CVMDirectoryListingEntry[_header.entryCount]; for (int i = 0; i < _header.entryCount; i++) { _subEntries[i] = new CVMDirectoryListingEntry(reader, this); } reader.AlignPosition(16); for (int i = 0; i < _header.entryCount; i++) { if (i > 1 && _subEntries[i].Flags.HasFlagUnchecked(RecordFlags.DirectoryRecord)) { _subEntries[i].DirectoryListing = new CVMDirectoryListing(reader, _subEntries[i]); } } }
/// <summary> /// Parses the <see cref="ControlFile"/>. /// </summary> /// <exception cref="InvalidOperationException">"No EMAX control file name was specified.</exception> /// <exception cref="FileNotFoundException">EMAX control file was not found.</exception> public void Parse() { if (string.IsNullOrEmpty(FileName)) throw new InvalidOperationException("No EMAX control file name was specified."); if (!File.Exists(FileName)) throw new FileNotFoundException(string.Format("EMAX control file {0} not found.", FileName)); m_parsedSuccesses.Clear(); m_parsedFailures.Clear(); byte byteValue; using (FileStream stream = File.OpenRead(FileName)) { // Read in header and file structure definitions using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true)) { // Read control header Header = reader.ReadStructure<CTL_HEADER>(); // Read byte that defines number of analog channels m_configuredAnalogChannels = BinaryCodedDecimal.Decode(Header.id.LowByte()); // Read byte that defines data size (i.e., 12 or 16 bits) byteValue = Header.id.HighByte(); if (!Enum.IsDefined(typeof(DataSize), byteValue)) throw new InvalidOperationException("Invalid EMAX data size code encountered: 0x" + byteValue.ToString("X").PadLeft(2, '0')); DataSize = (DataSize)byteValue; // Create array of file structures List<CTL_FILE_STRUCT> fileStructures = new List<CTL_FILE_STRUCT>(); CTL_FILE_STRUCT fileStructure = new CTL_FILE_STRUCT(reader); // ReSharper disable once LoopVariableIsNeverChangedInsideLoop while (fileStructure.type != StructureType.EndOfStructures) { if (fileStructure.type != StructureType.Unknown) fileStructures.Add(fileStructure); fileStructure = new CTL_FILE_STRUCT(reader); } FileStructures = fileStructures.ToArray(); } // Read in actual file structures for (int index = 0; index < FileStructures.Length; index++) { CTL_FILE_STRUCT fileStructure = FileStructures[index]; if (fileStructure.type == StructureType.Unknown) continue; // Set current type m_currentType = fileStructure.type; // Locate structure in the file stream.Position = fileStructure.offset; // Parse the structure type using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true)) { switch (m_currentType) { case StructureType.SYSTEM_PARAMETERS: AttemptParse(() => SystemParameters = reader.ReadStructure<SYSTEM_PARAMETERS>()); break; case StructureType.SYS_SETTINGS: AttemptParse(() => SystemSettings = reader.ReadStructure<SYS_SETTINGS>()); break; case StructureType.A_E_RSLTS: AttemptParse(() => AnalogEventResults = new A_E_RSLTS(reader, m_configuredAnalogChannels, SystemParameters.analog_groups)); break; case StructureType.ANALOG_GROUP: AttemptParse(() => AnalogGroup = new ANALOG_GROUP(reader, m_configuredAnalogChannels)); break; case StructureType.EVENT_GROUP: AttemptParse(() => EventGroup = reader.ReadStructure<EVENT_GROUP>()); break; case StructureType.ANLG_CHNL_NEW: AttemptParse(() => { AnalogChannelSettings = new Dictionary<int, ANLG_CHNL_NEW>(); ScalingFactors = new Dictionary<int, double>(); ANLG_CHNL_NEW settings; uint nextOffset = (index + 1 < FileStructures.Length) ? FileStructures[index + 1].offset : (uint)stream.Length; uint length = nextOffset - fileStructure.offset; Func<ANLG_CHNL_NEW> channelFactory; if (Marshal.SizeOf<ANLG_CHNL_NEW2>() * ConfiguredAnalogChannels <= length) channelFactory = () => reader.ReadStructure<ANLG_CHNL_NEW2>().ToAnlgChnlNew(); else channelFactory = () => reader.ReadStructure<ANLG_CHNL_NEW1>().ToAnlgChnlNew(); for (int i = 0; i < ConfiguredAnalogChannels; i++) { settings = channelFactory(); AnalogChannelSettings.Add(settings.ChannelNumber, settings); ScalingFactors.Add(settings.ChannelNumber, settings.ScalingFactor); } }); break; case StructureType.EVNT_CHNL_NEW: AttemptParse(() => { EventChannelSettings = new Dictionary<int, EVNT_CHNL_NEW>(); EVNT_CHNL_NEW settings; uint nextOffset = (index + 1 < FileStructures.Length) ? FileStructures[index + 1].offset : (uint)stream.Length; uint length = nextOffset - fileStructure.offset; Func<EVNT_CHNL_NEW> channelFactory; if (Marshal.SizeOf<EVNT_CHNL_NEW2>() * ConfiguredDigitalChannels <= length) channelFactory = () => reader.ReadStructure<EVNT_CHNL_NEW2>().ToEvntChnlNew(); else channelFactory = () => reader.ReadStructure<EVNT_CHNL_NEW1>().ToEvntChnlNew(); for (int i = 0; i < ConfiguredDigitalChannels; i++) { settings = channelFactory(); EventChannelSettings.Add(settings.EventNumber, settings); } }); break; case StructureType.ANLG_CHNLS: // TODO: Add decoder once structure definition is known... m_parsedFailures.Add(new Tuple<StructureType, Exception>(m_currentType, new NotImplementedException())); break; case StructureType.SHORT_HEADER: // TODO: Add decoder once structure definition is known... m_parsedFailures.Add(new Tuple<StructureType, Exception>(m_currentType, new NotImplementedException())); break; case StructureType.FAULT_HEADER: // TODO: Add decoder once structure definition is known... m_parsedFailures.Add(new Tuple<StructureType, Exception>(m_currentType, new NotImplementedException())); break; case StructureType.EVENT_DISPLAY: AttemptParse(() => EventDisplay = new EVENT_DISPLAY(reader, SystemParameters.event_groups)); break; case StructureType.IDENTSTRING: AttemptParse(() => { IdentityString = reader.ReadStructure<IDENTSTRING>(); IdentityString.value.TruncateRight(IdentityString.length); }); break; case StructureType.A_SELECTION: AttemptParse(() => AnalogSelection = new A_SELECTION(reader)); break; case StructureType.E_GROUP_SELECT: AttemptParse(() => EventGroupSelection = new E_GRP_SELECT(reader)); break; case StructureType.PHASOR_GROUP: AttemptParse(() => PhasorGroups = new PHASOR_GROUPS(reader)); break; case StructureType.LINE_CONSTANTS: AttemptParse(() => LineConstants = new LINE_CONSTANTS(reader)); break; case StructureType.LINE_NAMES: AttemptParse(() => LineNames = new LINE_NAMES(reader)); break; case StructureType.FAULT_LOCATION: AttemptParse(() => FaultLocations = new FAULT_LOCATIONS(reader)); break; case StructureType.SENS_RSLTS: AttemptParse(() => SensorResults = reader.ReadStructure<SENS_RSLTS>()); break; case StructureType.SEQUENCE_CHANNELS: AttemptParse(() => SequenceChannels = new SEQUENCE_CHANNELS(reader)); break; case StructureType.TPwrRcd: AttemptParse(() => PowerRecord = reader.ReadStructure<TPwrRcd>()); break; case StructureType.BoardAnalogEventChannels: AttemptParse(() => BoardAnalogEventChannels = reader.ReadStructure<BoardAnalogEventChannels>()); break; case StructureType.BREAKER_TRIP_TIMES: AttemptParse(() => BreakerTripTimes = new BREAKER_TRIP_TIMES(reader, m_configuredAnalogChannels, SystemParameters.analog_groups)); break; default: throw new ArgumentOutOfRangeException(); } } } } }
public SEQUENCE_CHANNELS(BinaryReader reader) { length = reader.ReadInt16(); channels = new SEQUENCE_CHANNEL[length / Marshal.SizeOf(typeof(SEQUENCE_CHANNEL))]; for (int i = 0; i < channels.Length; i++) { channels[i] = reader.ReadStructure<SEQUENCE_CHANNEL>(); } }
public FAULT_LOCATIONS(BinaryReader reader) { length = reader.ReadInt16(); locations = new FAULT_LOCATION[length / Marshal.SizeOf(typeof(FAULT_LOCATION))]; for (int i = 0; i < locations.Length; i++) { locations[i] = reader.ReadStructure<FAULT_LOCATION>(); } }
public LINE_NAMES(BinaryReader reader) { length = reader.ReadInt16(); names = new LINE_NAME[length / Marshal.SizeOf(typeof(LINE_NAME))]; for (int i = 0; i < names.Length; i++) { names[i] = reader.ReadStructure<LINE_NAME>(); } }
public LINE_CONSTANTS(BinaryReader reader) { length = reader.ReadInt16(); constants = new LINE_CONSTANT[length / Marshal.SizeOf(typeof(LINE_CONSTANT))]; for (int i = 0; i < constants.Length; i++) { constants[i] = reader.ReadStructure<LINE_CONSTANT>(); } }
public PHASOR_GROUPS(BinaryReader reader) { length = reader.ReadInt16(); groups = new PHASOR_GROUP[length / Marshal.SizeOf(typeof(PHASOR_GROUP))]; for (int i = 0; i < groups.Length; i++) { groups[i] = reader.ReadStructure<PHASOR_GROUP>(); } }
public A_E_RSLTS(BinaryReader reader, int channels, int analogGroups) { sequence_r = reader.ReadInt16(); flag_r = reader.ReadUInt16(); offset_r = reader.ReadUInt16(); channel_r = reader.ReadUInt16(); mSec_r = reader.ReadUInt16(); time_fault_r = reader.ReadUInt32(); rcd_sample_count_r = reader.ReadInt32(); aer = new A_E_RESULTS[channels * analogGroups]; for (int i = 0; i < aer.Length; i++) { aer[i] = reader.ReadStructure<A_E_RESULTS>(); } }
public void Deserialize(BinaryReader from) { // Bezier File Format : // Header ('BZR ') | sizeof(uint) // Version (1.1) | sizeof(uint) // Regular patch count | sizeof(uint) // Quad patch count | sizeof(uint) // Triangle patch count | sizeof(uint) this.Header = from.ReadStructure<BezierMesh.BezierHeader>(); byte[] expectedHeader = {Convert.ToByte(' '), Convert.ToByte('R'), Convert.ToByte('Z'), Convert.ToByte('B')}; if (BitConverter.IsLittleEndian) Array.Reverse(expectedHeader); if (Header.Header != BitConverter.ToInt32(expectedHeader,0) || (Header.Version != 0x0100 && Header.Version != 0x0101)) { throw new ArgumentException("Incorrect input header or unsupported file format version"); } // Part 1. Precomputed Control Points: // Regular Patches: // Bezier control points | 16 * regularPatchCount * sizeof(float3) // Texture coordinates | 16 * regularPatchCount * sizeof(float2) // Normal control points | 16 * regularPatchCount * sizeof(float3) _RegularBezierControlPoints = new List<Vector3>(from.ReadStructure<Vector3>(Header.RegularPatchCount * 16)); _RegularTextureCoordinates = new List<Vector2>(from.ReadStructure<Vector2>(Header.RegularPatchCount * 16)); if (Header.Version == 0x0101) { // Load normals _RegularNormals = new List<Vector3>(from.ReadStructure<Vector3>(Header.RegularPatchCount * 16)); } else { _RegularNormals = new List<Vector3>(); } // Quad Patches: // Bezier control points | 32 * quadPatchCount * sizeof(float3) // Gregory control points | 20 * quadPatchCount * sizeof(float3) // Pm control points | 24 * quadPatchCount * sizeof(float3) // Texture coordinates | 16 * quadPatchCount * sizeof(float2) // Normal control points | 16 * quadPatchCount * sizeof(float3) _QuadBezierControlPoints = new List<Vector3>(from.ReadStructure<Vector3>(Header.QuadPatchCount * 32)); _QuadGregoryControlPoints = new List<Vector3>(from.ReadStructure<Vector3>(Header.QuadPatchCount * 20)); _QuadPmControlPoints = new List<Vector3>(from.ReadStructure<Vector3>(Header.QuadPatchCount * 24)); _QuadTextureCoordinates = new List<Vector2>(from.ReadStructure<Vector2>(Header.QuadPatchCount * 16)); if (Header.Version == 0x0101) { // Load normals _QuadNormals = new List<Vector3>(from.ReadStructure<Vector3>(Header.QuadPatchCount * 16)); } else { _QuadNormals = new List<Vector3>(); } // Triangle Patches: // Gregory control points | 15 * trianglePatchCount * sizeof(float3) // Pm control points | 19 * trianglePatchCount * sizeof(float3) // Texture coordinates | 12 * trianglePatchCount * sizeof(float2) _TriGregoryControlPoints = new List<Vector3>(from.ReadStructure<Vector3>(Header.TriPatchCount * 15)); _TriPmControlPoints = new List<Vector3>(from.ReadStructure<Vector3>(Header.TriPatchCount * 19)); _TriTextureCoordinates = new List<Vector2>(from.ReadStructure<Vector2>(Header.TriPatchCount * 12)); // Part 2. Stencils: // faceTopologyCount | sizeof(int) // primitiveSize | sizeof(int) // Bezier Stencil | faceTopologyCount * 32 * m_primitiveSize * sizeof(float) // Gregory Stencil | faceTopologyCount * 20 * m_primitiveSize * sizeof(float) // regularFaceTopologyIndex | regularPatchCount * sizeof(uint) // regularVertexIndices | 16 * regularPatchCount * sizeof(uint) // regularStencilIndices | 16 * regularPatchCount * sizeof(uint) // quadVertexCount | quadPatchCount * sizeof(uint) // quadFaceTopologyIndex | quadPatchCount * sizeof(uint) // quadVertexIndices | primitiveSize * quadpatchCount * sizeof(uint) // quadStecilIndices | primitiveSize * quadPatchCount * sizeof(uint) FaceTopologyCount = from.ReadInt32(); PrimitiveSize = from.ReadInt32(); _BezierStencil = new List<float>(from.ReadStructure<float>(FaceTopologyCount * 32 * PrimitiveSize)); _GregoryStencil = new List<float>(from.ReadStructure<float>(FaceTopologyCount * 20 * PrimitiveSize)); _RegularFaceTopologyIndex = new List<int>(from.ReadStructure<int>(Header.RegularPatchCount)); _RegularVertexIndices = new List<int>(from.ReadStructure<int>(Header.RegularPatchCount * 16)); _RegularStencilIndices = new List<int>(from.ReadStructure<int>(Header.RegularPatchCount * 16)); _QuadFaceVertexCount = new List<int>(from.ReadStructure<int>(Header.QuadPatchCount)); _QuadFaceTopologyIndex = new List<int>(from.ReadStructure<int>(Header.QuadPatchCount)); _QuadVertexIndices = new List<int>(from.ReadStructure<int>(Header.QuadPatchCount * PrimitiveSize)); _QuadStencilIndices = new List<int>(from.ReadStructure<int>(Header.QuadPatchCount * PrimitiveSize)); // Part 3. Input Mesh Topology: // Vertex count | sizeof(uint) // Vertices | vertexCount * sizeof(float3) // Tangents/Bitangents | vertexCount * 2 * sizeof(float3) // Valences | vertexCount * sizeof(int) // Texture coordinates | vertexCount * sizeof(float2) // Max valence | sizeof(uint) // Regular face indices | 4 * regularPatchCount * sizeof(uint) // Quad face indices | 4 * irregularpatchCount * sizeof(uint) // Triangle face indices | 3 * trianglePatchCount * sizeof(uint) VertexCount = from.ReadInt32(); _Vertices = new List<Vector3>(from.ReadStructure<Vector3>(VertexCount)); _Tangents = new List<Vector3>(from.ReadStructure<Vector3>(VertexCount * 2)); _Valences = new List<int>(from.ReadStructure<int>(VertexCount)); _TextureCoordinates = new List<Vector2>(from.ReadStructure<Vector2>(VertexCount)); MaxValence = from.ReadInt32(); _RegularFaceIndices = new List<int>(from.ReadStructure<int>(Header.RegularPatchCount * 4)); _QuadFaceIndices = new List<int>(from.ReadStructure<int>(Header.QuadPatchCount * 4)); _TriFaceIndices = new List<int>(from.ReadStructure<int>(Header.TriPatchCount * 3)); #region Calculate the patch corner point indices relative to the patch // For each regular patch (bicubic bezier) set the indices for the // regular that define the face (i.e. the 4 corners) var regularIndices = new int[Header.RegularPatchCount * 4]; for (int k = 0; k < Header.RegularPatchCount; k++) { for (int j = 0; j < 4; j++) { for (int i = 0; i < 16; i++) { if (_RegularVertexIndices[k * 16 + i] == _RegularFaceIndices[k * 4 + j]) { regularIndices[k * 4 + j] = i; } } } } // For an irregular patch (Gregory patch) set the indices for the // patch that define the face (i.e. the 4 corners) var quadIndices = new int[Header.QuadPatchCount * 4]; for (int k = 0; k < Header.QuadPatchCount; k++) { for (int j = 0; j < 4; j++) { for (int i = 0; i < PrimitiveSize; i++) { if (_QuadVertexIndices[k * PrimitiveSize + i] == _QuadFaceIndices[k * 4 + j]) { quadIndices[k * 4 + j] = i; } } } } #endregion #region Compute input mesh indices var indexCount = 6 * Header.RegularPatchCount + 6 * Header.QuadPatchCount + 3 * Header.TriPatchCount; //var indexCount2 = 8 * Header.RegularPatchCount + 8 * Header.QuadPatchCount; var indices = new int[indexCount]; int idx = 0; for (int i = 0; i < Header.RegularPatchCount; i++) { indices[idx++] = _RegularFaceIndices[4 * i + 2]; indices[idx++] = _RegularFaceIndices[4 * i + 0]; indices[idx++] = _RegularFaceIndices[4 * i + 1]; indices[idx++] = _RegularFaceIndices[4 * i + 0]; indices[idx++] = _RegularFaceIndices[4 * i + 2]; indices[idx++] = _RegularFaceIndices[4 * i + 3]; } for (int i = 0; i < Header.QuadPatchCount; i++) { indices[idx++] = _QuadFaceIndices[4 * i + 2]; indices[idx++] = _QuadFaceIndices[4 * i + 0]; indices[idx++] = _QuadFaceIndices[4 * i + 1]; indices[idx++] = _QuadFaceIndices[4 * i + 0]; indices[idx++] = _QuadFaceIndices[4 * i + 2]; indices[idx++] = _QuadFaceIndices[4 * i + 3]; } for (int i = 0; i < Header.TriPatchCount; i++) { indices[idx++] = _TriFaceIndices[4 * i + 2]; indices[idx++] = _TriFaceIndices[4 * i + 0]; indices[idx++] = _TriFaceIndices[4 * i + 1]; } _Indices = new List<int>(indices); #endregion // Calculate the bounding box Vector3 minCorner = _Vertices[0]; Vector3 maxCorner = _Vertices[0]; foreach (var vertex in _Vertices) { if (minCorner.X > vertex.X) minCorner.X = vertex.X; else if (maxCorner.X < vertex.X) maxCorner.X = vertex.X; if (minCorner.Y > vertex.Y) minCorner.Y = vertex.Y; else if (maxCorner.Y < vertex.Y) maxCorner.Y = vertex.Y; if (minCorner.Z > vertex.Z) minCorner.Z = vertex.Z; else if (maxCorner.Z < vertex.Z) maxCorner.Z = vertex.Z; } Center = (minCorner + maxCorner) * 0.5f; }
public void Read(string path) { if (!File.Exists(path)) { System.Windows.Forms.MessageBox.Show("File not found - " + path, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); return; } var header = new FileIntegrityHeader(); var list = new List<FileIntegrityEntry>(); using (var reader = new BinaryReader(File.Open(FileIntegrity.FileName, FileMode.Open, FileAccess.Read))) { header = reader.ReadStructure<FileIntegrityHeader>(); for (int i = 0; i < header.EntryCount; i++) { var entry = new FileIntegrityEntry(); entry.FileNameLen = reader.ReadInt16(); entry.FileName = new string(reader.ReadChars(entry.FileNameLen)); entry.LastModifiedTime = reader.ReadInt64(); entry.Size = reader.ReadInt32(); entry.Checksum = reader.ReadInt32(); list.Add(entry); } } Scan(Environment.CurrentDirectory); foreach (var entry in Entries) { if (list.Contains(entry, new FileIntegrityEntryComparer())) { var lentry = list.Find(e => e.FileName == entry.FileName); if (lentry.LastModifiedTime != entry.LastModifiedTime) { System.Windows.Forms.MessageBox.Show("File time missmatch - " + entry.FileName); } if (lentry.Size != entry.Size) { System.Windows.Forms.MessageBox.Show("File size missmatch - " + entry.FileName); } if (lentry.Checksum != entry.Checksum) { System.Windows.Forms.MessageBox.Show("File checksum missmatch - " + entry.FileName); } } else { System.Windows.Forms.MessageBox.Show("File not found - " + entry.FileName); } } }
internal CVMDirectoryListingEntry(BinaryReader reader, CVMDirectoryListing originDirList) { _originDirList = originDirList; _header = reader.ReadStructure<DirectoryListingEntryHeader>(DirectoryListingEntryHeader.SIZE); }