CC: Tweaked

CC: Tweaked

57M Downloads

io.open(<path>, 'r+b') does not allow writing

tehbiga opened this issue ยท 3 comments

commented

Minecraft Version

1.18.x

Version

1.101.2

Details

Issue:
io.open(<path>, 'r+b') does not allow writing as standard Lua does preventing me from updating chunks of files.

According to the documentation seeking should be available for binary file handles, but trying various combinations of rb, wb, and ab with and without + in the middle, or at the end, does not allow me to modify a file without losing contents or update at an arbitrary seek position.

Not sure if this is intentional or if I've missed something in the docs. I know there are differences between official Lua interpreters and the mod's, but I'm assuming this was missed since everything else seems to work as expected.

In-game test:
https://i.imgur.com/v83tUmP.png

Expected:
Here is an ilua example I did on my desktop to show standard functionality:

> f = io.open('test.txt', 'wb')
> f:write('Hello world')
true
> f:close()
true

> f = io.open('test.txt', 'r+b')
> f:seek('set', 6)
6
> f:write('there')
true
> f:close()
true

> f = io.open('test.txt', 'rb')
> f:read('*a')
'Hello there'
commented

AFAIK, this is intentional, and has been the case since CC 1.0. I doubt it'll ever be properly fixed, but there's a slim chance that Squid might finally decide to implement it. No guarantees though :/

Your best bet is to just read the entire file in, modify it in memory, and then write it all out. Or if you only need to edit a section, then open for reading, seek, read, close, modify, open for writing, seek, write, and close.

And even if it was implemented, I doubt it would make its way to 1.18.x. Versions before 1.19.4 are currently in maintenance mode, so I wouldn't expect anything but serious bug fixes to land in 1.18.

commented

This is idly on my todo list. I put a prototype of this together as part of the multi-loader rework, but getting file size tracking working with it was quite nasty to get right (at least, not without adding a lot of overhead). Once #1456 is implemented, this is something I will revisit.

commented

@MCJack123

Yeah this is about what I figured anyway and I had the workarounds ready to go.
For now I'm holding open the file state until a chunk fails or it finishes gracefully.

Thanks Squid, keep up the good work!