diff --git a/Crowbar/Core/DebugLog/AccessedBytesDebugFile.vb b/Crowbar/Core/DebugLog/AccessedBytesDebugFile.vb new file mode 100644 index 0000000..7a20b29 --- /dev/null +++ b/Crowbar/Core/DebugLog/AccessedBytesDebugFile.vb @@ -0,0 +1,118 @@ +Imports System.IO + +Public Class AccessedBytesDebugFile + +#Region "Creation and Destruction" + + Public Sub New(ByVal outputFileStream As StreamWriter) + Me.theOutputFileStreamWriter = outputFileStream + End Sub + +#End Region + +#Region "Methods" + + Public Sub WriteHeaderComment() + Dim line As String = "" + + line = "// " + line += TheApp.GetHeaderComment() + Me.theOutputFileStreamWriter.WriteLine(line) + End Sub + + Public Sub WriteFileSeekLog(ByVal aFileSeekLog As FileSeekLog) + Dim line As String + + line = "====== File Size ======" + Me.WriteLogLine(0, line) + + line = aFileSeekLog.theFileSize.ToString("N0") + Me.WriteLogLine(1, line) + + line = "====== File Seek Log ======" + Me.WriteLogLine(0, line) + + line = "--- Summary ---" + Me.WriteLogLine(0, line) + + Dim offsetStart As Long + Dim offsetEnd As Long + offsetStart = -1 + offsetEnd = -1 + For i As Integer = 0 To aFileSeekLog.theFileSeekList.Count - 1 + If offsetStart = -1 Then + offsetStart = aFileSeekLog.theFileSeekList.Keys(i) + End If + offsetEnd = aFileSeekLog.theFileSeekList.Values(i) + + If aFileSeekLog.theFileSeekDescriptionList.Values(i).StartsWith("[ERROR] Unread bytes") Then + If i > 0 Then + line = offsetStart.ToString("N0") + " - " + (aFileSeekLog.theFileSeekList.Keys(i) - 1).ToString("N0") + Me.WriteLogLine(1, line) + End If + If aFileSeekLog.theFileSeekDescriptionList.Values(i).StartsWith("[ERROR] Unread bytes (all zeroes)") Then + line = aFileSeekLog.theFileSeekList.Keys(i).ToString("N0") + " - " + offsetEnd.ToString("N0") + " [ERROR] Unread bytes (all zeroes)" + Else + line = aFileSeekLog.theFileSeekList.Keys(i).ToString("N0") + " - " + offsetEnd.ToString("N0") + " [ERROR] Unread bytes (non-zero)" + End If + Me.WriteLogLine(1, line) + offsetStart = -1 + ElseIf (i = aFileSeekLog.theFileSeekList.Count - 1) OrElse (offsetEnd + 1 <> aFileSeekLog.theFileSeekList.Keys(i + 1)) Then + line = offsetStart.ToString("N0") + " - " + offsetEnd.ToString("N0") + Me.WriteLogLine(1, line) + offsetStart = -1 + End If + Next + + line = "------------------------" + Me.WriteLogLine(0, line) + line = "--- Each Section or Loop ---" + Me.WriteLogLine(0, line) + + offsetEnd = -1 + For i As Integer = 0 To aFileSeekLog.theFileSeekList.Count - 1 + offsetStart = aFileSeekLog.theFileSeekList.Keys(i) + offsetEnd = aFileSeekLog.theFileSeekList.Values(i) + + line = offsetStart.ToString("N0") + " - " + offsetEnd.ToString("N0") + " " + aFileSeekLog.theFileSeekDescriptionList.Values(i) + Me.WriteLogLine(1, line) + Next + + line = "========================" + Me.WriteLogLine(0, line) + End Sub + +#End Region + +#Region "Private Methods" + + Private Sub WriteFileSeparatorLines() + Dim line As String + + Me.WriteLogLine(0, "") + Me.WriteLogLine(0, "") + line = "################################################################################" + Me.WriteLogLine(0, line) + Me.WriteLogLine(0, "") + Me.WriteLogLine(0, "") + End Sub + + Private Sub WriteLogLine(ByVal indentLevel As Integer, ByVal line As String) + Dim indentedLine As String = "" + For i As Integer = 1 To indentLevel + indentedLine += vbTab + Next + indentedLine += line + Me.theOutputFileStreamWriter.WriteLine(indentedLine) + Me.theOutputFileStreamWriter.Flush() + End Sub + +#End Region + +#Region "Data" + + Private theOutputFileStreamWriter As StreamWriter + +#End Region + +End Class diff --git a/Crowbar/Core/DebugLog/DebugFormatModule.vb b/Crowbar/Core/DebugLog/DebugFormatModule.vb new file mode 100644 index 0000000..c246c8e --- /dev/null +++ b/Crowbar/Core/DebugLog/DebugFormatModule.vb @@ -0,0 +1,128 @@ +Module DebugFormatModule + + Public Function FormatByteWithHexLine(ByVal name As String, ByVal value As Byte) As String + Dim line As String + line = name + line += ": " + line += value.ToString("N0") + line += " (0x" + line += value.ToString("X2") + line += ")" + Return line + End Function + + Public Function FormatIntegerLine(ByVal name As String, ByVal value As Integer) As String + Dim line As String + line = name + line += ": " + line += value.ToString("N0") + Return line + End Function + + Public Function FormatIntegerAsHexLine(ByVal name As String, ByVal value As Integer) As String + Dim line As String + line = name + line += ": " + line += "0x" + line += value.ToString("X8") + Return line + End Function + + Public Function FormatIntegerWithHexLine(ByVal name As String, ByVal value As Integer) As String + Dim line As String + line = name + line += ": " + line += value.ToString("N0") + line += " (0x" + line += value.ToString("X8") + line += ")" + Return line + End Function + + Public Function FormatLongWithHexLine(ByVal name As String, ByVal value As Long) As String + Dim line As String + line = name + line += ": " + line += value.ToString("N0", TheApp.InternalNumberFormat) + line += " (0x" + line += value.ToString("X16") + line += ")" + Return line + End Function + + Public Function FormatSingleFloatLine(ByVal name As String, ByVal value As Single) As String + Dim line As String + line = name + line += ": " + line += value.ToString("N6", TheApp.InternalNumberFormat) + Return line + End Function + + Public Function FormatDoubleFloatLine(ByVal name As String, ByVal value As Double) As String + Dim line As String + line = name + line += ": " + line += value.ToString("N6", TheApp.InternalNumberFormat) + Return line + End Function + + Public Function FormatStringLine(ByVal name As String, ByVal value As String) As String + Dim line As String + line = name + line += ": " + line += CStr(value).TrimEnd(Chr(0)) + Return line + End Function + + Public Function FormatIndexLine(ByVal name As String, ByVal value As Integer) As String + Dim line As String + line = "[" + line += name + line += " index: " + line += value.ToString("N0") + line += "]" + Return line + End Function + + Public Function FormatVectorLine(ByVal name As String, ByVal x As Double, ByVal y As Double, ByVal z As Double) As String + Dim line As String + line = name + line += "[x,y,z]: (" + line += x.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += y.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += z.ToString("N6", TheApp.InternalNumberFormat) + line += ")" + Return line + End Function + + Public Function FormatVectorLine(ByVal name As String, ByVal value As SourceVector) As String + Dim line As String + line = name + line += "[x,y,z]: (" + line += value.x.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += value.y.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += value.z.ToString("N6", TheApp.InternalNumberFormat) + line += ")" + Return line + End Function + + Public Function FormatQuaternionLine(ByVal name As String, ByVal value As SourceQuaternion) As String + Dim line As String + line = name + line += "[x,y,z,w]: (" + line += value.x.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += value.y.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += value.z.ToString("N6", TheApp.InternalNumberFormat) + line += ", " + line += value.w.ToString("N6", TheApp.InternalNumberFormat) + line += ")" + Return line + End Function + +End Module diff --git a/Crowbar/Core/DebugLog/FileSeekLog.vb b/Crowbar/Core/DebugLog/FileSeekLog.vb new file mode 100644 index 0000000..410d5fa --- /dev/null +++ b/Crowbar/Core/DebugLog/FileSeekLog.vb @@ -0,0 +1,186 @@ +Imports System.IO + +Public Class FileSeekLog + + Public Sub New() + Me.theFileSeekList = New SortedList(Of Long, Long) + Me.theFileSeekDescriptionList = New SortedList(Of Long, String) + End Sub + + Public Function ContainsKey(ByVal startOffset As Long) As Boolean + Return Me.theFileSeekList.ContainsKey(startOffset) + End Function + + Public Sub Add(ByVal startOffset As Long, ByVal endOffset As Long, ByVal description As String) + Try + If Me.theFileSeekList.ContainsKey(startOffset) AndAlso Me.theFileSeekList(startOffset) = endOffset Then + Me.theFileSeekDescriptionList(startOffset) += "; " + description + ElseIf Me.theFileSeekList.ContainsKey(startOffset) Then + Dim temp As String + temp = Me.theFileSeekDescriptionList(startOffset) + Me.theFileSeekDescriptionList(startOffset) = "[ERROR] " + Me.theFileSeekDescriptionList(startOffset) += temp + "; [" + startOffset.ToString() + " - " + endOffset.ToString() + "] " + description + Else + Me.theFileSeekList.Add(startOffset, endOffset) + Me.theFileSeekDescriptionList.Add(startOffset, description) + End If + Catch ex As Exception + Dim debug As Integer = 4242 + End Try + End Sub + + Public Sub Clear() + Me.theFileSeekList.Clear() + Me.theFileSeekDescriptionList.Clear() + End Sub + + Public Property FileSize() As Long + Get + Return Me.theFileSize + End Get + Set(value As Long) + If Me.theFileSize <> value Then + Me.theFileSize = value + Me.Add(Me.theFileSize, Me.theFileSize, "END OF FILE + 1 (File size)") + End If + End Set + End Property + + Public Sub LogToEndAndAlignToNextStart(ByVal inputFileReader As BinaryReader, ByVal fileOffsetEnd As Long, ByVal byteAlignmentCount As Integer, ByVal description As String, Optional ByVal expectedAlignOffsetEnd As Long = -1) + Dim fileOffsetStart2 As Long + Dim fileOffsetEnd2 As Long + + fileOffsetStart2 = fileOffsetEnd + 1 + fileOffsetEnd2 = MathModule.AlignLong(fileOffsetStart2, byteAlignmentCount) - 1 + inputFileReader.BaseStream.Seek(fileOffsetEnd2 + 1, SeekOrigin.Begin) + If fileOffsetEnd2 >= fileOffsetStart2 Then + Dim allZeroesWereFound As Boolean + If expectedAlignOffsetEnd > -1 AndAlso expectedAlignOffsetEnd <> fileOffsetEnd2 Then + description = "[ERROR: Should end at " + CStr(expectedAlignOffsetEnd) + "] " + description + description += " - " + fileOffsetStart2.ToString() + ":" + Me.GetByteValues(inputFileReader, fileOffsetStart2, fileOffsetEnd2, allZeroesWereFound) + description += " - " + (fileOffsetEnd2 + 1).ToString() + ":" + Me.GetByteValues(inputFileReader, fileOffsetEnd2 + 1, expectedAlignOffsetEnd, allZeroesWereFound) + Else + description += Me.GetByteValues(inputFileReader, fileOffsetStart2, fileOffsetEnd2, allZeroesWereFound) + End If + + Me.Add(fileOffsetStart2, fileOffsetEnd2, description) + End If + End Sub + + Public Sub LogAndAlignFromFileSeekLogEnd(ByVal inputFileReader As BinaryReader, ByVal byteAlignmentCount As Integer, ByVal description As String) + 'Dim fileOffsetStart2 As Long + 'Dim fileOffsetEnd2 As Long + + 'fileOffsetStart2 = Me.theFileSeekList.Values(Me.theFileSeekList.Count - 1) + 1 + 'fileOffsetEnd2 = MathModule.AlignLong(fileOffsetStart2, byteAlignmentCount) - 1 + 'inputFileReader.BaseStream.Seek(fileOffsetEnd2 + 1, SeekOrigin.Begin) + 'If fileOffsetEnd2 >= fileOffsetStart2 Then + ' Me.Add(fileOffsetStart2, fileOffsetEnd2, description) + 'End If + 'Me.LogToEndAndAlignToNextStart(inputFileReader, Me.theFileSeekList.Values(Me.theFileSeekList.Count - 1), byteAlignmentCount, description) + 'NOTE: The "- 2" skips the final value that should be the "END OF FILE + 1 (File size)". + Me.LogToEndAndAlignToNextStart(inputFileReader, Me.theFileSeekList.Values(Me.theFileSeekList.Count - 2), byteAlignmentCount, description) + End Sub + + Public Sub LogUnreadBytes(ByVal inputFileReader As BinaryReader) + Dim offsetStart As Long + Dim offsetEnd As Long + Dim description As String + Dim byteValues As String + Dim allZeroesWereFound As Boolean + Dim tempFileSeekList As New SortedList(Of Long, Long)() + Dim tempFileSeekDescriptionList As New SortedList(Of Long, String)() + + offsetStart = -1 + offsetEnd = -1 + Try + For i As Integer = 0 To Me.theFileSeekList.Count - 1 + offsetStart = Me.theFileSeekList.Keys(i) + + If offsetEnd < offsetStart - 1 Then + description = "[ERROR] Unread bytes" + byteValues = Me.GetByteValues(inputFileReader, offsetEnd + 1, offsetStart - 1, allZeroesWereFound) + If allZeroesWereFound Then + description += " (all zeroes)" + Else + description += " (non-zero)" + End If + description += byteValues + + ' Can't add into the list that is being iterated, so use temp list. + If Me.theFileSeekList.ContainsKey(offsetEnd + 1) Then + Dim temp As String + temp = Me.theFileSeekDescriptionList(offsetEnd + 1) + Me.theFileSeekDescriptionList(offsetEnd + 1) = "[ERROR] " + Me.theFileSeekDescriptionList(offsetEnd + 1) += temp + "; [" + (offsetEnd + 1).ToString() + " - " + (offsetStart - 1).ToString() + "] " + description + Else + tempFileSeekList.Add(offsetEnd + 1, offsetStart - 1) + tempFileSeekDescriptionList.Add(offsetEnd + 1, description) + End If + End If + + offsetEnd = Me.theFileSeekList.Values(i) + Next + + For i As Integer = 0 To tempFileSeekList.Count - 1 + Me.Add(tempFileSeekList.Keys(i), tempFileSeekList.Values(i), tempFileSeekDescriptionList.Values(i)) + Next + Catch ex As Exception + Dim debug As Integer = 4242 + End Try + End Sub + + Public theFileSize As Long + Public theFileSeekList As SortedList(Of Long, Long) + Public theFileSeekDescriptionList As SortedList(Of Long, String) + + Private Function GetByteValues(ByVal inputFileReader As BinaryReader, ByVal fileOffsetStart2 As Long, ByVal fileOffsetEnd2 As Long, ByRef allZeroesWereFound As Boolean) As String + Dim byteValues As String + + Dim inputFileStreamPosition As Long + inputFileStreamPosition = inputFileReader.BaseStream.Position + + Dim byteValue As Byte + + Dim adjustedFileOffsetEnd2 As Long + If (fileOffsetEnd2 - fileOffsetStart2) > 20 Then + adjustedFileOffsetEnd2 = fileOffsetStart2 + 20 + Else + adjustedFileOffsetEnd2 = fileOffsetEnd2 + End If + + Try + inputFileReader.BaseStream.Seek(fileOffsetStart2, SeekOrigin.Begin) + allZeroesWereFound = True + byteValues = " [" + For byteOffset As Long = fileOffsetStart2 To adjustedFileOffsetEnd2 + byteValue = inputFileReader.ReadByte() + byteValues += " " + byteValue.ToString("X2") + If byteValue <> 0 Then + allZeroesWereFound = False + End If + Next + If (fileOffsetEnd2 - fileOffsetStart2) > 20 Then + byteValues += " ..." + ''NOTE: Indicate non-zeroes if more than 20 bytes unread because might be non-zeroes past the first 20. + 'allZeroesWereFound = False + For byteOffset As Long = adjustedFileOffsetEnd2 + 1 To fileOffsetEnd2 + byteValue = inputFileReader.ReadByte() + If byteValue <> 0 Then + allZeroesWereFound = False + Exit For + End If + Next + End If + byteValues += " ]" + + inputFileReader.BaseStream.Seek(inputFileStreamPosition, SeekOrigin.Begin) + Catch ex As Exception + allZeroesWereFound = False + byteValues = "[incomplete read due to error]" + End Try + + Return byteValues + End Function + +End Class diff --git a/Crowbar/Core/DebugLog/UnknownValue.vb b/Crowbar/Core/DebugLog/UnknownValue.vb new file mode 100644 index 0000000..98a331b --- /dev/null +++ b/Crowbar/Core/DebugLog/UnknownValue.vb @@ -0,0 +1,8 @@ +Public Class UnknownValue + + Public offset As Long + ' "Byte" or "Int32" + Public type As String + Public value As Integer + +End Class