check_for_updates.gd 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. extends HTTPRequest
  2. const GITHUB_API_URL := "https://api.github.com/repos/j-p-higgins/SoundThread/releases/latest"
  3. var current_version
  4. func _ready():
  5. $UpdatePopup.hide()
  6. #get current version from export presets
  7. var export_config = ConfigFile.new()
  8. export_config.load("res://export_presets.cfg")
  9. current_version = export_config.get_value("preset.0.options", "application/product_version", "version unknown")
  10. #call github api
  11. if not is_connected("request_completed", Callable(self, "_on_request_completed")):
  12. connect("request_completed", Callable(self, "_on_request_completed"))
  13. request(GITHUB_API_URL, ["User-Agent: SoundThread0"])
  14. func _on_request_completed(result, response_code, headers, body):
  15. if response_code != 200:
  16. print("Failed to check for updates.")
  17. return
  18. var response = JSON.parse_string(body.get_string_from_utf8())
  19. if typeof(response) != TYPE_DICTIONARY:
  20. print("Invalid JSON in GitHub response.")
  21. return
  22. var latest_version = response.get("tag_name", "")
  23. var update_notes = response.get("body", "")
  24. if _version_is_newer(latest_version, current_version):
  25. _show_update_popup(latest_version, update_notes)
  26. func _version_is_newer(latest: String, current: String) -> bool:
  27. #clean up version tags remove -alpha -beta and v and split the number sup
  28. latest = trim_suffix(latest, "-alpha")
  29. latest = trim_suffix(latest, "-beta")
  30. var latest_parts = latest.trim_prefix("v").split(".")
  31. current = trim_suffix(current, "-alpha")
  32. current = trim_suffix(current, "-beta")
  33. var current_parts = current.trim_prefix("v").split(".")
  34. #check if current version < latest
  35. for i in range(min(latest_parts.size(), current_parts.size())):
  36. var l = int(latest_parts[i])
  37. var c = int(current_parts[i])
  38. if l > c:
  39. return true
  40. elif l < c:
  41. return false
  42. return latest_parts.size() > current_parts.size()
  43. func trim_suffix(text: String, suffix: String) -> String:
  44. #used to remove -alpha and -beta tags
  45. if text.ends_with(suffix):
  46. return text.substr(0, text.length() - suffix.length())
  47. return text
  48. func _show_update_popup(new_version: String, update_notes: String):
  49. $UpdatePopup/Label.text = "A new version of SoundThread (" + new_version + ") is available to download."
  50. $UpdatePopup/UpdateNotes.text = "[b]Update Details[/b] \n" + update_notes
  51. $UpdatePopup.popup_centered()
  52. func _on_open_audio_settings_button_down() -> void:
  53. $UpdatePopup.hide()
  54. OS.shell_open("https://github.com/j-p-higgins/SoundThread/releases/latest")
  55. func _on_update_popup_close_requested() -> void:
  56. $UpdatePopup.hide()