Advertisement
Combreal

BHS.ps1

Dec 22nd, 2023 (edited)
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Join Halo CE servers conveniently.
  4. PS2EXE can be used to turn this script into an executabe :
  5. ps2exe ".\BHS.ps1" "BHS.exe"  -noConsole -version "1.0.0.1" -title "BHS" -description "Join Halo CE servers conveniently" -iconFile ".\BHS.ico"
  6. MT can be used to enforced UAC as this tool will need to be run as Adminstrator if installed in HaloCE folder :
  7. mt.exe -manifest C:\Temp\MISC\BHS.manifest "-outputresource:C:\Temp\MISC\BHS.exe;#1"
  8.  
  9. .NOTES
  10. This tool generates a configuration files and therefore must be installed somewhere it can write or else be run as Admnistrator.
  11. #>
  12.  
  13.  
  14. Add-Type -AssemblyName System.Windows.Forms
  15. Add-Type -AssemblyName System.Drawing
  16.  
  17. $ScriptDirectory = If (-Not $PSScriptRoot) { Split-Path -Parent (Convert-Path ([environment]::GetCommandLineArgs()[0])) } Else { $PSScriptRoot }
  18. $serversListPath = "$ScriptDirectory\BHS_ServersList"
  19. $commandLineArgumentsPath = "$ScriptDirectory\BHS_CommandLineArguments"
  20.  
  21. Function Help {
  22.     $helpMessage = "`n    Join Halo CE servers conveniently.`n
  23.    This tool generates configuration files and therefore must be`n    installed somewhere it can write or else be run as Admnistrator.`n`n"
  24.     [void][System.Windows.Forms.MessageBox]::Show($helpMessage, "BHS Helper")
  25. }
  26.  
  27. Function Get-CommandLineArguments {
  28.     $commandlineArguments = ""
  29.     If (Test-Path $commandLineArgumentsPath) {
  30.         $commandlineArguments = Get-Content $commandLineArgumentsPath
  31.     }
  32.     Return $commandlineArguments
  33. }
  34.  
  35. Function Start-Game {
  36.     $joinOptions = Get-CommandLineArguments
  37.     $IP = $serversDataGridView.CurrentRow.Cells["IP:PORT"].Value
  38.     If (Select-String -InputObject $IP -Pattern "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{4,5}$") {  
  39.         $officialArguments = @("Nosound", "Novideo", "Nojoystick", "Nogamma", "Use20", "Use14", "Use11", "UseFF", "Safemode", "Window", "Width640", "Screenshot", "Timedemo", "Console", "Devmode")
  40.         $BrokenGameArguments = $False
  41.         $joinOptions.Split("-") | % {
  42.             If (-Not [string]::IsNullOrEmpty($_)) {
  43.                 If ($officialArguments -notcontains $($_ -replace '\s','')) {
  44.                     [System.Windows.Forms.MessageBox]::Show("Command line arguments are incorrect.", "" , '0',  'Error')
  45.                     $BrokenGameArguments = $True
  46.                 }
  47.             }
  48.         }
  49.         If (-Not $BrokenGameArguments) {
  50.             $joinOptions = $joinOptions + " -Connect $IP"
  51.             Start-Process -WorkingDirectory "C:\Program Files (x86)\Microsoft Games\Halo Custom Edition" -FilePath "C:\Program Files (x86)\Microsoft Games\Halo Custom Edition\haloce.exe" -Verb "RunAs" -ArgumentList $joinOptions
  52.         }
  53.     }
  54.     Else {
  55.         [System.Windows.Forms.MessageBox]::Show("IP:PORT format  is incorrect.", "" , '0',  'Error')
  56.         Return
  57.     }
  58. }
  59.  
  60. $BHSform = New-Object System.Windows.Forms.Form
  61. $BHSform.Size = New-Object System.Drawing.Size 280,289
  62. $BHSform.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  63. $BHSform.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\System32\actioncentercpl.dll")
  64. $BHSform.BackColor = "DarkBlue"
  65. $BHSform.FormBorderStyle = "FixedDialog"
  66. $BHSform.MaximizeBox = $False
  67. $BHSform.Add_Closing({    
  68.     If ($serversDataGridView.RowCount -eq 0) { Return }
  69.     #Test if program can write here
  70.     $sw = New-Object System.IO.StreamWriter($serversListPath)
  71.     $serversDataGridView.Rows | % {
  72.         If ($_.Cells[0].Value.Length -gt 0 -And $_.Cells[1].Value.Length -gt 0) {
  73.             $sw.WriteLine(($_.Cells | % { $_.Value }) -join ',')
  74.         }
  75.     }
  76.     $sw.Close()
  77. })
  78.  
  79. $helpMenu = New-Object System.Windows.Forms.ToolStripMenuItem
  80. $helpMenu.Size = New-Object System.Drawing.Size 35,20
  81. $helpMenu.Text = "&Help"
  82. $helpMenu.BackColor = "White"
  83. $helpMenu.Add_Click({Help})
  84.  
  85. $signMenu = New-Object System.Windows.Forms.ToolStripMenuItem
  86. $signMenu.Size = New-Object System.Drawing.Size 35,20
  87. $signMenu.Text = "&?"
  88. $signMenu.BackColor = "White"
  89. $signMenu.DropDownItems.AddRange(@($helpMenu))
  90.  
  91. $menu = New-Object System.Windows.Forms.MenuStrip
  92. $menu.Size = New-Object System.Drawing.Size 35,20
  93. $menu.Text = "&Help"
  94. $menu.TabIndex = 0
  95. $menu.Location = New-Object Drawing.Point 150,60
  96. $menu.Items.AddRange(@($signMenu))
  97. $menu.RightToLeft = 'Yes'
  98. $menu.BackColor = "DarkBlue"
  99.  
  100. $serversLabel = New-Object Windows.Forms.Label
  101. $serversLabel.Location = New-Object Drawing.Point 10,10
  102. $serversLabel.Forecolor = 'White'
  103. $serversLabel.text = "Halo servers list :"
  104.  
  105. $serversDataGridView = New-Object System.Windows.Forms.DataGridView
  106. $serversDataGridView.Size = New-Object System.Drawing.Size 268,159 #251,157
  107. $serversDataGridView.Location = New-Object Drawing.Point 10,38
  108. $serversDataGridView.DefaultCellStyle.SelectionBackColor = "DarkBlue"
  109. $serversDataGridView.AllowUserToAddRows = $True
  110. $serversDataGridView.MultiSelect = $False
  111. $serversDataGridView.EnableHeadersVisualStyles = $False;
  112. $serversDataGridView.BackgroundColor = "DarkBlue"
  113. $serversDataGridView.GridColor = "DarkBlue"
  114. $serversDataGridView.BorderStyle = "None"
  115. $serversDataGridView.ColumnCount = 2
  116. $serversDataGridView.Columns[0].Name = "Name"
  117. $serversDatagridview.Columns[0].Width = 80
  118. $serversDatagridview.Columns[0].HeaderCell.Style.Alignment = "MiddleCenter"
  119. $serversDatagridview.Columns[0].DefaultCellStyle.Alignment = "MiddleCenter"
  120. $serversDataGridView.Columns[1].Name = "IP:PORT"
  121. $serversDatagridview.Columns[1].Width = 128
  122. $serversDatagridview.Columns[1].HeaderCell.Style.Alignment = "MiddleCenter"
  123. $serversDatagridview.Columns[1].DefaultCellStyle.Alignment = "MiddleCenter"
  124. If (Test-Path $serversListPath) {
  125.     $serversList = Get-content $serversListPath
  126.     If (-Not [String]::IsNullOrWhiteSpace($serversList)) {
  127.         $serversList | % {
  128.             $serverListData = $_.Split(",")
  129.             $serversDataGridView.Rows.Add($serverListData[0], $serverListData[1]) | Out-NUll
  130.         }
  131.     }
  132. }
  133.  
  134. $joinoptionsButton = New-Object Windows.Forms.Button
  135. $joinoptionsButton.Size = New-Object System.Drawing.Size 80,40
  136. $joinoptionsButton.Location = New-Object Drawing.Point 10,208
  137. $joinoptionsButton.Forecolor = 'White'
  138. $joinoptionsButton.text = "Edit join options"
  139. $joinoptionsButton.Add_Click({
  140.     $argumentTextBox.text = Get-CommandLineArguments
  141.     $BHSChildform.ShowDialog() | Out-NUll
  142. })
  143.  
  144. $joinButton = New-Object Windows.Forms.Button
  145. $joinButton.Size = New-Object System.Drawing.Size 164,40
  146. $joinButton.Location = New-Object Drawing.Point 97,208
  147. $joinButton.Forecolor = 'White'
  148. $joinButton.text = "Join server"
  149. $joinButton.Add_Click({Start-Game})
  150.  
  151. $BHSChildform = New-Object System.Windows.Forms.Form
  152. $BHSChildform.Size = New-Object System.Drawing.Size 326,140
  153. $BHSChildform.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  154. $BHSChildform.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\System32\actioncentercpl.dll")
  155. $BHSChildform.BackColor = "DarkBlue"
  156. $BHSChildform.FormBorderStyle = "FixedDialog"
  157. $BHSChildform.MaximizeBox = $False
  158.  
  159. $joinargumentLabel = New-Object Windows.Forms.Label
  160. $joinargumentLabel.Location = New-Object Drawing.Point 10,10
  161. $joinargumentLabel.Forecolor = 'White'
  162. $joinargumentLabel.text = "Join arguments :"
  163.  
  164. $argumentTextBox = New-Object Windows.Forms.TextBox
  165. $argumentTextBox.Size = New-Object System.Drawing.Size 300,30
  166. $argumentTextBox.Location = New-Object Drawing.Point 10,34
  167. $argumentTextBox.Forecolor = 'DarkBlue'
  168.  
  169. $saveButton = New-Object Windows.Forms.Button
  170. $saveButton.Size = New-Object System.Drawing.Size 70,30
  171. $saveButton.Location = New-Object Drawing.Point 123,66
  172. $saveButton.Forecolor = 'White'
  173. $saveButton.text = "Save"
  174. $saveButton.Add_Click({
  175.     Set-Content $commandlineArgumentsPath $($argumentTextBox.Text)
  176.     $BHSChildform.Close()
  177. })
  178.  
  179. $BHSChildform.Controls.Add($joinargumentLabel)
  180. $BHSChildform.Controls.Add($argumentTextBox)
  181. $BHSChildform.Controls.Add($saveButton)
  182.  
  183. $BHSform.Controls.Add($menu)
  184. $BHSform.Controls.Add($serversLabel)
  185. $BHSform.Controls.Add($serversDataGridView)
  186. $BHSform.Controls.Add($joinoptionsButton)
  187. $BHSform.Controls.Add($joinButton)
  188. $BHSform.ShowDialog() | Out-NUll
Tags: Halo CE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement