fs

This is the filesystem API. REFramework restricts scripts from unrestricted access to a user's system. All file APIs focus on safe subdirectories.

In newer builds of REFramework, the io API is supported with safety wrapping. io.popen is explicitly disabled (returns nil).

Path Safety Rules

All file APIs (fs.read, fs.write, fs.glob, io.open, io.lines, io.input, io.output, require, package.loadlib) enforce the following rules:

  • All paths are relative. A path with a drive or a root raises an error: C:\foo, but also \foo, /foo and C:foo.
  • Parent directory traversal (..) is rejected.
  • fs and io functions refuse .dll and .exe files, so a script can't write one. The check uses the name Windows gives the file, which drops trailing dots and spaces, so a name like x.dll. is refused too.
  • fs and io functions and package.loadlib reject paths that contain a NUL character.
  • By default, paths resolve under reframework/data/.

Path Tokens

Use these prefixes to select the base directory:

TokenResolves to
(none)reframework/data/ in the persistent directory
$natives/natives/ directory relative to the REFramework DLL
$autorun/reframework/autorun/ in the persistent directory

io Library Safety Wrapping

The standard Lua io library is available but wrapped with path safety:

  • io.open, io.lines, io.input, io.output — All wrapped with the same path safety rules as fs functions. nil as filepath passes through to stdin/stdout/stderr.
  • io.popen — Explicitly disabled (returns nil).
  • io.type, io.flush, io.close — Left as standard Lua.

require and package.loadlib

  • require is wrapped with the same path safety rules: a module name containing .. or starting with a drive or a root raises an error. package.path, package.cpath, and package.searchers are reset to pristine values before each require call.
  • package.loadlib(path, funcname) is restricted to safe paths and only allows .dll extensions. The DLL is loaded from the resolved path, by default inside reframework/data/. It is not looked up by name in the game directory, the system directories or PATH. On failure it returns nil plus an error message, like Lua's own package.loadlib.
  • Both only load a native library from a file that is a .dll under its real name on disk. A file with another name that is reached through its 8.3 short name (X~1.DLL) is refused.

For a complete list of available Lua standard libraries and disabled functions, see Lua Standard Libraries.

Methods

fs.glob(filter, modifier)

Returns a table of file paths that match the filter. filter should be a regex string for the files you wish to match.

modifier is optional. Supports $natives and $autorun to select the base directory.

Example

-- Get my mods JSON files.
local json_files = fs.glob([[my-cool-mod\\.*json]])

-- Iterate over them.
for k, v in ipairs(json_files) do
    -- v will be something like `my-cool-mod\config-file-1.json` 
end

fs.read(filename)

Reads filename and returns the data as a string.

fs.write(filename, data)

Writes data to filename. data is a string.