From b6b24b10c6fb056bfa297c6f3a6f29a9991f9fa1 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 13 Apr 2023 14:50:27 -0400 Subject: Client authenticates now --- Archipelago/client.gd | 47 +++++++++++ Archipelago/vendor/LICENSE | 21 +++++ Archipelago/vendor/uuid.gd | 195 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 Archipelago/vendor/LICENSE create mode 100644 Archipelago/vendor/uuid.gd (limited to 'Archipelago') diff --git a/Archipelago/client.gd b/Archipelago/client.gd index 489ec70..62f4d71 100644 --- a/Archipelago/client.gd +++ b/Archipelago/client.gd @@ -4,6 +4,8 @@ var ap_server = "" var ap_user = "" var ap_pass = "" +const ap_version = {"major": 0, "minor": 4, "build": 0, "class": "Version"} + var _client = WebSocketClient.new() var _last_state = WebSocketPeer.STATE_CLOSED var _should_process = false @@ -12,6 +14,15 @@ var _datapackage_checksum = "" var _item_name_to_id = {} var _location_name_to_id = {} +const uuid_util = preload("user://maps/Archipelago/vendor/uuid.gd") + +var _authenticated = false +var _team = 0 +var _slot = 0 +var _players = [] +var _checked_locations = [] +var _slot_data = {} + func _init(): global._print("Instantiated APClient") @@ -47,6 +58,7 @@ func _ready(): func _closed(was_clean = false): global._print("Closed, clean: " + was_clean) _should_process = false + _authenticated = false func _connected(_proto = ""): @@ -69,6 +81,9 @@ func _on_data(): if message["datapackage_checksums"].has("Lingo"): if _datapackage_checksum != message["datapackage_checksums"]["Lingo"]: requestDatapackage() + else: + connectToRoom() + elif cmd == "DataPackage": if message["data"]["games"].has("Lingo"): var lingo_pkg = message["data"]["games"]["Lingo"] @@ -77,6 +92,20 @@ func _on_data(): _location_name_to_id = lingo_pkg["location_name_to_id"] saveSettings() + connectToRoom() + + elif cmd == "Connected": + _authenticated = true + _team = message["team"] + _slot = message["slot"] + _players = message["players"] + _checked_locations = message["checked_locations"] + _slot_data = message["slot_data"] + + elif cmd == "ConnectionRefused": + global._print("Connection to AP refused") + global._print(message) + func _process(_delta): if _should_process: @@ -119,3 +148,21 @@ func sendMessage(msg): func requestDatapackage(): sendMessage([{"cmd": "GetDataPackage", "games": ["Lingo"]}]) + + +func connectToRoom(): + sendMessage( + [ + { + "cmd": "Connect", + "password": ap_pass, + "game": "Lingo", + "name": ap_user, + "uuid": uuid_util.v4(), + "version": ap_version, + "items_handling": 0b111, # always receive our items + "tags": [], + "slot_data": true + } + ] + ) diff --git a/Archipelago/vendor/LICENSE b/Archipelago/vendor/LICENSE new file mode 100644 index 0000000..115ba15 --- /dev/null +++ b/Archipelago/vendor/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Xavier Sellier + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Archipelago/vendor/uuid.gd b/Archipelago/vendor/uuid.gd new file mode 100644 index 0000000..b63fa04 --- /dev/null +++ b/Archipelago/vendor/uuid.gd @@ -0,0 +1,195 @@ +# Note: The code might not be as pretty it could be, since it's written +# in a way that maximizes performance. Methods are inlined and loops are avoided. +extends Node + +const BYTE_MASK: int = 0b11111111 + + +static func uuidbin(): + randomize() + # 16 random bytes with the bytes on index 6 and 8 modified + return [ + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + ((randi() & BYTE_MASK) & 0x0f) | 0x40, + randi() & BYTE_MASK, + ((randi() & BYTE_MASK) & 0x3f) | 0x80, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + randi() & BYTE_MASK, + ] + + +static func uuidbinrng(rng: RandomNumberGenerator): + rng.randomize() + return [ + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + ((rng.randi() & BYTE_MASK) & 0x0f) | 0x40, + rng.randi() & BYTE_MASK, + ((rng.randi() & BYTE_MASK) & 0x3f) | 0x80, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + rng.randi() & BYTE_MASK, + ] + + +static func v4(): + # 16 random bytes with the bytes on index 6 and 8 modified + var b = uuidbin() + + return ( + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" + % [ + # low + b[0], + b[1], + b[2], + b[3], + # mid + b[4], + b[5], + # hi + b[6], + b[7], + # clock + b[8], + b[9], + # clock + b[10], + b[11], + b[12], + b[13], + b[14], + b[15] + ] + ) + + +static func v4_rng(rng: RandomNumberGenerator): + # 16 random bytes with the bytes on index 6 and 8 modified + var b = uuidbinrng(rng) + + return ( + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" + % [ + # low + b[0], + b[1], + b[2], + b[3], + # mid + b[4], + b[5], + # hi + b[6], + b[7], + # clock + b[8], + b[9], + # clock + b[10], + b[11], + b[12], + b[13], + b[14], + b[15] + ] + ) + + +var _uuid: Array + + +func _init(rng := RandomNumberGenerator.new()) -> void: + _uuid = uuidbinrng(rng) + + +func as_array() -> Array: + return _uuid.duplicate() + + +func as_dict(big_endian := true) -> Dictionary: + if big_endian: + return { + "low": (_uuid[0] << 24) + (_uuid[1] << 16) + (_uuid[2] << 8) + _uuid[3], + "mid": (_uuid[4] << 8) + _uuid[5], + "hi": (_uuid[6] << 8) + _uuid[7], + "clock": (_uuid[8] << 8) + _uuid[9], + "node": + ( + (_uuid[10] << 40) + + (_uuid[11] << 32) + + (_uuid[12] << 24) + + (_uuid[13] << 16) + + (_uuid[14] << 8) + + _uuid[15] + ) + } + else: + return { + "low": _uuid[0] + (_uuid[1] << 8) + (_uuid[2] << 16) + (_uuid[3] << 24), + "mid": _uuid[4] + (_uuid[5] << 8), + "hi": _uuid[6] + (_uuid[7] << 8), + "clock": _uuid[8] + (_uuid[9] << 8), + "node": + ( + _uuid[10] + + (_uuid[11] << 8) + + (_uuid[12] << 16) + + (_uuid[13] << 24) + + (_uuid[14] << 32) + + (_uuid[15] << 40) + ) + } + + +func as_string() -> String: + return ( + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" + % [ + # low + _uuid[0], + _uuid[1], + _uuid[2], + _uuid[3], + # mid + _uuid[4], + _uuid[5], + # hi + _uuid[6], + _uuid[7], + # clock + _uuid[8], + _uuid[9], + # node + _uuid[10], + _uuid[11], + _uuid[12], + _uuid[13], + _uuid[14], + _uuid[15] + ] + ) + + +func is_equal(other) -> bool: + # Godot Engine compares Array recursively + # There's no need for custom comparison here. + return _uuid == other._uuid -- cgit 1.4.1