LokAccessStrategy¶
Inherits:
The LokAccessStrategy super class is responsible for defining how the writing and reading from files should be performed.
Description¶
This class should have its _save_partition and _load_partition methods overriden in order to provide concrete implementations for the saving and loading functionalities. The LokJSONAccessStrategy and LokEncryptedAccessStrategy classes are two strategies built-in to the LockerPlugin, but if you want, you can define your own access strategies inheriting from the LokAccessStrategy class. If you need to deal with the file system when inheriting this class, the LokFileSystemUtil class provides lots of static methods that help with decreasing the boilerplate code needed for that.
Version: 1.0.0 Author: github.com/nadjiel <github.com/nadjiel>_
Method Index¶
Method Descriptions¶
Dictionary create_result(Dictionary data = {}, int status = 0)¶
The create_result method helps with the creation of a Dictionary representing the result of an operation done by this LokAccessStrategy.
The returned Dictionary has two keys: the "status", which stores a @GlobalScope.Error code, and the "data", which stores a Dictionary with the resultant data of an operation.
Dictionary get_saved_files_ids(String files_path)¶
The get_saved_files_ids method returns a result Dictionary (with the same structure of the one created by the create_result) whose "data" field stores an Array of String <String>`s with the ids of all files saved in the ``files_path`.
Dictionary save_data(String file_path, String file_format, Dictionary data, bool replace = false)¶
The save_data method uses the _save_partition to save the information provided through the data Dictionary in their respective partitions.
The file_path parameter should specify the path to the folder where the data is to be saved and the file_format specifies what’s the format of the files that compose the data saved (such format shouldn’t include the ".").
Optionally, the replace parameter can be passed to tell if the data should override any already existent data.
The structure that the data Dictionary should have is as follows:
.. codeblock:
{
"partition_id_1": {
"accessor_id_1": {
...
"version": <String> (optional)
},
"accessor_id_n": { ... },
},
"partition_id_n": { ... }
}
The return of this method is a Dictionary with a "status" field representing the status of the operation and a "data" field with the data that was saved. That Dictionary follows the same structure as the one in returned by the load_data method.
Dictionary load_data(String file_path, String file_format, String <String>`[] partition_ids = ``[]`, String <String>`[] accessor_ids = ``[]`, String <String>`[] version_numbers = ``[]`)¶
The load_data method uses the _load_partition method to load the information from the save directory in the file_path.
The file_format parameter specifies from what file format the data should be read (such format shouldn’t include the ".").
Optionally, a partition_ids parameter can be passed to specify from what partitions the data should be loaded.
Also, accessor_ids and version_numbers can be passed to filter even more what information to bring back.
If left as default, that means all partitions, accessors, and versions are read, which corresponds to all data from the save file.
After completing the loading, this method returns a Dictionary containing all data obtained. Its format is as follows:
.. codeblock:
{
"status": <@GlobalScope.Error>,
"data": {
"accessor_id_1": {
...
"version": <String> (optional),
"partition": <String>
},
"accessor_id_n": { ... }
}
}
If an error occurs, the corresponding @GlobalScope.Error code is returned in the "status" field of the Dictionary.
Dictionary remove_data(String file_path, String file_format, String <String>`[] partition_ids = ``[]`, String <String>`[] accessor_ids = ``[]`, String <String>`[] version_numbers = ``[]`)¶
The remove_data method uses the _remove_partition method to remove the save directory in the file_path, or some of its data.
The file_format parameter specifies from what file format the data should be removed (such format shouldn’t include the ".").
Optionally, a partition_ids parameter can be passed to specify from what partitions the data should be removed.
Also, accessor_ids and version_numbers can be passed to filter even more what information to remove.
If left as default, that means all partitions, accessors, and versions are removed, which corresponds to all data from the save file.
After completing the removal, this method returns a Dictionary containing all data obtained. That Dictionary brings the removed data in the "data" field and the data the wasn’t removed stays in the "updated_data" field. The format of the returned Dictionary is shown in more details below:
.. codeblock:
{
"status": <@GlobalScope.Error>,
"data": {
"accessor_id_1": {
...
"version": <String> (optional),
"partition": <String>
},
"accessor_id_n": { ... }
},
"updated_data": { ... }
}
If an error occurs, the corresponding @GlobalScope.Error code is returned in the "status" field of the Dictionary.
String _get_partition_name(String file_path, String partition_id, String file_format)¶
The _get_partition_name method receives a file_path, a partition_id and a file_format, all of which are String representing the name of the partition represented by those data.
The partition name here refers to the file name of the partition with the format suffix.
Example:
.. codeblock:
var partition_name: String = _get_partition_name(
"res://saves/file_1", "partition_1", "sav"
)
# This would return "partition_1.sav"
In the case the partition_id is an empty String, this method considers it as being a partition with the same name as its file, so in the previous example, if the partition_id was "", the result would be "file_1.sav".
String file_path, String file_format, String <String>`[] wanted_ids = ``[]`)¶
The _get_partitions method searches the names of the partitions in a given file_path with a specific file_format.
Only the partitions with id specified in the wanted_ids are brought in the result. If that parameter in empty, though, all partitions found are returned.
Dictionary _filter_data(Dictionary data, String <String>`[] accessor_ids = ``[]`, String <String>`[] partition_ids = ``[]`, String <String>`[] version_numbers = ``[]`)¶
The _filter_data method receives a data Dictionary other parameters that serve as filters for which entries of that Dictionary should be kept.
The filter parameters are the accessor_ids, partition_ids and the version_numbers. All of these are Array of String <String>`s that identify the entries of the ``data` that should be kept in the Dictionary returned by this method.
To work properly, this method expects that the data parameter follows the structure:
.. codeblock:
{
"accessor_id_1": {
...
"partition": <String>,
"version": <String> (optional)
},
"accessor_id_n": { ... }
}
Dictionary _append_partition_to_data(Dictionary data, String partition_id)¶
The _append_partition_to_data method receives a data Dictionary and a partition_id String. The data parameter must be a Dictionary with other Dictionary <Dictionary>`s as its values, so that this method can set that ``partition_id` as the value of a "partition" key in each of those sub dictionaries.
String _get_file_id(String file_name)¶
The _get_file_id method returns a String with the id of a file that has file_name as its name.
If the file has no "_", its entire name is its id, else, its id is considered to be the part after the first "_".
Dictionary _remove_partition(String partition_path, String <String>`[] accessor_ids = ``[]`, String <String>`[] version_numbers = ``[]`)¶
The _remove_partition method removes data from the partition specified by the partition_path parameter.
At the end, this method returns a Dictionary with the data obtained. The format of that Dictionary follows the same structure as the one returned by the remove_data method.
Dictionary _save_partition(String _partition_path, Dictionary _data, bool _replace = false)¶
The _save_partition method should be overwritten so that it saves data in the partition specified by the partition_path parameter.
Optionally, the replace parameter can be passed to tell if the data should override any already existent data.
The format of the data Dictionary should follow the structure below:
.. codeblock:
{
"accessor_id_1": {
...
"version": <String> (optional)
},
"accessor_id_n": { ... },
}
Dictionary _load_partition(String _partition_path)¶
The _load_partition method should be overwritten so that it loads data from the partition specified by the partition_path parameter.
At the end, this method should return a Dictionary with the data obtained. The format of that Dictionary should follow the same structure as the one returned by the load_data method.