2783a52
Branches > main > 2783a52 > files
Add serverbound known packs packet
Commit: 2783a52b3f795e07b98bb9333f9f66790cee53d1
Parent: cfa14b4
4 changed files:
src/net/bowmap/server/protocol/readers.clj view file
@@ -6,7 +6,8 @@ (:require [clojure.spec.alpha :as s] [medley.core :refer [assoc-some]]- [net.bowmap.server.protocol.types :refer [max-varint-size]])+ [net.bowmap.server.protocol.types :refer [max-varint-size]]+ [net.bowmap.server.protocol.types :as types]) (:import [io.netty.buffer ByteBuf] [java.nio.charset Charset]@@ -71,6 +72,23 @@ result' (recur (inc num-read) result')))))) +(defn array<+ "Returns a readers that 'length' number of values into a vector from+ a buffer via 'reader'."+ [reader length]+ (let [reader (->reader reader)]+ (fn [buf]+ (into [] (repeatedly length #(reader buf))))))++(defn list<+ "Returns a reader that reads a varint length, then reads that many values+ into a vector via 'reader' from a buffer."+ [reader]+ (let [reader (->reader reader)]+ (fn [buf]+ (let [length (varint< buf)]+ ((array< reader length) buf)))))+ (defn string< [buf] (let [len (varint< buf)]@@ -78,6 +96,13 @@ (throw (Exception. "String length cannot be negative")) (ByteBuf/.readCharSequence buf len (Charset/forName "UTF-8"))))) +(defn split-identifier<+ "Reads an identifier from 2 strings: the namespace and the name."+ [buf]+ (let [namespace (string< buf)+ name (string< buf)]+ (types/decode-identifier namespace name)))+ (defn uuid< [buf] (UUID. (long< buf) (long< buf))) src/net/bowmap/server/protocol/schema.clj view file
@@ -1,6 +1,6 @@ (ns net.bowmap.server.protocol.schema (:require [clojure.spec.alpha :as s]- [net.bowmap.server.protocol.types :refer [identifier?]]))+ [net.bowmap.server.protocol.types :refer [identifier]])) -(s/def ::identifier identifier?)+(s/def ::identifier identifier)src/net/bowmap/server/protocol/serverbound_packets.clj view file
@@ -29,7 +29,8 @@ ::keep-alive nil ::pong nil ::resource-pack-response nil- ::known-packs nil+ ::known-packs [::p/known-packs (list< [::p/identifier split-identifier<+ ::p/pack-version string<])] ::custom-click-action nil ::accept-code-of-conduct nil] ::phase/playsrc/net/bowmap/server/protocol/types.clj view file
@@ -4,30 +4,42 @@ (def max-varint-size 5) -(defn identifier?- "Returns whether 'x' is a valid protocol identifier.+(def namespace-regex #"[a-z0-9.\-_]+")+(def name-regex #"[a-z0-9.\-_:]+")++(defn identifier+ "Returns 'x' when it is a valid protocol identifier. Identifiers are namespace qualified keywords with the syntax ':namespace/foo:bar:baz', where colons separate directories. While unqualified identifiers default to the 'minecraft' namespace in the protocol, these must be qualifeid as ':minecraft/name'." [x] (and (qualified-keyword? x)- (re-matches #"[a-z0-9.\-_]+" (namespace x))- (re-matches #"[a-z0-9.\-_:]+" (name x))))+ (re-matches namespace-regex (namespace x))+ (re-matches name-regex (name x))+ x)) (defn identifier-segments- "Returns a map of [:namespace :name], the encoded segments of 'identifer'.- Throws if 'identifier' is not a valid identifier."- [identifier]- (if (identifier? identifier)- {:namespace (namespace identifier)- :name (str/replace (name identifier) #":" "/")}+ "Returns a map of [:namespace :name], the encoded segments of the given identifier.+ Throws if it is not a valid identifier."+ [ident]+ (if (identifier ident)+ {:namespace (namespace ident)+ :name (str/replace (name ident) #":" "/")} (throw (ex-info "Cannot encode that value as an identifier." {:value identifier})))) +(defn decode-identifier+ "Returns an identifier from an encoded string namespace and name.+ Throws if the encoded values are not valid identifiers."+ [namespace name]+ (or (identifier (keyword namespace (str/replace name #"/" ":")))+ (throw (ex-info "Cannot decode that value into an identifier."+ {:namespace namespace :name name}))))+ (defn encode-identifier- "Encodes 'identifer' as a protocol identifier string.- Throws if 'identifier' is not valid identifier."- [identifier]- (let [{:keys! [namespace name]} (identifier-segments identifier)]+ "Encodes an identifier as a protocol identifier string.+ Throws if it is not valid identifier."+ [ident]+ (let [{:keys! [namespace name]} (identifier-segments ident)] (str namespace ":" name)))