|
| 1 | +@tool |
| 2 | +extends Node |
| 3 | +## Loads the catalog feed and reports what it got through `loaded`. |
| 4 | +## |
| 5 | +## - Uses the cached copy when it is under a day old (unless forced). |
| 6 | +## - Otherwise sends a conditional request (If-None-Match / If-Modified-Since); |
| 7 | +## a 304 keeps the cached copy and marks it fresh. |
| 8 | +## - When the request fails, or the feed is too new or broken, it falls back to |
| 9 | +## the cached copy and says why; with no cache it reports the problem. |
| 10 | + |
| 11 | +## state: {"feed": Dictionary ({} when there is none), "source": "network" | "cache" | "none", "message": String} |
| 12 | +signal loaded(state: Dictionary) |
| 13 | + |
| 14 | +const Feed := preload("feed.gd") |
| 15 | +const Cache := preload("feed_cache.gd") |
| 16 | + |
| 17 | +const TIMEOUT_SEC := 20.0 |
| 18 | +const USER_AGENT := "free-game-dev-assets-godot (+https://github.com/TMHSDigital/free-game-dev-assets-godot)" |
| 19 | + |
| 20 | +## Folder for the cached feed. The dock sets it to the editor's cache folder. |
| 21 | +var cache_dir := "" |
| 22 | +var url := Feed.URL |
| 23 | + |
| 24 | +var _http: HTTPRequest |
| 25 | +var _busy := false |
| 26 | + |
| 27 | + |
| 28 | +func _ready() -> void: |
| 29 | + _http = HTTPRequest.new() |
| 30 | + _http.timeout = TIMEOUT_SEC |
| 31 | + _http.accept_gzip = true |
| 32 | + add_child(_http) |
| 33 | + _http.request_completed.connect(_on_request_completed) |
| 34 | + |
| 35 | + |
| 36 | +func is_busy() -> bool: |
| 37 | + return _busy |
| 38 | + |
| 39 | + |
| 40 | +## Loads the feed. `force` skips the once-a-day rule (the Refresh button). |
| 41 | +func refresh(force := false) -> void: |
| 42 | + if _busy: |
| 43 | + return |
| 44 | + var cached := _cached() |
| 45 | + var meta := Cache.read_meta(cache_dir) |
| 46 | + if not force and cached["ok"] and Cache.is_fresh(meta, _now()): |
| 47 | + loaded.emit(_state(cached["feed"], "cache", "")) |
| 48 | + return |
| 49 | + var headers := PackedStringArray(["User-Agent: " + USER_AGENT]) |
| 50 | + # Validators only make sense when there is a cached body to fall back on. |
| 51 | + if cached["ok"]: |
| 52 | + if String(meta.get("etag", "")) != "": |
| 53 | + headers.append("If-None-Match: " + String(meta["etag"])) |
| 54 | + if String(meta.get("last_modified", "")) != "": |
| 55 | + headers.append("If-Modified-Since: " + String(meta["last_modified"])) |
| 56 | + _busy = true |
| 57 | + var err := _http.request(url, headers) |
| 58 | + if err != OK: |
| 59 | + _busy = false |
| 60 | + _fall_back(cached, "the request could not start (error %d)" % err) |
| 61 | + |
| 62 | + |
| 63 | +func _on_request_completed(result: int, code: int, headers: PackedStringArray, body: PackedByteArray) -> void: |
| 64 | + _busy = false |
| 65 | + var cached := _cached() |
| 66 | + if result != HTTPRequest.RESULT_SUCCESS: |
| 67 | + _fall_back(cached, "offline or unreachable (%s)" % _result_text(result)) |
| 68 | + return |
| 69 | + if code == 304 and cached["ok"]: |
| 70 | + var meta := Cache.read_meta(cache_dir) |
| 71 | + meta["fetched_at"] = _now() |
| 72 | + Cache.write_meta(cache_dir, meta) |
| 73 | + loaded.emit(_state(cached["feed"], "cache", "")) |
| 74 | + return |
| 75 | + if code != 200: |
| 76 | + _fall_back(cached, "the catalog answered HTTP %d" % code) |
| 77 | + return |
| 78 | + var text := body.get_string_from_utf8() |
| 79 | + var parsed := Feed.parse(text) |
| 80 | + if not parsed["ok"]: |
| 81 | + _fall_back(cached, parsed["error"]) |
| 82 | + return |
| 83 | + Cache.write(cache_dir, text, { |
| 84 | + "etag": _header(headers, "etag"), |
| 85 | + "last_modified": _header(headers, "last-modified"), |
| 86 | + "fetched_at": _now(), |
| 87 | + }) |
| 88 | + loaded.emit(_state(parsed["feed"], "network", "")) |
| 89 | + |
| 90 | + |
| 91 | +func _fall_back(cached: Dictionary, why: String) -> void: |
| 92 | + if cached["ok"]: |
| 93 | + var version := String(cached["feed"].get("version", "")) |
| 94 | + loaded.emit(_state(cached["feed"], "cache", "Showing saved data from %s: %s." % [version, why])) |
| 95 | + else: |
| 96 | + loaded.emit(_state({}, "none", "Could not load the catalog: %s." % why)) |
| 97 | + |
| 98 | + |
| 99 | +func _cached() -> Dictionary: |
| 100 | + var text := Cache.read_text(cache_dir) |
| 101 | + if text == "": |
| 102 | + return {"ok": false} |
| 103 | + # A cache written by a newer plugin version can be too new for this one. |
| 104 | + return Feed.parse(text) |
| 105 | + |
| 106 | + |
| 107 | +func _state(feed: Dictionary, source: String, message: String) -> Dictionary: |
| 108 | + return {"feed": feed, "source": source, "message": message} |
| 109 | + |
| 110 | + |
| 111 | +static func _header(headers: PackedStringArray, name: String) -> String: |
| 112 | + var prefix := name.to_lower() + ":" |
| 113 | + for h in headers: |
| 114 | + if h.to_lower().begins_with(prefix): |
| 115 | + return h.substr(prefix.length()).strip_edges() |
| 116 | + return "" |
| 117 | + |
| 118 | + |
| 119 | +static func _result_text(result: int) -> String: |
| 120 | + match result: |
| 121 | + HTTPRequest.RESULT_CANT_CONNECT, HTTPRequest.RESULT_CANT_RESOLVE: |
| 122 | + return "no connection" |
| 123 | + HTTPRequest.RESULT_TIMEOUT: |
| 124 | + return "timed out" |
| 125 | + HTTPRequest.RESULT_TLS_HANDSHAKE_ERROR: |
| 126 | + return "TLS error" |
| 127 | + _: |
| 128 | + return "request error %d" % result |
| 129 | + |
| 130 | + |
| 131 | +func _now() -> float: |
| 132 | + return Time.get_unix_time_from_system() |
0 commit comments