downloader.gd 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 get_stats() -> Download:
  30. return currentDownload
  31. func _reset() -> void:
  32. currentDownload = Download.new()
  33. func _downloads_done() -> void:
  34. set_process(false)
  35. _update_stats()
  36. _file.close()
  37. emit_signal("downloads_finished", currentDownload)
  38. _reset()
  39. downloading = false
  40. func _update_stats() -> void:
  41. if blind_mode == false:
  42. _calculate_percentage()
  43. emit_signal("stats_updated",currentDownload)
  44. func _process(delta):
  45. if not downloading:
  46. if len(downloads) == 0:
  47. set_process(false)
  48. return
  49. for kind in downloads:
  50. for file in downloads[kind]:
  51. var dl = downloads[kind][file]
  52. currentDownload = dl
  53. _download()
  54. _timer += delta
  55. if _timer > 1:
  56. _update_stats()
  57. _timer = 0.0
  58. func _download():
  59. ensureFolder(currentDownload._kind)
  60. download_file = currentDownload._file_name
  61. .connect("stats_updated", Messenger, "_on_statusUpdate")
  62. _send_head_request()
  63. print("downloading %s" %currentDownload._file_name)
  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. emit_signal("file_downloaded", currentDownload)
  116. Messenger._on_file_downloaded(currentDownload)
  117. downloads[currentDownload._kind].erase(currentDownload._file_name)
  118. if len(downloads[currentDownload._kind]) == 0:
  119. downloads.erase(currentDownload._kind)
  120. if len(downloads) == 0:
  121. _downloads_done()
  122. elif p_result == RESULT_DOWNLOAD_FILE_CANT_OPEN:
  123. push_error("Download cannot open file %s" %currentDownload._file_name)
  124. else:
  125. print("HTTP Request error: ", p_result)
  126. func _calculate_percentage() -> void:
  127. var error : int
  128. error = _file.open(download_file, File.READ)
  129. if error == OK:
  130. currentDownload._downloaded_size = _file.get_len()
  131. currentDownload._downloaded_percent = (currentDownload._downloaded_size / currentDownload._file_size) *100
  132. func _extract_regex_from_header(p_regex : String,
  133. p_header : String) -> String:
  134. var regex = RegEx.new()
  135. regex.compile(p_regex)
  136. var result = regex.search(p_header)
  137. return result.get_string()