@@ -3,17 +3,17 @@ Imports System.IO
33Imports System.Net
44Imports System.Text.RegularExpressions
55Imports System.Windows.Media.Animation
6+ Imports System.Windows.Threading
67
78Public Class NewMainWindow
89
910 Dim WithEvents HDL_Dump As New Process()
10- Dim WithEvents WNBDConnectClient As New Process()
1111
1212 Public Shared MountedDrive As MountedPSXDrive
1313 Dim HDLGameID As String = ""
1414 Dim CurrentProjectDirectory As String = ""
1515
16- 'Public WithEvents HDL_DumpWorker As New BackgroundWorker() With {.WorkerReportsProgress = True }
16+ Dim WithEvents ConnectDelay As New DispatcherTimer With {.Interval = TimeSpan.FromSeconds( 2 ) }
1717 Dim WithEvents ContentDownloader As New WebClient()
1818
1919 Public Structure MountedPSXDrive
@@ -201,6 +201,10 @@ Public Class NewMainWindow
201201 End Sub
202202
203203 Private Function IsNBDConnected() As Boolean
204+
205+ Dim ProcessOutput As String ()
206+ Dim NBDDriveName As String = ""
207+
204208 Using WNBDClient As New Process()
205209
206210 If File.Exists(My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Ceph\bin\wnbd-client.exe" ) Then
@@ -214,49 +218,39 @@ Public Class NewMainWindow
214218 WNBDClient.StartInfo.UseShellExecute = False
215219 WNBDClient.StartInfo.CreateNoWindow = True
216220 WNBDClient.Start()
221+ WNBDClient.WaitForExit()
217222
218223 Dim OutputReader As StreamReader = WNBDClient.StandardOutput
219- Dim ProcessOutput As String = OutputReader.ReadToEnd()
224+ ProcessOutput = OutputReader.ReadToEnd().Split({vbCrLf}, StringSplitOptions.None)
225+ End Using
220226
221- If ProcessOutput.Contains( "wnbd-client" ) Then 'This is only shown when a drive is actually mounted
227+ For Each ReturnedLine As String In ProcessOutput
228+ If ReturnedLine.Contains( "wnbd-client" ) Then
229+ NBDDriveName = ReturnedLine.Split( New String () { " " }, StringSplitOptions.RemoveEmptyEntries)( 4 ).Trim()
230+ Exit For
231+ End If
232+ Next
222233
223- If ProcessOutput.Contains( "PS2HDD" ) Then 'Used by PFS BatchKit Manager
224- If Dispatcher.CheckAccess() = False Then
225- Dispatcher.BeginInvoke( Sub ()
226- MountedDrive.NBDDriveName = "PS2HDD"
227- PSXIPTextBox.Text = GetConnectedNBDIP( "PS2HDD" )
228- End Sub )
229- Else
230- MountedDrive.NBDDriveName = "PS2HDD"
231- PSXIPTextBox.Text = GetConnectedNBDIP( "PS2HDD" )
232- End If
233- ElseIf ProcessOutput.Contains( "PSXHDD" ) Then 'Used by PSX XMB Manager
234- If Dispatcher.CheckAccess() = False Then
235- Dispatcher.BeginInvoke( Sub ()
236- MountedDrive.NBDDriveName = "PSXHDD"
237- PSXIPTextBox.Text = GetConnectedNBDIP( "PSXHDD" )
238- End Sub )
239- Else
240- MountedDrive.NBDDriveName = "PSXHDD"
241- PSXIPTextBox.Text = GetConnectedNBDIP( "PSXHDD" )
242- End If
243- End If
234+ If Not String .IsNullOrEmpty(NBDDriveName) Then
244235
245- If Dispatcher.CheckAccess() = False Then
246- Dispatcher.BeginInvoke( Sub ()
247- MountedDrive.HDLDriveName = GetHDLDriveName()
248- MountedDrive.DriveID = GetHDDID()
249- End Sub )
250- Else
251- MountedDrive.HDLDriveName = GetHDLDriveName()
252- MountedDrive.DriveID = GetHDDID()
253- End If
236+ MountedDrive.NBDDriveName = NBDDriveName
254237
255- Return True
238+ If PSXIPTextBox.Dispatcher.CheckAccess() = False Then
239+ PSXIPTextBox.Dispatcher.BeginInvoke( Sub ()
240+ PSXIPTextBox.Text = GetConnectedNBDIP(NBDDriveName)
241+ End Sub )
256242 Else
257- Return False
243+ PSXIPTextBox.Text = GetConnectedNBDIP(NBDDriveName)
258244 End If
259- End Using
245+
246+ MountedDrive.HDLDriveName = GetHDLDriveName()
247+ MountedDrive.DriveID = GetHDDID()
248+
249+ Return True
250+ Else
251+ Return False
252+ End If
253+
260254 End Function
261255
262256 Private Function IsLocalHDDConnected() As Boolean
@@ -303,6 +297,7 @@ Public Class NewMainWindow
303297
304298 Private Function GetConnectedNBDIP(NBDDriveName As String ) As String
305299
300+ Dim ProcessOutput As String ()
306301 Dim NBDIP As String = ""
307302
308303 'Get the connected IP address
@@ -319,18 +314,19 @@ Public Class NewMainWindow
319314 WNBDClient.StartInfo.UseShellExecute = False
320315 WNBDClient.StartInfo.CreateNoWindow = True
321316 WNBDClient.Start()
317+ WNBDClient.WaitForExit()
322318
323319 Dim OutputReader As StreamReader = WNBDClient.StandardOutput
324- Dim ProcessOutput As String () = OutputReader.ReadToEnd().Split({vbCrLf}, StringSplitOptions.None)
325-
326- For Each ReturnedLine As String In ProcessOutput
327- If ReturnedLine.Contains( "Hostname" ) Then
328- NBDIP = ReturnedLine.Split( ":"c )( 1 ).Trim()
329- Exit For
330- End If
331- Next
320+ ProcessOutput = OutputReader.ReadToEnd().Split({vbCrLf}, StringSplitOptions.None)
332321 End Using
333322
323+ For Each ReturnedLine As String In ProcessOutput
324+ If ReturnedLine.Contains( "Hostname" ) Then
325+ NBDIP = ReturnedLine.Split( ":"c )( 1 ).Trim()
326+ Exit For
327+ End If
328+ Next
329+
334330 Return NBDIP
335331 End Function
336332
@@ -345,6 +341,7 @@ Public Class NewMainWindow
345341 HDLDump.StartInfo.UseShellExecute = False
346342 HDLDump.StartInfo.CreateNoWindow = True
347343 HDLDump.Start()
344+ HDLDump.WaitForExit()
348345
349346 'Read the output
350347 Dim OutputReader As StreamReader = HDLDump.StandardOutput
@@ -389,6 +386,7 @@ Public Class NewMainWindow
389386 WMIC.StartInfo.UseShellExecute = False
390387 WMIC.StartInfo.CreateNoWindow = True
391388 WMIC.Start()
389+ WMIC.WaitForExit()
392390
393391 'Read the output
394392 Dim OutputReader As StreamReader = WMIC.StandardOutput
@@ -406,9 +404,9 @@ Public Class NewMainWindow
406404 End If
407405 End If
408406 Next
409-
410- Return DriveID
411407 End Using
408+
409+ Return DriveID
412410 End Function
413411
414412 Public Sub ReloadProjects()
@@ -564,22 +562,20 @@ Public Class NewMainWindow
564562 Private Sub ConnectButton_Click(sender As Object , e As RoutedEventArgs) Handles ConnectButton.Click
565563 If ConnectButton.Content.ToString = "Connect" Then
566564
567- WNBDConnectClient = New Process()
568-
569- If File.Exists(My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Ceph\bin\wnbd-client.exe" ) Then
570- WNBDConnectClient.StartInfo.FileName = My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Ceph\bin\wnbd-client.exe"
571- Else
572- WNBDConnectClient.StartInfo.FileName = My.Computer.FileSystem.CurrentDirectory + "\Tools\wnbd-client.exe"
573- End If
565+ Using WNBDConnectClient As New Process()
566+ If File.Exists(My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Ceph\bin\wnbd-client.exe" ) Then
567+ WNBDConnectClient.StartInfo.FileName = My.Computer.FileSystem.SpecialDirectories.ProgramFiles + "\Ceph\bin\wnbd-client.exe"
568+ Else
569+ WNBDConnectClient.StartInfo.FileName = My.Computer.FileSystem.CurrentDirectory + "\Tools\wnbd-client.exe"
570+ End If
574571
575- WNBDConnectClient.StartInfo.Arguments = "map PSXHDD " + PSXIPTextBox.Text
576- WNBDConnectClient.StartInfo.RedirectStandardError = True
577- AddHandler WNBDConnectClient.ErrorDataReceived, AddressOf ErrorDataHandler
578- WNBDConnectClient.StartInfo.UseShellExecute = False
579- WNBDConnectClient.StartInfo.CreateNoWindow = True
572+ WNBDConnectClient.StartInfo.Arguments = "map PSXHDD " + PSXIPTextBox.Text
573+ WNBDConnectClient.StartInfo.UseShellExecute = False
574+ WNBDConnectClient.StartInfo.CreateNoWindow = True
575+ WNBDConnectClient.Start()
576+ End Using
580577
581- WNBDConnectClient.Start()
582- WNBDConnectClient.BeginErrorReadLine()
578+ ConnectDelay.Start()
583579
584580 ElseIf ConnectButton.Content.ToString = "Disconnect" Then
585581
@@ -1028,53 +1024,6 @@ Public Class NewMainWindow
10281024 CreateGamePartition()
10291025 End Sub
10301026
1031- Public Sub ErrorDataHandler(sender As Object , e As DataReceivedEventArgs)
1032- If Not String .IsNullOrEmpty(e.Data) Then
1033- 'libwnbd.dll!WnbdIoctlCreate ERROR Could not create WNBD disk. - Probably NBD driver not correctly installed or just entered the wrong IP
1034- If e.Data.Contains( "libwnbd.dll!WnbdIoctlCreate" ) Then
1035- WNBDConnectClient.CancelErrorRead()
1036- MsgBox( "Could not map your PSX HDD, please check if your NBD server is running and set up correctly." + vbCrLf + "Also check if you entered the correct IP address." , MsgBoxStyle.Exclamation)
1037- Exit Sub
1038- ElseIf e.Data.Contains( "the option '--hostname' is required but missing" ) Then
1039- WNBDConnectClient.CancelErrorRead()
1040- MsgBox( "Please enter an IP address." , MsgBoxStyle.Exclamation)
1041- Exit Sub
1042- ElseIf e.Data.Contains( "INFO Successfully connected to NBD server" ) Then
1043-
1044- 'Cancel reading the connection status
1045- WNBDConnectClient.CancelErrorRead()
1046-
1047- If IsNBDConnected() Then
1048-
1049- If InstallButton.CheckAccess() = False Then
1050- InstallButton.Dispatcher.BeginInvoke( Sub () InstallButton.IsEnabled = True )
1051- Else
1052- InstallButton.IsEnabled = True
1053- End If
1054-
1055- If NBDConnectionLabel.CheckAccess() = False Then
1056- NBDConnectionLabel.Dispatcher.BeginInvoke( Sub ()
1057- NBDConnectionLabel.Text = "Connected"
1058- NBDConnectionLabel.Foreground = Brushes.Green
1059- End Sub )
1060- Else
1061- NBDConnectionLabel.Text = "Connected"
1062- NBDConnectionLabel.Foreground = Brushes.Green
1063- End If
1064-
1065- If ConnectButton.CheckAccess() = False Then
1066- ConnectButton.Dispatcher.BeginInvoke( Sub () ConnectButton.Content = "Disconnect" )
1067- Else
1068- ConnectButton.Content = "Disconnect"
1069- End If
1070-
1071- MsgBox( "Your PSX HDD is now connected." + vbCrLf + "You can now install your project on the PSX." , MsgBoxStyle.Information)
1072- End If
1073-
1074- End If
1075- End If
1076- End Sub
1077-
10781027 Private Sub ProjectsMenuItem_Click(sender As Object , e As RoutedEventArgs) Handles ProjectsMenuItem.Click
10791028
10801029 'Switch to the ProjectsGrid
@@ -1134,4 +1083,38 @@ Public Class NewMainWindow
11341083 Windows.Application.Current.Shutdown()
11351084 End Sub
11361085
1086+ Private Sub ConnectDelay_Tick(sender As Object , e As EventArgs) Handles ConnectDelay.Tick
1087+ 'Get drive properties after the connect delay
1088+ If IsNBDConnected() Then
1089+
1090+ If InstallButton.CheckAccess() = False Then
1091+ InstallButton.Dispatcher.BeginInvoke( Sub () InstallButton.IsEnabled = True )
1092+ Else
1093+ InstallButton.IsEnabled = True
1094+ End If
1095+
1096+ If NBDConnectionLabel.CheckAccess() = False Then
1097+ NBDConnectionLabel.Dispatcher.BeginInvoke( Sub ()
1098+ NBDConnectionLabel.Text = "Connected"
1099+ NBDConnectionLabel.Foreground = Brushes.Green
1100+ End Sub )
1101+ Else
1102+ NBDConnectionLabel.Text = "Connected"
1103+ NBDConnectionLabel.Foreground = Brushes.Green
1104+ End If
1105+
1106+ If ConnectButton.CheckAccess() = False Then
1107+ ConnectButton.Dispatcher.BeginInvoke( Sub () ConnectButton.Content = "Disconnect" )
1108+ Else
1109+ ConnectButton.Content = "Disconnect"
1110+ End If
1111+
1112+ MsgBox( "Your PSX HDD is now connected." + vbCrLf + "You can now install your project on the PSX." , MsgBoxStyle.Information)
1113+ Else
1114+ MsgBox( "Could not connect to the PSX." + vbCrLf + "Please check your IP address." , MsgBoxStyle.Exclamation)
1115+ End If
1116+
1117+ ConnectDelay.Stop()
1118+ End Sub
1119+
11371120End Class
0 commit comments