PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PRoCon.Core/AutoUpdates/UpdateDownloader.cs

#
C# | 127 lines | 88 code | 36 blank | 3 comment | 22 complexity | f1958259bb7dc816cb60963f264e2d77 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, LGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using Ionic.Zip;
  6. using System.Security.Cryptography;
  7. namespace PRoCon.Core.AutoUpdates {
  8. using Core.Remote;
  9. public class UpdateDownloader {
  10. public delegate void DownloadUnzipCompleteHandler();
  11. public event DownloadUnzipCompleteHandler DownloadUnzipComplete;
  12. public delegate void UpdateDownloadingHandler(CDownloadFile cdfDownloading);
  13. public event UpdateDownloadingHandler UpdateDownloading;
  14. public delegate void CustomDownloadErrorHandler(string strError);
  15. public event CustomDownloadErrorHandler CustomDownloadError;
  16. private CDownloadFile m_cdfPRoConUpdate;
  17. private string m_updatesDirectoryName;
  18. public CDownloadFile VersionChecker {
  19. get;
  20. private set;
  21. }
  22. public UpdateDownloader(string updatesDirectoryName) {
  23. this.m_updatesDirectoryName = updatesDirectoryName;
  24. this.VersionChecker = new CDownloadFile("http://www.phogue.net/procon/version3.php");
  25. this.VersionChecker.DownloadComplete += new CDownloadFile.DownloadFileEventDelegate(VersionChecker_DownloadComplete);
  26. }
  27. public void DownloadLatest() {
  28. this.VersionChecker.BeginDownload();
  29. }
  30. private string MD5File(string strFileName) {
  31. StringBuilder sbStringifyHash = new StringBuilder();
  32. if (File.Exists(strFileName) == true) {
  33. MD5 md5Hasher = MD5.Create();
  34. byte[] a_bHash = md5Hasher.ComputeHash(File.ReadAllBytes(strFileName));
  35. for (int x = 0; x < a_bHash.Length; x++) {
  36. sbStringifyHash.Append(a_bHash[x].ToString("x2"));
  37. }
  38. }
  39. return sbStringifyHash.ToString();
  40. }
  41. private string MD5Data(byte[] a_bData) {
  42. StringBuilder sbStringifyHash = new StringBuilder();
  43. MD5 md5Hasher = MD5.Create();
  44. byte[] a_bHash = md5Hasher.ComputeHash(a_bData);
  45. for (int x = 0; x < a_bHash.Length; x++) {
  46. sbStringifyHash.Append(a_bHash[x].ToString("x2"));
  47. }
  48. return sbStringifyHash.ToString();
  49. }
  50. private void VersionChecker_DownloadComplete(CDownloadFile cdfSender) {
  51. string[] a_strVersionData = System.Text.Encoding.UTF8.GetString(cdfSender.CompleteFileData).Split('\n');
  52. if (a_strVersionData.Length >= 4 && (this.m_cdfPRoConUpdate == null || this.m_cdfPRoConUpdate.FileDownloading == false)) {
  53. // Download file, alert or auto apply once complete with release notes.
  54. this.m_cdfPRoConUpdate = new CDownloadFile(a_strVersionData[2], a_strVersionData[3]);
  55. this.m_cdfPRoConUpdate.DownloadComplete += new CDownloadFile.DownloadFileEventDelegate(cdfPRoConUpdate_DownloadComplete);
  56. if (this.UpdateDownloading != null) {
  57. FrostbiteConnection.RaiseEvent(this.UpdateDownloading.GetInvocationList(), this.m_cdfPRoConUpdate);
  58. }
  59. this.m_cdfPRoConUpdate.BeginDownload();
  60. }
  61. }
  62. private void cdfPRoConUpdate_DownloadComplete(CDownloadFile cdfSender) {
  63. if (String.Compare(this.MD5Data(cdfSender.CompleteFileData), (string)cdfSender.AdditionalData, true) == 0) {
  64. string strUpdatesFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.m_updatesDirectoryName);
  65. try {
  66. if (Directory.Exists(strUpdatesFolder) == false) {
  67. Directory.CreateDirectory(strUpdatesFolder);
  68. }
  69. using (ZipFile zip = ZipFile.Read(cdfSender.CompleteFileData)) {
  70. zip.ExtractAll(strUpdatesFolder, ExtractExistingFileAction.OverwriteSilently);
  71. }
  72. if (this.DownloadUnzipComplete != null) {
  73. FrostbiteConnection.RaiseEvent(this.DownloadUnzipComplete.GetInvocationList());
  74. }
  75. }
  76. catch (Exception e) {
  77. if (this.CustomDownloadError != null) {
  78. FrostbiteConnection.RaiseEvent(this.CustomDownloadError.GetInvocationList(), e.Message);
  79. }
  80. //this.Invoke(new DownloadErrorDelegate(DownloadError_Callback), e.Message);
  81. }
  82. }
  83. else {
  84. if (this.CustomDownloadError != null) {
  85. FrostbiteConnection.RaiseEvent(this.CustomDownloadError.GetInvocationList(), "Downloaded file failed checksum, please try again or download direct from http://phogue.net");
  86. }
  87. //this.Invoke(new DownloadErrorDelegate(DownloadError_Callback), "Downloaded file failed checksum, please try again or download direct from http://phogue.net");
  88. }
  89. }
  90. }
  91. }