downloader.gd 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. extends HTTPRequest
  2. var kinds = {
  3. "topo": "http://imbrium.mit.edu/DATA/SLDEM2015/TILES/FLOAT_IMG/%s",
  4. "color": ""
  5. }
  6. var downloads = {}
  7. var downloading = false
  8. signal file_downloaded
  9. signal downloads_finished
  10. signal stats_updated
  11. signal downloads_started
  12. export(bool) var blind_mode : bool = false
  13. var _headers : Array = []
  14. var _last_method : int
  15. var _ssl : bool = false
  16. var _file := File.new()
  17. var _timer : float = 0.0
  18. var currentDownload : Download
  19. class Download:
  20. var _file_name : String
  21. var _file_size : float
  22. var _current_url : String
  23. var _kind : String
  24. var _downloaded_percent : float = 0
  25. var _downloaded_size : float = 0
  26. func _init() -> void:
  27. set_process(false)
  28. connect("request_completed", self, "_on_request_completed")
  29. connect("stats_updated", Messenger, "_on_statusUpdate")
  30. func get_stats() -> Download:
  31. return currentDownload
  32. func _reset() -> void:
  33. currentDownload = Download.new()
  34. func _downloads_done() -> void:
  35. set_process(false)
  36. _update_stats()
  37. _file.close()
  38. emit_signal("downloads_finished", currentDownload)
  39. _reset()
  40. downloading = false
  41. func _update_stats() -> void:
  42. if blind_mode == false:
  43. _calculate_percentage()
  44. emit_signal("stats_updated",currentDownload)
  45. func _process(delta):
  46. if not downloading:
  47. if len(downloads) == 0:
  48. set_process(false)
  49. return
  50. for kind in downloads:
  51. for file in downloads[kind]:
  52. var dl = downloads[kind][file]
  53. currentDownload = dl
  54. _download()
  55. _timer += delta
  56. if _timer > 1:
  57. _update_stats()
  58. _timer = 0.0
  59. func _download():
  60. ensureFolder(currentDownload._kind)
  61. download_file = currentDownload._file_name
  62. _send_head_request()
  63. print("downloading %s" %currentDownload._current_url)
  64. downloading = true
  65. func ensureFolder(kind):
  66. var dir = Directory.new()
  67. dir.open("user://")
  68. if not dir.dir_exists(kind):
  69. dir.make_dir(kind)
  70. func download(file, kind):
  71. set_process(true)
  72. if not kinds.has(kind):
  73. print("don't know kind %s"%kind)
  74. return
  75. if not downloads.has(kind):
  76. downloads[kind] = {}
  77. if downloads[kind].has(file):
  78. return
  79. var download = Download.new()
  80. download._file_name = "user://%s/%s"%[kind, file]
  81. download._current_url = make_filename(kind, file)
  82. download._kind = kind
  83. downloads[kind][file] = download
  84. func make_filename(kind, file):
  85. return kinds[kind]%file
  86. func _send_head_request() -> void:
  87. # The HEAD method only gets the head and not the body. Therefore, doesn't
  88. # download the file.
  89. request(currentDownload._current_url, _headers, _ssl, HTTPClient.METHOD_HEAD)
  90. _last_method = HTTPClient.METHOD_HEAD
  91. func _send_get_request() -> void:
  92. var error = request(currentDownload._current_url, _headers, _ssl, HTTPClient.METHOD_GET)
  93. if error == OK:
  94. emit_signal("downloads_started")
  95. _last_method = HTTPClient.METHOD_GET
  96. set_process(true)
  97. elif error == ERR_INVALID_PARAMETER:
  98. print("Given string isn't a valid url ")
  99. elif error == ERR_CANT_CONNECT:
  100. print("Can't connect to host")
  101. func _on_request_completed(p_result,
  102. _p_response_code,
  103. p_headers,
  104. _p_body) -> void:
  105. if p_result == RESULT_SUCCESS:
  106. if _last_method == HTTPClient.METHOD_HEAD and blind_mode == false:
  107. var regex = "(?i)content-length: [0-9]*"
  108. var size = _extract_regex_from_header(regex, p_headers.join(' '))
  109. size = size.replace("Content-Length: ", "")
  110. currentDownload._file_size = size.to_float()
  111. _send_get_request()
  112. elif _last_method == HTTPClient.METHOD_HEAD and blind_mode == true:
  113. _send_get_request()
  114. elif _last_method == HTTPClient.METHOD_GET:
  115. print("file download complete %s"%currentDownload._current_url)
  116. _file.close()
  117. emit_signal("file_downloaded", currentDownload)
  118. Messenger._on_file_downloaded(currentDownload)
  119. downloads[currentDownload._kind].erase(currentDownload._file_name)
  120. if len(downloads[currentDownload._kind]) == 0:
  121. downloads.erase(currentDownload._kind)
  122. if len(downloads) == 0:
  123. _downloads_done()
  124. elif p_result == RESULT_DOWNLOAD_FILE_CANT_OPEN:
  125. push_error("Download cannot open file %s" %currentDownload._file_name)
  126. else:
  127. print("HTTP Request error: ", p_result)
  128. func _calculate_percentage() -> void:
  129. var error : int
  130. error = _file.open(download_file, File.READ)
  131. if error == OK:
  132. currentDownload._downloaded_size = _file.get_len()
  133. currentDownload._downloaded_percent = (currentDownload._downloaded_size / currentDownload._file_size) *100
  134. func _extract_regex_from_header(p_regex : String,
  135. p_header : String) -> String:
  136. var regex = RegEx.new()
  137. regex.compile(p_regex)
  138. var result = regex.search(p_header)
  139. return result.get_string()