Esempio n. 1
0
    /// <summary>
    /// Load <see cref="SaveFile"/> (if exists) from savefiles path.
    /// </summary>
    /// <param name="saveName">Savefile name</param>
    public bool LoadSave(string saveName)
    {
        var fileName = SaveFilesPath + saveName + ".txt";

        if (!File.Exists(fileName))
        {
            Debug.LogWarning("No such savefile exists.");
            OnFileLoadingFail?.Invoke("No such savefile exists.");
            return(false);
        }

        var data = File.ReadAllText(fileName);

        try
        {
            CurrentSave = DataSerializer.DeserializeObject <SaveFile>(data);
            OnFileLoadingComplete?.Invoke("Savefile loaded.");
            Debug.Log($"Savefile {CurrentSave.Name} was loaded");
            return(true);
        }
        catch (Exception e)
        {
            Debug.LogWarning("Error appeared during savefile loading. Error message:\n" + e.Message);
            OnFileLoadingFail?.Invoke("Error appeared during savefile loading.");
            return(false);
        }
    }