Panakotta00 / FicsIt-Networks

Control, Monitor, Manage and Automate your Satisfactory.
https://ficsit.app/mod/FicsItNetworks
GNU General Public License v3.0
156 stars 51 forks source link

file:read() doesn't read? #201

Closed abesto closed 2 years ago

abesto commented 2 years ago

Maybe I'm doing something completely wrong, but: in all ways I've tried, reading from disks doesn't seem to work. Note that filesystem.doFile does work; here's a minimal script to reproduce:

local fs = filesystem

fs.initFileSystem("/dev")
fs.makeFileSystem("tmpfs", "tmp")
fs.mount("/dev/tmp","/")

local fw = fs.open("/foo", "w")
fw:write("return 42")
fw:close()

print("Expect to see 42: ", fs.doFile("/foo"))

local fr = fs.open("/foo", "r")
local read_contents = fr:read("*all")
fr:close()
print("Expect to see 'return 42': ", read_contents)

And the full output:

Expect to see 42:  42
Expect to see 'return 42':  

This behavior seems consistent across both tmpfs and drive holder / disk backed mounts (also no content is read with just fr:read(), without the "*all"; fr:read() then returns nil instead of an empty string)

abesto commented 2 years ago

After taking a look at https://github.com/Panakotta00/FicsIt-Networks/blob/361f9effbc12a8e67f9887adaf3146c8f52fba0c/Source/FicsItNetworks/FicsItKernel/Processor/Lua/LuaFileSystemAPI.cpp#L371-L383 it seems like this read implementation takes the number of bytes to read? (Didn't verify, I'm out of time for playing with this for today unfortunately). If that's correct, then this is the problem, as this is not how :read() works in Lua (as documented at the page the FicsIt-Networks docs point to: https://www.lua.org/pil/21.1.html)

Panakotta00 commented 2 years ago

Thats right, At some point in development I choose to ditch buffered streams, previously we had them implemented and there they were implemented behind my VFS abstraction (little pana not knowing yet how it should actually happen)... I havent added a buffered reader in the end to before the abstraction, because now it seems actually more "realistic" and it is more similar to the API of open computers :3 The buffer and API similar to Lua can be implemented in Lua directly. Its just, I'm very Lazy in regards of documentation and havent updated it yet.

abesto commented 2 years ago

Understood :) Yeah given this, I could now easily implement my own read_all function. Worth calling out in docs whenever you get to it: math.huge (i.e. inf) does not play nice with file:read, an actual integer is required.

Panakotta00 commented 2 years ago

Current way of read all:

words = ""
while true do
 local r = file:read(256)
 if not r then break end
 words = words .. r
end

Thats the old method but it does the job... there is a faster method in regards of Lua speed... dont know it out of my head right now tho