check_for_updates.gd 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. print("Latest GitHub version: ", latest_version)
  24. var update_notes = response.get("body", "")
  25. if _version_is_newer(latest_version, current_version):
  26. _show_update_popup(latest_version, update_notes)
  27. func _version_is_newer(latest: String, current: String) -> bool:
  28. #clean up version tags remove -alpha -beta and v and split the number sup
  29. latest = trim_suffix(latest, "-alpha")
  30. latest = trim_suffix(latest, "-beta")
  31. var latest_parts = latest.trim_prefix("v").split(".")
  32. current = trim_suffix(current, "-alpha")
  33. current = trim_suffix(current, "-beta")
  34. var current_parts = current.trim_prefix("v").split(".")
  35. #check if current version < latest
  36. for i in range(min(latest_parts.size(), current_parts.size())):
  37. var l = int(latest_parts[i])
  38. var c = int(current_parts[i])
  39. if l > c:
  40. return true
  41. elif l < c:
  42. return false
  43. return latest_parts.size() > current_parts.size()
  44. func trim_suffix(text: String, suffix: String) -> String:
  45. #used to remove -alpha and -beta tags
  46. if text.ends_with(suffix):
  47. return text.substr(0, text.length() - suffix.length())
  48. return text
  49. func _show_update_popup(new_version: String, update_notes: String):
  50. $UpdatePopup/Label.text = "A new version of SoundThread (" + new_version + ") is available to download."
  51. $UpdatePopup/UpdateNotes.text = "[b]Update Details[/b] \n" + update_notes
  52. $UpdatePopup.popup_centered()
  53. func _on_open_audio_settings_button_down() -> void:
  54. $UpdatePopup.hide()
  55. OS.shell_open("https://github.com/j-p-higgins/SoundThread/releases/latest")
  56. func _on_update_popup_close_requested() -> void:
  57. $UpdatePopup.hide()