Skip to content

Commit

Permalink
AOT compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Weathercold committed Jan 24, 2023
1 parent d493bfb commit 89718b2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/** linguist-generated
.flake.lock linguist-generated linguist-language=JSON
1 change: 0 additions & 1 deletion .idea/extra-logging.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Android_build.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/runConfigurations/REPL.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"run-dex" ["do" "uberjar," "dex," "copy," "launch"]}

;; Uncomment for AOT (default)
;;:aot :all
;;:omit-source true)
:aot :all
:omit-source true)

;; Uncomment for JIT
:jar-exclusions [#"\.java"])
;;:jar-exclusions [#"\.java"])
8 changes: 5 additions & 3 deletions src/clojure/logging/core/events.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(try (Class/forName
(if (str/includes? s ".") s (str "mindustry.game.EventType$" s)))
(catch Exception e
;; FIXME: ugh side-effect inside transaction
;; FIXME: ugh side effect inside transaction
(err (str "Cannot get event class with name " s) e))))

(defn- trace [o]
Expand All @@ -38,5 +38,7 @@
(map str->event)
(remove nil?)))

(when @enable (run! (fn [c] (Events/on c (cons1 #(log @log-level (trace %)))))
@listening-events))
(defn -main []
(when @enable
(run! (fn [c] (Events/on c (cons1 #(log @log-level (trace %)))))
@listening-events)))
66 changes: 34 additions & 32 deletions src/clojure/logging/core/log_handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,43 @@
#(try (DateTimeFormatter/ofPattern %)
(catch Exception e
(if @client-loaded
;; FIXME: ugh side-effect inside transaction
;; FIXME: ugh side effect inside transaction
(err "Time format invalid" e)
(Events/on EventType$ClientLoadEvent
(cons1 (fn [_] (err "Time format invalid" e)))))
DateTimeFormatter/ISO_LOCAL_TIME)))

(set! Log/logger
(reify Log$LogHandler
(log [_ lvl s]
(let [timestamp (.format ^DateTimeFormatter @time-formatter (LocalTime/now))
term-msg (-> @terminal-format
(str/replace "$t" timestamp)
(str/replace "$L" (level->code lvl))
(str/replace "$l" (level->icon lvl))
(str/replace "$m" s)
(#(if @colored-terminal
(-> % color/str-code->escseq color/str-tag->escseq)
(color/remove-colors %))))
cons-msg (-> @console-format
(str/replace "$t" timestamp)
(str/replace "$L" (level->tag lvl))
(str/replace "$l" (level->icon lvl))
(str/replace "$m" s)
color/str-escseq->tag
color/str-code->tag)]
(println term-msg)
(when-not Vars/headless
(if @client-loaded
(.. Vars/ui -consolefrag (addMessage cons-msg))
(dosync (alter log-buffer conj cons-msg))))))))
(defn -main []
(set! Log/logger
(reify Log$LogHandler
(log [_ lvl s]
(let [timestamp (.format ^DateTimeFormatter @time-formatter (LocalTime/now))
term-msg (-> @terminal-format
(str/replace "$t" timestamp)
(str/replace "$L" (level->code lvl))
(str/replace "$l" (level->icon lvl))
(str/replace "$m" s)
(#(if @colored-terminal
(-> % color/str-code->escseq color/str-tag->escseq)
(color/remove-colors %))))
cons-msg (-> @console-format
(str/replace "$t" timestamp)
(str/replace "$L" (level->tag lvl))
(str/replace "$l" (level->icon lvl))
(str/replace "$m" s)
color/str-escseq->tag
color/str-code->tag)]
(println term-msg)
(when-not Vars/headless
(if @client-loaded
(.. Vars/ui -consolefrag (addMessage cons-msg))
(dosync (alter log-buffer conj cons-msg))))))))

(Events/on EventType$ClientLoadEvent
(cons1 (fn [_] (run! #(.. Vars/ui -consolefrag (addMessage %))
(dosync
(let [logs @log-buffer]
(ref-set client-loaded true)
(ref-set log-buffer [])
logs))))))
(Events/on EventType$ClientLoadEvent
(cons1 (fn [_]
(run! #(.. Vars/ui -consolefrag (addMessage %))
(dosync
(let [logs @log-buffer]
(ref-set client-loaded true)
(ref-set log-buffer [])
logs)))))))
9 changes: 4 additions & 5 deletions src/clojure/logging/core/setting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
(defrecord Setting [setting default transform value]
IDeref (deref [_] @value))

(defn set-setting [^Setting {:keys [setting default transform value]}]
(dosync (ref-set value (transform (.get Core/settings setting default)))))

(defmacro defsetting
([n setting default] `(defsetting ~n ~setting ~default identity))
([n setting default transform]
`(def ~n (doto (->Setting ~setting ~default ~transform (ref nil))
set-setting))))
`(def ~n (->Setting ~setting ~default ~transform (ref nil)))))

(defn set-setting [^Setting {:keys [setting default transform value]}]
(dosync (ref-set value (transform (.get Core/settings setting default)))))
33 changes: 16 additions & 17 deletions src/clojure/logging/core/settings.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
[translating :as transl])
(logging.util [lambdas :refer [cons1 s-proc]]
[log :as log]))
(:import (arc Core Events)
(:import (arc Core)
(arc.util Log Log$LogLevel)
(logging.core.setting Setting)
(mindustry Vars)
(mindustry.game EventType$ClientLoadEvent)
(mindustry.gen Icon)
(mindustry.ui.dialogs SettingsMenuDialog$SettingsTable
SettingsMenuDialog$SettingsTable$Setting)))
Expand Down Expand Up @@ -57,23 +56,23 @@

(defn refresh! []
(dosync
(set-setting log/enable-meta-debugging)
(set-setting log/meta-color)
(run! set-setting [log/enable-meta-debugging
log/meta-color

(set-setting log-h/log-level)
(set-setting log-h/colored-terminal)
(set-setting log-h/terminal-format)
(set-setting log-h/console-format)
(set-setting log-h/time-formatter)
log-h/log-level
log-h/colored-terminal
log-h/terminal-format
log-h/console-format
log-h/time-formatter

(set-setting events/enable)
(set-setting events/log-level)
events/enable
events/log-level

(set-setting transl/enable-translation))
transl/enable-translation]))
(set! Log/level @log-h/log-level))

(Events/on EventType$ClientLoadEvent
(cons1 (fn [_]
(doto (.-settings Vars/ui)
(.addCategory "@extra-logging.displayname" Icon/wrench (cons1 register))
(.hidden refresh!)))))
(defn -init []
(when-not Vars/headless
(doto (.-settings Vars/ui)
(.addCategory "@extra-logging.displayname" Icon/wrench (cons1 register))
(.hidden refresh!))))
33 changes: 14 additions & 19 deletions src/clojure/logging/core/translating.clj
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
(ns logging.core.translating
(:require [logging.core.setting :refer [defsetting]]
[logging.util.lambdas :refer [cons1]]
[logging.util.log :refer [info]])
(:import (arc Events)
(arc.util CommandHandler Reflect)
(:import (arc.util CommandHandler Reflect)
(java.util Locale)
(mindustry Vars)
(mindustry.game EventType$ClientLoadEvent)))
(mindustry Vars)))

(def foo-has-translation
"Whether foo client supports chat translation."
(try (Reflect/get (Class/forName "mindustry.client.ClientVars") "enableTranslation")
true
(catch Exception _ false)))
(def target-lang (.getLanguage (Locale/getDefault)))
(delay
(try (Reflect/get (Class/forName "mindustry.client.ClientVars") "enableTranslation")
true
(catch Exception _ false))))
(def target-lang (delay (.getLanguage (Locale/getDefault))))
(def supported-langs (agent nil)) ; TODO: initialize

(defsetting enable-translation
"extra-enabletranslation"
(not (or foo-has-translation Vars/headless))
(defsetting enable-translation "extra-enabletranslation" true
#(and
%
(not foo-has-translation)
(not @foo-has-translation)
(not Vars/headless)))
(def supported-langs (agent nil)) ; TODO: initialize

(defn register-command [^CommandHandler handler])
(defn -init []
(when @foo-has-translation (info "@extra-logging.footranslation"))
(when Vars/headless (info "@extra-logging.headlesstranslation")))

(Events/on EventType$ClientLoadEvent
(cons1 (fn [_]
(when foo-has-translation (info "@extra-logging.footranslation"))
(when Vars/headless (info "@extra-logging.headlesstranslation")))))
(defn -register-client-commands [^CommandHandler _])
25 changes: 17 additions & 8 deletions src/clojure/logging/main.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
(ns logging.main
(:require [logging.core.settings]
[logging.core.translating :as transl]
(:require (logging.core [events :as events]
[log-handler :as log-h]
[settings :as settings]
[translating :as transl])
[logging.util.log :refer [debug]])
(:import (arc.util CommandHandler)))

(defn main [] (debug "Creating mod"))
(defn -main []
(settings/refresh!)
(log-h/-main)
(debug "Creating mod")
(events/-main))

(defn init [] (debug "Initializing"))
(defn -init []
(debug "Initializing")
(settings/-init)
(transl/-init))

(defn load-content [] (debug "Loading content"))
(defn -load-content [] (debug "Loading content"))

(defn register-client-commands [^CommandHandler handler]
(defn -register-client-commands [^CommandHandler handler]
(debug "Registering commands")
(transl/register-command handler))
(transl/-register-client-commands handler))

(defn register-server-commands [^CommandHandler _] (debug "Registering commands"))
(defn -register-server-commands [^CommandHandler _] (debug "Registering commands"))
12 changes: 6 additions & 6 deletions src/java/logging/CljLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,42 @@ public class CljLoader extends Mod {

public CljLoader() {
try {
Clojure.var(ns, "main").invoke();
Clojure.var(ns, "-main").invoke();
} catch (Throwable ignored) {}
}

@Override
public void init() {
try {
Clojure.var(ns, "init").invoke();
Clojure.var(ns, "-init").invoke();
} catch (Throwable ignored) {}
}

@Override
public void loadContent() {
try {
Clojure.var(ns, "load-content").invoke();
Clojure.var(ns, "-load-content").invoke();
} catch (Throwable ignored) {}
}

@Override
public void registerClientCommands(CommandHandler handler) {
try {
Clojure.var(ns, "register-client-commands").invoke(handler);
Clojure.var(ns, "-register-client-commands").invoke(handler);
} catch (Throwable ignored) {}
}

@Override
public void registerServerCommands(CommandHandler handler) {
try {
Clojure.var(ns, "register-server-commands").invoke(handler);
Clojure.var(ns, "-register-server-commands").invoke(handler);
} catch (Throwable ignored) {}
}

@Override
public Fi getConfig() {
try {
return (Fi) Clojure.var(ns, "get-config").invoke();
return (Fi) Clojure.var(ns, "-get-config").invoke();
} catch (IllegalStateException e) {
return super.getConfig();
}
Expand Down

0 comments on commit 89718b2

Please sign in to comment.