using System.Collections; using System.Collections.Generic; using UnityEngine; using RPGCreationKit; using UnityEngine.SceneManagement; using System.IO; using RPGCreationKit.Player; using System; using RPGCreationKit.CellsSystem; namespace RPGCreationKit.SaveSystem { public class SaveSystemManager : MonoBehaviour { public static SaveSystemManager instance; public static bool CAN_SAVE = true; [SerializeField] private GameObject EnvironmentManager; private void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(this.gameObject); } else Destroy(this.gameObject); } [SerializeField] public SaveFile saveFile; public FileInfo saveInfo; public void LoadSaveGame(FileInfo _info, SaveFile _file) { saveFile.LootingPointsData.allLootingPoints.Clear(); saveFile.ItemsInWorldData.allItemsInWorld.Clear(); saveInfo = _info; saveFile = _file; LoadingScreenInfo.whatToLoadPriority.Add("_PersistentReferences_"); LoadingScreenInfo.whatToLoad.Add("_WorldLoader_"); SceneManager.LoadScene("_LoadingScreen_"); } public void SaveAndOverwrite(FileInfo _info, SaveFile _saveFile) { saveInfo = _info; System.DateTime date = System.DateTime.Now; // Fill the relevant save info // SaveFileData saveFile.SaveFileData.saveType = RCKSettings.SAVE_TYPE_VERSION; saveFile.SaveFileData.fileVersion = RCKSettings.FILE_SAVE_VERSION; saveFile.SaveFileData.saveNumber++; saveFile.SaveFileData.saveDate = date.ToShortDateString() + " - " + date.Hour + ":" + date.Minute; // PlayerData //saveFile.PlayerData.playerName = name; //saveFile.PlayerData.playerLevel = level; //saveFile.PlayerData.playerSex = sex; saveFile.PlayerData.playerLocation = WorldManager.instance.currentCenterCell.cellName; saveFile.PlayerData.playerWorldspaceID = WorldManager.instance.currentWorldspace.worldSpaceID; saveFile.PlayerData.playerCellID = WorldManager.instance.currentCenterCell.ID; saveFile.PlayerData.playerPos = RckPlayer.instance.transform.position; saveFile.PlayerData.playerRot = RckPlayer.instance.transform.rotation; saveFile.PlayerData.mouseRotX = RckPlayer.instance.mouseLook.xRotation; saveFile.PlayerData.playerCrouched = RckPlayer.instance.IsCrouching; saveFile.PlayerData.weaponDrawn = PlayerCombat.instance.weaponDrawn; saveFile.PlayerData.isInThirdPerson = RckPlayer.instance.isInThirdPerson; saveFile.PlayerData.playerAttributes = EntityAttributes.PlayerAttributes.ToSaveData(); saveFile.PlayerData.playerInventory = Inventory.PlayerInventory.ToSaveData(); saveFile.PlayerData.spellsKnowledge = SpellsKnowledge.Player.ToSaveData(); // Environment if (EnvironmentManager == null) { EnvironmentManager = GameObject.Find("EnvironmentManager"); } SunLightManager EnviroMan = EnvironmentManager.GetComponent(); saveFile.Environment.TimeOfDay = EnviroMan.TimeOfDay; saveFile.Environment.Year = EnviroMan.Year; saveFile.Environment.Month = EnviroMan.Month; saveFile.Environment.Day = EnviroMan.Day; // Save all factions saveFile.PlayerData.playerFactions.Clear(); for (int i = 0; i < RckPlayer.instance.belongsToFactions.Count; i++) saveFile.PlayerData.playerFactions.Add(RckPlayer.instance.belongsToFactions[i].ID); // Save all quests saveFile.QuestData = QuestManager.instance.ToSaveData(); // Save all touched items and ai foreach (KeyValuePair cellInfo in CellInformation.activeCells) { Debug.Log(cellInfo.Value.cell.ID); cellInfo.Value.SaveTouchedItems(); // Save AI cellInfo.Value.SaveRckAI(); } string jsonFile = JsonUtility.ToJson(saveFile, RCKSettings.JSON_PRETTY_PRINT); string path = _info.FullName; File.WriteAllText(path, jsonFile); TakeScreenShot(path); } public void SaveOnNewFile() { System.DateTime date = System.DateTime.Now; // Fill the relevant save info // SaveFileData saveFile.SaveFileData.saveType = RCKSettings.SAVE_TYPE_VERSION; saveFile.SaveFileData.fileVersion = RCKSettings.FILE_SAVE_VERSION; saveFile.SaveFileData.saveNumber++; saveFile.SaveFileData.saveDate = date.ToShortDateString() + " - " + date.Hour + ":" + date.Minute; // PlayerData //saveFile.PlayerData.playerName = name; //saveFile.PlayerData.playerLevel = level; //saveFile.PlayerData.playerSex = sex; saveFile.PlayerData.playerLocation = WorldManager.instance.currentCenterCell.cellName; saveFile.PlayerData.playerWorldspaceID = WorldManager.instance.currentWorldspace.worldSpaceID; saveFile.PlayerData.playerCellID = WorldManager.instance.currentCenterCell.ID; saveFile.PlayerData.playerPos = RckPlayer.instance.transform.position; saveFile.PlayerData.playerRot = RckPlayer.instance.transform.rotation; saveFile.PlayerData.mouseRotX = RckPlayer.instance.mouseLook.xRotation; saveFile.PlayerData.playerCrouched = RckPlayer.instance.IsCrouching; saveFile.PlayerData.weaponDrawn = PlayerCombat.instance.weaponDrawn; saveFile.PlayerData.isInThirdPerson = RckPlayer.instance.isInThirdPerson; saveFile.PlayerData.playerAttributes = EntityAttributes.PlayerAttributes.ToSaveData(); saveFile.PlayerData.playerInventory = Inventory.PlayerInventory.ToSaveData(); saveFile.PlayerData.spellsKnowledge = SpellsKnowledge.Player.ToSaveData(); // Environment if (EnvironmentManager == null) { EnvironmentManager = GameObject.Find("EnvironmentManager"); } SunLightManager EnviroMan = EnvironmentManager.GetComponent(); saveFile.Environment.TimeOfDay = EnviroMan.TimeOfDay; // Save all factions saveFile.PlayerData.playerFactions.Clear(); for (int i = 0; i < RckPlayer.instance.belongsToFactions.Count; i++) saveFile.PlayerData.playerFactions.Add(RckPlayer.instance.belongsToFactions[i].ID); // Save all quests saveFile.QuestData = QuestManager.instance.ToSaveData(); // Save all touched items and ai foreach(KeyValuePair cellInfo in CellInformation.activeCells) { cellInfo.Value.SaveTouchedItems(); // Save AI cellInfo.Value.SaveRckAI(); } string jsonFile = JsonUtility.ToJson(saveFile, RCKSettings.JSON_PRETTY_PRINT); string path = saveInfo.FullName; uint add = saveFile.SaveFileData.saveNumber; while (File.Exists(path)) { add++; path = saveInfo.FullName; path = saveInfo.FullName.Substring(0, saveInfo.FullName.IndexOf('_')); path += "_ " + add.ToString(); path += ".json"; } File.WriteAllText(path, jsonFile); // Save screenshot TakeScreenShot(path); } public Savegame CreateNewCharacter(string _name, bool _gender, string _raceID, FaceBlendshapesSaveData _faceData, int _hairType, int _eyesType) { Savegame savegame = SaveSystemManager.instance.gameObject.AddComponent(); string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Saves"; if (!Directory.Exists(path)) Directory.CreateDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Saves"); // Get all files SaveFile saveFile = new SaveFile(); path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Saves\" + _name + "_"; int add = 0; if (File.Exists(path)) { do { add++; path = path.Substring(0, path.IndexOf('_')); path += "_ " + add.ToString(); path += ".json"; } while (File.Exists(path)); } else path += ".json"; FileInfo saveInfo = new FileInfo(path); System.DateTime date = System.DateTime.Now; // Fill the relevant save info // SaveFileData saveFile.SaveFileData.saveType = RCKSettings.SAVE_TYPE_VERSION; saveFile.SaveFileData.fileVersion = RCKSettings.FILE_SAVE_VERSION; saveFile.SaveFileData.saveNumber++; saveFile.SaveFileData.saveDate = date.ToShortDateString() + " - " + date.Hour + ":" + date.Minute; // PlayerData saveFile.PlayerData.playerName = _name; saveFile.PlayerData.playerRace = _raceID; saveFile.PlayerData.playerLevel = RCKSettings.RCK_NEW_STARTING_LEVEL; saveFile.PlayerData.playerSex = _gender; saveFile.PlayerData.hairType = _hairType; saveFile.PlayerData.eyesType = _eyesType; saveFile.PlayerData.playerLocation = RCKSettings.RCK_NEW_STARTING_LOCATION; saveFile.PlayerData.playerWorldspaceID = RCKSettings.RCK_NEW_STARTING_WORLDSPACEID; saveFile.PlayerData.playerCellID = RCKSettings.RCK_NEW_STARTING_CELLID; saveFile.PlayerData.playerPos = RCKSettings.RCK_NEW_STARTING_POS; saveFile.PlayerData.playerRot = Quaternion.Euler(RCKSettings.RCK_NEW_STARTING_ROT); /* saveFile.PlayerData.mouseRotX = RckPlayer.instance.mouseLook.xRotation; saveFile.PlayerData.playerCrouched = RckPlayer.instance.IsCrouching; saveFile.PlayerData.weaponDrawn = PlayerCombat.instance.weaponDrawn; saveFile.PlayerData.playerAttributes = EntityAttributes.PlayerAttributes.ToSaveData(); saveFile.PlayerData.playerInventory = Inventory.PlayerInventory.ToSaveData(); // Save all quests saveFile.QuestData = QuestManager.instance.ToSaveData(); // Save all touched items and ai foreach (KeyValuePair cellInfo in CellInformation.activeCells) { cellInfo.Value.SaveTouchedItems(); // Save AI cellInfo.Value.SaveRckAI(); } */ saveFile.PlayerData.faceData = _faceData; string jsonFile = JsonUtility.ToJson(saveFile, RCKSettings.JSON_PRETTY_PRINT); File.WriteAllText(path, jsonFile); savegame.saveFile = saveFile; savegame.fileInfo = saveInfo; return savegame; } public void TakeScreenShot(string _savePath) { Camera mainCamera = null; mainCamera = (RckPlayer.instance.isInThirdPerson) ? RckPlayer.instance.tpsCamera : RckPlayer.instance.weaponCamera; // Let FPS camera render also the first person if(!RckPlayer.instance.isInThirdPerson) mainCamera.cullingMask |= 1 << RCKLayers.FirstPerson; RenderTexture rt = new RenderTexture(470, 250, 24); mainCamera.targetTexture = rt; Texture2D screenShot = new Texture2D(470, 250, TextureFormat.RGB24, false); mainCamera.Render(); RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, 470, 250), 0, 0); mainCamera.targetTexture = null; RenderTexture.active = null; // JC: added to avoid errors Destroy(rt); byte[] bytes = screenShot.EncodeToPNG(); string screenPath = _savePath.Substring(0, _savePath.IndexOf('.')); screenPath += ".png"; System.IO.File.WriteAllBytes(screenPath, bytes); // Restore culling mask if (!RckPlayer.instance.isInThirdPerson) mainCamera.cullingMask &= ~(1 << RCKLayers.FirstPerson); } } }