Skip to content

Commit

Permalink
Final API change for FileSystem. Remove the root enum and replace usi…
Browse files Browse the repository at this point in the history
…ng uri scheema.

The interenal implemenations still needs a little work, also, can we avoid reading the filesystem *every* frame when showing the load files dialog?  Would be nice.
  • Loading branch information
JonBooth78 committed Oct 9, 2023
1 parent 53b4c38 commit fbb7002
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 68 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
6 changes: 3 additions & 3 deletions data/meta/FileSystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
---@class FileSystem
local FileSystem = {}

---@param root string A FileSystemRoot constant. Can be either "DATA" or "USER"
---@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(root, path)
function FileSystem.ReadDirectory(root, path) end
--- > 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.
Expand Down
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
55 changes: 12 additions & 43 deletions src/lua/LuaFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,6 @@ int LuaFileSystem::l_patched_io_open(lua_State* L)

return rv;
}
/*
* Interface: FileSystem
*
* A global table that provides access to the filesystem.
*
* This interface is protected. It can only be used by scripts in Pioneer's
* data directory. Mods and scripts in the user directory that try to use it
* will get a Lua error.
*/

FileSystem::FileSource* get_filesytem_for_root(LuaFileSystem::Root root)
{
FileSystem::FileSource *fs = nullptr;
switch (root) {
case LuaFileSystem::ROOT_USER:
fs = &FileSystem::userFiles;
break;

case LuaFileSystem::ROOT_DATA:
fs = &FileSystem::gameDataFiles;
break;

default:
assert(0); // can't happen
}
return fs;
}

static void push_date_time(lua_State *l, const Time::DateTime &dt)
{
Expand All @@ -160,13 +133,12 @@ static void push_date_time(lua_State *l, const Time::DateTime &dt)
/*
* Function: ReadDirectory
*
* > local files, dirs = FileSystem.ReadDirectory(root, path)
* > local files, dirs = FileSystem.ReadDirectory(path)
*
* Return a list of files and dirs in the specified directory.
*
* Parameters:
*
* root - a <FileSystemRoot> constant for the root of the dir to read from
* path - optional. a directory under the root
*
* Returns:
Expand All @@ -184,30 +156,27 @@ static void push_date_time(lua_State *l, const Time::DateTime &dt)
*/
static int l_filesystem_read_dir(lua_State *l)
{
LuaFileSystem::Root root = static_cast<LuaFileSystem::Root>(LuaConstants::GetConstantFromArg(l, "FileSystemRoot", 1));
std::string path;
if (lua_gettop(l) > 1)
path = luaL_checkstring(l, 2);

FileSystem::FileSource* fs = get_filesytem_for_root(root);
const char* path_str = luaL_checkstring(l, 1);
ParsedFilePath path = ParsedFilePath(l, path_str);

assert(fs);
// shame these methods can't take a string_view.
std::string rel_path = std::string(path.GetRelativePath());

{
try {
const FileSystem::FileInfo &info = fs->Lookup(path);
FileSystem::FileInfo info = path.GetFileSource().Lookup(rel_path);
if (!info.IsDir()) {
luaL_error(l, "'%s' is not a directory", path.c_str());
luaL_error(l, "'%s' is not a directory", path_str);
return 0;
}
} catch (const std::invalid_argument &) {
luaL_error(l, "'%s' is not a valid path", path.c_str());
luaL_error(l, "'%s' is not a valid path", path_str);
return 0;
}
}

FileSystem::FileEnumerator files(*fs, FileSystem::FileEnumerator::IncludeDirs);
files.AddSearchRoot(path);
FileSystem::FileEnumerator files(path.GetFileSource(), FileSystem::FileEnumerator::IncludeDirs);
files.AddSearchRoot(rel_path);

lua_newtable(l);
int filesTable = lua_gettop(l);
Expand All @@ -223,9 +192,9 @@ static int l_filesystem_read_dir(lua_State *l)
lua_setfield(l, -2, "mtime");

if (info.IsDir())
lua_rawseti(l, dirsTable, lua_rawlen(l, dirsTable) + 1);
lua_rawseti(l, dirsTable, (int)lua_rawlen(l, dirsTable) + 1);
else
lua_rawseti(l, filesTable, lua_rawlen(l, filesTable) + 1);
lua_rawseti(l, filesTable, (int)lua_rawlen(l, filesTable) + 1);
}

return 2;
Expand Down
5 changes: 0 additions & 5 deletions src/lua/LuaFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ typedef int (*lua_CFunction) (lua_State* L);
namespace LuaFileSystem {
void Register();

enum Root { // <enum scope='LuaFileSystem' name=FileSystemRoot prefix=ROOT_ public>
ROOT_USER,
ROOT_DATA
};

void register_raw_io_open_function(lua_CFunction open);
int l_patched_io_open(lua_State* L);
} // namespace LuaFileSystem
Expand Down

0 comments on commit fbb7002

Please sign in to comment.