1. Home
  2. Docs
  3. UniREST Solution 3.5
  4. UniREST Client
  5. Online Files
  6. Save / Load Files

Save / Load Files

The UniREST solution is able to save data into files stored in your Server and load data from those files. The UniRESTClient.Async class contains a File class that allows you to easily perform the basic remote files operations: save and load. Online files management, like deleting or renaming files, is available throught the UniRESTClient.Async.FileManager class.

How it works

The File class works with binary strings, that is a very convenient format. It is lightweight, flexible and can be obtained from different kinds of resources. This means that you can send and receive data (virtually) of any kind. The File class uses the UniRESTBinary class to take advantage of the binary format.

Security

Data transmission travels within the UniREST Solution communication channel, therefore it is subject to encryption / decryption and has the same high level of security as all other types of communication.

Furthermore, the files are saved in PHP format. This means that calling that files by their direct links, their content will be totally invisible and not accessible.


Instantiate a File object

The first thing to do for working with remote files is to declare an instance of the UniRESTClient.Async.File class. This will initialize a file that can be saved and loaded.

§var§ myFileInstance = §new§ #UniRESTClient.Async#.$File$(%fileName, targetFolder%, ^useDataCompression = false^);

fileName|string
The name of the file to work with.
It should be a simple name. Extension is optional and has not a real effect.
It can contain path to subfolders. They will be automatically created if they don’t exist.
__
targetFolder|Enum
Where the file is or will be in your Server:
UserFolder = the logged-in user’s dedicated folder;
GameFolder = the Game folder.
__
useDataCompression|(bool)
Default true.
When true, data compression will be used so as to reduce the use of memory and file size.

File object
A File class object that can be saved and loaded.


Save()

The Save() method saves the given object data into the remote file, placed where indicated in the File instance.

myFileInstance..$Save$(%data, callBack%);

data|object
The data to save.
__
callBack|Action<bool>
The function to call on operation completed.

bool
true if the data is saved, false if an error occurred.

// gameSettings
[System.Serializable]
class MyData 
{
    string playerName = "";
    int health = 0;
}
var gameSettings = new MyData();

. . .

// Instance of a file named 'game_settings' that is or will be in the Game Folder.
var myFileInstance = UniRESTClient.Async.File("game_settings", UniRESTClient.GameFolder);

// Saving of the 'gameSettings' variable, containing the data to save.
_ = myFileInstance.Save(
        gameSettings,
        (bool ok) =>
        {
            // Checking if everything's ok.
            if (ok) Debug.Log("File saved!"); else Debug.LogError("File not saved!");
        }
);

Load()

The Load() method loads data from a remote file, placed where indicated in the File instance.

myFileInstance..$Load$<£T£>(%callBack%);

T|Data-type
The data-type of the file’s content. It can be a common data-type or a custom class.
__
callBack|Action<T, bool>
The function to call on operation completed.

T
The file’s content converted in the given T data-type.
bool
true if the data is loaded, false if an error occurred.

// gameSettings
[System.Serializable]
class MyData 
{
    string playerName = "";
    int health = 0;
}

. . .

// Instance of a file named 'game_settings' that is or will be in the Game Folder.
var myFileInstance = UniRESTClient.Async.File("game_settings", UniRESTClient.GameFolder);

// Loading the 'gameSettings' variable from that onlie file.
_ = myFileInstance.Load(
        (MyData settings, bool ok) =>
        {
            // Some code here...
        }
);

FileError property

The File instance has a string property called FileError. If the Save() or Load() methods return an error, the error code is stored in this property.

myFileInstance.FileError;
Error codeDescription
ERROR_FILE_SAVEThe file can’t be saved online.
FILE_NOT_EXISTSThe file doesn’t exists.
NO_AUTHSystem authorization error.