• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS Package System

Shinmaru

エロルアー Scripter!
Joined
Aug 20, 2007
Messages
1,988
Reaction score
88
Location
Puerto Rico
A Discussion on thoughts about a Package System for TFS(all in Lua).

I have always thought of making something simple for TFS and that is an easy way to install scripts to a server.

This is an example code, on how it should look, btw this was inspired by an user here that made his system self install into the server and by Arch Linux's AUR.

Warning: This is a Unix-only script it will not work on Windows.(I don't plan to make it for Windows.)

INSTALL script:
Code:
local PKG = {
    USER = "Shinmaru",
    NAME = "Install Library",
    VERSION = "1.0",
    EMAIL = "",
    LICENSE = "",
    DISCLAIMER = "",
    URL = 'http://myhost.com',
    FILE = '/install_libs.tar.gz',
    INSTALLFILE = "/TFS_Package_System.install",
    GET = function()
        require "luasocket"
        c = assert(socket.connect(PKG.URL, 80))
        c:send("GET " ..PKG.FILE.. " HTTP/1.0\r\n\r\n")
        c:close()
    end,
    INSTALL = function()
        -- Install instructions go here.
    end,
    REMOVE = function()
        -- Remove instructions go here.
    end
}
dofile("repo.lua")
if REPO[PKG.NAME][1] ~= "standby-installed" and REPO[PKG.NAME][1] == "install" then
    PKG.GET()
    PKG.INSTALL()
elseif REPO[PKG.NAME][1] == "remove" then
    PKG.REMOVE()
end

Repository Table
Code:
--[[
    REPO Information.
    States:
        install, remove, standby and standby-installed

        Descriptions:
            install = Will automatically install on next server startup.
            remove = Will automatically remove on next server startup.
            standby = Awaits command to install or remove.
            standby-installed = Installed package state.
          
        State Changes:
            install > standby-installed
            remove > standby
            standby remains standby untill issued the install command, which will turn to standby-installed.
            standby-installed > standby
]]--

REPO = {
    ["Package Name"] = {"install", "http://HOSTNAME.net/PACKAGE_NAME/PACKAGE.INSTALL"}
}

INSTALL Library (Still not complete)
Code:
if os.execute("[ -z \"/usr/bin/luarocks\" ]") ~= 0 then
    os.execute("wget \"http://luarocks.org/releases/luarocks-2.0.11.tar.gz\" && tar -xzf luarocks-2.0.11.tar.gz && cd luarocks-2.0.11/ && sh configure && make && su -c \"make install\" && echo \"LuaRocks installed.\"")
    if os.execute("[ -z \"/usr/lib/lua/5.1/lfs.so\" ]") ~= 0 then
        os.execute("[ `su -c \"luarocks install luafilesystem && ln -s /usr/local/lua/5.1/lfs.so /usr/lib/lua/5.1/lfs.so\" ] || printf \">> Error installing or linking .so file.\"")
    end
end

lfs = require "lfs"

fs = {
    Exist = function(path)
        return (os.execute("[ -d " .. path .. " ] || [ -f " .. path .. " ]") == 0 and true or false)
    end,
    Dir = function(path)
        path = (path ~= nil and lfs.dir(path) or lfs.dir(lfs.currentdir()))
        for file in path do
            if file ~= "." and ".." then
                local mode = (lfs.attributes(path..'/'..file, "mode")
                print(file.." >> "..mode)
            end
        end
    end,
    DirSearch = function(search, path)
        path = (path ~= nil and lfs.dir(path) or lfs.dir(lfs.currentdir()))
        for file in path do
            if search == file then
                print("Found ".. lfs.attributes(path..'/'..file, "mode").. " \""..file.."\".")
                return true
            end
        end
    end,
    Write = {
        Tag = function(file, tag, comment, tagClose) -- Credits to Wake's Libraries.
            local f = io.open(file, "r")
            local content = f:read("*all")
            local content, _ = content:gsub(tagClose, tag .. "<!-- " .. comment .. " -->\n" .. tagClose)
            f:close()

            local fi = io.open(file, "w+")
            fi:write(content)
            fi:close()
            return true
        end,
        Content = function(file, content)
            -- TODO

        end
    },
    Remove = function(path)
        if os.execute(" [ -d \""..path.."\" ]") == 0 then
            lfs.rmdir(path)
            return true
        elseif os.execute(" [ -f \""..path.."\" ]") == 0 then
            os.execute("rm \""..path.."\"")
            return true
        end
    return false
    end,
    Create = function(file, content, overwrite)
        if fs.Exist(file) then
            if content ~= nil then
                if overwrite == true then
                    os.execute("echo -e " .. content .. " > " .. file)
                end
            end
        elseif content ~= nil then
            os.execute("echo -e " .. content .. " > " .. file)
        else
            os.execute("echo > "..file)
        end
    end,
    Move = function(Ifile, Ofile)
        return os.execute("mv "..Ifile.." "..Ofile)
    end,
    Copy = function(Ifile, Ofile, recursive)
        return os.execute("cp "..(recursive == true and "-R " or "") and Ifile.." "..Ofile)
    end,
    Wget = function(url)
        os.execute("wget \""..url.."\"")
    end,
    Tar = function(file, parameters)
        os.execute("tar "..parameters.." "..file)
    end
}
function repo(arg, package) -- TODO
    if isInArray({"install", "-i"}, arg) then
        fs.Wget(REPO[package][2])
        dofile(package..".install")
    end
end


if fs.DirSearch("repo.lua") == true then
    dofile("repo.lua")
    for i = 1, #REPO do
        if REPO[1] == "install" then
            fs.Wget(REPO[2])
            dofile(REPO..".install")
        elseif REPO[1] == "remove" then
            fs.Wget(REPO[2])
            dofile(REPO..".install")
        end
    end          
end
I need someone that knows better Lua than me to look at this and tell me if I'm doing it right, because I'm not testing it.

All instructions goes into "INSTALL" function.

Added Repo and change the main Install libraries.(using Luarocks and LuaFileSystem)

Tell me what you guys think? it will have libraries to handle most actions with ease.
 
Last edited:
This would be really useful, I'd like to see a completed version and I would use it for sure
 
Cykotitan maybe sn4ake can support you with the best they have, they are the best on OtLand, I guess.

@System,
I'd use it for sure, love the idea tho, Good luck.


Regards,
H4CK3R
 
The function definition in a table style is probably not what you want, let me guess, the way you are doing it would be called like this:
Code:
INSTALL.GET[1](params_go_here)
what you probably want is:
Code:
INSTALL = {
...
GET = function(...)
...
end
}
 
The function definition in a table style is probably not what you want, let me guess, the way you are doing it would be called like this:
Code:
INSTALL.GET[1](params_go_here)
what you probably want is:
Code:
INSTALL = {
...
GET = function(...)
...
end
}

Will correct it!
 
To be honest, if I were you, I would write this in C(++) not just because of lua limitations but lua is not exactly for stuff like this, therefore it _will_ be slow.
 
To be honest, if I were you, I would write this in C(++) not just because of lua limitations but lua is not exactly for stuff like this, therefore it _will_ be slow.

lol, you know me and C/C++ don't shake hands too well!
 
Code:
    Exist = function(FILE)
        return (io.open(FILE, "r")) and true or false
    end,
you're not closing the file handle, therefore it's a leak
Code:
    Write = {
        Tag = function([COLOR="#FF0000"]FILE[/COLOR], tag, comment, tagClose)
            local f = io.open([COLOR="#FF0000"]file[/COLOR], "r")
wrong case?:p

Code:
    Copy = function(IFILE, OFILE, recursive)
        return os.execute("cp " ..[B](recursive == true and "-R ")[/B].."" .. IFILE .. " " .. OFILE)
    end,
add or "" otherwise it'll try to concatenate false on failure :p
To be honest, if I were you, I would write this in C(++) not just because of lua limitations but lua is not exactly for stuff like this, therefore it _will_ be slow.
how much slower are lua syscalls and what are the other downsides?
i don't think install performance is going to matter or even differ noticeably, this is a premature optimization
 
Last edited:
Code:
    Exist = function(FILE)
        return (io.open(FILE, "r")) and true or false
    end,
you're not closing the file handle, therefore it's a leak
Code:
    Write = {
        Tag = function([COLOR="#FF0000"]FILE[/COLOR], tag, comment, tagClose)
            local f = io.open([COLOR="#FF0000"]file[/COLOR], "r")
wrong case?:p

Code:
    Copy = function(IFILE, OFILE, recursive)
        return os.execute("cp " ..[B](recursive == true and "-R ")[/B].."" .. IFILE .. " " .. OFILE)
    end,
add or "" otherwise it'll try to concatenate false on failure :p

how much slower are lua syscalls and what are the other downsides?
i don't think install performance is going to matter or even differ noticeably, this is a premature optimization

Fixed all those mishaps, I'll keep in mind to always close file handles.
 
Code:
    Exist = function(FILE)
        f = io.open(FILE, "r")
dawaj na local:p
 
Bump.gif


I think it should already be good for practical use.
Still needs a few things to be done, since I'm not a pro at this.
 
Still needs a few things to be done, since I'm not a pro at this.

Well, if you ever decide to make an improved version with C++, feel free to hit me up, you got my Skype, we can figure something out together :p

how much slower are lua syscalls and what are the other downsides?
i don't think install performance is going to matter or even differ noticeably, this is a premature optimization

Requires luasocket/luafs though while it can be easily done with simple socket and GET message.
 
Well, if you ever decide to make an improved version with C++, feel free to hit me up, you got my Skype, we can figure something out together :p

like make a native extension for TFS core files?
if so I'll think about it, I'm reading up C and I'm about to go to C++ like in a week or so.
 
I don't like these ugly uses of os.execute <,< if you REALLY want to make it in lua then I'd suggest writing a small lua bindings file in C(++) then using these bindings in lua, this will make it a lot easier porting it to other platforms (Since you're currently targetting Linux only).
 
I don't like these ugly uses of os.execute <,< if you REALLY want to make it in lua then I'd suggest writing a small lua bindings file in C(++) then using these bindings in lua, this will make it a lot easier porting it to other platforms (Since you're currently targetting Linux only).

I think I might make it in C++ so it can go native with TFS's core. My C++ Tutorial are doing good.
 
Back
Top