Skip to content

Commit

Permalink
Update lua FileSystem to use URI based parameters and allow opening o…
Browse files Browse the repository at this point in the history
…f files and creation of directories

Rather than passing a Root field we use a URI, with either a "data" or "user" scheme to indicate where they are working

Add a MakeDirectory method
Add a Open method that returns a lua io file interface
Also add a redacted lua io interface.
Add a FileSystem.lua with meta stubs for lint/hinting.
  • Loading branch information
JonBooth78 committed Oct 12, 2023
1 parent c375375 commit 1cc5f3d
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 61 deletions.
6 changes: 0 additions & 6 deletions data/meta/Constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@
-- A <Constants.DetailLevel> string
---@class DetailLevel: string

-- A <Constants.FileSystemRoot> string
---@class FileSystemRoot: string

-- A <Constants.PiGuiFaceFlags> string
---@class PiGuiFaceFlags: string

Expand Down Expand Up @@ -147,9 +144,6 @@ Constants.BodySuperType = {}
---@type DetailLevel[]
Constants.DetailLevel = {}

---@type FileSystemRoot[]
Constants.FileSystemRoot = {}

---@type PiGuiFaceFlags[]
Constants.PiGuiFaceFlags = {}

Expand Down
49 changes: 49 additions & 0 deletions data/meta/FileSystem.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

-- This file implements type information about C++ classes for Lua static analysis
-- This is used in FileSyestem.lua whcih then extends that.

---@meta

---@class FileSystem
local FileSystem = {}

---@param path string The directory to read the contents of.
---@return string[] files A list of files as full paths from the root
---@return string[] dirs A list of dirs as full paths from the root
---
--- Example:
--- > local files, dirs = FileSystem.ReadDirectory("user://savefiles")
function FileSystem.ReadDirectory(path) end

--- Join the passed arguments into a path, correctly handling separators and .
--- and .. special dirs.
---
---@param arg string[] A list of path elements to be joined
---@return string The joined path elements
function FileSystem.JoinPath( ... ) end

---@param dir_name string The name of the folder to create
---@return boolean Success
function FileSystem.MakeDirectory( dir_name ) end

--- Wrapper for our patched io.open that ensures files are opened inside the sandbox.
--- Prefer using this to io.open
---
--- Files in the user folder can be read or written to
--- Files in the data folder are read only.
---
---
---
--- Example:
--- > f = FileSystem.Open( "user://my_file.txt", "w" )
--- > f:write( "file contents" )
--- > f:close()
---
---@param filename string The name of the file to open, must start either user:// or data://
---@param mode string|nil The mode to open the file in, defaults to read only. Only user location files can be written
---@return file A lua io file
function FileSystem.Open( filename ) end

return FileSystem
2 changes: 1 addition & 1 deletion data/modules/AutoSave/AutoSave.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local FileSystem = require 'FileSystem'
local max_autosaves = 9

local function PickNextAutosave()
local ok, files, _ = pcall(FileSystem.ReadDirectory, 'USER', 'savefiles')
local ok, files, _ = pcall(FileSystem.ReadDirectory, 'user://savefiles')
if not ok then
print('Error picking auto-save')
return '_autosave1'
Expand Down
3 changes: 2 additions & 1 deletion data/pigui/modules/saveloadgame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ local function displaySave(f)
end

local function showSaveFiles()
local ok, files, _ = pcall(FileSystem.ReadDirectory, "USER","savefiles")
-- TODO: This is reading the files of disc every frame, think about refactoring to not do this.
local ok, files, _ = pcall(FileSystem.ReadDirectory, "user://savefiles")
if not ok then
print('Error: ' .. files)
saveFileCache = {}
Expand Down
8 changes: 0 additions & 8 deletions src/enum_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,6 @@ const struct EnumItem ENUM_DetailLevel[] = {
{ 0, 0 },
};

const struct EnumItem ENUM_FileSystemRoot[] = {
{ "USER", int(LuaFileSystem::ROOT_USER) },
{ "DATA", int(LuaFileSystem::ROOT_DATA) },
{ 0, 0 },
};

const struct EnumItem ENUM_PiGuiFaceFlags[] = {
{ "RAND", int(PiGui::Face::RAND) },
{ "MALE", int(PiGui::Face::MALE) },
Expand Down Expand Up @@ -325,7 +319,6 @@ const struct EnumTable ENUM_TABLES[] = {
{ "BodyType", ENUM_BodyType },
{ "BodySuperType", ENUM_BodySuperType },
{ "DetailLevel", ENUM_DetailLevel },
{ "FileSystemRoot", ENUM_FileSystemRoot },
{ "PiGuiFaceFlags", ENUM_PiGuiFaceFlags },
{ "ModelDebugFlags", ENUM_ModelDebugFlags },
{ "CruiseDirection", ENUM_CruiseDirection },
Expand Down Expand Up @@ -355,7 +348,6 @@ const struct EnumTable ENUM_TABLES_PUBLIC[] = {
{ "BodyType", ENUM_BodyType },
{ "BodySuperType", ENUM_BodySuperType },
{ "DetailLevel", ENUM_DetailLevel },
{ "FileSystemRoot", ENUM_FileSystemRoot },
{ "PiGuiFaceFlags", ENUM_PiGuiFaceFlags },
{ "ModelDebugFlags", ENUM_ModelDebugFlags },
{ "CruiseDirection", ENUM_CruiseDirection },
Expand Down
1 change: 0 additions & 1 deletion src/enum_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ extern const struct EnumItem ENUM_PolitGovType[];
extern const struct EnumItem ENUM_BodyType[];
extern const struct EnumItem ENUM_BodySuperType[];
extern const struct EnumItem ENUM_DetailLevel[];
extern const struct EnumItem ENUM_FileSystemRoot[];
extern const struct EnumItem ENUM_PiGuiFaceFlags[];
extern const struct EnumItem ENUM_ModelDebugFlags[];
extern const struct EnumItem ENUM_CruiseDirection[];
Expand Down
Loading

0 comments on commit 1cc5f3d

Please sign in to comment.