Advertisement
posicat

/cattech/file.lua

Sep 11th, 2024 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. --File: /cattech/file.lua
  2.  
  3. defaultConfig = "/cattech/config.hash"
  4. defaultContinue = "/cattech/continue.hash"
  5.  
  6. function downloadNamedFileFromPastebin(pastebinCode)
  7. local url = "https://pastebin.com/raw/" .. pastebinCode -- Construct the URL
  8. local response, err = http.get(url) -- Perform the HTTP GET request
  9.  
  10. if not response then
  11. error("Pastebin download " .. pastebinCode .. " error: " .. err)
  12. else
  13. local firstLine = response.readLine()
  14. if not firstLine then
  15. error("The file is empty or could not be read.")
  16. else
  17. if firstLine:sub(1, 8) == "--File: " then
  18. local filePath = firstLine:sub(9)
  19. local fileContent = firstLine .. "\n" .. response.readAll()
  20.  
  21. local file = fs.open(filePath, "w")
  22. file.write(fileContent)
  23. file.close()
  24. print(pastebinCode .. " -> " .. filePath)
  25. else
  26. print("PB " .. pastebinCode .. " No File : " .. firstLine)
  27. error("Expected first line '--File: filepath'")
  28. end
  29. end
  30. end
  31.  
  32. response.close()
  33. end
  34.  
  35. function saveHash(filename, hash)
  36. hash["error"] = nil
  37.  
  38. local file = fs.open(filename, "w") -- Open the file for writing
  39. if file then
  40. file.write(textutils.serialize(hash)) -- Serialize the table and write it to the file
  41. file.close()
  42. else
  43. print("Failed writing file : ",filename)
  44. end
  45. end
  46.  
  47. function loadHash(filename)
  48. local file = fs.open(filename, "r") -- Attempt to open the file for reading
  49. local hash = {} -- Initialize an empty hash
  50.  
  51. if file then
  52. local content = file.readAll() -- Read the entire file content
  53. file.close()
  54.  
  55. if content and content ~= "" then
  56. local loadedHash = textutils.unserialize(content) -- Deserialize the content
  57.  
  58. if loadedHash then
  59. return loadedHash -- Return the loaded hash if successful
  60. else
  61. print("Failed to unserialize content. The content may not be a valid serialized table.")
  62. hash["error"] = "Invalid serialized data."
  63. end
  64. else
  65. print("File is empty.")
  66. hash["error"] = "File is empty."
  67. end
  68. else
  69. print("Failed to open file for reading.")
  70. hash["error"] = "File not found."
  71. end
  72.  
  73. return hash -- Return the empty hash with the error message
  74. end
  75.  
  76. function displayHash(hash)
  77. print("---Hash---")
  78. for key, value in pairs(hash) do
  79. print(key .. ": " .. tostring(value))
  80. end
  81. print("--- end ---")
  82. end
  83.  
  84. function getContinue(filename)
  85. local continue = loadHash(defaultContinue)
  86. return continue[filename]
  87. end
  88.  
  89. function updateContinue(filename,blocksRemaining)
  90. local continue = loadHash(defaultContinue)
  91. continue[filename]=blocksRemaining
  92. saveHash(defaultContinue,continue)
  93. end
  94.  
  95. function readOrSetDefault(hash, key, defaultValue)
  96. if hash == nil then
  97. hash = {}
  98. end
  99. if hash[key] == nil then
  100. -- If key does not exist, set it to the default value
  101. hash[key] = defaultValue
  102. end
  103. -- Return the value from the hash
  104. return hash[key]
  105. end
  106.  
  107. function loadConfigAndDefaults()
  108. local config = loadHash(defaultConfig)
  109.  
  110. readOrSetDefault(config,"stairHeight",5)
  111. readOrSetDefault(config,"tunnelHeight",3)
  112. readOrSetDefault(config,"maxSteps",8)
  113. readOrSetDefault(config,"pathDirection","level")
  114. readOrSetDefault(config,"replaceDirection","down")
  115. readOrSetDefault(config,"safeTunnel","yes")
  116.  
  117. saveHash(defaultConfig,config)
  118.  
  119. return config
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement