SRB2 Message Board

SRB2 Message Board (https://mb.srb2.org/index.php)
-   Mods (https://mb.srb2.org/forumdisplay.php?f=69)
-   -   Synchronised input/output support for Lua! (https://mb.srb2.org/showthread.php?t=43381)

LJ Sonik 04-11-2018 03:27 PM

Synchronised input/output support for Lua!
 
2 Attachment(s)
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:
Spoiler:

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 are available for 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 luafilesfolder.
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:
Spoiler:

  • 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

fickleheart 05-06-2018 10:15 PM

I think everything works as it should, so welcome to Releases


All times are GMT. The time now is 09:51 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.