iteration
This commit is contained in:
		
							
								
								
									
										79
									
								
								lib/lns.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								lib/lns.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,79 @@
 | 
			
		||||
local cache = {}
 | 
			
		||||
 | 
			
		||||
settings.define("lns_client.allow_dynamic_lns_hosts", {
 | 
			
		||||
    description = "Allow dynamic LNS hosts",
 | 
			
		||||
    type = "boolean",
 | 
			
		||||
    default = false
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
settings.define("lns_client.lns_server", {
 | 
			
		||||
    description = "LNS Server Rednet ID",
 | 
			
		||||
    type = "number",
 | 
			
		||||
    default = 8
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function _load_cache()
 | 
			
		||||
    if fs.exists("lns_cache.db") then
 | 
			
		||||
        db = fs.open("lns_cache.db", "r")
 | 
			
		||||
        db_contents = db.readAll()
 | 
			
		||||
        db.close()
 | 
			
		||||
 | 
			
		||||
        cache = textutils.unserialize(db_contents)
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function _save_cache()
 | 
			
		||||
    db = fs.open("lns_cache.db", "w")
 | 
			
		||||
    db.write(textutils.serialize(cache))
 | 
			
		||||
    db.close()
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function server()
 | 
			
		||||
    local lns_server_id = settings.get("lns_client.lns_server")
 | 
			
		||||
    local allow_dynamic_lns_hosts = settings.get("lns_client.allow_dynamic_lns_hosts")
 | 
			
		||||
 | 
			
		||||
    if allow_dynamic_lns_hosts then
 | 
			
		||||
        lns_server_id = rednet.lookup("lns", lns_server)
 | 
			
		||||
        if lns_server_id == nil then
 | 
			
		||||
            io.write("LNS Server not found!\n")
 | 
			
		||||
            return nil
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    return lns_server_id
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function lookup(remote_hostname)
 | 
			
		||||
 | 
			
		||||
    _load_cache()
 | 
			
		||||
    if cache[remote_hostname] ~= nil then
 | 
			
		||||
        return cache[remote_hostname]
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    local data = {
 | 
			
		||||
        ["action"] = "lookup",
 | 
			
		||||
        ["hostname"] = remote_hostname
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rednet.send(server(), data, "lns")
 | 
			
		||||
 | 
			
		||||
    while true do
 | 
			
		||||
        id, msg = rednet.receive("lns")
 | 
			
		||||
        if id == lns_server_id then
 | 
			
		||||
            if msg == nil then
 | 
			
		||||
                return nil
 | 
			
		||||
            else
 | 
			
		||||
                cache[remote_hostname] = msg
 | 
			
		||||
                _save_cache()
 | 
			
		||||
                return msg
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function clear_cache()
 | 
			
		||||
    cache = {}
 | 
			
		||||
    _save_cache()
 | 
			
		||||
    print("Cleared cache!")
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										22
									
								
								lib/lum.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								lib/lum.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
local target_branch = "main"
 | 
			
		||||
local bin_path = "/bin/"
 | 
			
		||||
local lib_path = "/lib/"
 | 
			
		||||
local startup_path = "/startup/"
 | 
			
		||||
 | 
			
		||||
function download_lua(remote_path, local_path)
 | 
			
		||||
    response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil)
 | 
			
		||||
    if response.getResponseCode() == 200 then
 | 
			
		||||
        file = fs.open(local_path, "w")
 | 
			
		||||
        file.write(response.readAll())
 | 
			
		||||
        file.close()
 | 
			
		||||
    end
 | 
			
		||||
    else
 | 
			
		||||
        io.write("Failed to update " .. program_name .. ".lua\n")
 | 
			
		||||
        io.write(response) 
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function install(package)
 | 
			
		||||
    io.write("Installing " .. package .. "\n")
 | 
			
		||||
    download_lua("packages/" .. package .. ".lua", bin_path .. package .. ".lua")
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user