Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ MUSHclient
➜ Suggestions
➜ 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 page
Posted by
| Bast
(78 posts) 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 | Top |
|
Posted by
| David Haley
USA (3,881 posts) 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 | Top |
|
Posted by
| Nick Gammon
Australia (23,046 posts) 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 | Top |
|
Posted by
| Bast
(78 posts) 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 | Top |
|
Posted by
| Twisol
USA (2,257 posts) 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 | Top |
|
Posted by
| David Haley
USA (3,881 posts) 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 | 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.
22,775 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top