359 lines
8.8 KiB
C#
359 lines
8.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using BitStrap;
|
|
using CodeStage.AntiCheat.ObscuredTypes;
|
|
using UnityEngine;
|
|
|
|
public class SaveManager : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class PlayerProfile
|
|
{
|
|
public int id;
|
|
|
|
public string prefix = "PROFILE_";
|
|
|
|
public string playerName = string.Empty;
|
|
|
|
public bool wasCreated;
|
|
|
|
public PlayerSettingsMy.Difficulty difficulty;
|
|
|
|
public int playerLevel;
|
|
}
|
|
|
|
public List<PlayerProfile> profiles = new List<PlayerProfile>();
|
|
|
|
public int currentProfileId = -1;
|
|
|
|
public int nrOfProfiles = 5;
|
|
|
|
public bool isProfileLoaded;
|
|
|
|
public int lastUsedProfile;
|
|
|
|
public string firstLangugeSaved = string.Empty;
|
|
|
|
private void Awake()
|
|
{
|
|
LoadProfiles();
|
|
CreateBackupFolder();
|
|
MakeBackup();
|
|
lastUsedProfile = ObscuredPrefs.GetInt("lastUsedProfile", 0);
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
if ((bool)SteamCloudManager.Instance)
|
|
{
|
|
SteamCloudManager.Instance.FileWrite();
|
|
}
|
|
}
|
|
|
|
public void CreateProfile(int profileId, string playerName)
|
|
{
|
|
profiles[profileId].playerName = playerName;
|
|
profiles[profileId].wasCreated = true;
|
|
SaveProfiles();
|
|
}
|
|
|
|
public void RenameProfile(string playerName)
|
|
{
|
|
GetCurrentProfile().playerName = playerName;
|
|
GetCurrentProfile().wasCreated = true;
|
|
GlobalSettings.Instance.playerSettings.playersName = GetCurrentProfile().playerName;
|
|
SaveProfiles();
|
|
}
|
|
|
|
public void DeleteProfile(int profileId)
|
|
{
|
|
currentProfileId = profileId;
|
|
profiles[profileId].playerName = string.Empty;
|
|
profiles[profileId].wasCreated = false;
|
|
Reset();
|
|
SaveProfiles();
|
|
}
|
|
|
|
public void ResetProfile(int profileId)
|
|
{
|
|
}
|
|
|
|
public void SelectProfile(int profileId)
|
|
{
|
|
if (currentProfileId != profileId)
|
|
{
|
|
currentProfileId = profileId;
|
|
lastUsedProfile = profileId;
|
|
ObscuredPrefs.SetInt("lastUsedProfile", lastUsedProfile);
|
|
Load();
|
|
}
|
|
GlobalSettings.Instance.playerSettings.playersName = GetCurrentProfile().playerName;
|
|
}
|
|
|
|
public void SaveProfiles()
|
|
{
|
|
for (int i = 0; i < nrOfProfiles; i++)
|
|
{
|
|
if (profiles[i].wasCreated)
|
|
{
|
|
using (ES2Writer eS2Writer = ES2Writer.Create(profiles[i].prefix))
|
|
{
|
|
eS2Writer.Write(profiles[i].playerName, "playerName");
|
|
eS2Writer.Write(profiles[i].wasCreated, "wasCreated");
|
|
eS2Writer.Save();
|
|
}
|
|
}
|
|
else if (ES2.Exists(profiles[i].prefix))
|
|
{
|
|
ES2.Delete(profiles[i].prefix);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void LoadProfiles()
|
|
{
|
|
profiles.Clear();
|
|
for (int i = 0; i < nrOfProfiles; i++)
|
|
{
|
|
PlayerProfile playerProfile = new PlayerProfile();
|
|
playerProfile.id = i;
|
|
playerProfile.prefix = "PROFILE_" + i;
|
|
playerProfile.playerName = string.Empty;
|
|
playerProfile.wasCreated = false;
|
|
profiles.Add(playerProfile);
|
|
}
|
|
for (int j = 0; j < nrOfProfiles; j++)
|
|
{
|
|
PlayerProfile playerProfile2 = profiles[j];
|
|
playerProfile2.id = j;
|
|
playerProfile2.prefix = "PROFILE_" + j;
|
|
string path = Application.persistentDataPath + "/" + playerProfile2.prefix;
|
|
if (File.Exists(path) && File.ReadAllBytes(path).Length == 0)
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
if (ES2.Exists(playerProfile2.prefix))
|
|
{
|
|
using (ES2Reader reader = ES2Reader.Create(playerProfile2.prefix))
|
|
{
|
|
if (!IsSaveFileConsistent(reader))
|
|
{
|
|
playerProfile2.playerName = "ERROR_LOADING";
|
|
playerProfile2.wasCreated = false;
|
|
File.Delete(path);
|
|
continue;
|
|
}
|
|
playerProfile2.playerName = Read(reader, "playerName", string.Empty);
|
|
playerProfile2.wasCreated = Read(reader, "wasCreated", false);
|
|
playerProfile2.difficulty = (PlayerSettingsMy.Difficulty)Read(reader, "difficultyObscured", 0);
|
|
playerProfile2.playerLevel = Read(reader, "playersLevel", 1);
|
|
if (firstLangugeSaved == string.Empty)
|
|
{
|
|
firstLangugeSaved = Read(reader, "language", string.Empty);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
playerProfile2.playerName = string.Empty;
|
|
playerProfile2.wasCreated = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public PlayerProfile GetProfile(int profileId)
|
|
{
|
|
return profiles[profileId];
|
|
}
|
|
|
|
public PlayerProfile GetCurrentProfile()
|
|
{
|
|
return profiles[currentProfileId];
|
|
}
|
|
|
|
public string GetCurrentProfilePrefix()
|
|
{
|
|
if (currentProfileId < 0 || currentProfileId >= profiles.Count)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
return profiles[currentProfileId].prefix;
|
|
}
|
|
|
|
public bool HasAnyProfiles()
|
|
{
|
|
for (int i = 0; i < nrOfProfiles; i++)
|
|
{
|
|
if (GetProfile(i).playerName != string.Empty)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
[Button]
|
|
public void OpenSaveFolder()
|
|
{
|
|
Application.OpenURL(Application.persistentDataPath);
|
|
}
|
|
|
|
[Button]
|
|
public void OpenBackupFolder()
|
|
{
|
|
Application.OpenURL(Application.persistentDataPath + "/BACKUPS");
|
|
}
|
|
|
|
public void CreateBackupFolder()
|
|
{
|
|
string path = Application.persistentDataPath + "/BACKUPS";
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void MakeBackup()
|
|
{
|
|
CreateBackupFolder();
|
|
string persistentDataPath = Application.persistentDataPath;
|
|
string text = Application.persistentDataPath + "/BACKUPS";
|
|
for (int i = 0; i < nrOfProfiles; i++)
|
|
{
|
|
bool flag = false;
|
|
if (ES2.Exists(profiles[i].prefix))
|
|
{
|
|
using (ES2Reader reader = ES2Reader.Create(profiles[i].prefix))
|
|
{
|
|
flag = Read(reader, "wasCreated", false) && Read(reader, "playersScore", 0) > 0;
|
|
}
|
|
}
|
|
if (flag && File.Exists(persistentDataPath + "/" + profiles[i].prefix))
|
|
{
|
|
File.Copy(persistentDataPath + "/" + profiles[i].prefix, text + "/" + profiles[i].prefix, true);
|
|
}
|
|
}
|
|
persistentDataPath += "/EDITOR_LEVELS";
|
|
text += "/EDITOR_LEVELS";
|
|
Utilities.CopyDirectory(persistentDataPath, text);
|
|
}
|
|
|
|
[Button]
|
|
public void RecoverBackup()
|
|
{
|
|
string persistentDataPath = Application.persistentDataPath;
|
|
string text = Application.persistentDataPath + "/BACKUPS";
|
|
for (int i = 0; i < nrOfProfiles; i++)
|
|
{
|
|
if (File.Exists(text + "/" + profiles[i].prefix))
|
|
{
|
|
File.Copy(text + "/" + profiles[i].prefix, persistentDataPath + "/" + profiles[i].prefix, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void SaveCrashTest()
|
|
{
|
|
StartCoroutine(SaveTest(3f));
|
|
StartCoroutine(SaveTest(0.5f));
|
|
}
|
|
|
|
public IEnumerator SaveTest(float delay)
|
|
{
|
|
string profilePrefix = GlobalSettings.Instance.saveManager.GetCurrentProfilePrefix();
|
|
if (string.IsNullOrEmpty(profilePrefix))
|
|
{
|
|
Debug.LogError("Couldn't get current profile prefix", this);
|
|
yield break;
|
|
}
|
|
using (ES2Writer writer = ES2Writer.Create(profilePrefix))
|
|
{
|
|
writer.Write(GlobalSettings.Instance.gameVersion, "gameVersion");
|
|
yield return new WaitForSeconds(delay);
|
|
writer.Save();
|
|
Debug.Log("Save SaveCrashTest");
|
|
}
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
GlobalSettings.Instance.playerSettings.Save();
|
|
GlobalSettings.Instance.renderSettings.Save();
|
|
GlobalSettings.Instance.soundSettings.Save();
|
|
GlobalSettings.Instance.levelsManager.Save();
|
|
GlobalSettings.Instance.equipmentManager.Save();
|
|
GlobalSettings.Instance.skillsManager.Save();
|
|
GlobalSettings.Instance.fishManager.Save();
|
|
TutorialManager.Instance.Save();
|
|
AchievementManager.Instance.Save();
|
|
VRManager.Instance.Save();
|
|
}
|
|
|
|
public void Load()
|
|
{
|
|
GlobalSettings.Instance.playerSettings.Load();
|
|
GlobalSettings.Instance.renderSettings.Load();
|
|
GlobalSettings.Instance.soundSettings.Load();
|
|
GlobalSettings.Instance.levelsManager.Load();
|
|
GlobalSettings.Instance.equipmentManager.Load();
|
|
GlobalSettings.Instance.skillsManager.Load();
|
|
GlobalSettings.Instance.fishManager.Load();
|
|
TutorialManager.Instance.Load();
|
|
AchievementManager.Instance.Load();
|
|
VRManager.Instance.Load();
|
|
if (GlobalSettings.Instance.playerSettings.IsSandbox() && !GlobalSettings.Instance.skillsManager.skills[6].isUnlocked)
|
|
{
|
|
GlobalSettings.Instance.playerSettings.UnlockEverythingAndSave(true);
|
|
Debug.LogError("Sandbox fix");
|
|
}
|
|
if (GlobalSettings.Instance.currentPlatform == GlobalSettings.Platform.ARCADE)
|
|
{
|
|
GlobalSettings.Instance.playerSettings.UnlockEverythingAndSave(true);
|
|
GlobalSettings.Instance.renderSettings.Reset();
|
|
TutorialManager.Instance.Reset();
|
|
VRManager.Instance.Reset();
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
GlobalSettings.Instance.playerSettings.Reset();
|
|
GlobalSettings.Instance.renderSettings.Reset();
|
|
GlobalSettings.Instance.soundSettings.Reset();
|
|
GlobalSettings.Instance.levelsManager.Reset();
|
|
GlobalSettings.Instance.equipmentManager.Reset();
|
|
GlobalSettings.Instance.skillsManager.Reset();
|
|
GlobalSettings.Instance.fishManager.Reset();
|
|
TutorialManager.Instance.Reset();
|
|
AchievementManager.Instance.Reset();
|
|
VRManager.Instance.Reset();
|
|
}
|
|
|
|
public static T Read<T>(ES2Reader reader, string tag, T defaultValue)
|
|
{
|
|
if (reader.TagExists(tag))
|
|
{
|
|
return reader.Read<T>(tag);
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
public bool IsSaveFileConsistent(ES2Reader reader)
|
|
{
|
|
try
|
|
{
|
|
reader.TagExists("test_tag");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError("SaveManager Read Exception " + ex.Message);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|