Skip to content

Commit

Permalink
Convert Download of Garry's Mod item from LZMA file to GMA file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeqMacaw committed Oct 20, 2019
1 parent ac54c2c commit 192e3f1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 32 deletions.
41 changes: 41 additions & 0 deletions Crowbar/Core/SteamAppInfos/GarrysModSteamAppInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,47 @@ Public Class GarrysModSteamAppInfo
Me.TagsControlType = GetType(GarrysModTagsUserControl)
End Sub

Public Overrides Function ProcessFileAfterDownload(ByVal givenPathFileName As String, ByVal bw As BackgroundWorkerEx) As String
Dim processedPathFileName As String = Path.ChangeExtension(givenPathFileName, ".gma")

bw.ReportProgress(0, "Decompressing downloaded Garry's Mod workshop file into a GMA file." + vbCrLf)
Dim lzmaExeProcess As New Process()
Try
lzmaExeProcess.StartInfo.UseShellExecute = False
'NOTE: From Microsoft website:
' On Windows Vista and earlier versions of the Windows operating system,
' the length of the arguments added to the length of the full path to the process must be less than 2080.
' On Windows 7 and later versions, the length must be less than 32699.
'FROM BAT file: lzma.exe d %1 "%~n1.gma"
lzmaExeProcess.StartInfo.FileName = TheApp.LzmaExePathFileName
lzmaExeProcess.StartInfo.Arguments = "d """ + givenPathFileName + """ """ + processedPathFileName + """"
#If DEBUG Then
lzmaExeProcess.StartInfo.CreateNoWindow = False
#Else
lzmaExeProcess.StartInfo.CreateNoWindow = True
#End If
lzmaExeProcess.Start()
lzmaExeProcess.WaitForExit()
lzmaExeProcess.Close()
Catch ex As Exception
Throw New System.Exception("Crowbar tried to decompress the file """ + givenPathFileName + """ to """ + processedPathFileName + """ but Windows gave this message: " + ex.Message)
Finally
lzmaExeProcess.Close()
bw.ReportProgress(0, "Decompress done." + vbCrLf)
End Try

Try
If File.Exists(givenPathFileName) Then
File.Delete(givenPathFileName)
bw.ReportProgress(0, "Deleted: """ + givenPathFileName + """" + vbCrLf)
End If
Catch ex As Exception
bw.ReportProgress(0, "Crowbar tried to delete the file """ + givenPathFileName + """ but Windows gave this message: " + ex.Message)
End Try

Return processedPathFileName
End Function

Public Overrides Function ProcessFileBeforeUpload(ByVal item As WorkshopItem, ByVal bw As BackgroundWorkerEx) As String
Dim processedPathFileName As String = item.ContentPathFolderOrFileName
Me.theBackgroundWorker = bw
Expand Down
2 changes: 1 addition & 1 deletion Crowbar/Core/SteamAppInfos/SteamAppInfoBase.vb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Public Class SteamAppInfoBase
Me.ContentFileExtensionsAndDescriptions = New SortedList(Of String, String)()
End Sub

Public Overridable Function ProcessFileAfterDownload(ByVal givenPathFileName As String) As String
Public Overridable Function ProcessFileAfterDownload(ByVal givenPathFileName As String, ByVal bw As BackgroundWorkerEx) As String
Dim processedPathFileName As String = givenPathFileName
Return processedPathFileName
End Function
Expand Down
91 changes: 60 additions & 31 deletions Crowbar/Widgets/Main Tabs/DownloadUserControl.vb
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,13 @@ Public Class DownloadUserControl
End Sub

Private Sub WebClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
Dim pathFileName As String = CType(e.UserState, String)

If e.Cancelled Then
Me.LogTextBox.AppendText("Download cancelled." + vbCrLf)
Me.DownloadProgressBar.Text = ""
Me.DownloadProgressBar.Value = 0

Dim pathFileName As String = CType(e.UserState, String)
If File.Exists(pathFileName) Then
Try
File.Delete(pathFileName)
Expand All @@ -194,15 +195,9 @@ Public Class DownloadUserControl
End Try
End If
Else
Dim pathFileName As String = CType(e.UserState, String)
If File.Exists(pathFileName) Then
Me.ProcessFileAfterDownload(pathFileName)
If File.Exists(pathFileName) Then
Me.LogTextBox.AppendText("Download complete." + vbCrLf + "Downloaded file: """ + pathFileName + """" + vbCrLf)
Me.DownloadedItemTextBox.Text = pathFileName
Else
Me.LogTextBox.AppendText("Download failed." + vbCrLf)
End If
Me.LogTextBox.AppendText("Download complete." + vbCrLf + "Downloaded file: """ + pathFileName + """" + vbCrLf)
Me.DownloadedItemTextBox.Text = pathFileName
Else
Me.LogTextBox.AppendText("Download failed." + vbCrLf)
End If
Expand All @@ -214,6 +209,10 @@ Public Class DownloadUserControl

Me.DownloadButton.Enabled = True
Me.CancelDownloadButton.Enabled = False

If Not e.Cancelled AndAlso File.Exists(pathFileName) Then
Me.ProcessFileAfterDownload(pathFileName)
End If
End Sub

Private Sub DownloadItem_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
Expand Down Expand Up @@ -260,9 +259,9 @@ Public Class DownloadUserControl

File.WriteAllBytes(outputPathFileName, outputInfo.ContentFile)
If File.Exists(outputPathFileName) Then
Me.ProcessFileAfterDownload(outputPathFileName)
Me.LogTextBox.AppendText("Download complete." + vbCrLf + "Downloaded file: """ + outputPathFileName + """" + vbCrLf)
Me.DownloadedItemTextBox.Text = outputPathFileName
Me.ProcessFileAfterDownload(outputPathFileName)
Else
Me.LogTextBox.AppendText("Download failed." + vbCrLf)
End If
Expand Down Expand Up @@ -525,34 +524,32 @@ Public Class DownloadUserControl
Dim jss As JavaScriptSerializer = New JavaScriptSerializer()
Dim root As SteamRemoteStorage_PublishedFileDetails_Json = jss.Deserialize(Of SteamRemoteStorage_PublishedFileDetails_Json)(responseFromServer)
Dim file_url As String = root.response.publishedfiledetails(0).file_url
If file_url Is Nothing OrElse file_url = "" Then
appID = CUInt(root.response.publishedfiledetails(0).consumer_app_id)
Else
If file_url IsNot Nothing AndAlso file_url <> "" Then
itemLink = file_url

appID = CUInt(root.response.publishedfiledetails(0).consumer_app_id)
Me.theAppIdText = appID.ToString()
Me.theSteamAppInfo = Nothing
Try
If TheApp.Settings.PublishSteamAppUserInfos.Count > 0 Then
'NOTE: Use this temp var because appID as a ByRef var can not be used in a lambda expression used in next line.
Dim steamAppID As New Steamworks.AppId_t(appID)
Me.theSteamAppInfo = TheApp.SteamAppInfos.First(Function(info) info.ID = steamAppID)
End If
Catch ex As Exception
Dim debug As Integer = 4242
End Try
If Me.theSteamAppInfo Is Nothing Then
'NOTE: Value was not found, so unable to download.
appID = 0
End If

Me.theItemTitle = root.response.publishedfiledetails(0).title
Dim fileName As String = root.response.publishedfiledetails(0).filename
Me.theItemContentPathFileName = fileName
Me.theItemIdText = root.response.publishedfiledetails(0).publishedfileid
Me.theItemTimeUpdatedText = root.response.publishedfiledetails(0).time_updated.ToString()
End If

appID = CUInt(root.response.publishedfiledetails(0).consumer_app_id)
Me.theAppIdText = appID.ToString()
Me.theSteamAppInfo = Nothing
Try
If TheApp.Settings.PublishSteamAppUserInfos.Count > 0 Then
'NOTE: Use this temp var because appID as a ByRef var can not be used in a lambda expression used in next line.
Dim steamAppID As New Steamworks.AppId_t(appID)
Me.theSteamAppInfo = TheApp.SteamAppInfos.First(Function(info) info.ID = steamAppID)
End If
Catch ex As Exception
Dim debug As Integer = 4242
End Try
If Me.theSteamAppInfo Is Nothing Then
'NOTE: Value was not found, so unable to download.
appID = 0
End If
Finally
If reader IsNot Nothing Then
reader.Close()
Expand Down Expand Up @@ -703,18 +700,50 @@ Public Class DownloadUserControl
Private Sub ProcessFileAfterDownload(ByRef pathFileName As String)
If Me.theSteamAppInfo IsNot Nothing Then
Try
pathFileName = Me.theSteamAppInfo.ProcessFileAfterDownload(pathFileName)
Me.DownloadButton.Enabled = False
Me.CancelDownloadButton.Enabled = True

Me.theProcessAfterDownloadWorker = New BackgroundWorkerEx()
Me.theProcessAfterDownloadWorker.WorkerSupportsCancellation = True
Me.theProcessAfterDownloadWorker.WorkerReportsProgress = True
AddHandler Me.theProcessAfterDownloadWorker.DoWork, AddressOf ProcessAfterDownloadWorker_DoWork
AddHandler Me.theProcessAfterDownloadWorker.ProgressChanged, AddressOf ProcessAfterDownloadWorker_ProgressChanged
AddHandler Me.theProcessAfterDownloadWorker.RunWorkerCompleted, AddressOf ProcessAfterDownloadWorker_RunWorkerCompleted
Me.theProcessAfterDownloadWorker.RunWorkerAsync(pathFileName)
Catch ex As Exception
Me.LogTextBox.AppendText("ERROR: " + ex.Message + vbCrLf)
End Try
End If
End Sub

'NOTE: This is run in a background thread.
Private Sub ProcessAfterDownloadWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
e.Result = Me.theSteamAppInfo.ProcessFileAfterDownload(CType(e.Argument, String), Me.theProcessAfterDownloadWorker)
End Sub

Private Sub ProcessAfterDownloadWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
If e.ProgressPercentage = 0 Then
Me.LogTextBox.AppendText(CStr(e.UserState))
'ElseIf e.ProgressPercentage = 1 Then
' Me.LogTextBox.AppendText(vbTab + CStr(e.UserState))
End If
End Sub

Private Sub ProcessAfterDownloadWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
If e.Cancelled Then
Else
Dim pathFileName As String = CType(e.Result, String)
Me.LogTextBox.AppendText("Final file: """ + pathFileName + """" + vbCrLf)
Me.DownloadedItemTextBox.Text = pathFileName
End If
End Sub

#End Region

#Region "Data"

Private theWebClient As WebClient
Private theProcessAfterDownloadWorker As BackgroundWorkerEx
Private theAppIdText As String
Private theSteamAppInfo As SteamAppInfoBase

Expand Down

0 comments on commit 192e3f1

Please sign in to comment.