[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Suggestions
. . -> [Subject]  request to add 2 lua files

request to add 2 lua files

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Bast   (78 posts)  [Biography] bio
Date Mon 06 Dec 2010 01:11 AM (UTC)

Amended on Mon 06 Dec 2010 01:12 AM (UTC) by Bast

Message
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

Bast

Scripts: http://github.com/endavis
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #1 on Mon 06 Dec 2010 05:34 AM (UTC)
Message
If you only ever use the file scanner in conjunction with dofile, why not use require?

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Mon 06 Dec 2010 09:33 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Bast   (78 posts)  [Biography] bio
Date Reply #3 on Tue 07 Dec 2010 04:39 PM (UTC)
Message
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

Bast

Scripts: http://github.com/endavis
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] bio
Date Reply #4 on Wed 08 Dec 2010 12:02 AM (UTC)
Message
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().

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #5 on Wed 08 Dec 2010 05:39 AM (UTC)
Message
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. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,371 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]