diff options
Diffstat (limited to 'apworld/client/vendor')
| -rw-r--r-- | apworld/client/vendor/LICENSE | 28 | ||||
| -rw-r--r-- | apworld/client/vendor/WebSocketServer.gd | 173 | ||||
| -rw-r--r-- | apworld/client/vendor/uuid.gd | 195 |
3 files changed, 187 insertions, 209 deletions
| diff --git a/apworld/client/vendor/LICENSE b/apworld/client/vendor/LICENSE index 115ba15..12763b1 100644 --- a/apworld/client/vendor/LICENSE +++ b/apworld/client/vendor/LICENSE | |||
| @@ -1,21 +1,21 @@ | |||
| 1 | MIT License | 1 | WebSocketServer.gd: |
| 2 | 2 | ||
| 3 | Copyright (c) 2023 Xavier Sellier | 3 | Copyright (c) 2014-present Godot Engine contributors. Copyright (c) 2007-2014 |
| 4 | Juan Linietsky, Ariel Manzur. | ||
| 4 | 5 | ||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 | of this software and associated documentation files (the "Software"), to deal | 7 | this software and associated documentation files (the "Software"), to deal in |
| 7 | in the Software without restriction, including without limitation the rights | 8 | the Software without restriction, including without limitation the rights to |
| 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | copies of the Software, and to permit persons to whom the Software is | 10 | the Software, and to permit persons to whom the Software is furnished to do so, |
| 10 | furnished to do so, subject to the following conditions: | 11 | subject to the following conditions: |
| 11 | 12 | ||
| 12 | The above copyright notice and this permission notice shall be included in all | 13 | The above copyright notice and this permission notice shall be included in all |
| 13 | copies or substantial portions of the Software. | 14 | copies or substantial portions of the Software. |
| 14 | 15 | ||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 18 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 19 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 20 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | SOFTWARE. \ No newline at end of file | ||
| diff --git a/apworld/client/vendor/WebSocketServer.gd b/apworld/client/vendor/WebSocketServer.gd new file mode 100644 index 0000000..2cee494 --- /dev/null +++ b/apworld/client/vendor/WebSocketServer.gd | |||
| @@ -0,0 +1,173 @@ | |||
| 1 | class_name WebSocketServer | ||
| 2 | extends Node | ||
| 3 | |||
| 4 | signal message_received(peer_id: int, message: String) | ||
| 5 | signal client_connected(peer_id: int) | ||
| 6 | signal client_disconnected(peer_id: int) | ||
| 7 | |||
| 8 | @export var handshake_headers := PackedStringArray() | ||
| 9 | @export var supported_protocols := PackedStringArray() | ||
| 10 | @export var handshake_timout := 3000 | ||
| 11 | @export var use_tls := false | ||
| 12 | @export var tls_cert: X509Certificate | ||
| 13 | @export var tls_key: CryptoKey | ||
| 14 | @export var refuse_new_connections := false: | ||
| 15 | set(refuse): | ||
| 16 | if refuse: | ||
| 17 | pending_peers.clear() | ||
| 18 | |||
| 19 | |||
| 20 | class PendingPeer: | ||
| 21 | var connect_time: int | ||
| 22 | var tcp: StreamPeerTCP | ||
| 23 | var connection: StreamPeer | ||
| 24 | var ws: WebSocketPeer | ||
| 25 | |||
| 26 | func _init(p_tcp: StreamPeerTCP) -> void: | ||
| 27 | tcp = p_tcp | ||
| 28 | connection = p_tcp | ||
| 29 | connect_time = Time.get_ticks_msec() | ||
| 30 | |||
| 31 | |||
| 32 | var tcp_server := TCPServer.new() | ||
| 33 | var pending_peers: Array[PendingPeer] = [] | ||
| 34 | var peers: Dictionary | ||
| 35 | |||
| 36 | |||
| 37 | func listen(port: int) -> int: | ||
| 38 | assert(not tcp_server.is_listening()) | ||
| 39 | return tcp_server.listen(port) | ||
| 40 | |||
| 41 | |||
| 42 | func stop() -> void: | ||
| 43 | tcp_server.stop() | ||
| 44 | pending_peers.clear() | ||
| 45 | peers.clear() | ||
| 46 | |||
| 47 | |||
| 48 | func send(peer_id: int, message: String) -> int: | ||
| 49 | var type := typeof(message) | ||
| 50 | if peer_id <= 0: | ||
| 51 | # Send to multiple peers, (zero = broadcast, negative = exclude one). | ||
| 52 | for id: int in peers: | ||
| 53 | if id == -peer_id: | ||
| 54 | continue | ||
| 55 | if type == TYPE_STRING: | ||
| 56 | peers[id].send_text(message) | ||
| 57 | else: | ||
| 58 | peers[id].put_packet(message) | ||
| 59 | return OK | ||
| 60 | |||
| 61 | assert(peers.has(peer_id)) | ||
| 62 | var socket: WebSocketPeer = peers[peer_id] | ||
| 63 | if type == TYPE_STRING: | ||
| 64 | return socket.send_text(message) | ||
| 65 | return socket.send(var_to_bytes(message)) | ||
| 66 | |||
| 67 | |||
| 68 | func get_message(peer_id: int) -> Variant: | ||
| 69 | assert(peers.has(peer_id)) | ||
| 70 | var socket: WebSocketPeer = peers[peer_id] | ||
| 71 | if socket.get_available_packet_count() < 1: | ||
| 72 | return null | ||
| 73 | var pkt: PackedByteArray = socket.get_packet() | ||
| 74 | if socket.was_string_packet(): | ||
| 75 | return pkt.get_string_from_utf8() | ||
| 76 | return bytes_to_var(pkt) | ||
| 77 | |||
| 78 | |||
| 79 | func has_message(peer_id: int) -> bool: | ||
| 80 | assert(peers.has(peer_id)) | ||
| 81 | return peers[peer_id].get_available_packet_count() > 0 | ||
| 82 | |||
| 83 | |||
| 84 | func _create_peer() -> WebSocketPeer: | ||
| 85 | var ws := WebSocketPeer.new() | ||
| 86 | ws.supported_protocols = supported_protocols | ||
| 87 | ws.handshake_headers = handshake_headers | ||
| 88 | return ws | ||
| 89 | |||
| 90 | |||
| 91 | func poll() -> void: | ||
| 92 | if not tcp_server.is_listening(): | ||
| 93 | return | ||
| 94 | |||
| 95 | while not refuse_new_connections and tcp_server.is_connection_available(): | ||
| 96 | var conn: StreamPeerTCP = tcp_server.take_connection() | ||
| 97 | assert(conn != null) | ||
| 98 | pending_peers.append(PendingPeer.new(conn)) | ||
| 99 | |||
| 100 | var to_remove := [] | ||
| 101 | |||
| 102 | for p in pending_peers: | ||
| 103 | if not _connect_pending(p): | ||
| 104 | if p.connect_time + handshake_timout < Time.get_ticks_msec(): | ||
| 105 | # Timeout. | ||
| 106 | to_remove.append(p) | ||
| 107 | continue # Still pending. | ||
| 108 | |||
| 109 | to_remove.append(p) | ||
| 110 | |||
| 111 | for r: RefCounted in to_remove: | ||
| 112 | pending_peers.erase(r) | ||
| 113 | |||
| 114 | to_remove.clear() | ||
| 115 | |||
| 116 | for id: int in peers: | ||
| 117 | var p: WebSocketPeer = peers[id] | ||
| 118 | p.poll() | ||
| 119 | |||
| 120 | if p.get_ready_state() != WebSocketPeer.STATE_OPEN: | ||
| 121 | client_disconnected.emit(id) | ||
| 122 | to_remove.append(id) | ||
| 123 | continue | ||
| 124 | |||
| 125 | while p.get_available_packet_count(): | ||
| 126 | message_received.emit(id, get_message(id)) | ||
| 127 | |||
| 128 | for r: int in to_remove: | ||
| 129 | peers.erase(r) | ||
| 130 | to_remove.clear() | ||
| 131 | |||
| 132 | |||
| 133 | func _connect_pending(p: PendingPeer) -> bool: | ||
| 134 | if p.ws != null: | ||
| 135 | # Poll websocket client if doing handshake. | ||
| 136 | p.ws.poll() | ||
| 137 | var state := p.ws.get_ready_state() | ||
| 138 | if state == WebSocketPeer.STATE_OPEN: | ||
| 139 | var id := randi_range(2, 1 << 30) | ||
| 140 | peers[id] = p.ws | ||
| 141 | client_connected.emit(id) | ||
| 142 | return true # Success. | ||
| 143 | elif state != WebSocketPeer.STATE_CONNECTING: | ||
| 144 | return true # Failure. | ||
| 145 | return false # Still connecting. | ||
| 146 | elif p.tcp.get_status() != StreamPeerTCP.STATUS_CONNECTED: | ||
| 147 | return true # TCP disconnected. | ||
| 148 | elif not use_tls: | ||
| 149 | # TCP is ready, create WS peer. | ||
| 150 | p.ws = _create_peer() | ||
| 151 | p.ws.accept_stream(p.tcp) | ||
| 152 | return false # WebSocketPeer connection is pending. | ||
| 153 | |||
| 154 | else: | ||
| 155 | if p.connection == p.tcp: | ||
| 156 | assert(tls_key != null and tls_cert != null) | ||
| 157 | var tls := StreamPeerTLS.new() | ||
| 158 | tls.accept_stream(p.tcp, TLSOptions.server(tls_key, tls_cert)) | ||
| 159 | p.connection = tls | ||
| 160 | p.connection.poll() | ||
| 161 | var status: StreamPeerTLS.Status = p.connection.get_status() | ||
| 162 | if status == StreamPeerTLS.STATUS_CONNECTED: | ||
| 163 | p.ws = _create_peer() | ||
| 164 | p.ws.accept_stream(p.connection) | ||
| 165 | return false # WebSocketPeer connection is pending. | ||
| 166 | if status != StreamPeerTLS.STATUS_HANDSHAKING: | ||
| 167 | return true # Failure. | ||
| 168 | |||
| 169 | return false | ||
| 170 | |||
| 171 | |||
| 172 | func _process(_delta: float) -> void: | ||
| 173 | poll() | ||
| diff --git a/apworld/client/vendor/uuid.gd b/apworld/client/vendor/uuid.gd deleted file mode 100644 index b63fa04..0000000 --- a/apworld/client/vendor/uuid.gd +++ /dev/null | |||
| @@ -1,195 +0,0 @@ | |||
| 1 | # Note: The code might not be as pretty it could be, since it's written | ||
| 2 | # in a way that maximizes performance. Methods are inlined and loops are avoided. | ||
| 3 | extends Node | ||
| 4 | |||
| 5 | const BYTE_MASK: int = 0b11111111 | ||
| 6 | |||
| 7 | |||
| 8 | static func uuidbin(): | ||
| 9 | randomize() | ||
| 10 | # 16 random bytes with the bytes on index 6 and 8 modified | ||
| 11 | return [ | ||
| 12 | randi() & BYTE_MASK, | ||
| 13 | randi() & BYTE_MASK, | ||
| 14 | randi() & BYTE_MASK, | ||
| 15 | randi() & BYTE_MASK, | ||
| 16 | randi() & BYTE_MASK, | ||
| 17 | randi() & BYTE_MASK, | ||
| 18 | ((randi() & BYTE_MASK) & 0x0f) | 0x40, | ||
| 19 | randi() & BYTE_MASK, | ||
| 20 | ((randi() & BYTE_MASK) & 0x3f) | 0x80, | ||
| 21 | randi() & BYTE_MASK, | ||
| 22 | randi() & BYTE_MASK, | ||
| 23 | randi() & BYTE_MASK, | ||
| 24 | randi() & BYTE_MASK, | ||
| 25 | randi() & BYTE_MASK, | ||
| 26 | randi() & BYTE_MASK, | ||
| 27 | randi() & BYTE_MASK, | ||
| 28 | ] | ||
| 29 | |||
| 30 | |||
| 31 | static func uuidbinrng(rng: RandomNumberGenerator): | ||
| 32 | rng.randomize() | ||
| 33 | return [ | ||
| 34 | rng.randi() & BYTE_MASK, | ||
| 35 | rng.randi() & BYTE_MASK, | ||
| 36 | rng.randi() & BYTE_MASK, | ||
| 37 | rng.randi() & BYTE_MASK, | ||
| 38 | rng.randi() & BYTE_MASK, | ||
| 39 | rng.randi() & BYTE_MASK, | ||
| 40 | ((rng.randi() & BYTE_MASK) & 0x0f) | 0x40, | ||
| 41 | rng.randi() & BYTE_MASK, | ||
| 42 | ((rng.randi() & BYTE_MASK) & 0x3f) | 0x80, | ||
| 43 | rng.randi() & BYTE_MASK, | ||
| 44 | rng.randi() & BYTE_MASK, | ||
| 45 | rng.randi() & BYTE_MASK, | ||
| 46 | rng.randi() & BYTE_MASK, | ||
| 47 | rng.randi() & BYTE_MASK, | ||
| 48 | rng.randi() & BYTE_MASK, | ||
| 49 | rng.randi() & BYTE_MASK, | ||
| 50 | ] | ||
| 51 | |||
| 52 | |||
| 53 | static func v4(): | ||
| 54 | # 16 random bytes with the bytes on index 6 and 8 modified | ||
| 55 | var b = uuidbin() | ||
| 56 | |||
| 57 | return ( | ||
| 58 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" | ||
| 59 | % [ | ||
| 60 | # low | ||
| 61 | b[0], | ||
| 62 | b[1], | ||
| 63 | b[2], | ||
| 64 | b[3], | ||
| 65 | # mid | ||
| 66 | b[4], | ||
| 67 | b[5], | ||
| 68 | # hi | ||
| 69 | b[6], | ||
| 70 | b[7], | ||
| 71 | # clock | ||
| 72 | b[8], | ||
| 73 | b[9], | ||
| 74 | # clock | ||
| 75 | b[10], | ||
| 76 | b[11], | ||
| 77 | b[12], | ||
| 78 | b[13], | ||
| 79 | b[14], | ||
| 80 | b[15] | ||
| 81 | ] | ||
| 82 | ) | ||
| 83 | |||
| 84 | |||
| 85 | static func v4_rng(rng: RandomNumberGenerator): | ||
| 86 | # 16 random bytes with the bytes on index 6 and 8 modified | ||
| 87 | var b = uuidbinrng(rng) | ||
| 88 | |||
| 89 | return ( | ||
| 90 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" | ||
| 91 | % [ | ||
| 92 | # low | ||
| 93 | b[0], | ||
| 94 | b[1], | ||
| 95 | b[2], | ||
| 96 | b[3], | ||
| 97 | # mid | ||
| 98 | b[4], | ||
| 99 | b[5], | ||
| 100 | # hi | ||
| 101 | b[6], | ||
| 102 | b[7], | ||
| 103 | # clock | ||
| 104 | b[8], | ||
| 105 | b[9], | ||
| 106 | # clock | ||
| 107 | b[10], | ||
| 108 | b[11], | ||
| 109 | b[12], | ||
| 110 | b[13], | ||
| 111 | b[14], | ||
| 112 | b[15] | ||
| 113 | ] | ||
| 114 | ) | ||
| 115 | |||
| 116 | |||
| 117 | var _uuid: Array | ||
| 118 | |||
| 119 | |||
| 120 | func _init(rng := RandomNumberGenerator.new()) -> void: | ||
| 121 | _uuid = uuidbinrng(rng) | ||
| 122 | |||
| 123 | |||
| 124 | func as_array() -> Array: | ||
| 125 | return _uuid.duplicate() | ||
| 126 | |||
| 127 | |||
| 128 | func as_dict(big_endian := true) -> Dictionary: | ||
| 129 | if big_endian: | ||
| 130 | return { | ||
| 131 | "low": (_uuid[0] << 24) + (_uuid[1] << 16) + (_uuid[2] << 8) + _uuid[3], | ||
| 132 | "mid": (_uuid[4] << 8) + _uuid[5], | ||
| 133 | "hi": (_uuid[6] << 8) + _uuid[7], | ||
| 134 | "clock": (_uuid[8] << 8) + _uuid[9], | ||
| 135 | "node": | ||
| 136 | ( | ||
| 137 | (_uuid[10] << 40) | ||
| 138 | + (_uuid[11] << 32) | ||
| 139 | + (_uuid[12] << 24) | ||
| 140 | + (_uuid[13] << 16) | ||
| 141 | + (_uuid[14] << 8) | ||
| 142 | + _uuid[15] | ||
| 143 | ) | ||
| 144 | } | ||
| 145 | else: | ||
| 146 | return { | ||
| 147 | "low": _uuid[0] + (_uuid[1] << 8) + (_uuid[2] << 16) + (_uuid[3] << 24), | ||
| 148 | "mid": _uuid[4] + (_uuid[5] << 8), | ||
| 149 | "hi": _uuid[6] + (_uuid[7] << 8), | ||
| 150 | "clock": _uuid[8] + (_uuid[9] << 8), | ||
| 151 | "node": | ||
| 152 | ( | ||
| 153 | _uuid[10] | ||
| 154 | + (_uuid[11] << 8) | ||
| 155 | + (_uuid[12] << 16) | ||
| 156 | + (_uuid[13] << 24) | ||
| 157 | + (_uuid[14] << 32) | ||
| 158 | + (_uuid[15] << 40) | ||
| 159 | ) | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | func as_string() -> String: | ||
| 164 | return ( | ||
| 165 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" | ||
| 166 | % [ | ||
| 167 | # low | ||
| 168 | _uuid[0], | ||
| 169 | _uuid[1], | ||
| 170 | _uuid[2], | ||
| 171 | _uuid[3], | ||
| 172 | # mid | ||
| 173 | _uuid[4], | ||
| 174 | _uuid[5], | ||
| 175 | # hi | ||
| 176 | _uuid[6], | ||
| 177 | _uuid[7], | ||
| 178 | # clock | ||
| 179 | _uuid[8], | ||
| 180 | _uuid[9], | ||
| 181 | # node | ||
| 182 | _uuid[10], | ||
| 183 | _uuid[11], | ||
| 184 | _uuid[12], | ||
| 185 | _uuid[13], | ||
| 186 | _uuid[14], | ||
| 187 | _uuid[15] | ||
| 188 | ] | ||
| 189 | ) | ||
| 190 | |||
| 191 | |||
| 192 | func is_equal(other) -> bool: | ||
| 193 | # Godot Engine compares Array recursively | ||
| 194 | # There's no need for custom comparison here. | ||
| 195 | return _uuid == other._uuid | ||
