diff --git a/caching.gd b/caching.gd index 2b7c7c2..060509f 100644 --- a/caching.gd +++ b/caching.gd @@ -12,15 +12,35 @@ var _emitted_start = 0 var _consts = preload("res://data/consts.gd") -func _init() -> void: - _req_headers = PackedStringArray(["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"]) - +func _all_downloads_done() -> bool: + return _emitted_done == _emitted_start + +func _setup_cache_in_mem(): var file = FileAccess.open("user://bulk.json", FileAccess.READ) _bulk_data = JSON.parse_string(file.get_as_text()) file = null + +func setup() -> Error: + if !FileAccess.file_exists("user://bulk.json"): + get_bulk_data(false) + push_error("Bulk Data was not downloaded! Downloading now!") + return FAILED + + if !_all_downloads_done(): + push_error("Not done downloading Bulk Data.") + return FAILED + + _setup_cache_in_mem() + return OK + +func _init() -> void: + _req_headers = PackedStringArray(["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"]) + fetch_done.connect(_on_end_emit) fetch_start.connect(_on_start_emit) + + func _on_start_emit() -> void: _emitted_start += 1 @@ -165,9 +185,11 @@ func get_bulk_data(force: bool) -> Error: data_cache.store_string(unprocessed_body) data_cache = null + fetch_done.emit() + return OK func _notification(what): if what == NOTIFICATION_PREDELETE: - if _emitted_done != _emitted_start: + if !_all_downloads_done(): push_error("ERR::MEM::CACHE\nCache being deleted before all threads have finished processing!") diff --git a/card.gd b/card.gd index 1384778..7af90a7 100644 --- a/card.gd +++ b/card.gd @@ -64,7 +64,7 @@ func _gui_input(event: InputEvent) -> void: MOUSE_BUTTON_RIGHT: pass - + func _unhandled_key_input(event: InputEvent) -> void: if not event.is_action_pressed("default_action"): return diff --git a/tabletop.gd b/tabletop.gd index 30df792..6bbb7b0 100644 --- a/tabletop.gd +++ b/tabletop.gd @@ -5,11 +5,16 @@ var deck_input = preload("res://deck_input.gd") var _caching = preload("res://caching.gd") +func _bulk_callback(cache) -> void: + cache.setup() + # Called when the node enters the scene tree for the first time. func _ready() -> void: var cache = _caching.new() add_child(cache) - cache.get_bulk_data(false) + + if cache.setup() != OK: + cache.fetch_done.connect(_bulk_callback.bind(cache)) # TODO: Create 2-4 player instances as children of this tabletop node. var player = player_class.new()