d3a817b
Branches > main > d3a817b > files / src / dev / grahp
Rewrite packet id builders
Commit: d3a817b8f70e9fc759d5cdbd11470d8492059443
Parent: f490e57
3 changed files:
src/dev/grahp/build.clj view file
@@ -0,0 +1,9 @@+(ns dev.grahp.build)++(defmacro build+ "Builds up a map of pairs using 'for'."+ {:clj-kondo/lint-as 'clojure.core/for}+ [bindings & body]+ `(into {}+ (for ~bindings+ ~@body)))src/net/bowmap/packet/client.clj view file
@@ -2,6 +2,7 @@ "Clientbound packets" (:require [clojure.alpha.spec :as s]+ [dev.grahp.build :refer [build]] [dev.grahp.error :refer [assert!]] [net.bowmap.client.state :as state] [net.bowmap.packet :as pk]@@ -9,14 +10,12 @@ (:import [io.netty.buffer ByteBuf Unpooled])) -;; TODO this also looks identical to the serverbound packet ids...-;; It is equally unreadable lmao (defn build-packet-ids [registry]- (into {}- (mapcat (fn [[state packet-ids]]- (into {} (map-indexed (fn [i pk] [{::state/state state ::pk/id pk} i]))- packet-ids)))- registry))+ (build [[state packet-ids] registry+ [i id] (map-indexed vector packet-ids)]+ [{::state/state state+ ::pk/id id}+ i])) (defn build-packets [registry] (reduce (fn [acc [packet-id table]]src/net/bowmap/packet/server.clj view file
@@ -1,10 +1,11 @@ (ns net.bowmap.packet.server (:require [clojure.alpha.spec :as s]+ [dev.grahp.build :refer [build]] [dev.grahp.error :refer [raise]] [net.bowmap.client.state :as state]- [net.bowmap.packet.read :as read]- [net.bowmap.packet :as pk])+ [net.bowmap.packet :as pk]+ [net.bowmap.packet.read :as read]) (:import [io.netty.buffer ByteBuf])) @@ -17,30 +18,27 @@ ::pk/id packet-id))))) {} registry)) --;; TODO this is unreadable lol (defn build-packet-ids [registry]- (into {}- (mapcat (fn [[state packet-ids]]- (into {}- (map-indexed (fn [i pk] [{::state/state state ::pk/numeric-id i} pk]))- packet-ids)))- registry))+ (build [[state packet-ids] registry+ [i id] (map-indexed vector packet-ids)]+ [{::state/state state+ ::pk/numeric-id i}+ id])) (def ^{:spec (s/map-of (s/schema [::state/state ::pk/numeric-id]) ::pk/id)} packet-ids (build-packet-ids- {::state/handshaking- [::handshaking]- ::state/status- [::status-request- ::ping-request]- ::state/login- [::login-start- ::encryption-response- ::login-plugin-response- ::login-acknowledged- ::login-cookie-response]}))+ {::state/handshaking+ [::handshaking]+ ::state/status+ [::status-request+ ::ping-request]+ ::state/login+ [::login-start+ ::encryption-response+ ::login-plugin-response+ ::login-acknowledged+ ::login-cookie-response]})) (def ^{:spec (s/map-of ::pk/id (s/fspec :args (s/cat :buf #(instance? ByteBuf %)) :ret ::pk/packet))}@@ -64,8 +62,8 @@ 3. The contents of 'buf' are not a valid packet" [buf state] {:pre [(read/can-read-packet? buf)]}- ;; The length is currently unsed, though there are packets that require it in order to decode.- ;; None of those packets have been implemented yet, though.+ ;; The length field is currently unused+ ;; login 0x04 (::net.bowmap.packet.client/login-plugin-request) requires the length (let [_unused-length (read/read-varint! buf) numeric-id (read/read-varint! buf) packet-id (packet-ids {::state/state state ::pk/numeric-id numeric-id})