PDA

View Full Version : LuaRocks support


demonstar55
06-17-2016, 11:56 PM
I added support to auto set up lua's path and cpath to be able to use the lua_modules folder for a Rocks tree. This should simplify the process of those who wish to extend lua with modules that are not provided by their package manager on linux and greatly simply for windows.

LuaRocks (https://luarocks.org/) is a package management system for Lua similar to CPAN for PERL.

Lets get some examples.

Let's say we wanted to use inspect (https://luarocks.org/modules/kikito/inspect) module help us with some logging of our quests.

Now I have my server set up with the server in ~/server and I use the default lua_modules path for my lua_models, so to install inspect I do

cd ~/server
luarocks --tree=lua_modules install inspect

This will set up some folders which the change will auto add to our path, so it's simple to set up.

Now lets look at a simple quest (lots of details left out)

local inspect = require("inspect")
local trade_ins = {} -- a table that keeps track of some trade ins

...

function event_trade(e)
if e.trade.item1 ~= nil then
table.insert(trade_ins, e.trade.item1:GetID())
end

if e.trade.item2 ~= nil then
table.insert(trade_ins, e.trade.item2:GetID())
end

if e.trade.item3 ~= nil then
table.insert(trade_ins, e.trade.item3:GetID())
end

if e.trade.item4 ~= nil then
table.insert(trade_ins, e.trade.item4:GetID())
end

eq.debug(e.self:GetName() .. " has been given " .. inspect(trade_ins) .. " this session", 3)

...

end

...

Now this example may not be very useful, but it's simple to show how we can use LuaRocks. Now why lua inspect is useful here is an example from my lua terminal

> inspect = require("inspect")
> t = {1,2,3}
> print(t)
table: 0xa86260
> print(inspect(t))
{ 1, 2, 3 }

Now, some modules actually make use of the lua's C API so might be a bit more complicated to install like luasql (https://keplerproject.github.io/luasql/) for example. (Some distros actually package this, some don't so ...)

Usually they will print helpful (or not) error messages like luasql-mysql

luarocks --local --tree=lua_modules install luasql-mysql
Installing https://rocks.moonscript.org/luasql-mysql-2.3.0-1.src.rock...
Using https://rocks.moonscript.org/luasql-mysql-2.3.0-1.src.rock... switching to 'build' mode

Error: Could not find expected file mysql.h, or mysql.h for MYSQL -- you may have to install MYSQL in your system and/or pass MYSQL_DIR or MYSQL_INCDIR to the luarocks command. Example: luarocks install luasql-mysql MYSQL_DIR=/usr/local

To install this on my Linux box I had to use (what you need to do for Windows might be different)

luarocks --tree=lua_modules install luasql-mysql MYSQL_INCDIR=/usr/include

Here is a simple (missing error checking etc) test quest command

function command_itemname(e)
local driver = require("luasql.mysql")
env = driver.mysql()
con = env:connect("db", "user", "pass")
cur = con:execute("select Name from items where id = " .. e.args[1])
e.self:Message(335, "ID " .. e.args[1] .. " is " .. cur:fetch())
end

rhyotte
06-21-2016, 06:14 PM
Nice!

/10char