gnuvince / clj-starcraft

No description has been added.

Clone this repository (size: 138.8 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/gnuvince/clj-starcraft/
commit 50: 9117e4a5e134
parent 49: 097123435bc4
branch: default
Replaced make-reader macro with a function that returns a closure.
Vincent Foley / gnuvince
11 months ago

Changed (Δ90 bytes):

raw changeset »

src/starcraft/replay/parse.clj (18 lines added, 17 lines removed)

src/starcraft/replay/unpack.clj (1 lines added, 1 lines removed)

Up to file-list src/starcraft/replay/parse.clj:

3
3
           [java.nio ByteBuffer]
4
4
           [java.util Date]))
5
5
6
(defn mask8  [x] (bit-and x 0xff))
7
(defn mask16 [x] (bit-and x 0xffff))
6
8
7
9
(defn read-null-string [#^ByteBuffer buf len]
8
10
  (let [#^bytes arr (make-array Byte/TYPE len)]
9
11
    (.get buf arr)
10
12
    (apply str (map char (take-while (complement zero?) arr)))))
11
13
12
(defmacro make-reader
13
  [get-fn mask-fn]
14
  `(fn [#^ByteBuffer buf# len#]
15
     (if (= len# 1)
16
       (~mask-fn (. buf# (~get-fn)))
17
       (let [#^ints arr# (int-array len#)]
18
         (dotimes [i# len#]
19
           (aset-int arr# i# (~mask-fn (. buf# (~get-fn)))))
20
         arr#))))
14
(defn make-reader
15
  [type mask-fn]
16
  (let [getter ({Byte (fn [#^ByteBuffer buf] (.get buf))
17
                 Short (fn [#^ByteBuffer buf] (.getShort buf))
18
                 Integer (fn [#^ByteBuffer buf] (.getInt buf))} type)]
19
    (fn [#^ByteBuffer buf len]
20
      (if (= len 1)
21
        (mask-fn (getter buf))
22
        (let [#^ints arr (int-array len)]
23
          (dotimes [i len]
24
            (let [x (int (mask-fn (getter buf)))]
25
              (aset-int arr i x)))
26
          arr)))))
21
27
22
28
(def read-field
23
     (let [mask8 (fn [x] (bit-and x 0xff))
24
           mask16 (fn [x] (bit-and x 0xffff))
25
           read-bytes (make-reader get mask8)
26
           read-shorts (make-reader getShort mask16)
27
           read-ints (make-reader getInt identity)
28
           readers {Byte read-bytes
29
                    Short read-shorts
30
                    Integer read-ints
29
     (let [readers {Byte (make-reader Byte mask8)
30
                    Short (make-reader Short mask16)
31
                    Integer (make-reader Integer identity)
31
32
                    String read-null-string}]
32
33
       (fn [#^ByteBuffer buf size type]
33
34
         (let [real-size (if (vector? size)

Up to file-list src/starcraft/replay/unpack.clj:

104
104
              action (apply parse-buffer buf fields)]
105
105
          (if action
106
106
            (recur (update-in cmds
107
                              [(int player-id)]
107
                              [player-id]
108
108
                              conj
109
109
                              (merge {:tick tick
110
110
                                      :name name}