source: scenarios/GerminationX/oak/src/oak/io.clj @ 844

Revision 844, 3.3 KB checked in by dave, 10 years ago (diff)

added game world persistence

Line 
1;; Copyright (C) 2010 FoAM vzw
2;; This program is free software: you can redistribute it and/or modify
3;; it under the terms of the GNU Affero General Public License as
4;; published by the Free Software Foundation, either version 3 of the
5;; License, or (at your option) any later version.
6;;
7;; This program is distributed in the hope that it will be useful,
8;; but WITHOUT ANY WARRANTY; without even the implied warranty of
9;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10;; GNU Affero General Public License for more details.
11;;
12;; You should have received a copy of the GNU Affero General Public License
13;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
15(ns oak.io
16  (:use
17   clojure.xml
18   clojure.contrib.io)
19  (:import
20   java.io.File
21   java.io.FileInputStream
22   java.io.FileOutputStream
23   java.io.PushbackReader
24   java.io.FileReader
25   java.net.Socket
26   java.nio.ByteBuffer
27   java.nio.CharBuffer
28   java.nio.channels.SocketChannel
29   java.nio.charset.Charset
30   java.io.IOException
31   java.io.ByteArrayInputStream))
32
33;(defn msg-waiting? [socket]
34;  (> (. (. socket getInputStream) available) 0))
35
36; (defn read-msg [socket]
37;  (defn read-bytes [rdr count]
38;   (let [result (. rdr read)]
39;     (if (= count 1)
40;       '()
41;       (cons (char result) (read-bytes rdr (- count 1))))))
42; (apply str (read-bytes
43;             (. socket getInputStream)
44;             (. (. socket getInputStream) available))))
45
46;(defn send-msg [socket msg]
47; (println msg)
48; (try
49;   (let [aux (str msg "\n")
50;         out (. socket getOutputStream)]
51;     (. out write (. aux getBytes "UTF-8"))
52;     (. out flush)
53;     true)
54;   (catch IOException e
55;     (. e printStackTrace)
56;     false)))
57
58(comment
59(defn serialise
60  "Print a data structure to a file so that we may read it in later."
61  [data-structure #^String filename]
62  (with-out-writer
63    (java.io.File. filename)
64    (binding [*print-dup* true] (prn data-structure))))
65
66;; This allows us to then read in the structure at a later time, like so:
67(defn deserialise [filename]
68  (with-open [r (PushbackReader. (FileReader. filename))]
69    (read r))))
70
71(defn serialise [o filename]
72  (with-open [outp (-> (java.io.File. filename) java.io.FileOutputStream. java.io.ObjectOutputStream.)]
73    (.writeObject outp o)))
74
75(defn deserialise [filename]
76  (with-open [inp (-> (java.io.File. filename) java.io.FileInputStream. java.io.ObjectInputStream.)]
77    (.readObject inp)))
78
79(def buf (ByteBuffer/allocateDirect 4096))
80
81(defn read-msg [sc]
82  (.clear buf)
83  (let [r (.read sc buf)]
84    (if (> r 0)
85      (do
86        (.flip buf)
87        (let [bytearr (byte-array (.remaining buf))]
88          (.get buf bytearr)
89          (new String bytearr)))
90      false)))
91 
92(defn send-msg [sc msg]
93  (println "sending:" msg)
94  (let [msg (str msg "\n")
95        enc (.newEncoder (Charset/forName "US-ASCII"))] 
96    (.write sc (.encode enc (CharBuffer/wrap msg)))))
97
98(defn load-object [fname]
99  (reduce
100   (fn [r t]
101     (let [toks (.split t " ")]
102       (assoc r (first toks) (second toks))))
103   {"name" fname}
104   (.split (slurp (str "data/objects/" fname ".txt")) "\r\n")))
105
106; for each object, look for a file and parse it into a hash-map
107(defn load-objects [objects]
108  (map
109   (fn [obj]
110     (load-object obj))
111   objects))
112
113(defn parse-xml [str]
114  (parse (ByteArrayInputStream. (.getBytes str "UTF-8"))))
Note: See TracBrowser for help on using the repository browser.