# HG changeset patch # User Robert McIntyre # Date 1331263013 21600 # Node ID 9ba461a5c60ff7c0dccf9ad73c8deec3a423bfcb # Parent d7c38ce834214484eb317b9ed379c6a2d87d7fc0 disk backup complete diff -r d7c38ce83421 -r 9ba461a5c60f clojure/com/aurellem/gb_driver.clj --- a/clojure/com/aurellem/gb_driver.clj Thu Mar 08 19:48:54 2012 -0600 +++ b/clojure/com/aurellem/gb_driver.clj Thu Mar 08 21:16:53 2012 -0600 @@ -1,11 +1,12 @@ (ns com.aurellem.gb-driver (:import com.aurellem.gb.Gb) (:import java.io.File) + (:import org.apache.commons.io.FileUtils) (:import (java.nio IntBuffer ByteOrder))) (Gb/loadVBA) -(def ^:dynamic *max-history* 1e4) +(def ^:dynamic *max-history* 10) (def ^:dynamic *backup-saves-to-disk* true) @@ -85,9 +86,7 @@ (defn frame [] @current-frame) (defn save-state [] - (SaveState. - (frame) - (Gb/saveState))) + (SaveState. (frame) (Gb/saveState))) (defn load-state [#^SaveState save] (reset! current-frame (:frame save)) @@ -105,8 +104,7 @@ (let [save (frame->disk-save frame)] (if (.exists save) (let [buf (Gb/saveBuffer) - bytes (org.apache.commons.io.FileUtils/readFileToByteArray - save)] + bytes (FileUtils/readFileToByteArray save)] (.put buf bytes) (.flip buf) (SaveState. frame buf))))) @@ -116,9 +114,8 @@ bytes (byte-array (.limit buf)) dest (frame->disk-save (:frame save))] (.get buf bytes) - (org.apache.commons.io.FileUtils/writeByteArrayToFile - dest bytes) - (.rewind buf))) + (FileUtils/writeByteArrayToFile dest bytes) + (.rewind buf) dest)) (defn find-save-state [frame] (let [save (@history frame)] @@ -140,9 +137,11 @@ ([n] (goto (- @current-frame n)))) (defn backup-state [frame] - (swap! history #(assoc % frame (save-state))) - (if (> (count @history) *max-history*) - (swap! history #(dissoc % (first (first %)))))) + (let [save (save-state)] + (swap! history #(assoc % frame save)) + (store-save-to-disk save) + (if (> (count @history) *max-history*) + (swap! history #(dissoc % (first (first %))))))) (defn advance [] (if *save-history* @@ -157,16 +156,8 @@ (Gb/step mask-or-buttons) (Gb/step (button-mask mask-or-buttons))))) -(defn step! [& args] - (binding [*save-history* false] - (apply step args))) - (defn play - ([n] (dorun (dotimes [_ n] (step)))) - ([] (play Integer/MAX_VALUE))) + ([] (play Integer/MAX_VALUE)) + ([n] (dorun (dotimes [_ n] (step))))) -(defn buf-seq [buffer] - (let [bytes (byte-array (.capacity buffer))] - (.get buffer bytes) - (.rewind buffer) - (seq bytes))) \ No newline at end of file +