Facepunch / garrysmod-issues

Garry's Mod issue tracker
141 stars 56 forks source link

file.Read() fails to read Lua files clientside when they do not truly exist clientside #1798

Open EstevanTH opened 9 years ago

EstevanTH commented 9 years ago

Hello robotboy and co,

I found a problem when trying to read Lua files. If the Lua file does not exist clientside (in general because of addon not mounted), the file can be found but it fails when attempting to read.

Here is my code to load vehicles Lua files with the minimum of problems regarding the startup Lua includes limit. It delays the load of Lua files and safely run every Lua script.

-- Run vehicles Lua scripts:
AddCSLuaFile()
hook.Add("PreGamemodeLoaded", "mr_vehicles", function ()
    local path = "mohamed_rachid/vehicles/"
    local succ,err
    for _,LuaFile in ipairs(file.Find(path.."*.lua", "LUA")) do
        local content = file.Read(path..LuaFile, "LUA")
        if isstring(content) then
            succ,err = pcall(RunStringEx, content, "lua/"..path..LuaFile)
            if !succ then
                ErrorNoHalt("\nlua/"..path..LuaFile..": "..tostring(err).."\n\n")
            end
            if SERVER then
                AddCSLuaFile(path..LuaFile)
            end
        else
            ErrorNoHalt("Unable to load lua/"..path..LuaFile.."\n\n") -- ERROR WITH file.Read()
        end
    end
end)

For people who have my addon mounted (SVN, Workshop or probably Workshop sent by the server), the Lua files exist clientside so the Lua files can be read. But for any Lua file that is sent by the server with no matching Lua file mounted clientside (not subscribed to the SVN and no usage of Workshop), then the Lua file cannot be read.

willox commented 9 years ago

Use CompileFile. The decompressed contents of the Lua files don't actually exist anywhere on the file system so I don't see fixing this as necessary.

http://wiki.garrysmod.com/page/Global/CompileFile

EstevanTH commented 9 years ago

That is useful! Thank you. :smiley: Anyway it's too late, I chose to use include() when the file cannot be read properly, but I'm not sure that it will work if startup includes limit is over.

willox commented 9 years ago

You're thinking of the CS Lua file limit? That limit is only for the amount of files which can be sent to clients by the server. Even with file.Read, include or CompileFile you'll be limited by the limit.

One issue with your file.Read system would be people (and people do abuse these systems) using it to get past sv_allowcslua.

EstevanTH commented 9 years ago

Nope, the problem that breaks includes (of internal stuff as well) after a given number of Lua files included. This is called the "too many addons" bug.

EDIT: Also, thank you for the info, I need to fix that finally...

thegrb93 commented 4 years ago

Just hit this problem myself. Luckily I can network the files pretty easily.

thegrb93 commented 4 years ago

Done... but file.Read would be a lot faster.

govigg commented 4 years ago

So yeah, we've got CompileString, but what when i want to read file contents instead of compiling it into lua function?

thegrb93 commented 4 years ago

You can't, devs apparently don't want clients reading server's client code; (pretty sure there's workaround for this anyway though).

govigg commented 4 years ago

It's very simple outside of lua, secondly, if file.Exists returns true for these files, does the file actually exist or not?

Grocel commented 4 months ago

I mean, if nobody requested it in like 10 years, probably nobody wants it.

So Rubat, it looks like someone did, huh? (For context: #5719)