Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 16.85 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Text
  3. Imports System.Runtime.InteropServices
  4.  
  5. Public Class MainWindow
  6.  
  7.     'SHARED VARIABLES
  8.     Dim modsfile As String = Application.StartupPath & "/mods.cfg"
  9.     Dim oProcess As New Process()
  10.  
  11.     'MAIN WINDOW
  12.     Private Sub MainWindow_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  13.  
  14.         If My.Settings.firstRun Then
  15.             Dim firstRunDialog As New firstRun
  16.             firstRunDialog.ShowDialog()
  17.         Else
  18.             steamDirBox.Text = My.Settings.steamDir
  19.             serverDirBox.Text = My.Settings.serverDir
  20.             userNameBox.Text = My.Settings.userName
  21.             userPassBox.Text = My.Settings.userPass
  22.             branchCombo.Text = My.Settings.selectedBranch
  23.  
  24.             If My.Settings.installedBranch = "" Then
  25.                 infoBranchBox.Text = "N/A"
  26.             Else
  27.                 infoBranchBox.Text = My.Settings.installedBranch
  28.             End If
  29.  
  30.             If My.Settings.lastUpdated = "00:00:00" Then
  31.                 infoUpdatedBox.Text = "N/A"
  32.             Else
  33.                 infoUpdatedBox.Text = My.Settings.lastUpdated
  34.             End If
  35.  
  36.         End If
  37.  
  38.         UpdateModGrid()
  39.  
  40.     End Sub
  41.  
  42.     Private Sub MainWindow_Close(sender As Object, e As EventArgs) Handles MyBase.Closed
  43.         Try
  44.             oProcess.Kill()
  45.         Catch ex As Exception
  46.  
  47.         End Try
  48.     End Sub
  49.  
  50.     Private Sub InstallCMDToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InstallCMDToolStripMenuItem.Click
  51.         Dim installSteam As New installSteamCMD
  52.         installSteam.ShowDialog()
  53.     End Sub
  54.  
  55.     Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
  56.         Me.Close()
  57.     End Sub
  58.  
  59.     Private Sub OpenSteamCMDDirToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenSteamCMDDirToolStripMenuItem.Click
  60.         Process.Start(My.Settings.steamDir)
  61.     End Sub
  62.  
  63.     Private Sub OpenA3DirectoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenA3DirectoryToolStripMenuItem.Click
  64.         Process.Start(My.Settings.serverDir)
  65.     End Sub
  66.  
  67.     Private Sub OpenModsLocationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenModsLocationToolStripMenuItem.Click
  68.         Try
  69.             Process.Start(My.Settings.steamDir & "\steamapps\workshop\content\107410")
  70.         Catch ex As Exception
  71.             MessageBox.Show("No MODS currently installed in the for the selected steam install.", "Information")
  72.             MsgBox("An exception occurred:" & vbCrLf & ex.Message)
  73.         End Try
  74.     End Sub
  75.  
  76.     'MAIN TAB
  77.     Private Sub SteamDirSelect_Click(sender As Object, e As EventArgs) Handles steamDirButton.Click
  78.         ChooseSteamDir()
  79.     End Sub
  80.  
  81.     Private Sub CancelUpdateButton_Click(sender As Object, e As EventArgs) Handles cancelUpdateButton.Click
  82.         oProcess.Kill()
  83.         steamOutputBox.Text = "Process Cancelled" & Environment.NewLine
  84.     End Sub
  85.  
  86.     Private Sub ShowUserPassButton_Click(sender As Object, e As EventArgs) Handles showUserPassButton.Click
  87.         If userPassBox.PasswordChar = "*" Then
  88.             userPassBox.PasswordChar = ""
  89.             showUserPassButton.BackgroundImage = My.Resources.Invisible_32px
  90.         Else
  91.             userPassBox.PasswordChar = "*"
  92.             showUserPassButton.BackgroundImage = My.Resources.Visible_32px
  93.         End If
  94.     End Sub
  95.  
  96.     Private Sub UpdateServer(sender As Object, e As EventArgs) Handles updateServerButton.Click
  97.         Dim branchId As String
  98.         Dim steamCommand As String
  99.         Dim steamCMD As String = steamDirBox.Text + "\steamcmd.exe"
  100.  
  101.         If branchCombo.SelectedItem = "Stable" Then
  102.             branchId = "233780"
  103.         Else
  104.             branchId = "107410 -beta development"
  105.         End If
  106.  
  107.         steamCommand = "+login " & userNameBox.Text & " " & userPassBox.Text & " +force_install_dir " & serverDirBox.Text & " +app_update " & branchId & " validate +quit"
  108.  
  109.         My.Settings.installedBranch = branchCombo.SelectedItem
  110.         infoBranchBox.Text = branchCombo.SelectedItem
  111.         My.Settings.lastUpdated = DateTime.Now
  112.         infoUpdatedBox.Text = My.Settings.lastUpdated.ToString
  113.  
  114.         RunSteamCommand(steamCMD, steamCommand, "server")
  115.     End Sub
  116.  
  117.     Private Sub ServerDirButton_Click(sender As Object, e As EventArgs) Handles serverDirButton.Click
  118.         ChooseServerDir()
  119.     End Sub
  120.  
  121.     Private Sub SteamDirBox_TextChanged(sender As Object, e As EventArgs) Handles steamDirBox.TextChanged
  122.         steamDirBox.BackColor = Color.Empty
  123.         My.Settings.steamDir = steamDirBox.Text
  124.     End Sub
  125.  
  126.     Private Sub UserNameBox_TextChanged(sender As Object, e As EventArgs) Handles userNameBox.TextChanged
  127.         userNameBox.BackColor = Color.Empty
  128.         My.Settings.userName = userNameBox.Text
  129.     End Sub
  130.  
  131.     Private Sub UserPassBox_TextChanged(sender As Object, e As EventArgs) Handles userPassBox.TextChanged
  132.         userPassBox.BackColor = Color.Empty
  133.         My.Settings.userPass = userPassBox.Text
  134.     End Sub
  135.  
  136.     Private Sub BranchCombo_SelectedValueChanged(sender As Object, e As EventArgs) Handles branchCombo.SelectedValueChanged
  137.         My.Settings.selectedBranch = branchCombo.Text
  138.         If My.Settings.selectedBranch = "Development" Then
  139.             MessageBox.Show("WARNING - Due to the A3 Dev branch server using the main game you will also download all Workshop Items for A3 that you are subscribed to on the Steam account you are currently logged into. These mods will be auto detected and added to the mods tab.", "WARNING - Dev Branch")
  140.         End If
  141.     End Sub
  142.  
  143.     Private Sub ServerDirBox_TextChanged(sender As Object, e As EventArgs) Handles serverDirBox.TextChanged
  144.         serverDirBox.BackColor = Color.Empty
  145.         My.Settings.serverDir = serverDirBox.Text
  146.     End Sub
  147.  
  148.     Public Sub ChooseSteamDir()
  149.         If steamDirDialog.ShowDialog() = DialogResult.OK Then
  150.             steamDirBox.Text = steamDirDialog.SelectedPath
  151.         End If
  152.     End Sub
  153.  
  154.     Public Sub ChooseServerDir()
  155.         If serverDirDialog.ShowDialog() = DialogResult.OK Then
  156.             serverDirBox.Text = serverDirDialog.SelectedPath
  157.         End If
  158.     End Sub
  159.  
  160.  
  161.     'MODS TAB
  162.     Private Sub ImportMod_Click(sender As Object, e As EventArgs) Handles modUrlButton.Click
  163.         Dim importDiaglog As New ImportMod
  164.         importDiaglog.ShowDialog()
  165.     End Sub
  166.  
  167.     Private Sub ModLauncherButton_Click(sender As Object, e As EventArgs) Handles modLauncherButton.Click
  168.         ImportModSetFile()
  169.     End Sub
  170.  
  171.     Private Sub ModsDataGrid_CellContentClick(sender As System.Object, e As DataGridViewCellEventArgs) Handles modsDataGrid.CellContentClick
  172.         Dim senderGrid = DirectCast(sender, DataGridView)
  173.  
  174.         'UPDATE MOD
  175.         If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.ColumnIndex = 4 Then
  176.             Dim modID As String = modsDataGrid.Rows(e.RowIndex).Cells(0).Value
  177.             Dim modName As String = modsDataGrid.Rows(e.RowIndex).Cells(1).Value
  178.  
  179.             UpdateMod(modID, modName)
  180.         End If
  181.  
  182.         'VIEW DETAILS
  183.         If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.ColumnIndex = 5 Then
  184.             Dim url As String = "http://steamcommunity.com/sharedfiles/filedetails/?id=" & modsDataGrid.Rows(e.RowIndex).Cells(0).Value
  185.             Process.Start(url)
  186.         End If
  187.  
  188.         'DELETE BUTTON
  189.         If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso e.ColumnIndex = 6 Then
  190.             Dim modID As Integer = modsDataGrid.Rows(e.RowIndex).Cells(0).Value
  191.             Dim modName As String = modsDataGrid.Rows(e.RowIndex).Cells(1).Value
  192.  
  193.             DeleteMod(modID, modName)
  194.         End If
  195.     End Sub
  196.  
  197.     Public Sub ImportModSetFile()
  198.         If importModSet.ShowDialog() = DialogResult.OK Then
  199.             Dim modSetFile As String = importModSet.FileName
  200.  
  201.             Dim dataReader As New StreamReader(modSetFile, Encoding.Default)
  202.             Dim modLine As String
  203.  
  204.             Do
  205.                 modLine = dataReader.ReadLine
  206.                 If modLine Is Nothing Then Exit Do
  207.                 Dim values() As String = modLine.Split(Environment.NewLine)
  208.                 If modLine.Contains("data-type=""Link"">") Then
  209.                     Dim link As String
  210.                     link = modLine.Substring(modLine.IndexOf("http://steam"))
  211.                     link = StrReverse(link)
  212.                     link = link.Substring(link.IndexOf("epyt-atad") + 11)
  213.                     link = StrReverse(link)
  214.                     ImportMod.ImportModToList(link)
  215.                 End If
  216.             Loop
  217.             dataReader.Close()
  218.  
  219.         End If
  220.     End Sub
  221.  
  222.     Public Sub UpdateModGrid()
  223.         modsDataGrid.Rows.Clear()
  224.         Try
  225.  
  226.             If My.Computer.FileSystem.FileExists(modsfile) Then
  227.                 Dim dataReader As New StreamReader(modsfile, Encoding.Default)
  228.                 Dim modLine As String
  229.  
  230.                 Do
  231.                     modLine = dataReader.ReadLine
  232.                     If modLine Is Nothing Then Exit Do
  233.                     Dim values() As String = modLine.Split(",")
  234.                     modsDataGrid.Rows.Add("")
  235.                     For ix As Integer = 0 To 2
  236.                         modsDataGrid.Rows(modsDataGrid.Rows.Count - 1).Cells(ix).Value = values(ix)
  237.                     Next
  238.                 Loop
  239.                 dataReader.Close()
  240.             End If
  241.  
  242.         Catch ex As Exception
  243.             MsgBox("An exception occurred:" & vbCrLf & ex.Message)
  244.         End Try
  245.     End Sub
  246.  
  247.     Public Sub UpdateMod(modID As String, modName As String)
  248.         Dim steamCommand As String
  249.         Dim steamCMD As String = steamDirBox.Text + "\steamcmd.exe"
  250.         Dim modPath As String = My.Settings.steamDir & "\steamapps\workshop\content\107410\" & modID
  251.  
  252.         steamCommand = "+login " & userNameBox.Text & " " & userPassBox.Text & " +workshop_download_item 107410 " & modID & " validate +quit"
  253.  
  254.         Try
  255.             'C:\mklink /D C:\TestFolder C:\Users\Geek\TestFolder
  256.  
  257.             Dim linkCommand As String
  258.             Dim linkPath As String = My.Settings.serverDir & "\@" & modName
  259.  
  260.             linkCommand = "/c mklink /D " & linkPath & " " & modPath
  261.  
  262.             Dim oStartInfo As New ProcessStartInfo("cmd", linkCommand)
  263.             oStartInfo.CreateNoWindow = True
  264.             oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
  265.             oProcess.StartInfo = oStartInfo
  266.             oProcess.Start()
  267.  
  268.         Catch ex As Exception
  269.             MsgBox("An exception occurred:" & vbCrLf & ex.Message)
  270.         End Try
  271.  
  272.         RunSteamCommand(steamCMD, steamCommand, "addon")
  273.  
  274.     End Sub
  275.  
  276.     Public Sub DeleteMod(modID As String, modName As String)
  277.         Dim modPath As String = My.Settings.steamDir & "\steamapps\workshop\content\107410\" & modID
  278.         Dim workshopFile As String = My.Settings.steamDir & "\steamapps\workshop\appworkshop_107410.acf"
  279.         Dim shortcutPath As String = My.Settings.serverDir & "\@" & modName & ".lnk"
  280.  
  281.         Dim lines As List(Of String) = System.IO.File.ReadAllLines(modsfile).ToList
  282.         lines.RemoveAt(GetLineNo(modID, modsfile) - 1)
  283.         System.IO.File.WriteAllLines(modsfile, lines)
  284.  
  285.         Try
  286.             Dim lines2 As List(Of String) = System.IO.File.ReadAllLines(workshopFile).ToList
  287.             Dim startLine As Integer = GetLineNo(modID, workshopFile) - 1
  288.             lines2.RemoveRange(startLine, 6)
  289.             System.IO.File.WriteAllLines(workshopFile, lines2)
  290.         Catch ex As Exception
  291.             MsgBox("An exception occurred:" & vbCrLf & ex.Message)
  292.         End Try
  293.  
  294.         UpdateModGrid()
  295.  
  296.         Try
  297.             System.IO.Directory.Delete(modPath, True)
  298.             System.IO.File.Delete(shortcutPath)
  299.         Catch ex As Exception
  300.             MsgBox("An exception occurred:" & vbCrLf & ex.Message)
  301.         End Try
  302.  
  303.     End Sub
  304.  
  305.  
  306.     'SHARED
  307.     Public Function GetLineNo(target As String, targetFile As String)
  308.         Dim count As Integer
  309.         For Each line As String In System.IO.File.ReadLines(targetFile)
  310.             count = count + 1
  311.             If line.Contains(target) Then Return count
  312.         Next
  313.         Return ""
  314.     End Function
  315.  
  316.     Public Function ReadyToUpdate() As Boolean
  317.  
  318.         If steamDirBox.Text = "" Then
  319.             Return False
  320.         ElseIf userNameBox.Text = "" Then
  321.             Return False
  322.         ElseIf userPassBox.Text = "" Then
  323.             Return False
  324.         ElseIf serverDirBox.Text = "" Then
  325.             Return False
  326.         Else
  327.             Return True
  328.         End If
  329.  
  330.     End Function
  331.  
  332.     Public Async Sub RunSteamCommand(steamCMD As String, steamCommand As String, type As String)
  333.  
  334.         If ReadyToUpdate() Then
  335.             steamProgressBar.Value = 0
  336.             cancelUpdateButton.Enabled = True
  337.             categoryTabs.SelectedTab = serverTab
  338.             Dim tasks As New List(Of Task)()
  339.  
  340.             updateServerButton.Enabled = False
  341.            
  342.             modsDataGrid.Enabled = False
  343.  
  344.             If type Is "addon" Then
  345.                 steamOutputBox.Text = "Starting SteamCMD to update Addon" & Environment.NewLine
  346.  
  347.             ElseIf type Is "server" Then
  348.                 steamOutputBox.Text = "Starting SteamCMD to update Server" & Environment.NewLine
  349.             Else
  350.  
  351.             End If
  352.  
  353.             tasks.Add(Task.Run(
  354.                     Sub()
  355.  
  356.                         Dim oStartInfo As New ProcessStartInfo(steamCMD, steamCommand)
  357.                         oStartInfo.CreateNoWindow = True
  358.                         oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
  359.                         oStartInfo.UseShellExecute = False
  360.                         oStartInfo.RedirectStandardOutput = True
  361.                         oProcess.StartInfo = oStartInfo
  362.                         oProcess.Start()
  363.  
  364.                         Dim sOutput As String
  365.                         Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
  366.                             Do Until oProcess.HasExited
  367.                                 Try
  368.                                     sOutput = oStreamReader.ReadLine
  369.  
  370.                                     If sOutput Like "*Update state*" Then
  371.                                         Dim counter As Integer = sOutput.IndexOf(":")
  372.                                         Dim progress As String = sOutput.Substring(counter + 2, 2)
  373.                                         Dim progressValue As Integer
  374.  
  375.                                         If progress.Contains(".") Then
  376.                                             progressValue = progress.Substring(0, 1)
  377.                                         Else
  378.                                             progressValue = progress
  379.                                         End If
  380.                                         steamProgressBar.Invoke(
  381.                                                 Sub()
  382.                                                     steamProgressBar.Value = progressValue
  383.                                                 End Sub
  384.                                         )
  385.                                     End If
  386.  
  387.                                     If sOutput Like "*Success*" Then
  388.                                         steamProgressBar.Invoke(
  389.                                                 Sub()
  390.                                                     steamProgressBar.Value = 100
  391.                                                 End Sub
  392.                                         )
  393.                                     End If
  394.  
  395.                                     steamOutputBox.Invoke(
  396.                                     Sub()
  397.                                         steamOutputBox.AppendText(sOutput & Environment.NewLine)
  398.                                         steamOutputBox.SelectionStart = steamOutputBox.Text.Length
  399.                                         steamOutputBox.ScrollToCaret()
  400.                                     End Sub
  401.                                     )
  402.                                 Catch ex As Exception
  403.                                     MsgBox("An exception occurred:" & vbCrLf & ex.Message)
  404.                                 End Try
  405.  
  406.                             Loop
  407.  
  408.                         End Using
  409.  
  410.                     End Sub
  411.                 ))
  412.  
  413.             Await Task.WhenAll(tasks)
  414.  
  415.             steamOutputBox.Text = steamOutputBox.Text.TrimEnd
  416.  
  417.             modsDataGrid.Enabled = True
  418.             cancelUpdateButton.Enabled = False
  419.             updateServerButton.Enabled = True
  420.  
  421.         Else
  422.             MessageBox.Show("Please check all fields are correct:" & Environment.NewLine & Environment.NewLine & "   -  Steam Dir" & Environment.NewLine & "   -  User Name & Pass" & Environment.NewLine & "   -  Server Dir", "Error")
  423.         End If
  424.  
  425.     End Sub
  426.  
  427. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement