Advertisement
evelynshilosky

SaveManager - Part 25

Nov 15th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.56 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8.  
  9. public class SaveManager : MonoBehaviour
  10. {
  11.     public static SaveManager Instance { get; set; }
  12.     private void Awake()
  13.     {
  14.         if (Instance != null && Instance != this)
  15.         {
  16.             Destroy(gameObject);
  17.         }
  18.         else
  19.         {
  20.             Instance = this;
  21.         }
  22.  
  23.         DontDestroyOnLoad(gameObject);
  24.  
  25.     }
  26.  
  27.     // Json Project Save Path
  28.     string jsonPathProject;
  29.     // Json External/Real Save Path
  30.     string jsonPathPersistant;
  31.     // Binary Save Path
  32.     string binaryPath;
  33.  
  34.  
  35.     string fileName = "SaveGame";
  36.  
  37.  
  38.     public bool isSavingToJson;
  39.  
  40.     private void Start()
  41.     {
  42.         jsonPathProject = Application.dataPath + Path.AltDirectorySeparatorChar;
  43.         jsonPathPersistant = Application.persistentDataPath + Path.AltDirectorySeparatorChar;
  44.         binaryPath = Application.persistentDataPath + Path.AltDirectorySeparatorChar;
  45.     }
  46.  
  47.     #region || ---------- General Section ---------- ||
  48.  
  49.  
  50.     #region || ---------- Saving ---------- ||
  51.     public void SaveGame(int slotNumber)
  52.     {
  53.         AllGameData data = new AllGameData();
  54.  
  55.         data.playerData = GetPlayerData();
  56.  
  57.         SavingTypeSwitch(data, slotNumber);
  58.     }
  59.  
  60.     private PlayerData GetPlayerData()
  61.     {
  62.         float[] playerStats = new float[3];
  63.         playerStats[0] = PlayerState.Instance.currentHealth;
  64.         playerStats[1] = PlayerState.Instance.currentCalories ;
  65.         playerStats[2] = PlayerState.Instance.currentHydrationPercent;
  66.  
  67.         float[] playerPosAndRot = new float[6];
  68.         playerPosAndRot[0] = PlayerState.Instance.playerBody.transform.position.x;
  69.         playerPosAndRot[1] = PlayerState.Instance.playerBody.transform.position.y;
  70.         playerPosAndRot[2] = PlayerState.Instance.playerBody.transform.position.z;
  71.  
  72.         playerPosAndRot[3] = PlayerState.Instance.playerBody.transform.rotation.x;
  73.         playerPosAndRot[4] = PlayerState.Instance.playerBody.transform.rotation.y;
  74.         playerPosAndRot[5] = PlayerState.Instance.playerBody.transform.rotation.z;
  75.  
  76.         return new PlayerData(playerStats, playerPosAndRot);
  77.  
  78.  
  79.     }
  80.  
  81.     public void SavingTypeSwitch(AllGameData gameData, int slotNumber)
  82.     {
  83.         if (isSavingToJson)
  84.         {
  85.             SaveGameDataToJsonFile(gameData, slotNumber);
  86.         }
  87.         else
  88.         {
  89.             SaveGameDataToBinaryFile(gameData, slotNumber);
  90.         }
  91.     }
  92.     #endregion
  93.  
  94.  
  95.     #region || ---------- Loading ---------- ||
  96.  
  97.     public AllGameData LoadingTypeSwitch(int slotNumber)
  98.     {
  99.         if (isSavingToJson)
  100.         {
  101.             AllGameData gameData = LoadGameDataFromJsonFile(slotNumber);
  102.             return gameData;
  103.         }
  104.         else
  105.         {
  106.             AllGameData gameData = LoadGameDataFromBinaryFile(slotNumber);
  107.             return gameData;
  108.         }
  109.     }
  110.  
  111.     public void LoadGame(int slotNumber)
  112.     {
  113.         // Player Data
  114.         SetPlayerData(LoadingTypeSwitch(slotNumber).playerData);
  115.  
  116.         //Environment Data
  117.        
  118.     }
  119.  
  120.     private void SetPlayerData(PlayerData playerData)
  121.     {
  122.         // Setting Player Stats
  123.  
  124.         PlayerState.Instance.currentHealth = playerData.playerStats[0];
  125.         PlayerState.Instance.currentCalories = playerData.playerStats[1];
  126.         PlayerState.Instance.currentHydrationPercent = playerData.playerStats[2];
  127.  
  128.         // Setting Player Position
  129.  
  130.         Vector3 loadedPosition;
  131.         loadedPosition.x = playerData.playerPositionAndRotation[0];
  132.         loadedPosition.y = playerData.playerPositionAndRotation[1];
  133.         loadedPosition.z = playerData.playerPositionAndRotation[2];
  134.  
  135.         PlayerState.Instance.playerBody.transform.position = loadedPosition;
  136.  
  137.         // Setting Player Rotation
  138.  
  139.         Vector3 loadedRotation;
  140.         loadedRotation.x = playerData.playerPositionAndRotation[3];
  141.         loadedRotation.y = playerData.playerPositionAndRotation[4];
  142.         loadedRotation.z = playerData.playerPositionAndRotation[5];
  143.  
  144.         PlayerState.Instance.playerBody.transform.rotation = Quaternion.Euler(loadedRotation);
  145.        
  146.     }
  147.  
  148.     public void StartLoadedGame(int slotNumber)
  149.     {
  150.         SceneManager.LoadScene("GameScene");
  151.  
  152.         StartCoroutine(DelayedLoading(slotNumber));
  153.     }
  154.     private IEnumerator DelayedLoading(int slotNumber)
  155.     {
  156.  
  157.         yield return new WaitForSeconds(1f);
  158.  
  159.         LoadGame(slotNumber);
  160.     }
  161.  
  162.  
  163.     #endregion
  164.  
  165.  
  166.     #endregion
  167.  
  168.     #region || ---------- To Binary Section ---------- ||
  169.  
  170.     public void SaveGameDataToBinaryFile(AllGameData gameData, int slotNumber)
  171.     {
  172.         BinaryFormatter formatter = new BinaryFormatter();
  173.  
  174.  
  175.         FileStream stream = new FileStream(binaryPath + fileName + slotNumber + ".bin", FileMode.Create);
  176.  
  177.         formatter.Serialize(stream, gameData);
  178.         stream.Close();
  179.  
  180.         print("Data saved to" + binaryPath + fileName + slotNumber + ".bin");
  181.  
  182.     }
  183.  
  184.     public AllGameData LoadGameDataFromBinaryFile(int slotNumber)
  185.     {
  186.      
  187.  
  188.         if (File.Exists(binaryPath + fileName + slotNumber + ".bin"))
  189.         {
  190.             BinaryFormatter formatter = new BinaryFormatter();
  191.             FileStream stream = new FileStream(binaryPath + fileName + slotNumber + ".bin", FileMode.Open);
  192.  
  193.             AllGameData data = formatter.Deserialize(stream) as AllGameData;
  194.             stream.Close();
  195.  
  196.  
  197.             print("Data Loaded from" + binaryPath + fileName + slotNumber + ".bin");
  198.  
  199.  
  200.             return data;
  201.         }
  202.         else
  203.         {
  204.             return null;
  205.         }
  206.     }
  207.  
  208.     #endregion
  209.  
  210.     #region || ---------- To Json Section ---------- ||
  211.  
  212.     public void SaveGameDataToJsonFile(AllGameData gameData, int slotNumber)
  213.     {
  214.         string json = JsonUtility.ToJson(gameData);
  215.  
  216.         string encrypted = EncryptionDecryption(json);
  217.  
  218.         using (StreamWriter writer = new StreamWriter(jsonPathProject + fileName + slotNumber + ".json")) // SaveGame1.json
  219.         {
  220.                 writer.Write(encrypted);
  221.                 print("Saved Game to Json file at : " + jsonPathProject + fileName + slotNumber + ".json");
  222.         };
  223.     }
  224.  
  225.     public AllGameData LoadGameDataFromJsonFile(int slotNumber)
  226.     {
  227.         using (StreamReader reader = new StreamReader(jsonPathProject + fileName + slotNumber + ".json")) // SaveGame1.json
  228.         {
  229.             string json = reader.ReadToEnd();
  230.  
  231.             string decrypted = EncryptionDecryption(json);
  232.  
  233.             AllGameData data = JsonUtility.FromJson<AllGameData>(decrypted);
  234.             return data;
  235.         };
  236.  
  237.     }
  238.  
  239.  
  240.  
  241.  
  242.     #endregion
  243.  
  244.     #region || ---------- Settings Section ---------- ||
  245.  
  246.     #region || ---------- Volume Settings ---------- ||
  247.     [System.Serializable]
  248.     public class VolumeSettings
  249.     {
  250.         public float music;
  251.         public float effects;
  252.         public float master;
  253.     }
  254.  
  255.     public void SaveVolumeSettings(float _music, float _effects, float _master)
  256.     {
  257.         VolumeSettings volumeSettings = new VolumeSettings()
  258.         {
  259.             music = _music,
  260.             effects= _effects,
  261.             master = _master
  262.         };
  263.  
  264.         PlayerPrefs.SetString("Volume", JsonUtility.ToJson(volumeSettings));
  265.         PlayerPrefs.Save();
  266.  
  267.         print("Saved to Player Pref");
  268.     }
  269.  
  270.     public VolumeSettings LoadVolumeSettings()
  271.     {
  272.         return JsonUtility.FromJson<VolumeSettings>(PlayerPrefs.GetString("Volume"));
  273.     }
  274.     #endregion
  275.  
  276.  
  277.  
  278.     #endregion
  279.  
  280.     #region || ---------- Encryption ---------- ||
  281.    
  282.     public string EncryptionDecryption(string jsonString)
  283.     {
  284.         string keyword = "1234567";
  285.  
  286.         string result = "";
  287.  
  288.         for (int i = 0; i < jsonString.Length; i++)
  289.         {
  290.             result += (char)(jsonString[i] ^ keyword[i % keyword.Length]);
  291.         }
  292.  
  293.         return result; // Encrypted or Decrypted String
  294.  
  295.  
  296.         // XOR = "is there a difference"
  297.  
  298.         // --- Encrypt --- \\
  299.         // Evelyn - 01000101 01110110 01100101 01101100 01111001 01101110
  300.         // E - 01000101
  301.         // Key - 00000001
  302.         //
  303.         // Encrypted - 01000100
  304.  
  305.         // --- Decrypt --- \\
  306.         // Encrypted - 01000100
  307.         // Key -       00000001
  308.         //
  309.         // M -         01000101
  310.  
  311.     }
  312.  
  313.  
  314.     #endregion
  315.  
  316.  
  317.     #region || ---------- Utility ---------- ||
  318.  
  319.     public bool DoesFileExists(int slotNumber)
  320.     {
  321.         if (isSavingToJson)
  322.         {
  323.             if (System.IO.File.Exists(jsonPathProject + fileName + slotNumber + ".json")) //SaveGame1.json
  324.             {
  325.                 return true;
  326.             }
  327.             else
  328.             {
  329.                 return false;
  330.             }
  331.         }
  332.         else
  333.         {
  334.             if (System.IO.File.Exists(binaryPath + fileName + slotNumber + ".bin")) //SaveGame1.bin
  335.             {
  336.                 return true;
  337.             }
  338.             else
  339.             {
  340.                 return false;
  341.             }
  342.         }
  343.     }
  344.  
  345.     public bool IsSlotEmpty(int slotNumber)
  346.     {
  347.         if (DoesFileExists(slotNumber))
  348.         {
  349.             return false;
  350.         }
  351.         else
  352.         {
  353.             return true;
  354.         }
  355.     }
  356.  
  357.  
  358.     public void DeselectButton()
  359.     {
  360.         GameObject myEventSystem = GameObject.Find("EventSystem");
  361.         myEventSystem.GetComponent<UnityEngine.EventSystems.EventSystem>().SetSelectedGameObject(null);
  362.     }
  363.  
  364.     #endregion
  365. }
  366.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement