downloader.gd 4.6 KB

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