public void LoadGameState() { Debug.Log("Start loading game @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay); // Clean up & prepare loadingPrepared = false; loadedBodyData = new SaveFile_BodyData(); initializers.Clear(); // 0. Read file and convert Json string fileContent = System.IO.File.ReadAllText(saveGameFilePath); // read file from disk // 1. Deserialize SaveFileSerializer.Deserialize("Head", fileContent, encryptSaveFile, ref HeadData); SaveFileSerializer.Deserialize("Body", fileContent, encryptSaveFile, ref loadedBodyData); // 2. Create ScriptableObject Instances and load data into them foreach (var data in loadedBodyData.scriptableObjectDatas) { LoadScriptableObject(data); } Debug.Log("Loaded all scriptableObjects @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay); // 3. All Guids auf bestehenden Objekten initialisieren GuidManager.InitializeGuidComponents(); Debug.Log("Initialized all GuidComponents @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay); // 4. Load Components List <SaveableComponent> components = ComponentExtensions.FindAllComponentsOfType <SaveableComponent>(); foreach (var componentData in loadedBodyData.componentDatas) { var component = FindSaveableComponentByGuid(componentData.Id, components); if (component != null) { component.RestoreData(componentData); } } // 5. Cleanup Resources.UnloadUnusedAssets(); // 6. Loading complete Debug.Log("Loading complete @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay); }
/*[Button, DisableInEditorMode]*/ public void SaveGameState(string _saveGameFileName) { bodyDataToSave = new SaveFile_BodyData(); saveGameFileName = _saveGameFileName; // 1. Collect SaveableScriptableObjects ScriptableObjectsGuidMapSingleton.Instance.scriptableObjectsGuidMap.CollectAllRuntimeInstantiatedSOs(); //Set Guids for new SOs var saveableScriptableObjects = new List <ScriptableObject>(); foreach (var so in scriptableObjectsToSave) { if (so == null) { Debug.LogError($"There is a null entry in ScriptableObjectsToSave on the SaveLoadController {this.name}. Please fix.", this); continue; } saveableScriptableObjects.AddRange(SaveableScriptableObjectsCollector.Collect(so)); } // 2. Save ScriptableObjects List <SaveableScriptableObjectData> scriptableObjectsData = new List <SaveableScriptableObjectData>(); scriptableObjectsData.AddRange(SaveLoadScriptableObjects.SaveScriptableObjects(saveableScriptableObjects)); bodyDataToSave.scriptableObjectDatas.AddRange(scriptableObjectsData); // 3. Save Components if (saveComponentsOfActiveScenes) { saveableComponents = ComponentExtensions.FindAllComponentsOfType <SaveableComponent>(); foreach (var saveableComponent in saveableComponents) { bodyDataToSave.componentDatas.Add(saveableComponent.StoreData()); } } // 4. Write file string fileContent = string.Empty; // 4.1 Write version data var currentVersion = new Version(Application.version); fileContent += SaveFileSerializer.Serialize("Version", typeof(string), currentVersion.ToString(), encryptSaveFile); // 4.2 write head data HeadData = new SaveFile_HeadData(DateTime.Now, ScreenshotTaker.TakeScreenshot(UnityEngine.Camera.main, Screen.width / 4, Screen.height / 4)); fileContent += SaveFileSerializer.Serialize("Head", typeof(SaveFile_HeadData), HeadData, encryptSaveFile); // 4.3 write body data fileContent += SaveFileSerializer.Serialize("Body", typeof(SaveFile_BodyData), bodyDataToSave, encryptSaveFile); // 4.4 write to disk System.IO.Directory.CreateDirectory(saveGameDirectoryPath); // Create save game directory path if it doesn't already exists System.IO.File.WriteAllText(saveGameFilePath, fileContent); // Write file to disk // 5. Cleanup Resources.UnloadUnusedAssets(); // 6. Finished saving Debug.Log("Game saved!"); }
//--- Gibt eine Liste zurück, die gespeicherte SaveGames enthält. Kann benutzt werden, um eine Ansicht zu erstellen, wo der Spieler ein Game laden kann. --- public static Dictionary <string /*Hash + Filename*/, SaveFilePreview> LoadList(SaveLoadController _saveLoadController) { var loadedHeadDatas = new Dictionary <string, SaveFilePreview>(); string folderPath = _saveLoadController.saveGameDirectoryPath; string fileExtension = _saveLoadController.saveGameFileExtension; bool encryptSaveFile = _saveLoadController.encryptSaveFile; string[] files = encryptSaveFile ? System.IO.Directory.GetFiles(folderPath, "*" + fileExtension) : System.IO.Directory.GetFiles(folderPath, "*.json"); //remove deleted files from the cached data var fileIdentifiers = new List <string>(); var entriesToRemove = new List <string>(); foreach (var filePath in files) //get file identifiers (hash + filename) for all files { string fileIdentifier = FileHash.GetHashSha256AsString(filePath) + Path.GetFileNameWithoutExtension(filePath); fileIdentifiers.Add(fileIdentifier); } foreach (var fileIdentifier in loadedHeadDatas.Keys.ToList()) { if (!fileIdentifiers.Contains(fileIdentifier)) { entriesToRemove.Add(fileIdentifier); } } foreach (var entry in entriesToRemove) { loadedHeadDatas.Remove(entry); } //-- load head data -- foreach (var filePath in files) { //ignore files that have already been loaded and cached before string fileHash = FileHash.GetHashSha256AsString(filePath); string fileName = Path.GetFileNameWithoutExtension(filePath); string fileIdentifier = fileHash + fileName; if (loadedHeadDatas.ContainsKey(fileIdentifier)) { continue; } //if not already loaded and cached: load the head data var previewData = new SaveFilePreview(); previewData.filePath = filePath; previewData.fileName = fileName; try { //try to read the file as text string fileContent = System.IO.File.ReadAllText(filePath); // read file from disk //try to deserialize the version string version = ""; SaveFileSerializer.Deserialize("Version", fileContent, encryptSaveFile, ref version); previewData.version = version; //check if the version is compatible if (!GameVersion.IsCompatible(version)) { previewData.compatibility = SaveFilePreview.SaveFileCompatibility.NotCompatible; loadedHeadDatas.Add(fileIdentifier, previewData); continue; } //try to load the head data var headData = new SaveFile_HeadData(); SaveFileSerializer.Deserialize("Head", fileContent, encryptSaveFile, ref headData); previewData.headData = headData; previewData.previewImage = Sprite.Create(headData.screenshot, new Rect(0, 0, headData.screenshot.width, headData.screenshot.height), new Vector2(0.5f, 0.5f)); loadedHeadDatas.Add(fileIdentifier, previewData); } catch (Exception exception) { Debug.LogWarning("Could not read file: " + filePath + " \n" + exception); previewData.compatibility = SaveFilePreview.SaveFileCompatibility.FileError; loadedHeadDatas.Add(fileIdentifier, previewData); continue; } } return(loadedHeadDatas); }