Filesystem
Functions for reading, writing, deleting, and managing local files and buffers.
fs.does_file_exist
Signature: fs.does_file_exist(file_name)
Description: Checks if a file exists at the specified path.
Parameters:
file_name (string): The path of the file to check.
Returns:
boolean:
true
if the file exists,false
otherwise.
Example:
-- Check if a configuration file exists
local config_file = "settings.json"
if fs.does_file_exist(config_file) then
engine.log("Config file found!", 0, 255, 0, 255)
-- Load the configuration
local config_data = fs.read_from_file(config_file)
-- Process config_data...
else
engine.log("Config file not found, creating default...", 255, 255, 0, 255)
-- Create a default configuration file
fs.write_to_file(config_file, "{ \"setting1\": true, \"setting2\": 100 }")
end
fs.read_from_file
Signature: fs.read_from_file(file_name)
Description: Reads data from a file as a string.
Parameters:
file_name (string): The path of the file to read.
Returns:
string: The contents of the file as a string, or
nil
if the file cannot be read.
Example:
-- Read data from a text file
function load_text_file(file_path)
if not fs.does_file_exist(file_path) then
engine.log("Error: File does not exist: " .. file_path, 255, 0, 0, 255)
return nil
end
local data = fs.read_from_file(file_path)
if data then
engine.log("Successfully read " .. #data .. " bytes from " .. file_path, 0, 255, 0, 255)
return data
else
engine.log("Error: Failed to read file: " .. file_path, 255, 0, 0, 255)
return nil
end
end
-- Usage example
local log_content = load_text_file("logs/app.log")
if log_content then
-- Process log content
local line_count = select(2, string.gsub(log_content, "\n", "")) + 1
engine.log("Log file contains " .. line_count .. " lines", 255, 255, 255, 255)
end
fs.write_to_file
Signature: fs.write_to_file(file_name, data)
Description: Writes data to a file as a string. If the file already exists, it will be overwritten.
Parameters:
file_name (string): The path of the file to write.
data (string): The data to write to the file.
Returns:
boolean:
true
if the write operation was successful,false
otherwise.
Example:
-- Save application settings to a file
function save_settings(settings_table)
-- Convert the settings table to a JSON string
local settings_json = json.stringify(settings_table)
-- Write to the settings file
local success = fs.write_to_file("settings.json", settings_json)
if success then
engine.log("Settings saved successfully", 0, 255, 0, 255)
else
engine.log("Failed to save settings", 255, 0, 0, 255)
end
return success
end
-- Usage example
local settings = {
volume = 75,
fullscreen = true,
resolution = "1920x1080",
username = engine.get_username()
}
save_settings(settings)
fs.delete_file
Signature: fs.delete_file(file_name)
Description: Deletes the specified file.
Parameters:
file_name (string): The path of the file to delete.
Returns:
boolean:
true
if the file was successfully deleted,false
otherwise.
Example:
-- Delete a temporary file
function cleanup_temp_file(temp_file_path)
if fs.does_file_exist(temp_file_path) then
local success = fs.delete_file(temp_file_path)
if success then
engine.log("Temporary file deleted: " .. temp_file_path, 0, 255, 0, 255)
else
engine.log("Failed to delete temporary file: " .. temp_file_path, 255, 0, 0, 255)
end
return success
else
engine.log("Temporary file does not exist: " .. temp_file_path, 255, 255, 0, 255)
return true -- Consider it a success if the file doesn't exist
end
end
-- Usage example
cleanup_temp_file("temp/cache.dat")
fs.write_to_file_from_buffer
Signature: fs.write_to_file_from_buffer(file_name, buffer_handle)
Description: Writes all contents of a memory buffer to a file.
Parameters:
file_name (string): The path of the file to write.
buffer_handle (handle): A handle to the memory buffer containing the data to write.
Returns:
boolean:
true
if the write operation was successful,false
otherwise.
Example:
-- Download and save an image file
function download_and_save_image(url, save_path)
-- Register a callback for when the network response is received
engine.register_on_network_callback(function(response_data, response_url)
if response_url == url then
-- Write the response buffer directly to a file
local success = fs.write_to_file_from_buffer(save_path, response_data)
if success then
engine.log("Image downloaded and saved to: " .. save_path, 0, 255, 0, 255)
else
engine.log("Failed to save image to: " .. save_path, 255, 0, 0, 255)
end
end
end)
-- Send the HTTP request to download the image
net.send_request(url, "User-Agent: MyApp/1.0", nil)
engine.log("Downloading image from: " .. url, 255, 255, 255, 255)
end
-- Usage example
download_and_save_image("https://example.com/image.jpg", "downloads/image.jpg")
fs.read_from_file_to_buffer
Signature: fs.read_from_file_to_buffer(file_name, buffer_handle)
Description: Reads all file contents into a memory buffer.
Parameters:
file_name (string): The path of the file to read.
buffer_handle (handle): A handle to the memory buffer where the file contents will be stored.
Returns:
boolean:
true
if the read operation was successful,false
otherwise.
Example:
-- Read an image file into a buffer and create a bitmap from it
function load_image_to_buffer(image_path)
if not fs.does_file_exist(image_path) then
engine.log("Image file does not exist: " .. image_path, 255, 0, 0, 255)
return nil
end
-- Get the file size to allocate the correct buffer size
local file_size = fs.get_file_size(image_path)
if file_size <= 0 then
engine.log("Invalid file size for: " .. image_path, 255, 0, 0, 255)
return nil
end
-- Allocate a buffer to hold the file contents
local buffer = m.alloc(file_size)
if not buffer then
engine.log("Failed to allocate buffer for file: " .. image_path, 255, 0, 0, 255)
return nil
end
-- Read the file into the buffer
local success = fs.read_from_file_to_buffer(image_path, buffer)
if not success then
engine.log("Failed to read file into buffer: " .. image_path, 255, 0, 0, 255)
m.free(buffer)
return nil
end
engine.log("Successfully loaded " .. file_size .. " bytes from " .. image_path, 0, 255, 0, 255)
return buffer
end
-- Usage example
local image_buffer = load_image_to_buffer("images/texture.png")
if image_buffer then
-- Create a bitmap from the buffer for rendering
local bitmap = render.create_bitmap_from_buffer(image_buffer)
-- Clean up the buffer when done
m.free(image_buffer)
end
fs.get_file_size
Signature: fs.get_file_size(file_name)
Description: Gets the size of a file in bytes.
Parameters:
file_name (string): The path of the file.
Returns:
number: The size of the file in bytes, or 0 if the file does not exist or cannot be accessed.
Example:
-- Check if a file is empty or exceeds a size limit
function check_file_size(file_path, size_limit)
if not fs.does_file_exist(file_path) then
engine.log("File does not exist: " .. file_path, 255, 0, 0, 255)
return false
end
local file_size = fs.get_file_size(file_path)
if file_size == 0 then
engine.log("File is empty: " .. file_path, 255, 255, 0, 255)
return false
end
if size_limit and file_size > size_limit then
engine.log("File exceeds size limit (" .. file_size .. " bytes): " .. file_path, 255, 0, 0, 255)
return false
end
engine.log("File size: " .. file_size .. " bytes", 0, 255, 0, 255)
return true
end
-- Usage example
check_file_size("data/large_file.dat", 1024 * 1024) -- Check if file is under 1 MB
fs.compress
Signature: fs.compress(string)
Description: Compresses a Lua string using a compression algorithm.
Parameters:
string (string): The string data to compress.
Returns:
string: The compressed data as a string, or
nil
if compression failed.
Example:
-- Compress and save a large text document
function compress_and_save_text(text, file_path)
-- Compress the text
local compressed_data = fs.compress(text)
if not compressed_data then
engine.log("Compression failed", 255, 0, 0, 255)
return false
end
-- Log compression ratio
local original_size = #text
local compressed_size = #compressed_data
local ratio = math.floor((compressed_size / original_size) * 100)
engine.log("Compressed " .. original_size .. " bytes to " .. compressed_size ..
" bytes (" .. ratio .. "% of original size)", 0, 255, 0, 255)
-- Save the compressed data to a file
return fs.write_to_file(file_path, compressed_data)
end
-- Usage example
local large_text = "..." -- Imagine a very large string here
compress_and_save_text(large_text, "data/document.compressed")
fs.decompress
Signature: fs.decompress(string)
Description: Decompresses a previously compressed string.
Parameters:
string (string): The compressed data string.
Returns:
string: The decompressed data as a string, or
nil
if decompression failed.
Example:
-- Load and decompress a saved file
function load_and_decompress(file_path)
if not fs.does_file_exist(file_path) then
engine.log("Compressed file not found: " .. file_path, 255, 0, 0, 255)
return nil
end
-- Read the compressed data
local compressed_data = fs.read_from_file(file_path)
if not compressed_data then
engine.log("Failed to read compressed file: " .. file_path, 255, 0, 0, 255)
return nil
end
-- Decompress the data
local decompressed_data = fs.decompress(compressed_data)
if not decompressed_data then
engine.log("Decompression failed for file: " .. file_path, 255, 0, 0, 255)
return nil
end
engine.log("Successfully decompressed data from: " .. file_path, 0, 255, 0, 255)
return decompressed_data
end
-- Usage example
local document_text = load_and_decompress("data/document.compressed")
if document_text then
-- Process the decompressed text
engine.log("Decompressed document size: " .. #document_text .. " bytes", 255, 255, 255, 255)
end
Last updated
Was this helpful?