request to add 2 lua files

Posted by Bast on Mon 06 Dec 2010 01:11 AM — 6 posts, 31,174 views.

#0
luapath.lua - or whatever you want to call it


plugin_path = string.match(GetPluginInfo(GetPluginID(), 6), "(.*)\.*$") .. "\lua\"
package.path = plugin_path .. "?;" .. plugin_path .. "?.lua;" .. package.path


This allows plugin authors to have the lua directory for their plugins added to the lua search path

findfile.lua - or whatever you want to call it


-- this finds a file searching from path including all subdirectories

function scan_dir_for_file (path, tfile)

  -- find all files in that directory
  local t = assert (utils.readdir (path .. "\*"))

  for k, v in pairs (t) do
   if not v.hidden and
      not v.system and
      k:sub (1, 1) ~= "." then
      -- recurse to process file or subdirectory
      if v.directory then
        found = scan_dir_for_file (path .. "\" .. k, tfile)
        if found then
          return found
        end
      elseif k == tfile then
        return path .. "\" .. k
      end -- if 

   end -- if

  end -- for
  return false
end


I use it like this:


require "filefind"

nfile = scan_dir_for_file (GetInfo(60), "telnet_options.lua")

if nfile then
  -- pull in telnet option handling
  dofile (nfile)
else
  print("Could not load telnet_options.lua, please copy it to your plugins directory")
end


Searches for a file given a path, useful for dofile and not having to worry about where the actual file is.

Bast
Amended on Mon 06 Dec 2010 01:12 AM by Bast
USA #1
If you only ever use the file scanner in conjunction with dofile, why not use require?
Australia Forum Administrator #2
http://www.gammon.com.au/forum/?id=9906

In the file commas.lua which is currently in the distribution, you have a generic directory scanner.

Whilst that doesn't look for a particular file, you could add in a function that checks the trailing part of the file name (after the final slash) to see if it is the required file or not. If so, then do a "dofile" on the full path.
#3
David Haley said:

If you only ever use the file scanner in conjunction with dofile, why not use require?


Because the file might not be in the lua search path. I want to be able to keep all my plugins in one directory and find files in other places if I want to use them. Such as telnet_options.lua in the new aardwolf client, which is not on the lua path.

Nick Gammon said:

In the file commas.lua which is currently in the distribution, you have a generic directory scanner.

Whilst that doesn't look for a particular file, you could add in a function that checks the trailing part of the file name (after the final slash) to see if it is the required file or not. If so, then do a "dofile" on the full path.


I did not know to look in commas.lua, but that seems a little complicated for just searching for a file in a directory tree.

Bast
USA #4
Bast said:
Because the file might not be in the lua search path.

So change the path. The *.lua path is in package.path, and the *.dll path is in package.cpath. Use utils.split to split by ';', insert/replace any of the paths, and table.join it back together. This is exactly what my plugin stub Plugger does to support creating structured plugins. You can even temporarily replace the whole path if you want to only affect the next call to require().
USA #5
Right, what Twisol said. See also:
http://www.lua.org/manual/5.1/manual.html#pdf-package.path

I suppose it's possible that package.path has been sandboxed away, but it seems weird that you'd have access to dofile but not to the path...

Basically, you're replicating well-understood and tested module code, which is somewhat odd. If there's more going on here, then ok, but if the only objection is "it's not on the path" when you can just add it to the path, maybe we don't need this after all. :-)