Skip to content

Commit

Permalink
Add netcode
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeodrippe committed Jul 30, 2024
1 parent 8260607 commit 76d3450
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 59 deletions.
6 changes: 6 additions & 0 deletions bin/jextract-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ if [ ! -d "libsodium-1.0.20" ]; then
cp "/usr/local/lib/${VYBE_SODIUM_LIB}" "native/${VYBE_LIB_PREFIX}netcode.$VYBE_EXTENSION"
fi

$VYBE_GCC \
-shared \
netcode/netcode.c \
-I netcode \
-o "native/${VYBE_LIB_PREFIX}netcode.$VYBE_EXTENSION"

if [[ $VYBE_EXTENSION == "dll" ]]; then
$VYBE_JEXTRACT \
--use-system-load-library \
Expand Down
3 changes: 3 additions & 0 deletions netcode.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns vybe.netcode
(:require
[vybe.netcode.c :as vn.c]))
3 changes: 3 additions & 0 deletions src/vybe/native/loader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
;; -- Flecs.
(vp/-copy-lib! "vybe_flecs")

;; -- Netcode.
(vp/-copy-lib! "netcode")

(defn -main
[& _args]
#_bogus)
11 changes: 11 additions & 0 deletions src/vybe/netcode/c.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns vybe.netcode.c
(:require
[vybe.netcode.impl :as vn.impl]))

(set! *warn-on-reflection* true)

(vn.impl/intern-methods)

(comment

())
101 changes: 43 additions & 58 deletions src/vybe/netcode/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

(set! *warn-on-reflection* true)

(netcode/netcode_init$descriptor)

(vp/-copy-lib! "netcode")

(def ^:private declared-methods
Expand All @@ -24,8 +22,6 @@
(:declaredMethods (vp/-try-bean "org.vybe.netcode.netcode_5"))
(:declaredMethods (vp/-try-bean "org.vybe.netcode.netcode_6"))))

(netcode_4$netcode_init)

(->> declared-methods
(filter #(str/starts-with? (str/lower-case (.getName ^Method %)) "netcode")))

Expand Down Expand Up @@ -80,68 +76,57 @@
(or (layout? t)
(= t :pointer)))

(def ^:private private-api)

(defn -methods
[]
(->> declared-methods
(filter #(str/includes? (.getName ^Method %) "$descriptor"))
(filter #(let [method-name (.getName ^Method %)]
(def method-name method-name)
(and (or (str/starts-with? (str/lower-case method-name) "netcode")
(str/starts-with? method-name "vybe_"))
(not (str/includes? method-name "internal"))
(not (contains? #{"netcode_encryption_manager_add_encryption_mapping"
"netcode_encryption_manager_find_encryption_mapping"
"netcode_server_process_connection_request_packet"}
(first (str/split method-name #"\$descriptor")))))))
(or (str/starts-with? (str/lower-case method-name) "netcode")
(str/starts-with? method-name "vybe_"))))
#_(filter #(= (.getName %) "GetMonitorName$descriptor"))
#_(take 10)
(pmap (fn [^Method method]
(try
(let [^FunctionDescriptor desc (.invoke method nil (into-array Object []))
args (.argumentLayouts desc)

ret' (.returnLayout desc)
ret-layout (when (and (.isPresent ret')
(instance? StructLayout (.get ret')))
(symbol (str "org.vybe.netcode." (.get (.name ^StructLayout (.get ret'))))
"layout"))
ret (when (.isPresent ret')
(->type (.get ret')))

desc-name ((comp :name bean) method)
main-name (str/replace desc-name #"\$descriptor" "")
^Method main-method (->> declared-methods
(filter (comp #(= main-name (.getName ^Method %))))
first)]
(when-not main-method
(throw (ex-info "Method for desc does not exist"
{:desc desc
:desc-name desc-name})))
(let [args (mapv (fn [v ^Parameter param]
{:name (.getName param)
:clj-type (if (= v :panama/allocator)
v
(->type v))})
args
;; If return is a layout, the method
;; receives an allocator (e.g. Arena) as
;; the first arg.
(if (layout? ret)
(rest (.getParameters main-method))
(.getParameters main-method)))]
(vector main-name
{:args args
:ret ret
:ret-layout ret-layout
:has-arena? (or (layout? ret)
(some (comp address? :clj-type)
args))
:main-thread? (nil? ret)})))
(catch Exception _
((comp :name bean) method)))))))
(def methods-to-intern (vec (-methods)))
(let [^FunctionDescriptor desc (.invoke method nil (into-array Object []))
args (.argumentLayouts desc)

ret' (.returnLayout desc)
ret-layout (when (and (.isPresent ret')
(instance? StructLayout (.get ret')))
(symbol (str "org.vybe.netcode." (.get (.name ^StructLayout (.get ret'))))
"layout"))
ret (when (.isPresent ret')
(->type (.get ret')))

desc-name ((comp :name bean) method)
main-name (str/replace desc-name #"\$descriptor" "")
^Method main-method (->> declared-methods
(filter (comp #(= main-name (.getName ^Method %))))
first)]
(when-not main-method
(throw (ex-info "Method for desc does not exist"
{:desc desc
:desc-name desc-name})))
(let [args (mapv (fn [v ^Parameter param]
{:name (.getName param)
:clj-type (if (= v :panama/allocator)
v
(->type v))})
args
;; If return is a layout, the method
;; receives an allocator (e.g. Arena) as
;; the first arg.
(if (layout? ret)
(rest (.getParameters main-method))
(.getParameters main-method)))]
(vector main-name
{:args args
:ret ret
:ret-layout ret-layout
:has-arena? (or (layout? ret)
(some (comp address? :clj-type)
args))
:main-thread? (nil? ret)})))))))
#_(def methods-to-intern (vec (-methods)))

(defn -debug
[v]
Expand Down
3 changes: 2 additions & 1 deletion test/vybe/game_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns vybe.game-test
(:require
[clojure.test :refer [deftest testing is]]
[vybe.panama :as vp]))
[vybe.panama :as vp]
vybe.netcode))

(when-not vp/linux?
;; In the CI for Linux, we have some linker issue.
Expand Down

0 comments on commit 76d3450

Please sign in to comment.