diff clojure/com/aurellem/gb_driver.clj @ 77:9ba461a5c60f

disk backup complete
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 21:16:53 -0600
parents d7c38ce83421
children 7ab48d728ee4
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb_driver.clj	Thu Mar 08 19:48:54 2012 -0600
     1.2 +++ b/clojure/com/aurellem/gb_driver.clj	Thu Mar 08 21:16:53 2012 -0600
     1.3 @@ -1,11 +1,12 @@
     1.4  (ns com.aurellem.gb-driver
     1.5    (:import com.aurellem.gb.Gb)
     1.6    (:import java.io.File)
     1.7 +  (:import org.apache.commons.io.FileUtils)
     1.8    (:import (java.nio IntBuffer ByteOrder)))
     1.9  
    1.10  (Gb/loadVBA)
    1.11  
    1.12 -(def ^:dynamic *max-history* 1e4)
    1.13 +(def ^:dynamic *max-history* 10)
    1.14  
    1.15  (def ^:dynamic *backup-saves-to-disk* true)
    1.16  
    1.17 @@ -85,9 +86,7 @@
    1.18  (defn frame [] @current-frame)
    1.19  
    1.20  (defn save-state []
    1.21 -  (SaveState.
    1.22 -   (frame)
    1.23 -   (Gb/saveState)))
    1.24 +  (SaveState. (frame) (Gb/saveState)))
    1.25  
    1.26  (defn load-state [#^SaveState save]
    1.27    (reset! current-frame (:frame save))
    1.28 @@ -105,8 +104,7 @@
    1.29    (let [save (frame->disk-save frame)]
    1.30      (if (.exists save)
    1.31        (let [buf (Gb/saveBuffer)
    1.32 -            bytes (org.apache.commons.io.FileUtils/readFileToByteArray
    1.33 -                   save)]
    1.34 +            bytes (FileUtils/readFileToByteArray save)]
    1.35          (.put buf bytes)
    1.36          (.flip buf)
    1.37          (SaveState. frame buf)))))
    1.38 @@ -116,9 +114,8 @@
    1.39          bytes (byte-array (.limit buf))
    1.40          dest (frame->disk-save (:frame save))]
    1.41      (.get buf bytes)
    1.42 -    (org.apache.commons.io.FileUtils/writeByteArrayToFile
    1.43 -     dest bytes)
    1.44 -    (.rewind buf)))
    1.45 +    (FileUtils/writeByteArrayToFile dest bytes)
    1.46 +    (.rewind buf) dest))
    1.47  
    1.48  (defn find-save-state [frame]
    1.49    (let [save (@history frame)]
    1.50 @@ -140,9 +137,11 @@
    1.51    ([n] (goto (- @current-frame n))))
    1.52  
    1.53  (defn backup-state [frame]
    1.54 -  (swap! history #(assoc % frame (save-state)))
    1.55 -  (if (> (count @history) *max-history*)
    1.56 -    (swap! history #(dissoc % (first (first %))))))
    1.57 +  (let [save (save-state)]
    1.58 +    (swap! history #(assoc % frame save))
    1.59 +    (store-save-to-disk save)
    1.60 +    (if (> (count @history) *max-history*)
    1.61 +      (swap! history #(dissoc % (first (first %)))))))
    1.62  
    1.63  (defn advance []
    1.64    (if *save-history*
    1.65 @@ -157,16 +156,8 @@
    1.66         (Gb/step mask-or-buttons)
    1.67         (Gb/step (button-mask mask-or-buttons)))))
    1.68  
    1.69 -(defn step! [& args]
    1.70 -  (binding [*save-history* false]
    1.71 -    (apply step args)))
    1.72 -  
    1.73  (defn play
    1.74 -  ([n] (dorun (dotimes [_ n] (step)))) 
    1.75 -  ([] (play Integer/MAX_VALUE)))
    1.76 +  ([] (play Integer/MAX_VALUE))
    1.77 +  ([n] (dorun (dotimes [_ n] (step)))))
    1.78  
    1.79 -(defn buf-seq [buffer]
    1.80 -  (let [bytes (byte-array (.capacity buffer))]
    1.81 -    (.get buffer bytes)
    1.82 -    (.rewind buffer)
    1.83 -    (seq bytes)))
    1.84 \ No newline at end of file
    1.85 +