Register to post in forums, or Log in to your existing account

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Fang Xianfu

Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Mon Mar 02, 2009 3:52 pm   

Using LuaSocket (and other lua packages) with CMUD.
TesterofLimitz PMed me asking for help getting LuaSocket set up. I thought I'd repost my reply here to help people trying to load Lua packages and modules for use in CMUD.


If you try to load a package and it doesn't exist, you'll raise a Lua error. You can do this simply by changing the command line to Lua mode and typing require("lol") or something. When it fails, the error will handily tell you where it's looking. The output on my system is:

module 'lol' not found:
no field package.preload['lol']
no file '.\lol.lua'
no file 'D:\CMUD\lua\lol.lua'
no file 'D:\CMUD\lua\lol\init.lua'
no file 'D:\CMUD\lol.lua
no file 'D:\CMUD\lol\init.lua

The error is truncated after a while, but you're not missing much - it just goes on to say that it's also looking for .dll files in each of those locations as well as .lua files. You can get a full list of what it's after by doing print(package.path) and print(package.cpath) if you're curious.

This is a useful exercise because it's telling you the steps that Lua is going through to find your package. The package.preload part doesn't matter to us - you can read more about it in the Lua manual if you're curious. The next line I have no idea where it's looking. The subsequent lines are more useful, though - it gives you a direct path to the file that CMUD is trying to find. Your path will probably be different to mine, but it's the CMUD installation folder you're after. I' going to use D:\CMUD throughout, substitute for your installation folder.

So first, download the package here. Inside the zip file there's a lua exe and dll - you don't need those, but pay attention to the name of the dll - in this case it's lua5.1.dll. Some packages need lua dlls with different names, in which case you'll have to make a copy of the lua dll in the CMUD directory and rename it. The default name is lua5.1.dll, so that's fine for this package.

The files we're interested are the socket, mime and lua ones. Notice how one of the paths it's looking in is D:\CMUD\lua\packagename\? That's a hint - you need to drop these folders straight into D:\CMUD, so that the lua folder is where CMUD will look for your package, and the socket and mime folders are where the package expects them in relation to itself.

So you end up with three new folders:


that contain the LuaSocket files. In this particular case, we're only interested in the http part of LuaSocket, so when we require it, we just use that bit. If you look inside D:\CMUD\lua\socket, you'll see that there's a http.lua file in there. This is what we load when we do require("socket.http").

So our files are now in the right place for CMUD to access them - we're ready to go. So, we run http=require("socket.http"), which loads up the http table with LuaSocket's http stuff. Most modules will want you to do somevariable = require(module), but some won't - read their docs to find out.

Anyway, we've done our require() and CMUD hasn't generated an error (hopefully), which means it's found the package. Now we just need to use our package to do whatever we want: print(assert(http.request(""))), for example.

Finally, a tip - require() looks in a list of loaded packages (it's located in package.loaded if you're curious - keys are the names of the loaded packages) first before it tries to load a package, so it won't load the package again - but there's no reason why you can't check yourself before going to the trouble of running require(), at least in this simple case: putting if not http then http = require("socket.http") end before anything that's using http.request will load the package if it's not loaded, and do nothing if it is.


As an addendum to this, a thought just occurred to me - it's possible you might not have write access to your CMUD folder, or not want to put the package there for whatever reason. If that's the case, then you can edit package.path and package.cpath and add a new location to look. Hopefully the format of those strings is clear from the other entries in there already.
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote

Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Tue Mar 03, 2009 11:46 am   Re: Using LuaSocket (and other lua packages) with CMUD.
Fang Xianfu wrote:

module 'lol' not found:
no field package.preload['lol']
no file '.\lol.lua'

The next line I have no idea where it's looking.

It's looking in the CWD, current working directory, which in Windows can often be hard to track down. Shortcuts have the "Start in:" field which sets the CWD for the application it is pointing to and this should be kept for the lifetime of the application, if none is provided it's the same directory the application lives in, but as with all things Windows it is buggy and other things going on in the system may change it.
Taz :)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by