about summary refs log tree commit diff stats
path: root/app.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-08-30 19:04:06 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-08-30 19:04:06 -0700
commitf82c0de5020437bf45afa8d4e441437801466af6 (patch)
tree2aa807519b085bb82dc48269ed683636a5f2b45c /app.lua
parent7e97a2a1e7564c410236c012d7a6c4b0f141484d (diff)
downloadlines.love-f82c0de5020437bf45afa8d4e441437801466af6.tar.gz
cleaner API for file-system access
Thanks to physfs and nativefs.lua

nativefs still introduces some inconsistencies with love.filesystem with
relative paths:

  * love.fs.read: reads from save dir if it exists, falls back to source dir if not
  * nativefs.read: reads from save dir if it exists, falls back to source dir if not ✓

  * love.fs.write: always writes to save dir
  * nativefs.write: always writes to source dir (since no restrictions)

  * love.fs.newFile followed by file:open('r'): reads from save dir if it exists, source dir if not
  * nativefs.newFile followed by file:open('r'): always reads from working dir

  * love.fs.newFile followed by file:open('w'): always writes to save dir
  * nativefs.newFile followed by file:open('w'): always writes to working dir

So avoid using relative paths with App primitives.
Diffstat (limited to 'app.lua')
-rw-r--r--app.lua78
1 files changed, 34 insertions, 44 deletions
diff --git a/app.lua b/app.lua
index 7099596..477a9b3 100644
--- a/app.lua
+++ b/app.lua
@@ -97,6 +97,9 @@ function App.initialize_for_test()
   App.screen.init{width=100, height=50}
   App.screen.contents = {}  -- clear screen
   App.filesystem = {}
+  App.source_dir = ''
+  App.current_dir = ''
+  App.save_dir = ''
   App.fake_keys_pressed = {}
   App.fake_mouse_state = {x=-1, y=-1}
   App.initialize_globals()
@@ -257,26 +260,13 @@ end
 
 function App.open_for_writing(filename)
   App.filesystem[filename] = ''
-  if Current_app == nil or Current_app == 'run' then
-    return {
-      write = function(self, ...)
-                local args = {...}
-                for i,s in ipairs(args) do
-                  App.filesystem[filename] = App.filesystem[filename]..s
-                end
-              end,
-      close = function(self)
-              end,
-    }
-  elseif Current_app == 'source' then
-    return {
-      write = function(self, s)
-                App.filesystem[filename] = App.filesystem[filename]..s
-              end,
-      close = function(self)
-              end,
-    }
-  end
+  return {
+    write = function(self, s)
+              App.filesystem[filename] = App.filesystem[filename]..s
+            end,
+    close = function(self)
+            end,
+  }
 end
 
 function App.open_for_reading(filename)
@@ -356,6 +346,8 @@ function prepend_debug_info_to_test_failure(test_name, err)
   table.insert(Test_errors, full_error)
 end
 
+nativefs = require 'nativefs'
+
 -- call this once all tests are run
 -- can't run any tests after this
 function App.disable_tests()
@@ -391,32 +383,30 @@ function App.disable_tests()
   App.screen.move = love.window.setPosition
   App.screen.position = love.window.getPosition
   App.screen.print = love.graphics.print
-  if Current_app == nil or Current_app == 'run' then
-    App.open_for_reading = function(filename) return io.open(filename, 'r') end
-    App.open_for_writing = function(filename) return io.open(filename, 'w') end
-  elseif Current_app == 'source' then
-    -- HACK: source editor requires a couple of different foundational definitions
-    App.open_for_reading =
-        function(filename)
-          local result = love.filesystem.newFile(filename)
-          local ok, err = result:open('r')
-          if ok then
-            return result
-          else
-            return ok, err
-          end
+  App.open_for_reading =
+      function(filename)
+        local result = nativefs.newFile(filename)
+        local ok, err = result:open('r')
+        if ok then
+          return result
+        else
+          return ok, err
         end
-    App.open_for_writing =
-        function(filename)
-          local result = love.filesystem.newFile(filename)
-          local ok, err = result:open('w')
-          if ok then
-            return result
-          else
-            return ok, err
-          end
+      end
+  App.open_for_writing =
+      function(filename)
+        local result = nativefs.newFile(filename)
+        local ok, err = result:open('w')
+        if ok then
+          return result
+        else
+          return ok, err
         end
-  end
+      end
+  App.files = nativefs.getDirectoryItems
+  App.source_dir = love.filesystem.getSource()..'/'
+  App.current_dir = nativefs.getWorkingDirectory()..'/'
+  App.save_dir = love.filesystem.getSaveDirectory()..'/'
   App.get_time = love.timer.getTime
   App.get_clipboard = love.system.getClipboardText
   App.set_clipboard = love.system.setClipboardText