Advertisement
Guest User

tess

a guest
Sep 20th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. Imports System.IO, System.Net, System.Web, System.Xml, System.Runtime.InteropServices
  2. Public Class FrmUpdate
  3.  
  4. Delegate Sub DownloadComplateSafe(ByVal cancelled As Boolean)
  5. Delegate Sub ChangeTextSafe(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
  6.  
  7.  
  8. Dim DataFile As WebClient
  9. Dim DataDownload As String = "https://github.com/ianpwk/FindMyWaifu/releases/download/0.0.0.20/FindMyWaifuPortable.zip"
  10. Dim msg As String = ""
  11. Dim Out As Integer
  12. Dim updates As Integer
  13. Dim fol As New CreateFolder()
  14. Dim filedownload As String = fol.appDataFMW & "\_data\updates\update.zip"
  15.  
  16. Dim check As Boolean = False
  17.  
  18. Private Declare Function InternetGetConnectedState Lib "wininet" (ByRef conn As Long, ByVal val As Long) As Boolean
  19.  
  20. Public Sub CheckForUpdates()
  21. If InternetGetConnectedState(Out, 0) = True Then
  22. Try
  23. Dim ver As String = ""
  24. Dim xmlUpdate As New XmlTextReader("https://onedrive.live.com/download?cid=9675D76E084032AB&resid=9675D76E084032AB%21815&authkey=APPoahifAoJiGZo")
  25. Dim newver As String = ""
  26. Dim desc As String = ""
  27.  
  28. While xmlUpdate.Read()
  29. Dim type = xmlUpdate.NodeType
  30. If xmlUpdate.Name = "version" Then
  31. newver = xmlUpdate.ReadInnerXml.ToString()
  32. End If
  33. If xmlUpdate.Name = "description" Then
  34. desc = xmlUpdate.ReadInnerXml.ToString()
  35. End If
  36. End While
  37.  
  38. Label2.Text = "Update Ver.: " + newver
  39. Dim lastver As String = "0.0.0.8" 'Application.ProductVersion
  40. updates = 1
  41.  
  42. If newver < lastver Then
  43. RichTextBox1.Text = desc
  44. Label3.Text = ""
  45. Else
  46. Label3.Text = "Sudah Terupdate"
  47. RichTextBox1.Text = "Versi anda sudah yang terbaru"
  48. Button1.Enabled = False
  49. End If
  50. Catch ex As Exception
  51. Label3.Text = ""
  52. updates = 0
  53. RichTextBox1.Text = "Internet sedang gangguan, kilk Retry untuk menyambung ulang"
  54. Button1.Text = "Retry"
  55. End Try
  56. Else
  57. Label3.Text = ""
  58. updates = 0
  59. RichTextBox1.Text = "Internet belum terkoneksi, kilk Retry untuk menyambung ulang"
  60. Button1.Text = "Retry"
  61. End If
  62. End Sub
  63.  
  64. Private Sub FrmUpdate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  65. Dim create As New CreateFolder()
  66. If (Not System.IO.Directory.Exists(create.appDataFMW & "\_data")) Then
  67. System.IO.Directory.CreateDirectory(create.appDataFMW & "\_data")
  68. End If
  69.  
  70. If (Not System.IO.Directory.Exists(create.appDataFMW & "\_data\updates")) Then
  71. System.IO.Directory.CreateDirectory(create.appDataFMW & "\_data\updates")
  72. End If
  73.  
  74. IO.File.SetAttributes(create.appDataFMW & "\_data", IO.FileAttributes.Hidden Or
  75. IO.FileAttributes.System)
  76.  
  77. RichTextBox1.ReadOnly = True
  78. RichTextBox1.BackColor = ColorTranslator.FromHtml("#f3f3f3")
  79. Label1.Text = "Curent ver.: " + Application.ProductVersion
  80. CheckForUpdates()
  81.  
  82. End Sub
  83.  
  84. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  85. Dim fileDetail = My.Computer.FileSystem.GetFileInfo(filedownload)
  86.  
  87. If Not File.Exists(filedownload) Then
  88. BackgroundWorker1.RunWorkerAsync()
  89. Else
  90. If fileDetail.Length = 0 Then
  91. BackgroundWorker1.RunWorkerAsync()
  92. Else
  93. Dim hasil As String = MsgBox("Update file sudah tersedia" + Chr(13) + "apakah ingin mendownload ulang?", vbInformation + vbYesNo, "Updating")
  94. If hasil = vbYes Then
  95. BackgroundWorker1.RunWorkerAsync()
  96. ElseIf hasil = vbNo Then
  97. Installing()
  98. End If
  99. End If
  100. End If
  101. End Sub
  102.  
  103. Private Sub Installing()
  104. Dim process As New Process()
  105. process.StartInfo.FileName = "UpdateMyWaifu.exe"
  106. process.StartInfo.Verb = "runas"
  107. process.StartInfo.UseShellExecute = True
  108. process.Start()
  109.  
  110. Form1.Close()
  111. End Sub
  112.  
  113. Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  114.  
  115. ' creating the request and getting the response
  116. Dim theResponse As HttpWebResponse
  117. Dim theRequest As HttpWebRequest
  118. If File.Exists(filedownload) Then
  119. File.Delete(filedownload)
  120. End If
  121.  
  122. Try 'check if the file is exist
  123.  
  124. theRequest = WebRequest.Create(DataDownload)
  125. theResponse = theRequest.GetResponse
  126.  
  127. Catch ex As Exception
  128.  
  129. MessageBox.Show("Update is error" & ControlChars.CrLf &
  130. "Check your connection", "error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  131. 'we will create the delegate here
  132. ' just wait fo a moment
  133.  
  134. Dim cancelDelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
  135. Me.Invoke(cancelDelegate, True)
  136. Exit Sub
  137. End Try
  138.  
  139. Dim lenght As Long = theResponse.ContentLength 'Size of the response (in bytes)
  140. ' we will create the functions for update the informations
  141. ' just wait for a moment
  142.  
  143.  
  144.  
  145. Dim safedelegate As New ChangeTextSafe(AddressOf ChangeText)
  146. Me.Invoke(safedelegate, lenght, 0, 0, 0)
  147.  
  148. Dim writestream As New IO.FileStream(Me.filedownload, IO.FileMode.Create)
  149.  
  150. 'Replacement for Stream.Position (webResponse stream doesn't support seek)
  151. Dim nRead As Integer
  152.  
  153. 'To calculate the download speed
  154. Dim speedTimer As New Stopwatch
  155. Dim currentspeed As Double = -1
  156. Dim readings As Integer = 0
  157.  
  158. Do
  159. If BackgroundWorker1.CancellationPending Then 'If user abort download
  160. Exit Do
  161. End If
  162. speedTimer.Start()
  163. Dim readBytes(4095) As Byte
  164. Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
  165. nRead += bytesread
  166.  
  167. Dim percent As Short = (nRead * 100) / lenght
  168. Me.Invoke(safedelegate, lenght, nRead, percent, currentspeed)
  169.  
  170. ' sorry for it, just replace the variable speed to double
  171. ' lets try it again
  172.  
  173. If bytesread = 0 Then Exit Do
  174.  
  175. writestream.Write(readBytes, 0, bytesread)
  176. speedTimer.Stop()
  177.  
  178. readings += 1
  179. If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
  180.  
  181. currentspeed = 20480 / (speedTimer.ElapsedMilliseconds / 1000)
  182. speedTimer.Reset()
  183. readings = 0
  184. End If
  185.  
  186. Loop
  187.  
  188. 'Close the streams
  189. theResponse.GetResponseStream.Close()
  190. writestream.Close()
  191.  
  192. If Me.BackgroundWorker1.CancellationPending Then
  193.  
  194. IO.File.Delete(Me.filedownload)
  195. Dim canceldelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
  196. Me.Invoke(canceldelegate, True)
  197. Exit Sub
  198. End If
  199.  
  200. Dim complatedelegate As New DownloadComplateSafe(AddressOf DownloadComplate)
  201. Me.Invoke(complatedelegate, False)
  202. End Sub
  203.  
  204. Public Sub DownloadComplate(ByVal cancelled As Boolean)
  205.  
  206. If cancelled Then
  207. Label3.Text = "Check Your Connection"
  208. Else
  209.  
  210. End If
  211.  
  212. End Sub
  213.  
  214. Public Sub ChangeText(ByVal lenght As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
  215.  
  216. Label3.Text = "Downloading Update " & ProgressBar1.Value & "% (" & Math.Round((position / 1024), 2) & " KB / " & Math.Round((lenght / 1024), 2) & " KB..."
  217. 'Me.Label4.Text = "File Size: " & Math.Round((lenght / 1024), 2) & " KB"
  218. 'Me.Label2.Text = "Downloading: " & Me.TextBox1.Text
  219. 'Me.Label6.Text = "Downloaded " & Math.Round((position / 1024), 2) & " KB OF " & Math.Round((lenght / 1024), 2) & " KB (" & ProgressBar1.Value & " %) "
  220.  
  221. 'If speed = -1 Then
  222.  
  223. ' Me.Label5.Text = "Speed : Calculating ..."
  224. 'Else
  225. ' Me.Label5.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
  226. 'End If
  227.  
  228. Me.ProgressBar1.Value = percent
  229.  
  230. End Sub
  231.  
  232. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement