d4a482d

Bowmap · bowmap.git · Tickets

Branches > main > d4a482d > files

Update readers and writers

Grahp, 2026-10-12

Commit: d4a482ddff99696c83595f5dcd52bcd95950c2d1
Parent: 9c8448f

2 changed files:

src/net/bowmap/server/packet/readers.clj modified (view file)

@@ -50,6 +50,24 @@ (def float< ByteBuf/.readFloat) (def double< ByteBuf/.readDouble) +(defn apply<+  "Returns a reader that calls f with the result of reading with each reader."+  ([f reader]+   (fn [buf]+     (f+      (reader buf))))+  ([f reader1 reader2]+   (fn [buf]+     (f (reader1 buf)+        (reader2 buf)))))++(defn then<+  "Returns a reader that reads a value via 'reader', calls (f value) to get+   the next reader, and reads with that."+  [reader f]+  (fn [buf]+    ((f (reader buf)) buf)))+ (def max-varint-size 5)  (defn varint< [buf]@@ -75,29 +93,18 @@    into a vector via 'reader' from a buffer.    When supplied 2 or more key value pairs, reads a vector of maps."   ([reader]-   (fn [buf]-     (let [length (varint< buf)]-       ((array< reader length) buf))))+   (then< varint< (partial array< reader)))   ([k v & kvs]    (list< (apply map< k v kvs)))) -(defn string<-  [buf]-  (let [len (varint< buf)]-    (if (neg? len)-      (throw (Exception. "String length cannot be negative"))-      (ByteBuf/.readCharSequence buf len StandardCharsets/UTF_8))))+(defn string< [buf]+  (ByteBuf/.readCharSequence buf (varint< buf) StandardCharsets/UTF_8)) -(defn split-identifier<+(def split-identifier<   "Reads an identifier from 2 strings: the namespace and the name."-  [buf]-  (let [namespace (string< buf)-        name (string< buf)]-    (identifier/decode-identifier namespace name)))+  (apply< identifier/decode-identifier string< string<)) -(defn uuid< [buf]-  (UUID. (long< buf) (long< buf)))+(def uuid< (apply< UUID/new long< long<))  (defn enum< [reader m]-  (fn [buf]-    (m (reader buf))))+  (apply< m reader))

src/net/bowmap/server/packet/writers.clj modified (view file)

@@ -21,6 +21,7 @@ (s/def ::writer (s/fspec                   :args (s/cat :buf #(instance? ByteBuf %)                                :value any?)+                  :ret #(instance? ByteBuf %)                   :fn #(identical? (-> % :args :buf)                                    (:ret %)))) @@ -44,7 +45,7 @@   [& keys-and-writers]   {:pre [(even? (count keys-and-writers))]}   (let [pairs (partition 2 keys-and-writers)]-    (fn map-writer [buf m]+    (fn [buf m]       (reduce (fn [acc [k writer]]                 (write-key writer acc m k))               buf pairs))))@@ -185,11 +186,8 @@   "Returns a writer that prefixes the value with a boolean indicating whether    it is present, and only writes the value via 'writer' if the value is non-nil."   [writer]-  (let [writer (context-optional> writer)]-    (fn [buf x]-      (-> buf-        (boolean> (some? x))-        (writer x)))))+  (then> (apply> some? boolean>)+         (context-optional> writer)))  (def game-profile> (map> ::p/uuid uuid>                          ::p/username (string> 16)@@ -203,8 +201,9 @@    when the value is nil."   [writer default]   (fn [buf x]-    (let [x (if (nil? x) default x)]-      (writer buf x))))+    (writer buf (if (some? x)+                  x+                  default))))  (defn enum>   "Returns a writer that writes one of a set of values via 'writer'.@@ -213,9 +212,7 @@    will write 1 (via 'writer') when given :foo, so the map is 'backwards'.    When the value is not in the enum map, calls 'writer' with nil."   [writer m]-  (let [m (set/map-invert m)]-    (fn [buf x]-      (writer buf (m x)))))+  (apply> (set/map-invert m) writer))  (defn block-location>   "Writes a block location as a 64-bit integer made of 3 signed integers: 26@@ -284,11 +281,8 @@    Typed arrays are prefixed by an int length,    followed by 0 or more values of that type."   [writer]-  (let [array-writer (array> writer)]-    (fn [buf xs]-      (-> buf-          (int> (count xs))-          (array-writer xs)))))+  (then> (apply> count int>)+         (array> writer)))  (defn nbt-writer   "Returns a writer for the given NBT type.