Client Auto Index

Hello everyone, I’m relatively new to lua, and im trying to find out why my function for indexing lua files on the client.
I am trying to make a for loop that will look through all the clients files within the directory i specify. For example, I have a folder called plugins within my gamemode, this folder has sub folders, and within those folders are the server side lua (SV_BLAH.LUA) and the client side needed files (CL_BLAH.LUA) so it looks something like this:
plugins/myPlug/sv_myPlug.lua
plugins/myPlug/cl_myPlug.lua

I have it using this to send the files the client needs.
[LUA]
function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
local list = file.FindDir(dir…"/*")
for _, fdir in pairs(list) do
if fdir != “.svn” then // don’t spam people with useless .svn folders
AddDir(dir…"/"…fdir)
end
end

for k,v in pairs(file.Find(dir.."/*")) do
    AddCSLuaFile(dir.."/"..v)
end

end

AddDir(“gamemodes/MY_GAMEMODE/gamemode/plugins”)
[/LUA]

But I need this same method on the client to include all the files. I have tried countless things and it cant find the files. [EDIT]Also when i use that, with the gamemode directory, it only loads it for me because I have those file on my client in those directories. So don’t be fooled if you try this. :stuck_out_tongue: [/EDIT] I need it this way to protect the plug and play method of the plugins folder… so i can just drop whatever addition I need, and not have to add includes to everything…
Also, if there is a better method to adding the files to the client then that, please tell me. Any help on this key functionality would be appreciated thank you for your time.

[EDIT 2] is there a lua highlight function for the code tags? thnx

use lua tags instead of code

also you don’t resource.AddFile for lua.

see **[G.AddCSLuaFile

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.AddCSLuaFile)**

sry, i was writing it in quick… its not resource add file… Let me update the post. And btw, thats not what i asked, did you read the entire post? Im trying to have something like that, that will auto include all the client gamemode files…

Just change the AddCSLua function to include?

[lua]
function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
local list = file.FindDir(dir…"/*")
for _, fdir in pairs(list) do
if fdir != “.svn” then // don’t spam people with useless .svn folders
AddDir(dir…"/"…fdir)
end
end

for k,v in pairs(file.Find(dir.."/*")) do
    include(dir.."/"..v)
end

end

AddDir(“gamemodes/MY_GAMEMODE/gamemode/plugins”)
[/lua]

but the client doesnt have the gamemode in the gamemode directory. So when it tries to load those files, the client doesnt have them there. Try again, PS: i have tried that too, so I know.

You need to use AddCSLuaFile() serverside, to make sure the clients download the scripts, and you need to send (again using AddCSLuaFile()) an autorun file to clients that will include the downloaded files clientside. Using AddCSLuaFile() alone will cause clients to download scripts, but not to run them.

I know that… That is why i need help finding a way to make a similar script to auto index the files clientside. So that I don’t have to add every single client lua file manually every time i shove a plugin in there… I’m asking if anyone knows how, or can show me a script that does that… I don’t know if you are saying that or not, but from what you said, It seems to me you don’t understand what I’m asking.

  1. Is is possible?
  2. If so, what would I do to create this auto index script, or can you paste me one?
    My goal is to get this script so I can just place my plugins in there and not have to manually add everything… just paste and let go. This way, people can run this gamemode and just shove their little additions in the plugins folder and be done.
    PS: that script in the main post is what accomplishes the AddCSLuaFile()
    PSS: if i am misunderstanding your post, and you mean by send the autorun file to the client, then how do i generate this autorun file, I dont know of a way to get lua to write lua files… for security reasons.
    And again to reiterate, I understand that AddCSLuaFile() doesn’t auto include the client lua file, this is why I’m asking for help on this script, and or someone to write it for me/everyone else…
    I’m frustrated because I don’t know if I’m explaining this correctly… :
    Keep replying guys… Still need help…

[EDIT]
Ok, on a very simple possibility, I think I could be asking is there a way to make a custom function that will do exactly that: Send the File, Make Client Auto Include it.
I think… omg this is complicated to explain.
[/EDIT]

Are you talking about something like this?
[lua]
for k,v in pairs(file.FindInLua(“Gamemode_stooph/gamemode/sh_modules/.lua")) do AddCSLuaFile(“sh_modules/”…v) include(“sh_modules/”…v) end
for k,v in pairs(file.FindInLua("Gamemode_stooph/gamemode/sv_modules/
.lua”)) do include(“sv_modules/”…v) end
for k,v in pairs(file.FindInLua(“Gamemode_stooph/gamemode/cl_modules/*.lua”)) do AddCSLuaFile(“cl_modules/”…v) end
[/lua]
[sp]Don’t take stuff from the wiki, that’s not how you learn.[/sp]

yeah, but the problem is not sending the files.
Its getting the client to automatically include those files downloaded from the server.
Like when it sends a file say:
plugins/myPlug/sv_myPlug.lua
plugins/myPlug/cl_myPlug.lua <<< THIS ONE
and yeah, this would be located ONLY ON THE SERVER, in said directory aka “Gamemode_stooph/gamemode/”

then i need some way for the client to automatically use include on it. So im not adding
include(“BLAH BLAH”)
include(“BLAH BLAH”)
include(“BLAH BLAH”)
include(“BLAH BLAH”)

EVERY TIME I add a plugin.
Something to make it auto include…
*Mental thought ( I must be explaining it wrong, nobody appears to understand what I need… )

so nobody knows how to do this? at all? :C I really need this, and I have no idea how to do it…

Why can’t you do both AddCSLuaFile and include?

[LUA]
for k,v in pairs(file.Find(dir…"/*")) do
AddCSLuaFile(dir…"/"…v)
include(dir…"/"…v)
end
[/LUA]

Because then the server would be including those files, not the client. The include function works for whatever “side” of the connection its run on. eg: server read server, client read client.
[EDIT] the server would have no use for those client functions and scripts, or they wont work serverside, thats why its only included on the client side.[/EDIT]
The problem is, how to get it to include those CSLuaFile’s on the client… I cant seem to find a way to create a simple indexing script to do it. its like none of the directories exist on the client. thats why im asking if anyone else can help.

You could have a user message function waiting clientside, and then use a for loop on the server to run the usermessage function for each file, e.g.

[lua]
–Code in a clientside autorun script (scriptloader.lua for this example)–
usermessage.Hook(“IncludeCSFile”, function(bf)
local f = bf:ReadString()
if !f || #f < 1 then return end
include(f)
end)

–SERVERSIDE–
AddCSLuaFile(“autorun/client/scriptloader.lua”) – Send the file with the hook first

function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
local list = file.FindDir(dir…"/*")
for _, fdir in pairs(list) do
if fdir != “.svn” then // don’t spam people with useless .svn folders
AddDir(dir…"/"…fdir)
end
end

for _, filename in pairs(file.Find(dir.."/*")) do
    local f = dir.."/"..filename
	AddCSLuaFile(f)
	SendUserMessage("IncludeCSFile", _, f)
end

end

AddDir(“gamemodes/MY_GAMEMODE/gamemode/plugins”)
[/lua]

Make sure the script with the user message hook is in autorun/client, or it wont run when added with AddCSLuaFile (I know it’s obvious but people tend to forget that).

Dude, awesome i hope it works, i dont have time to test it right now, but thanks. :slight_smile: