[Open Assets] Synchronised input/output support for Lua!

This content may be freely modified and/or maintained by anyone.
Status
Not open for further replies.

LJ Sonik

Developer
Sonic Team Junior
This mod reimplements Lua's input/output library in a netgame-friendly way.
This allows your Lua scripts to read, edit, and create files, for instance to create custom saves or loading custom configuration files.

io.open has been modified in order to work properly in multiplayer:
Code:
io.open(string path, string mode, player_t player, function callback)
  • path: the path to the file, relative to the luafiles folder in the server's SRB2 folder. Yes, the file MUST be on the server's computer.
  • mode: a string indicating the access mode. Most useful values are "r" for reading and "w" for writing. See here for more details.
  • player: should ALWAYS be nil. The only exception is for local ("client-side") I/O, which should never be used unless you know exactly what you're doing (or like synch failures).
  • callback: a function that will be executed as soon as everyone has downloaded the file. This is where you do whatever operations you want on the file. See below for details about it.
The fourth parameter, callback, is a function that takes the following parameters:
Code:
function(file_t file, string filename)
  • file: a file handler. You can perform various operations on the file by using file:read, file:write, file:lines and file:seek. Other functions that are available might not work and are not guaranteed to be kept in later versions. If it is nil, then the file couldn't be opened for some reason (e.g. file not found).
  • filename: the name of the file, relative to the luafiles folder.
Once you exit this function, the file will automatically be closed. Depending on the file size and everyone's internet, there will be some delay between the call to io.open and the call to this function. The gameplay will keep running in the meanwhile, so your script must take this in account.


io.close
has been removed. Instead, the file is automatically closed after you are done with it, as explained previously.
Downloaded files are temporary: they are stored as luafiles/$$$XXXXXXXX.tmp (X are random digits) and removed immediately after the game closes them. Of course, if you are the server, the original file won't be removed.

Last, but not least, I/O can be quite dangerous, thus a number of limitations have been set:
  • A client using local I/O (DO NOT USE THIS, OK?) can only access files stored in luafiles/shared and its subfolders. This does not apply to the server. This should help prevent evil hosts from stealing private data from a client joining their server, such as a list of passwords for Terminal or a similar mod. Never put files in luafiles/shared unless you have a reason to (e.g. per-client config file).
  • .., and : cannot be used in path strings. Any attempt to do so will result in Lua throwing a warning.
  • Path strings cannot start with / or \\. This, along with the changes above, was done to prevent users from leaving the SRB2 directory.
  • All files are written into a new directory named luafiles by default. This was done for security and organization reasons.
  • You can specify non-existent subdirectories in your path string and the game will automatically create them for you.
  • io.popen() has been removed. This function would allow scripters to open programs with the Lua API. Naturally this is a terrible idea.
  • File size has been limited to 1MB. If a file is over 1MB and you attempt to append it, it will not be modified. If a file is under 1MB and your change would bring it over, it is discarded. (contents erased if mode is "w", contents unmodified if mode is "a+")
  • A whitelist has been included for specific filetypes. Any attempts to use different file types will result in Lua throwing a warning. Supported filetypes are .txt, .sav2, .cfg, .png, and .bmp.


Source code
 

Attachments

  • srb2io.7z
    1.3 MB · Views: 515
  • ioex.lua
    1.1 KB · Views: 530
Last edited:
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top