~technomancy/tremendous-quest-iv

tremendous-quest-iv/fs.fnl -rw-r--r-- 1.2 KiB
a3c24536Phil Hagelberg Bump to Fennel 0.9.1; fix some bugs. 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
;; also taken from Bussard for simulating the in-game computing environment

(local lume (require :polywell.lib.lume))
(local frontend (require :polywell.frontend))

(local root "sandbox/")

(fn read [path] (love.filesystem.read (.. root path)))

(fn write [path contents]
  (let [segments (lume.split path "/")
        _ (table.remove segments)
        dir (table.concat segments "/")]
    (love.filesystem.createDirectory (.. root dir))
    ;; use this env var in development so that your source gets written to
    ;; the repository, not just into ~/.local/share/love/TQIV
    (if (os.getenv "WRITESRC")
        (let [f (io.open (.. root path) :w)]
          (f:write contents)
          (f:close))
        (assert (love.filesystem.write (.. root path) contents)))))

(fn ls [path] (love.filesystem.getDirectoryItems (.. root path)))

(fn type [path] (. (or (love.filesystem.getInfo (.. root path)) {}) :type))

(local sandboxed {:read read :write write :ls ls :type type})

(fn wrap [name]
  (fn [path ...]
    (if (: path :find "^/")
        ((. frontend name) (: path :sub 2) ...)
        ((. sandboxed name) path ...))))

{:read (wrap :read) :write (wrap :write) :ls (wrap :ls) :type (wrap :type)}