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

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Suggestions
. . -> [Subject]  request to add 2 lua files
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: request to add 2 lua files
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

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

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Wed 08 Dec 2010 12:02 AM (UTC)  quote  ]
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 Bast   (77 posts)  [Biography] bio
Date Tue 07 Dec 2010 04:39 PM (UTC)  quote  ]
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://bastmush.googlecode.com
[Go to top] top

Posted by Nick Gammon   Australia  (19,500 posts)  [Biography] bio   Forum Administrator
Date Mon 06 Dec 2010 09:33 PM (UTC)  quote  ]
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 David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Mon 06 Dec 2010 05:34 AM (UTC)  quote  ]
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 Bast   (77 posts)  [Biography] bio
Date Mon 06 Dec 2010 01:11 AM (UTC)  quote  ]

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://bastmush.googlecode.com
[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.


3,284 views.

[New subject]  Start a new subject   [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.

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]