SRB2 Message Board  

Go Back   SRB2 Message Board > Sonic Robo Blast 2 > Releases > Mods

Reply
 
Thread Tools
Synchronised input/output support for Lua! Details »»
Synchronised input/output support for Lua!
Version: 1.0, by LJ Sonik LJ Sonik is offline
Developer Last Online: Sep 2018

Category: Version: SRB2 Rating:
Released: 04-11-2018 Last Update: Never Installs: 1
Re-Useable Content Code Changes

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

Download Now

File Type: 7z srb2io.7z (1.27 MB, 129 views)
File Type: lua ioex.lua (1.1 KB, 156 views)

Supporters / CoAuthors

Show Your Support

  • The author of this addon has given explicit permission for its content to be re-used and/or reproduced.

Comments
Old 05-06-2018   #2
fickleheart
gender collector
 
fickleheart's Avatar
Default

I think everything works as it should, so welcome to Releases
__________________
Quote:
<@toaster> '"sonic robot explosion 2", the hot new sensation for teens looking for a good time, tempts many children away from god each year'
av src
fickleheart is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:21 AM.


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