Lua method to get Minetest master directory

Leifanator
New member
 
Posts: 9
Joined: Sat Nov 10, 2012 05:43

Lua method to get Minetest master directory

by Leifanator » Mon Jul 22, 2013 00:59

I am having some issues with mod programming where this would be very helpful. On the Dev Wiki, I see minetest.get_modpath() for individual mod folders, but I need something that can get the "mods" folder itself. While at it, something like minetest.get_masterpath() might be useful to some modders as well.
Last edited by Leifanator on Mon Jul 22, 2013 00:59, edited 1 time in total.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Mon Jul 22, 2013 10:31

There are several mod paths, not only one.

Why do you need it, though?
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Mon Jul 22, 2013 10:37

You could extract the minetest path out of minetest.get_worldpath()
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Mon Jul 22, 2013 11:42

See this function (ported from PHP to Lua):

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function explode(div,str)
    if (div == '') then
        return nil
    else
        local pos,arr = 0,{}
        for st,sp in function() return string.find(str,div,pos,true) end do
            table.insert(arr,string.sub(str,pos,st-1))
            pos = sp + 1
        end
        table.insert(arr,string.sub(str,pos))
        return arr
    end
end


It splits a string (str) at every occurence of <div> and returns it as a list:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local minetestpath = minetest.get_worldpath()
 --> C:\Users\Christian\Desktop\Minetest\bin\..\worlds\Singleplayer

minetestpath = explode("\",minetestpath)
 --> {"C:","Users","Christian","Desktop","Minetest","bin","..","worlds","Singleplayer"}
 -- Now we remove the last four strings in the list and we have the minetest dir

minetestpath[table.getn(minetestpath) - 0] = nil
minetestpath[table.getn(minetestpath) - 1] = nil
minetestpath[table.getn(minetestpath) - 2] = nil
minetestpath[table.getn(minetestpath) - 3] = nil
 --> {"C:","Users","Christian","Desktop","Minetest"}

 -- Convert it to a string
 minetestpath = table.concat(minetestpath,"\")


This should work, but I didn't test it ;)
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Mon Jul 22, 2013 11:49

webdesigner97 wrote:See this function (ported from PHP to Lua):

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function explode(div,str)
    if (div == '') then
        return nil
    else
        local pos,arr = 0,{}
        for st,sp in function() return string.find(str,div,pos,true) end do
            table.insert(arr,string.sub(str,pos,st-1))
            pos = sp + 1
        end
        table.insert(arr,string.sub(str,pos))
        return arr
    end
end


It splits a string (str) at every occurence of <div> and returns it as a list:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local minetestpath = minetest.get_worldpath()
 --> C:\Users\Christian\Desktop\Minetest\bin\..\worlds\Singleplayer

minetestpath = explode("",minetestpath)
 --> {"C:","Users","Christian","Desktop","Minetest","bin","..","worlds","Singleplayer"}
 -- Now we remove the last four strings in the list and we have the minetest dir

minetestpath[table.getn(minetestpath) - 0] = nil
minetestpath[table.getn(minetestpath) - 1] = nil
minetestpath[table.getn(minetestpath) - 2] = nil
minetestpath[table.getn(minetestpath) - 3] = nil
 --> {"C:","Users","Christian","Desktop","Minetest"}

 -- Convert it to a string
 minetestpath = table.concat(minetestpath,"")


This should work, but I didn't test it ;)

And why dont you use string:split()? https://github.com/minetest/minetest/blob/master/doc/lua_api.txt#L1046
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Mon Jul 22, 2013 16:06

PilzAdam wrote:
webdesigner97 wrote:See this function (ported from PHP to Lua):

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function explode(div,str)
    if (div == '') then
        return nil
    else
        local pos,arr = 0,{}
        for st,sp in function() return string.find(str,div,pos,true) end do
            table.insert(arr,string.sub(str,pos,st-1))
            pos = sp + 1
        end
        table.insert(arr,string.sub(str,pos))
        return arr
    end
end


It splits a string (str) at every occurence of <div> and returns it as a list:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local minetestpath = minetest.get_worldpath()
 --> C:\Users\Christian\Desktop\Minetest\bin\..\worlds\Singleplayer

minetestpath = explode("",minetestpath)
 --> {"C:","Users","Christian","Desktop","Minetest","bin","..","worlds","Singleplayer"}
 -- Now we remove the last four strings in the list and we have the minetest dir

minetestpath[table.getn(minetestpath) - 0] = nil
minetestpath[table.getn(minetestpath) - 1] = nil
minetestpath[table.getn(minetestpath) - 2] = nil
minetestpath[table.getn(minetestpath) - 3] = nil
 --> {"C:","Users","Christian","Desktop","Minetest"}

 -- Convert it to a string
 minetestpath = table.concat(minetestpath,"")


This should work, but I didn't test it ;)

And why dont you use string:split()? https://github.com/minetest/minetest/blob/master/doc/lua_api.txt#L1046

Cause I'm not that good at lua and don't know all functions (same for the API) ;) But thx for the link
 

Leifanator
New member
 
Posts: 9
Joined: Sat Nov 10, 2012 05:43

by Leifanator » Tue Jul 23, 2013 06:45

webdesigner97 wrote:See this function (ported from PHP to Lua):

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function explode(div,str)
    if (div == '') then
        return nil
    else
        local pos,arr = 0,{}
        for st,sp in function() return string.find(str,div,pos,true) end do
            table.insert(arr,string.sub(str,pos,st-1))
            pos = sp + 1
        end
        table.insert(arr,string.sub(str,pos))
        return arr
    end
end


It splits a string (str) at every occurence of <div> and returns it as a list:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local minetestpath = minetest.get_worldpath()
 --> C:\Users\Christian\Desktop\Minetest\bin\..\worlds\Singleplayer

minetestpath = explode("",minetestpath)
 --> {"C:","Users","Christian","Desktop","Minetest","bin","..","worlds","Singleplayer"}
 -- Now we remove the last four strings in the list and we have the minetest dir

minetestpath[table.getn(minetestpath) - 0] = nil
minetestpath[table.getn(minetestpath) - 1] = nil
minetestpath[table.getn(minetestpath) - 2] = nil
minetestpath[table.getn(minetestpath) - 3] = nil
 --> {"C:","Users","Christian","Desktop","Minetest"}

 -- Convert it to a string
 minetestpath = table.concat(minetestpath,"")


This should work, but I didn't test it ;)


Thanks, that worked.
 

kahrl
Member
 
Posts: 236
Joined: Fri Sep 02, 2011 07:51

by kahrl » Tue Jul 23, 2013 14:31

Note that get_worldpath may at any point in the future collapse the "bin" and ".." parts, without warning. I think it already does if the world path was passed to minetest via the command line.
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Tue Jul 23, 2013 15:30

kahrl wrote:Note that get_worldpath may at any point in the future collapse the "bin" and ".." parts, without warning. I think it already does if the world path was passed to minetest via the command line.

Yes, this might happen. But this function is more like a workaround, people can customize it if they think they need it or if it doesn't work correctly.
 


Return to Minetest Features

Who is online

Users browsing this forum: No registered users and 5 guests

cron