diff --git a/bin/jextract-libs.sh b/bin/jextract-libs.sh index 3d18b769..99d2bbbb 100755 --- a/bin/jextract-libs.sh +++ b/bin/jextract-libs.sh @@ -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 \ diff --git a/netcode.clj b/netcode.clj new file mode 100644 index 00000000..ce40b120 --- /dev/null +++ b/netcode.clj @@ -0,0 +1,3 @@ +(ns vybe.netcode + (:require + [vybe.netcode.c :as vn.c])) diff --git a/src/vybe/native/loader.clj b/src/vybe/native/loader.clj index bcc0f242..b81861c2 100644 --- a/src/vybe/native/loader.clj +++ b/src/vybe/native/loader.clj @@ -19,6 +19,9 @@ ;; -- Flecs. (vp/-copy-lib! "vybe_flecs") +;; -- Netcode. +(vp/-copy-lib! "netcode") + (defn -main [& _args] #_bogus) diff --git a/src/vybe/netcode/c.clj b/src/vybe/netcode/c.clj new file mode 100644 index 00000000..ca06ddfd --- /dev/null +++ b/src/vybe/netcode/c.clj @@ -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 + + ()) diff --git a/src/vybe/netcode/impl.clj b/src/vybe/netcode/impl.clj index 10e8a5f5..65f9467e 100644 --- a/src/vybe/netcode/impl.clj +++ b/src/vybe/netcode/impl.clj @@ -11,8 +11,6 @@ (set! *warn-on-reflection* true) -(netcode/netcode_init$descriptor) - (vp/-copy-lib! "netcode") (def ^:private declared-methods @@ -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"))) @@ -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] diff --git a/test/vybe/game_test.clj b/test/vybe/game_test.clj index a9e17446..a4181688 100644 --- a/test/vybe/game_test.clj +++ b/test/vybe/game_test.clj @@ -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.