Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        //--- 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);
        }