# DataSaver

This class provides static methods for saving data to PlayerPrefs (securely and unsecurely), unsecurely to JSON files or securely to \*.data files and methods for loading data from there.

### Available methods

```csharp
public static bool SavePrefsSecurely<T>(string key, T data)
```

*This method saves custom **data** of type **T** into PlayerPrefs with certain **key**. In case if data has not serializable type, it will not be saved and method will return **false**. This method encrypts data by AES* symmetric algorithm.

*Example:*

```csharp
if (!DataSaver.SavePrefsSecurely("CurrenciesState", currenciesState))
{
    Debug.Log("Cannot serialize currenciesState!");
}
```

***

```csharp
public static bool LoadPrefsSecurely<T>(string key, out T data)
```

*This method loads custom **data** of type **T** saved in PlayerPrefs with known **key**. In case if this key isn't presented in PlayerPrefs, it returns **false**.*

*Example:*

```csharp
if (!DataSaver.LoadPrefsSecurely("CurrenciesState", out currenciesState))
{
    Debug.Log("Currencies state was not found!");
}
```

***

```csharp
public static void SaveData<T>(T data, string fileName)
```

*This method saves custom **data** of type **T** into JSON file with given **fileName**. In case if data has not serializable type, it will not be saved and method will return **false**. File will be stored in persistent application data path, on Windows it will be "C:\Users\\%username%\AppData\LocalLow\Heroicsolo\Heroic Engine\\" directory.*

*Example:*

```csharp
DataSaver.SaveData(knowledges, Guid + "_knowledges");
```

***

```csharp
public static bool SaveDataSecurely<T>(T data, string fileName)
```

*This method saves custom **data** of type **T** into encoded \*.data file with given **fileName**. In case if data has not serializable type, it will not be saved and method will return **false**. File will be stored in persistent application data path, on Windows it will be "C:\Users\\%username%\AppData\LocalLow\Heroicsolo\Heroic Engine\\" directory. This method encrypts data by AES* symmetric algorithm.

Example:

```csharp
// Save percetron weights and bias to file
private void SaveModel()
{
    ModelData modelData = new ModelData(inputWeights, outputWeights, hiddenBiases, bias);
    DataSaver.SaveDataSecurely(modelData, guid);
}
```

***

```csharp
public static bool LoadData<T>(string fileName, out T data)
```

*This method loads custom **data** of type **T** from JSON file with given **fileName**. File should be stored in persistent application data path, on Windows it will be "C:\Users\\%username%\AppData\LocalLow\Heroicsolo\Heroic Engine\\" directory. In case if file was not found, it returns **false**.*

*Example:*

```csharp
private void LoadKnowledges()
{
    if (!DataSaver.LoadData(Guid + "_knowledges", out knowledges))
    {
        knowledges = new AIKnowledgeBase();
        knowledges.solutions = new List<AIKnownSolution>();
    }
}
```

***

```csharp
public static bool LoadDataSecurely<T>(string fileName, out T data)
```

*This method loads custom **data** of type **T** from encrypted \*.data file with given **fileName**. File should be stored in persistent application data path, on Windows it will be "C:\Users\\%username%\AppData\LocalLow\Heroicsolo\Heroic Engine\\" directory. In case if file was not found, it returns **false**.*

*Example:*

```csharp
if (DataSaver.LoadDataSecurely(guid, out ModelData modelData))
{
    inputWeights = modelData.GetWeights();
    hiddenBiases = modelData.hiddenBiases;
    outputWeights = modelData.hiddenWeights;
    bias = modelData.bias;
}
```
