[Open Assets] SRB2+I/O - Input/Output support for Lua!

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

wolfs

Watch Symphogear
Administrator
Moderator
Sonic Team Junior
Kart Krew™️
Judge
Hi, wolfs is back!

This is probably the biggest expansion Lua has gotten in a long time. Originally removed for being too dangerous, this mod reimplements the input/output library into SRB2's Lua API. With this, you can now read, edit, or create files using Lua scripts! Of course, for sanity purposes, a lot of security restrictions had to be put in place. They are as follows:

  • .., :, 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.

I/O is pretty new, and is not documented on the SRB2 Wiki yet. For a reference on I/O functions in Lua and how they are used, check here.

The zip here includes the SDL2 build (srb2io.exe) and a DirectDraw build. (srb2iodd.exe) Linux guys, I'm sorry, but you're gonna have to compile your own binaries. I have no stable Linux testing environment aside from a VPS with a really specific compile setup.

About netgame compatibility:
The build here is compiled off of the latest public master branch of the SRB2 repo. The io branch itself is based on the next branch, but changing that requires copying a mere three files into any other repo. As it is compiled on master, it *should* be netgame compatible with 2.1.14, but I wouldn't rely on it much. If you are hosting with an I/O build, I strongly urge you not to use any functions that would affect other players. Use the functions locally and have them only apply to the server. Alternatively, you can avoid playing netgames with this build entirely and wait until I/O becomes a vanilla feature in 2.2. The choice is yours.

Please remember to report any and all bugs you find. I/O can be a rather volatile thing so it's important that any major system security issues are taken care of.

With that said, have fun, and please don't use the I/O library to be an asshole. :3


Source Code
LightDash Mirror
 

Attachments

  • srb20027.jpg
    srb20027.jpg
    150.8 KB · Views: 3,479
  • SRB2+IO.zip
    2 MB · Views: 1,531
Last edited:
I'll make sure to try it later, could maybe come in handy or something .
 
The more important question is "Why can we read/write to BMP and PNG files if there are no libraries accessible to Lua to process them in the first place?"
 
Code your own! BMPs are a simple filetype, so writing small images should be relatively simple if you have even a basic understanding of the file format (although I may be at an advantage because I had to write an essay on image file formats a couple weeks ago)... I might even try and give a BMP library for modders a shot myself at some point. This page is particularily helpful on the matter in the meantime...

I'm excited to see this be a thing and I can't wait to have actual free time to take advantage of this! In, like. Ten weeks or something. [gentle sobbing]
 
Last edited:
Yes, write a hefty script to parse PNGs that the game has no use for. Are we preparing for someone to create photoshop.lua or something?
 
That would be amazing, but I was just thinking the ability to write them would be useful for... silly Pokemon Snap style photographs that aren't just taking a regular screenshot with you in the way? I dunno.
 
The fires of greed will burn the weak; so we'll make freedom obsolete

The more important question is "Why can we read/write to BMP and PNG files if there are no libraries accessible to Lua to process them in the first place?"

toaster asked for them, and adding support was as simple as adding two lines to an array.
 
Last edited:
Is there any way to prevent other Lua scripts from overwriting the files saved by your own Lua? Or, do we have to try not to "conflict" one another?
 
I give my people the lives they need; the righteous will succeed

Is there any way to prevent other Lua scripts from overwriting the files saved by your own Lua? Or, do we have to try not to "conflict" one another?

To my knowledge the answer to this is no. If two scripts save the same file, all sorts of stuff can break. Nothing was done in hardcode regarding this in order to avoid creating more limitations.
 
The best we can do, is at least write in the file "In use by thisismylua.lua" And hope that other Luas read it and complain to the user.
 
The best we can do, is at least write in the file "In use by thisismylua.lua" And hope that other Luas read it and complain to the user.

You could always just prefix the name of your file with the name of your mod. Unless they were specifically trying to mess with you, the developer of, say, "Mystic Realm" would never read or write to a file called "torturedplanet_data.sav2."
 
So, what can we do with this besides writing custom save files? What would be really cool is if we could modify lumps as the game is running, like palettes, colormaps... Blockmaps? That's not happening, right?
 
So, what can we do with this besides writing custom save files?
You can also import them. With enough patience and skill and such, you could make something like Super Mario 64 (with multiple "missions" in each level, being able to go back and complete previous ones, and needing to clear X amount of "missions" to advance to more levels). Or you could save the values of custom Lua variables, so people don't have to re-input them every time.
 
I've carved my own path, you followed your wrath; but maybe we're both the same

So, what can we do with this besides writing custom save files? What would be really cool is if we could modify lumps as the game is running, like palettes, colormaps... Blockmaps? That's not happening, right?

As expected, no, you can't do any of that with I/O. What you can do with I/O is create parsers for scripts of different languages, store saves or config files used by Lua scripts, or write hilarious and impractical memes.

I should also note that I/O has no access to any of the game's assets (or, well, anything outside of the luafiles folder) for super obvious security reasons.
 
Yeah, we don't want players creating viruses ... do we?

I/O is also useful for Terminal, because it lets you save those permissions now.
 
Status
Not open for further replies.

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

Back
Top