Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --File: /cattech/file.lua
- defaultConfig = "/cattech/config.hash"
- defaultContinue = "/cattech/continue.hash"
- function downloadNamedFileFromPastebin(pastebinCode)
- local url = "https://pastebin.com/raw/" .. pastebinCode -- Construct the URL
- local response, err = http.get(url) -- Perform the HTTP GET request
- if not response then
- error("Pastebin download " .. pastebinCode .. " error: " .. err)
- else
- local firstLine = response.readLine()
- if not firstLine then
- error("The file is empty or could not be read.")
- else
- if firstLine:sub(1, 8) == "--File: " then
- local filePath = firstLine:sub(9)
- local fileContent = firstLine .. "\n" .. response.readAll()
- local file = fs.open(filePath, "w")
- file.write(fileContent)
- file.close()
- print(pastebinCode .. " -> " .. filePath)
- else
- print("PB " .. pastebinCode .. " No File : " .. firstLine)
- error("Expected first line '--File: filepath'")
- end
- end
- end
- response.close()
- end
- function saveHash(filename, hash)
- hash["error"] = nil
- local file = fs.open(filename, "w") -- Open the file for writing
- if file then
- file.write(textutils.serialize(hash)) -- Serialize the table and write it to the file
- file.close()
- else
- print("Failed writing file : ",filename)
- end
- end
- function loadHash(filename)
- local file = fs.open(filename, "r") -- Attempt to open the file for reading
- local hash = {} -- Initialize an empty hash
- if file then
- local content = file.readAll() -- Read the entire file content
- file.close()
- if content and content ~= "" then
- local loadedHash = textutils.unserialize(content) -- Deserialize the content
- if loadedHash then
- return loadedHash -- Return the loaded hash if successful
- else
- print("Failed to unserialize content. The content may not be a valid serialized table.")
- hash["error"] = "Invalid serialized data."
- end
- else
- print("File is empty.")
- hash["error"] = "File is empty."
- end
- else
- print("Failed to open file for reading.")
- hash["error"] = "File not found."
- end
- return hash -- Return the empty hash with the error message
- end
- function displayHash(hash)
- print("---Hash---")
- for key, value in pairs(hash) do
- print(key .. ": " .. tostring(value))
- end
- print("--- end ---")
- end
- function getContinue(filename)
- local continue = loadHash(defaultContinue)
- return continue[filename]
- end
- function updateContinue(filename,blocksRemaining)
- local continue = loadHash(defaultContinue)
- continue[filename]=blocksRemaining
- saveHash(defaultContinue,continue)
- end
- function readOrSetDefault(hash, key, defaultValue)
- if hash == nil then
- hash = {}
- end
- if hash[key] == nil then
- -- If key does not exist, set it to the default value
- hash[key] = defaultValue
- end
- -- Return the value from the hash
- return hash[key]
- end
- function loadConfigAndDefaults()
- local config = loadHash(defaultConfig)
- readOrSetDefault(config,"stairHeight",5)
- readOrSetDefault(config,"tunnelHeight",3)
- readOrSetDefault(config,"maxSteps",8)
- readOrSetDefault(config,"pathDirection","level")
- readOrSetDefault(config,"replaceDirection","down")
- readOrSetDefault(config,"safeTunnel","yes")
- saveHash(defaultConfig,config)
- return config
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement