Skip to content
Snippets Groups Projects
Commit 792aee98 authored by GoDillonAudris512's avatar GoDillonAudris512
Browse files

chore: comment some code before ready

parent a737616f
No related merge requests found
using System.Collections; //using System.Collections;
using System.Collections.Generic; //using System.Collections.Generic;
using UnityEngine; //using UnityEngine;
using System.Linq; //using System.Linq;
public class DataPersistenceManager : MonoBehaviour //public class DataPersistenceManager : MonoBehaviour
{ //{
[Header("File Storage Config")] // [Header("File Storage Config")]
[SerializeField] private string fileName; // [SerializeField] private string fileName;
private GameData gameData; // private GameData gameData;
private List<IDataPersistence> dataPersistenceObjects; // private List<IDataPersistence> dataPersistenceObjects;
private FileDataHandler dataHandler; // private FileDataHandler dataHandler;
public static DataPersistenceManager instance { get; private set; } // public static DataPersistenceManager instance { get; private set; }
private void Awake() // private void Awake()
{ // {
if (instance != null) // if (instance != null)
{ // {
Debug.LogError("Found more than one Data Persistence Manager in the scene.") // Debug.LogError("Found more than one Data Persistence Manager in the scene.");
}; // };
instance = this; // instance = this;
} // }
private void Start() // private void Start()
{ // {
this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName); // this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);
this.dataPersistenceObjects = FindAllDataPersistenceObjects(); // this.dataPersistenceObjects = FindAllDataPersistenceObjects();
LoadGame(); // LoadGame();
} // }
public void NewGame() // public void NewGame()
{ // {
this.gameData = new GameData(); // this.gameData = new GameData();
} // }
public void LoadGame() // public void LoadGame()
{ // {
// load any saved data from a file using data handler // // load any saved data from a file using data handler
this.gameData = dataHandler.Load(); // this.gameData = dataHandler.Load();
// if no data can be loaded, initialize to a new game // // if no data can be loaded, initialize to a new game
if (this.gameData == null) // if (this.gameData == null)
{ // {
Debug.Log("Mo data was found. Initializing data to defaults"); // Debug.Log("Mo data was found. Initializing data to defaults");
NewGame(); // NewGame();
} // }
// push the loaded data to all other scripts that need it // // push the loaded data to all other scripts that need it
foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) // foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects)
{ // {
dataPersistenceObj.LoadData(gameData); // dataPersistenceObj.LoadData(gameData);
} // }
Debug.Log("Loaded game data"); // Debug.Log("Loaded game data");
} // }
public void SaveGame() // public void SaveGame()
{ // {
// pass the data to other scripts so they can update it // // pass the data to other scripts so they can update it
foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) // foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects)
{ // {
dataPersistenceObj.SaveData(ref gameData); // dataPersistenceObj.SaveData(ref gameData);
} // }
Debug.Log("Saved game data"); // Debug.Log("Saved game data");
// save that data to a file using data handler // // save that data to a file using data handler
dataHandler.Save(gameData); // dataHandler.Save(gameData);
} // }
private void OnApplicationQuit() // private void OnApplicationQuit()
{ // {
SaveGame(); // SaveGame();
} // }
private List<IDataPersistence> FindAllDataPersistenceObjects() // private List<IDataPersistence> FindAllDataPersistenceObjects()
{ // {
IEnumerable<IDataPersistence> dataPersistenceObjects = FindObjectsOfType<MonoBehaviour>().OfType<IDataPersistence>(); // IEnumerable<IDataPersistence> dataPersistenceObjects = FindObjectsOfType<MonoBehaviour>().OfType<IDataPersistence>();
return new List<IDataPersistence>(dataPersistenceObjects); // return new List<IDataPersistence>(dataPersistenceObjects);
} // }
} //}
using System.Collections; //using System.Collections;
using System.Collections.Generic; //using System.Collections.Generic;
using UnityEngine; //using UnityEngine;
using System; //using System;
using System.IO; //using System.IO;
public class FileDataHandler //public class FileDataHandler
{ //{
private string dataDirPath = ""; // private string dataDirPath = "";
private string dataFileName = ""; // private string dataFileName = "";
public FileDataHandler(string dataDirPath, string dataFileName) // public FileDataHandler(string dataDirPath, string dataFileName)
{ // {
this.dataDirPath = dataDirPath; // this.dataDirPath = dataDirPath;
this.dataFileName = dataFileName; // this.dataFileName = dataFileName;
} // }
public GameData Load() // public GameData Load()
{ // {
// use Path.combine to account for different OS's having different path separators // // use Path.combine to account for different OS's having different path separators
string fullPath = Path.Combine(dataDirPath, dataFileName); // string fullPath = Path.Combine(dataDirPath, dataFileName);
GameData loadedData = null; // GameData loadedData = null;
if (File.Exists(fullPath)) // if (File.Exists(fullPath))
{ // {
try // try
{ // {
// load the serialized data from the file // // load the serialized data from the file
string dataToLoad = ""; // string dataToLoad = "";
using (FileStream stream = new FileStream(fullPath, FileMode.Open)) // using (FileStream stream = new FileStream(fullPath, FileMode.Open))
{ // {
using (StreamReader reader = new StreamReader(stream)) // using (StreamReader reader = new StreamReader(stream))
{ // {
dataToLoad = reader.ReadToEnd(); // dataToLoad = reader.ReadToEnd();
} // }
} // }
//deserialize the data from Json back into the C# object // //deserialize the data from Json back into the C# object
loadedData = JsonUtility.FromJson<GameData>(dataToLoad); // loadedData = JsonUtility.FromJson<GameData>(dataToLoad);
} // }
catch (Exception e) // catch (Exception e)
{ // {
Debug.LogError("Error occured when trying to load data to file: " + fullPath + "\n" + e); // Debug.LogError("Error occured when trying to load data to file: " + fullPath + "\n" + e);
} // }
} // }
} // }
public void Save(GameData data) // public void Save(GameData data)
{ // {
// use Path.combine to account for different OS's having different path separators // // use Path.combine to account for different OS's having different path separators
string fullPath = Path.Combine(dataDirPath, dataFileName); // string fullPath = Path.Combine(dataDirPath, dataFileName);
try // try
{ // {
// create the directory the file will be written to if it doesn't already exist // // create the directory the file will be written to if it doesn't already exist
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); // Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
//serialize the C# game data object into Json // //serialize the C# game data object into Json
string dataToStore = JsonUtility.ToJson(data, true); // string dataToStore = JsonUtility.ToJson(data, true);
// write the serialized data to the file // // write the serialized data to the file
using (FileStream stream = new FileStream(fullPath, FileMode.Create)) // using (FileStream stream = new FileStream(fullPath, FileMode.Create))
{ // {
using (StreamWriter writer = new StreamWriter(stream)) // using (StreamWriter writer = new StreamWriter(stream))
{ // {
writer.Write(dataToStore); // writer.Write(dataToStore);
} // }
} // }
} // }
catch (Exception e) // catch (Exception e)
{ // {
Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e); // Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e);
} // }
} // }
} //}
using System; //using System;
using System.Collections.Generic; //using System.Collections.Generic;
using System.IO; //using System.IO;
using UnityEngine; //using UnityEngine;
public class SaveManager : MonoBehaviour
{
private string saveFileName = "savegame.json"; // Change this to your desired filename
public void SaveGame(SaveData saveData) //public class SaveManager : MonoBehaviour
{ //{
try // private string saveFileName = "savegame.json"; // Change this to your desired filename
{
string jsonData = JsonUtility.ToJson(saveData);
File.WriteAllText(Application.persistentDataPath + "/" + saveFileName, jsonData);
}
catch (Exception e)
{
Debug.LogError("Error saving game: " + e.Message);
}
}
public SaveData LoadGame() // public void SaveGame(SaveData saveData)
{ // {
if (File.Exists(Application.persistentDataPath + "/" + saveFileName)) // try
{ // {
string jsonData = File.ReadAllText(Application.persistentDataPath + "/" + saveFileName); // string jsonData = JsonUtility.ToJson(saveData);
return JsonUtility.FromJson<SaveData>(jsonData); // File.WriteAllText(Application.persistentDataPath + "/" + saveFileName, jsonData);
} // }
else // catch (Exception e)
{ // {
return new SaveData(); // Return a default save data if no save file exists // Debug.LogError("Error saving game: " + e.Message);
} // }
} // }
}
public class SaveSlotManager : MonoBehaviour // public SaveData LoadGame()
{ // {
private SaveManager saveManager; // if (File.Exists(Application.persistentDataPath + "/" + saveFileName))
private List<SaveData> saveSlots = new List<SaveData>(); // {
// string jsonData = File.ReadAllText(Application.persistentDataPath + "/" + saveFileName);
// return JsonUtility.FromJson<SaveData>(jsonData);
// }
// else
// {
// return new SaveData(); // Return a default save data if no save file exists
// }
// }
//}
public int GetNumSaveSlots() //public class SaveSlotManager : MonoBehaviour
{ //{
return saveSlots.Count; // Assuming saveSlots holds the save data // private SaveManager saveManager;
} // private List<SaveData> saveSlots = new List<SaveData>();
private void Start() // public int GetNumSaveSlots()
{ // {
saveManager = GetComponent<SaveManager>(); // return saveSlots.Count; // Assuming saveSlots holds the save data
LoadSaveSlots(); // Load save data from existing save files // }
}
public void SaveGameToSlot(int slotIndex, SaveData saveData) // private void Start()
{ // {
saveSlots[slotIndex] = saveData; // saveManager = GetComponent<SaveManager>();
SaveSaveSlots(); // Save updated save data to files // LoadSaveSlots(); // Load save data from existing save files
} // }
public SaveData LoadGameFromSlot(int slotIndex) // public void SaveGameToSlot(int slotIndex, SaveData saveData)
{ // {
return saveSlots[slotIndex]; // saveSlots[slotIndex] = saveData;
} // SaveSaveSlots(); // Save updated save data to files
// }
private void LoadSaveSlots() // public SaveData LoadGameFromSlot(int slotIndex)
{ // {
// Load save data from multiple save files // return saveSlots[slotIndex];
} // }
private void SaveSaveSlots() // private void LoadSaveSlots()
{ // {
// Save updated save data to multiple files // // Load save data from multiple save files
} // }
}
\ No newline at end of file // private void SaveSaveSlots()
// {
// // Save updated save data to multiple files
// }
//}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment