# 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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heroicsolo.gitbook.io/heroic-engine/basics/engine-utilities/datasaver.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
