220 lines
6.0 KiB
C#
220 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Security;
|
|
using System.Text;
|
|
using Steamworks;
|
|
using UnityEngine;
|
|
|
|
public class EasySaveSystemManager : Singleton<EasySaveSystemManager>
|
|
{
|
|
private readonly string _settingsDataSaveFile = "ConfigFinal.cfg";
|
|
|
|
private readonly string _playerDataSaveFile = "PlayerDataFinal.ufs2";
|
|
|
|
private readonly string _saveLogFile = "SaveLog.log";
|
|
|
|
private string _settingsDataSaveFileDestination = "";
|
|
|
|
private string _playerDataSaveFileDestination = "";
|
|
|
|
private string _saveLogFileDestination = "";
|
|
|
|
public const string PlayerDataPassword = "IvVGe1Hzejd36ne";
|
|
|
|
private static StreamWriter _saveLogOutputStream;
|
|
|
|
private const string _saveLogPrefix = "[EasySave 3] ";
|
|
|
|
private static bool _stopLogStream = true;
|
|
|
|
private new void Awake()
|
|
{
|
|
base.Awake();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
UnityEngine.Object.DontDestroyOnLoad(this);
|
|
string text = "";
|
|
if (SteamManager.Initialized)
|
|
{
|
|
Dictionary<string, string> dictionary = new Dictionary<string, string>
|
|
{
|
|
{ "public", "" },
|
|
{ "default", "" },
|
|
{ "beta", "MultiplayerTesting" },
|
|
{ "testing_krzysiek", "MultiplayerTesting" },
|
|
{ "alpha", "MultiplayerTesting" },
|
|
{ "server", "MultiplayerTesting" }
|
|
};
|
|
if (SteamApps.GetCurrentBetaName(out var pchName, 100))
|
|
{
|
|
text = ((!dictionary.ContainsKey(pchName)) ? pchName : dictionary[pchName]);
|
|
if (!string.IsNullOrEmpty(text))
|
|
{
|
|
string path = Path.Combine(Application.persistentDataPath, "Saves", text);
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
text += "/";
|
|
}
|
|
}
|
|
}
|
|
_settingsDataSaveFileDestination = Application.persistentDataPath + "/Saves/" + text + _settingsDataSaveFile;
|
|
_playerDataSaveFileDestination = Application.persistentDataPath + "/Saves/" + text + _playerDataSaveFile;
|
|
if (ES3.FileExists(GetSettingsDataSaveDestination()))
|
|
{
|
|
WriteSaveLog("Settings file exists.");
|
|
ES3.LoadInto("SettingsData", _settingsDataSaveFileDestination, Singleton<SaveDataManager>.Instance.SettingsData);
|
|
}
|
|
else
|
|
{
|
|
WriteSaveLog("Settings file does not exists in local folder.");
|
|
}
|
|
Singleton<SaveDataManager>.Instance.CreatePlayerData();
|
|
if (ES3.FileExists(GetPlayerDataSaveDestination()))
|
|
{
|
|
WriteSaveLog("PlayerData file exists.");
|
|
LoadPlayerDataFromFile();
|
|
}
|
|
else
|
|
{
|
|
WriteSaveLog("PlayerData file does not exist in local folder.");
|
|
}
|
|
}
|
|
|
|
private void LoadPlayerDataFromFile()
|
|
{
|
|
ES3Settings settings = new ES3Settings(ES3.EncryptionType.AES, "IvVGe1Hzejd36ne");
|
|
try
|
|
{
|
|
foreach (SaveDataManager.SavePlayerDataClass item in ES3.Load("PlayerData", _playerDataSaveFileDestination, settings) as List<SaveDataManager.SavePlayerDataClass>)
|
|
{
|
|
WriteSaveLog("Data in file with cipher: " + item.PlayerName);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
foreach (SaveDataManager.SavePlayerDataClass item2 in ES3.Load("PlayerData", _playerDataSaveFileDestination) as List<SaveDataManager.SavePlayerDataClass>)
|
|
{
|
|
WriteSaveLog("Data in file without cipher: " + item2.PlayerName);
|
|
}
|
|
}
|
|
try
|
|
{
|
|
ES3.LoadInto("PlayerData", _playerDataSaveFileDestination, Singleton<SaveDataManager>.Instance.PlayerData, settings);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteSaveLog(ex.ToString());
|
|
ES3.LoadInto("PlayerData", _playerDataSaveFileDestination, Singleton<SaveDataManager>.Instance.PlayerData);
|
|
}
|
|
foreach (SaveDataManager.SavePlayerDataClass playerDatum in Singleton<SaveDataManager>.Instance.PlayerData)
|
|
{
|
|
WriteSaveLog("Data after loading in EasySaveSystemManager: " + playerDatum.PlayerName);
|
|
}
|
|
}
|
|
|
|
private void InitializeSaveLogger()
|
|
{
|
|
_saveLogFileDestination = Application.persistentDataPath + "/" + _saveLogFile;
|
|
_saveLogOutputStream = new StreamWriter(_saveLogFileDestination, append: false, Encoding.UTF8);
|
|
_saveLogOutputStream.AutoFlush = true;
|
|
}
|
|
|
|
public static void WriteSaveLog(string textToLog)
|
|
{
|
|
if (!_stopLogStream)
|
|
{
|
|
_saveLogOutputStream.WriteLine("[EasySave 3] " + textToLog);
|
|
}
|
|
}
|
|
|
|
public static void WriteSaveError(string textToLog)
|
|
{
|
|
if (!_stopLogStream)
|
|
{
|
|
_saveLogOutputStream.WriteLine("ERROR!: [EasySave 3] " + textToLog);
|
|
}
|
|
}
|
|
|
|
public string GetPlayerDataSaveDestination()
|
|
{
|
|
return _playerDataSaveFileDestination;
|
|
}
|
|
|
|
public string GetSettingsDataSaveDestination()
|
|
{
|
|
return _settingsDataSaveFileDestination;
|
|
}
|
|
|
|
public void SavePlayerSettings()
|
|
{
|
|
try
|
|
{
|
|
ES3.Save("SettingsData", Singleton<SaveDataManager>.Instance.SettingsData, GetSettingsDataSaveDestination());
|
|
}
|
|
catch (IOException)
|
|
{
|
|
WriteSaveError("[EasySaveSystemManager] The file is open elsewhere or there was not enough storage space.");
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
WriteSaveError("[EasySaveSystemManager] You do not have the required permissions.");
|
|
}
|
|
}
|
|
|
|
public bool AreAllProfilesEmpty()
|
|
{
|
|
foreach (SaveDataManager.SavePlayerDataClass playerDatum in Singleton<SaveDataManager>.Instance.PlayerData)
|
|
{
|
|
if (playerDatum.PlayerName != "")
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void SavePlayerData()
|
|
{
|
|
if (AreAllProfilesEmpty())
|
|
{
|
|
WriteSaveLog("Profiles are empty, so won't save it locally.");
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
ES3Settings settings = new ES3Settings(ES3.EncryptionType.AES, "IvVGe1Hzejd36ne");
|
|
ES3.Save("PlayerData", Singleton<SaveDataManager>.Instance.PlayerData, GetPlayerDataSaveDestination(), settings);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
WriteSaveError("[EasySaveSystemManager] The file is open elsewhere or there was not enough storage space.");
|
|
}
|
|
catch (SecurityException)
|
|
{
|
|
WriteSaveError("[EasySaveSystemManager] You do not have the required permissions.");
|
|
}
|
|
catch (Exception ex3)
|
|
{
|
|
WriteSaveLog("Save saved without cipher");
|
|
WriteSaveLog(ex3.ToString());
|
|
ES3.Save("PlayerData", Singleton<SaveDataManager>.Instance.PlayerData, GetPlayerDataSaveDestination());
|
|
}
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
Singleton<SaveDataManager>.Instance.ResetPlayerBaskets();
|
|
SavePlayerSettings();
|
|
SavePlayerData();
|
|
WriteSaveLog("Stop EasySaveSystem work.");
|
|
_stopLogStream = true;
|
|
_saveLogOutputStream.Flush();
|
|
_saveLogOutputStream.Close();
|
|
}
|
|
}
|