Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Module errorChecker
Sub Main()
Console.WriteLine("=========================================================")
Console.WriteLine(" errorChecker.exe - by z@c")
Console.WriteLine("=========================================================")
Environment.ExitCode = mainF(Environment.GetCommandLineArgs())
Console.WriteLine("=========================================================")
End Sub
Function mainF(ByVal args As String()) As Integer
Dim logFile As String = "", logFileContents As String, launchstring As String = "", blaunch As Boolean = False
For x As Integer = 1 To args.Length - 1
If args(x) = "-launch" Then
blaunch = True
ElseIf blaunch Then
launchstring &= " " & IIf(args(x).Contains(" "), """" & args(x).Replace("""", "") & """", args(x))
ElseIf Trim(args(x)).Length > 0 Then
logFile = Trim(args(x))
End If
Next x
If launchstring.Length > 0 Then launchstring = launchstring.Substring(1)
If logFile = "" Then
Console.WriteLine("Error: no logfile specified")
usage()
Return 1
End If
Try
Console.WriteLine("Reading: " + logFile)
logFileContents = File.ReadAllText(logFile)
If logFileContents.Length = 0 Then Throw New Exception("File is empty!")
Catch ex As Exception
Console.WriteLine("Error reading log file: " & ex.Message)
Return 1
End Try
If Not File.Exists("errorData.txt") Or File.GetCreationTime("errorData.txt").AddDays(7) < DateTime.Now Then
Dim result As Boolean = downloadErrorList()
If Not result And Not File.Exists("errorData.txt") Then Return 1
End If
Dim e As Integer = 0, w As Integer = 0, n As Integer = 0, outp As String = "", sAllErrors As String = "", iLine As Integer = 1, numErrors As Integer
Dim sErrors As Dictionary(Of String, String) = New Dictionary(Of String, String)
Try
Dim lines As String() = File.ReadAllLines("errorData.txt")
Dim regex As Regex, matches As MatchCollection
Console.WriteLine("Reading {0} errors from errorData.txt", lines(0))
If Not Integer.TryParse(lines(0), numErrors) Then Throw New Exception()
Do
regex = New Regex(lines(iLine).Substring(2), RegexOptions.IgnoreCase + RegexOptions.Multiline)
matches = regex.Matches(logFileContents)
If matches.Count > 0 Then
Select Case lines(iLine)(0)
Case "0"
n += matches.Count
Case "1"
n += matches.Count
Case "2"
w += matches.Count
Case "3"
w += matches.Count
Case Else
e += matches.Count
End Select
For Each mtch As Match In matches
If mtch.Success() And Not sErrors.ContainsKey(mtch.Value) Then
Dim currOutp As String = lines(iLine + 1)
For y As Integer = 1 To mtch.Groups.Count()
currOutp = currOutp.Replace("[sub:" & y & "]", mtch.Groups(y).Value)
Next
sErrors.Add(mtch.Value, "" & mtch.Value & "" & currOutp)
End If
Next
End If
iLine += 2
Loop While iLine < numErrors * 2
Do
sAllErrors &= lines(iLine) & vbCrLf
iLine += 1
Loop While iLine < lines.Length
For Each kv As KeyValuePair(Of String, String) In sErrors
logFileContents = logFileContents.Replace(kv.Key, kv.Value)
Next
'get output
Dim asm As System.Reflection.[Assembly] = System.Reflection.[Assembly].GetExecutingAssembly()
Dim reader As New System.IO.StreamReader(asm.GetManifestResourceStream(asm.GetName().Name + ".output.htm"))
outp = reader.ReadToEnd()
reader.Close()
Dim vmfName = Path.GetFileNameWithoutExtension(logFile)
If vmfName.Contains(" ") Then
logFileContents = "
Mapname containing spaces
Your mapname contains spaces, which are not allowed in map's names, and may cause weird errors
" & logFileContents
End If
logFileContents = logFileContents.Replace(vbLf, "
")
outp = outp.Replace("__contents__", logFileContents)
outp = outp.Replace("__e__", e)
outp = outp.Replace("__w__", w)
outp = outp.Replace("__n__", n)
outp = outp.Replace("__version__", My.Application.Info.Version.ToString())
outp = outp.Replace("__file__", vmfName) 'mapname
outp = outp.Replace("__date__", Date.Now.ToString("f"))
outp = outp.Replace("__errors__", sAllErrors)
Catch ex As Exception
File.Delete("errorData.txt")
Console.WriteLine("Error while parsing errorData.txt, file possibly corrupt; deleted")
Console.WriteLine("Please try again")
Return 1
End Try
Console.WriteLine("Found: {0} errors, {1} warnings, {2} notices", e, w, n)
Try
logFile = logFile.Substring(0, logFile.LastIndexOf(".") + 1) & "htm"
Console.WriteLine("Writing: {0}", logFile)
If File.Exists(logFile) Then File.Delete(logFile)
If e > 0 Or w > 0 Or n > 0 Then
File.WriteAllText(logFile, outp)
If e > 0 Or w > 0 Then
System.Diagnostics.Process.Start(logFile)
If launchstring.Length < 3 Then Return 1
Console.WriteLine("Waiting for ok...")
If MsgBox("There were errors or warnings in the logfile. Do you want to continue launching the game?", MsgBoxStyle.YesNo, "errorChecker") = MsgBoxResult.No Then Throw New Exception("Mod launch aborted")
ElseIf n > 0 Then
System.Diagnostics.Process.Start(logFile)
End If
End If
If launchstring.Length > 3 Then Shell(launchstring, AppWinStyle.NormalFocus)
Return 0
Catch ex As Exception
Console.WriteLine(ex.Message)
Return 1
End Try
End Function
Sub usage()
Console.WriteLine("=========================================================")
Console.WriteLine(" Usage of errorChecker")
Console.WriteLine("=========================================================")
Console.WriteLine("errorChecker.exe C:\steam\steamapps\srcsd\maps\mymap.log")
Console.WriteLine("or, if the path contains a space:")
Console.WriteLine("errorChecker.exe ""C:\steam\steamapps\srcsd\all my maps\mymap.log""")
Console.WriteLine("to launch a game, depending on whether or not errors are found, use -launch, and put all parameters after that:")
Console.WriteLine("errorChecker.exe D:\mylog.log -launch ""C:\program files\steam\hl2.exe"" -parameter1 ""D:\other parameter\map.bsp""")
Console.WriteLine("Again, add quotes around the parameters if they contain a space")
End Sub
Function downloadErrorList() As Boolean
Console.WriteLine("Downloading error data...")
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Try
request = WebRequest.Create("http://www.interlopers.net/includes/errorpage/errorChecker.txt")
response = request.GetResponse
Catch ex As Exception
Console.WriteLine("An error occurred while downloading file. Possibe causes:" & vbCrLf & "1) File doesn't exist" & vbCrLf & "2) Remote server error")
Return False
End Try
Dim nRead As Long = 0
Dim nLength As Long = response.ContentLength
Dim fs As IO.FileStream
Try
fs = IO.File.OpenWrite("_errorData.txt")
Console.Write("Downloading: 0 bytes of " & nLength)
Do
Dim readBytes(1023) As Byte
Dim bytesread As Integer = response.GetResponseStream.Read(readBytes, 0, 1024)
nRead += bytesread
Console.CursorLeft = 13
Console.Write(nRead & " bytes of " & nLength)
If bytesread = 0 Then Exit Do
fs.Write(readBytes, 0, bytesread)
Loop
fs.Close()
File.Delete("errorData.txt")
File.Move("_errorData.txt", "errorData.txt")
Catch ex As Exception
Console.WriteLine(vbCrLf & "Can't open errorData.txt for writing")
File.Delete("_errorData.txt")
Return False
End Try
Console.WriteLine(vbCrLf & "Download finished")
fs.Close()
response.Close()
Return True
End Function
End Module