LokFileSystemUtil¶
Inherits:
The LokFileSystemUtil class provides utilities to deal with the file system.
Description¶
The objective of this class is to help with boilerplate code when manipulating files or directories in the file system.
Version: 1.0.0 Author: Daniel Sousa (github.com/nadjiel <github.com/nadjiel>_)
Method Index¶
Method Descriptions¶
void push_error_directory_creation_failed(String path, int error_code)¶
The push_error_directory_creation_failed method is used to push an error when a directory creation fails.
The path of where the creation was tried is expected in the parameters together with an error_code indicating what kind of error happened.
void push_error_file_writing_or_creation_failed(String path, int error_code)¶
The push_error_file_writing_or_creation_failed method is used to push an error when a file creation or writing operation fails.
The path of where the operation was tried is expected in the parameters together with an error_code indicating what kind of error happened.
void push_error_directory_not_found(String path)¶
The push_error_directory_not_found method is used to push an error warning that the path doesn’t point to an existing directory.
void push_error_file_not_found(String path)¶
The push_error_file_not_found method is used to push an error warning that the path doesn’t point to an existing file.
void push_error_file_reading_failed(String path, int error_code)¶
The push_error_file_reading_failed method is used to push an error when a file reading fails.
The path of where the operation was tried is expected in the parameters together with an error_code indicating what kind of error happened.
void push_error_json_parse_failed(JSON json, int error_code)¶
The push_error_json_parse_failed method is used to push an error when a JSON parsing fails.
The json argument is the JSON instance that failed and the error_code argument represents the @GlobalScope.Error that occured.
void push_error_directory_or_file_removal_failed(String path, int error_code)¶
The push_error_directory_or_file_removal_failed method is used to push an error when a directory or file removal fails.
The path where the removal was tried is expected in the parameters together with an error_code indicating what kind of error happened.
int create_directory(String path)¶
The create_directory method creates a new directory in the path specified by the path parameter.
If an error occurs, this method pushes it, either way, this method returns an @GlobalScope.Error code specifying the success of the operation.
int create_directory_if_not_exists(String path)¶
The create_directory_if_not_exists method uses the directory_exists and create_directory methods to create a directory only if it doesn’t already exist.
If it already exists, this method returns the ERR_ALREADY_EXISTS error.
bool directory_exists(String path)¶
The directory_exists method checks if a directory exists in the path specified by the path parameter and returns a bool indicating the result.
PackedStringArray get_file_names(String path, String <String>`[] formats = ``[]`)¶
The get_file_names method scans the files of a directory in a given path and returns their names in a PackedStringArray.
The formats parameter is used to filter what file formats should be included in the final result (without the “.”).
If this parameter is left as default, that means all file formats are included.
If the path doesn’t point to an existing directory, an error is pushed and the method returns an empty PackedStringArray.
PackedStringArray get_subdirectory_names(String path)¶
The get_subdirectory_names method returns the names of the subdirectories of a directory in a given path in a PackedStringArray.
If the path doesn’t point to an existing directory, an error is pushed and the method returns an empty PackedStringArray.
bool directory_is_empty(String path)¶
The directory_is_empty method looks for the files and subdirectories in the directory in the path and tells whether that directory is empty or not.
If the path doesn’t point to an existing directory, an error is pushed and the method returns true, indicating that the directory is empty because it doesn’t exist.
int remove_directory_or_file(String path)¶
The remove_directory_or_file method removes a directory or file from the path specified by the path parameter.
If path points to a directory that isn’t empty this method won’t succed, so make sure to empty it first.
If an error occurs, this method pushes it, either way, this method returns an @GlobalScope.Error code specifying the success of the operation.
int remove_directory_recursive(String path)¶
The remove_directory_recursive method removes a directory from the path specified by the path parameter, making sure to remove all the subdirectories or files within it.
If an error occurs during the operation, this method pushes it and cancels, either way, this method returns an @GlobalScope.Error code specifying the success of the operation.
int remove_directory_if_exists(String path)¶
The remove_directory_if_exists method uses the directory_exists and remove_directory_or_file methods to remove a directory only if exists.
If it doesn’t exist, this method returns the ERR_DOES_NOT_EXIST error.
int remove_directory_recursive_if_exists(String path)¶
The remove_directory_recursive_if_exists method uses the directory_exists and remove_directory_or_file methods to remove a directory only if exists.
If it doesn’t exist, this method returns the ERR_DOES_NOT_EXIST error.
String get_directory_name(String directory_path)¶
The get_directory_name method is a utility method that grabs the name of a directory from a directory_path.
int write_or_create_file(String path, String content = "")¶
The write_or_create_file method creates a new file in the path specified by the path parameter, if it doesn’t already exists, else, it simply writes in that file.
Optionally, this method can receive a content parameter that defines what should be written in the file.
If an error occurs during the operation, this method pushes it and cancels, either way, this method returns an @GlobalScope.Error code specifying the success of the operation.
int write_or_create_encrypted_file(String path, String encryption_pass, String content = "")¶
The write_or_create_encrypted_file method creates a new file in the path specified by the path parameter, if it doesn’t already exist, else, it simply writes in that file using encryption.
The encryption_pass parameter is used as the password to encrypt the contents of the file.
Optionally, this method can receive a content parameter that defines what should be written in the file.
If an error occurs during the operation, this method pushes it and cancels, either way, this method returns an @GlobalScope.Error code specifying the success of the operation.
int create_file_if_not_exists(String path)¶
The create_file_if_not_exists method uses the file_exists and write_or_create_file methods to create a file only if it doesn’t already exist.
If it already exists, this method returns the ERR_ALREADY_EXISTS error.
int create_encrypted_file_if_not_exists(String path, String encryption_pass, String content = "")¶
The create_encrypted_file_if_not_exists method uses the file_exists and write_or_create_encrypted_file methods to create an encrypted file only if it doesn’t already exist.
If it already exists, this method returns the ERR_ALREADY_EXISTS error.
bool file_exists(String path)¶
The file_exists method checks if a file exists in the path specified by the path parameter and returns a bool indicating the result.
String read_file(String path)¶
The read_file method reads from a file in the path specified by the path parameter.
If an error occurs, this method pushes it and returns "", otherwise, it returns the String read from the file.
String read_encrypted_file(String path, String encryption_pass, bool suppress_errors = false)¶
The read_encrypted_file method reads from a encrypted file in the path specified by the path parameter.
The encryption_pass parameter is used as the password to decrypt the contents of the file.
If an error occurs, this method pushes it and returns "", otherwise, it returns the String read from the file.
If the suppress_errors is true, though, no errors are pushed, except for errors that come from the FileAccess.open_encrypted_with_pass method.
Variant parse_json_from_string(String string, bool suppress_errors)¶
The parse_json_from_string method can be used to parse a String into a Dictionary using a JSON instance.
If the parsing fails, an error is pushed and an empty Dictionary is returned.
If the suppress_errors is true, though, no errors are pushed.
Resource directory_path, String resource_type = "")¶
The load_resources method is a quick way to load all Resource <Resource>`s located in a specific ``directory_path`.
Optionally, a resource_type String can be passed to filter what types of Resource <Resource>`s should be loaded or to prevent unknown file types from being loaded.
As an example, if a `”Script”`` String is passed in the resource_type parameter, only files with formats that can represent Script types are dictated by the ResourceLoader.get_recognized_extensions_for_type method.
int remove_file_if_exists(String path)¶
The remove_file_if_exists method uses the file_exists and remove_directory_or_file methods to remove a file only if exists.
If it doesn’t exist, this method returns the ERR_DOES_NOT_EXIST error.
String join_file_name(String file_prefix, String file_format)¶
The join_file_name method takes a file_prefix String and a file_format String and joins them together with a "." in the middle, so that they form a complete file_name.
String get_file_name(String file_path)¶
The get_file_name method is a utility method that grabs the name of a file from a file_path, including its format.
String get_file_format(String file_name)¶
The get_file_format method is a utility method that grabs the format of a file from a file_name.
The return of this method doesn’t include the "." of the format.
String get_file_prefix(String file_name)¶
The get_file_prefix method is a utility method that grabs the name of a file without its format.
The return of this method doesn’t include the "." of the format.