Exemple #1
0
        private void LoadJson(string jsonPath, Action <string> callback)
        {
            string res = null;

            try
            {
                res = Encoding.UTF8.GetString(File.ReadAllBytes(jsonPath));
            }
            catch (Exception e)
            {
                Log($"Failed to read json!: {e.Message}");
            }

            CoreLoop.InvokeNext(() => { callback?.Invoke(res); });
        }
        private static void LoadJson(string jsonPath, Action <string> callback)
        {
            string res = null;

            try
            {
                res = Encoding.UTF8.GetString(File.ReadAllBytes(jsonPath));
            }
            catch (Exception exception)
            {
                Debug.LogException(exception);
            }

            CoreLoop.InvokeNext(() => { callback?.Invoke(res); });
        }
Exemple #3
0
        public void SaveGame(int saveSlot, Action <bool> callback)
        {
            if (saveSlot >= 0)
            {
                this.SaveLevelState();
                if (!this.gameConfig.disableSaveGame)
                {
                    this.ShowSaveIcon();
                    if (this.achievementHandler != null)
                    {
                        this.achievementHandler.FlushRecordsToDisk();
                    }
                    else
                    {
                        Debug.LogError("Error saving achievements (PlayerAchievements is null)");
                    }

                    if (this.playerData != null)
                    {
                        this.playerData.playTime += this.sessionPlayTimer;
                        this.ResetGameTimer();
                        this.playerData.version   = Constants.GAME_VERSION;
                        this.playerData.profileID = saveSlot;
                        this.playerData.CountGameCompletion();
                    }
                    else
                    {
                        Debug.LogError("Error updating PlayerData before save (PlayerData is null)");
                    }

                    try
                    {
                        SaveGameData obj = new SaveGameData(this.playerData, this.sceneData);

                        ModHooks.Instance.OnBeforeSaveGameSave(obj);

                        string text = null;

                        try
                        {
                            text = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings()
                            {
                                ContractResolver = ShouldSerializeContractResolver.Instance,
                                TypeNameHandling = TypeNameHandling.Auto
                            });
                        }
                        catch (Exception e)
                        {
                            Logger.LogError("Failed to serialize save using Json.NET, trying fallback.");

                            Logger.APILogger.LogError(e);

                            // If this dies, not much we can do about it.
                            text = JsonUtility.ToJson(obj);
                        }

                        bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected;

                        if (flag)
                        {
                            string          graph           = Encryption.Encrypt(text);
                            BinaryFormatter binaryFormatter = new BinaryFormatter();
                            MemoryStream    memoryStream    = new MemoryStream();
                            binaryFormatter.Serialize(memoryStream, graph);
                            byte[] binary = memoryStream.ToArray();
                            memoryStream.Close();
                            Platform.Current.WriteSaveSlot
                            (
                                saveSlot,
                                binary,
                                delegate(bool didSave)
                            {
                                this.HideSaveIcon();
                                callback(didSave);
                            }
                            );
                        }
                        else
                        {
                            Platform.Current.WriteSaveSlot
                            (
                                saveSlot,
                                Encoding.UTF8.GetBytes(text),
                                delegate(bool didSave)
                            {
                                this.HideSaveIcon();
                                if (callback != null)
                                {
                                    callback(didSave);
                                }
                            }
                            );
                        }
                    }
                    catch (Exception arg)
                    {
                        Debug.LogError("GM Save - There was an error saving the game: " + arg);
                        this.HideSaveIcon();
                        if (callback != null)
                        {
                            CoreLoop.InvokeNext(delegate { callback(false); });
                        }
                    }

                    ModHooks.Instance.OnSavegameSave(saveSlot);
                }
                else
                {
                    Debug.Log("Saving game disabled. No save file written.");
                    if (callback != null)
                    {
                        CoreLoop.InvokeNext(delegate { callback(false); });
                    }
                }
            }
            else
            {
                Debug.LogError("Save game slot not valid: " + saveSlot);
                if (callback != null)
                {
                    CoreLoop.InvokeNext(delegate { callback(false); });
                }
            }
        }
Exemple #4
0
        public void GetSaveStatsForSlot(int saveSlot, Action <global::SaveStats> callback)
        {
            if (!Platform.IsSaveSlotIndexValid(saveSlot))
            {
                Debug.LogErrorFormat
                (
                    "Cannot get save stats for invalid slot {0}",
                    new object[]
                {
                    saveSlot
                }
                );
                if (callback != null)
                {
                    CoreLoop.InvokeNext(delegate { callback(null); });
                }

                return;
            }

            Platform.Current.ReadSaveSlot
            (
                saveSlot,
                delegate(byte[] fileBytes)
            {
                if (fileBytes == null)
                {
                    if (callback != null)
                    {
                        CoreLoop.InvokeNext(delegate { callback(null); });
                    }

                    return;
                }

                try
                {
                    bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected;
                    string json;
                    if (flag)
                    {
                        BinaryFormatter binaryFormatter  = new BinaryFormatter();
                        MemoryStream serializationStream = new MemoryStream(fileBytes);
                        string encryptedString           = (string)binaryFormatter.Deserialize(serializationStream);
                        json = Encryption.Decrypt(encryptedString);
                    }
                    else
                    {
                        json = Encoding.UTF8.GetString(fileBytes);
                    }

                    SaveGameData saveGameData;
                    try
                    {
                        saveGameData = JsonConvert.DeserializeObject <SaveGameData>(json, new JsonSerializerSettings()
                        {
                            ContractResolver = ShouldSerializeContractResolver.Instance,
                            TypeNameHandling = TypeNameHandling.Auto
                        });
                    }
                    catch (Exception)
                    {
                        // Not a huge deal, this happens on saves with mod data which haven't been converted yet.
                        Logger.APILogger.LogWarn($"Failed to get save stats for slot {saveSlot} using Json.NET, falling back");

                        saveGameData = JsonUtility.FromJson <SaveGameData>(json);
                    }

                    global::PlayerData playerData = saveGameData.playerData;
                    SaveStats saveStats           = new SaveStats
                                                    (
                        playerData.maxHealthBase,
                        playerData.geo,
                        playerData.mapZone,
                        playerData.playTime,
                        playerData.MPReserveMax,
                        playerData.permadeathMode,
                        playerData.bossRushMode,
                        playerData.completionPercentage,
                        playerData.unlockedCompletionRate
                                                    );
                    if (callback != null)
                    {
                        CoreLoop.InvokeNext(delegate { callback(saveStats); });
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogError
                    (
                        string.Concat
                        (
                            new object[]
                    {
                        "Error while loading save file for slot ",
                        saveSlot,
                        " Exception: ",
                        ex
                    }
                        )
                    );
                    if (callback != null)
                    {
                        CoreLoop.InvokeNext(delegate { callback(null); });
                    }
                }
            }
            );
        }
Exemple #5
0
        public void LoadGame(int saveSlot, Action <bool> callback)
        {
            if (!Platform.IsSaveSlotIndexValid(saveSlot))
            {
                Debug.LogErrorFormat
                (
                    "Cannot load from invalid save slot index {0}",
                    new object[]
                {
                    saveSlot
                }
                );
                if (callback != null)
                {
                    CoreLoop.InvokeNext(delegate { callback(false); });
                }

                return;
            }

            Platform.Current.ReadSaveSlot
            (
                saveSlot,
                delegate(byte[] fileBytes)
            {
                bool obj;
                try
                {
                    bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected;
                    string json;
                    if (flag)
                    {
                        BinaryFormatter binaryFormatter  = new BinaryFormatter();
                        MemoryStream serializationStream = new MemoryStream(fileBytes);
                        string encryptedString           = (string)binaryFormatter.Deserialize(serializationStream);
                        json = Encryption.Decrypt(encryptedString);
                    }
                    else
                    {
                        json = Encoding.UTF8.GetString(fileBytes);
                    }

                    SaveGameData saveGameData;

                    try
                    {
                        saveGameData = JsonConvert.DeserializeObject <SaveGameData>(json, new JsonSerializerSettings()
                        {
                            ContractResolver = ShouldSerializeContractResolver.Instance,
                            TypeNameHandling = TypeNameHandling.Auto
                        });
                    }
                    catch (Exception e)
                    {
                        Logger.APILogger.LogError("Failed to read save using Json.NET (GameManager::LoadGame), falling back.");
                        Logger.APILogger.LogError(e);

                        saveGameData = JsonUtility.FromJson <SaveGameData>(json);
                    }

                    global::PlayerData instance = saveGameData.playerData;
                    SceneData instance2         = saveGameData.sceneData;
                    global::PlayerData.instance = instance;
                    this.playerData             = instance;
                    SceneData.instance          = instance2;
                    ModHooks.Instance.OnAfterSaveGameLoad(saveGameData);
                    this.sceneData = instance2;
                    this.profileID = saveSlot;
                    this.inputHandler.RefreshPlayerData();
                    ModHooks.Instance.OnSavegameLoad(saveSlot);
                    obj = true;
                }
                catch (Exception ex)
                {
                    Debug.LogFormat
                    (
                        "Error loading save file for slot {0}: {1}",
                        new object[]
                    {
                        saveSlot,
                        ex
                    }
                    );
                    obj = false;
                }

                if (callback != null)
                {
                    callback(obj);
                }
            }
            );
        }
 public void GetSaveStatsForSlot(int saveSlot, Action <global::SaveStats> callback)
 {
     if (!Platform.IsSaveSlotIndexValid(saveSlot))
     {
         Debug.LogErrorFormat("Cannot get save stats for invalid slot {0}", new object[]
         {
             saveSlot
         });
         if (callback != null)
         {
             CoreLoop.InvokeNext(delegate
             {
                 callback(null);
             });
         }
         return;
     }
     Platform.Current.ReadSaveSlot(saveSlot, delegate(byte[] fileBytes)
     {
         if (fileBytes == null)
         {
             if (callback != null)
             {
                 CoreLoop.InvokeNext(delegate
                 {
                     callback(null);
                 });
             }
             return;
         }
         try
         {
             bool flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected;
             string json;
             if (flag)
             {
                 BinaryFormatter binaryFormatter  = new BinaryFormatter();
                 MemoryStream serializationStream = new MemoryStream(fileBytes);
                 string encryptedString           = (string)binaryFormatter.Deserialize(serializationStream);
                 json = Encryption.Decrypt(encryptedString);
             }
             else
             {
                 json = Encoding.UTF8.GetString(fileBytes);
             }
             SaveGameData saveGameData     = JsonUtility.FromJson <SaveGameData>(json);
             global::PlayerData playerData = saveGameData.playerData;
             SaveStats saveStats           = new SaveStats(playerData.maxHealthBase, playerData.geo, playerData.mapZone,
                                                           playerData.playTime, playerData.MPReserveMax, playerData.permadeathMode, playerData.bossRushMode,
                                                           playerData.completionPercentage, playerData.unlockedCompletionRate);
             if (callback != null)
             {
                 CoreLoop.InvokeNext(delegate
                 {
                     callback(saveStats);
                 });
             }
         }
         catch (Exception ex)
         {
             Debug.LogError(string.Concat(new object[]
             {
                 "Error while loading save file for slot ",
                 saveSlot,
                 " Exception: ",
                 ex
             }));
             if (callback != null)
             {
                 CoreLoop.InvokeNext(delegate
                 {
                     callback(null);
                 });
             }
         }
     });
 }
 public void SaveGame(int saveSlot, Action <bool> callback)
 {
     if (saveSlot >= 0)
     {
         this.SaveLevelState();
         if (!this.gameConfig.disableSaveGame)
         {
             this.ShowSaveIcon();
             if (this.achievementHandler != null)
             {
                 this.achievementHandler.FlushRecordsToDisk();
             }
             else
             {
                 Debug.LogError("Error saving achievements (PlayerAchievements is null)");
             }
             if (this.playerData != null)
             {
                 this.playerData.playTime += this.sessionPlayTimer;
                 this.ResetGameTimer();
                 this.playerData.version   = "1.4.2.4";
                 this.playerData.profileID = saveSlot;
                 this.playerData.CountGameCompletion();
             }
             else
             {
                 Debug.LogError("Error updating PlayerData before save (PlayerData is null)");
             }
             try
             {
                 SaveGameData obj = new SaveGameData(this.playerData, this.sceneData);
                 ModHooks.Instance.OnBeforeSaveGameSave(obj);
                 string text = JsonUtility.ToJson(obj);
                 bool   flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected;
                 if (flag)
                 {
                     string          graph           = Encryption.Encrypt(text);
                     BinaryFormatter binaryFormatter = new BinaryFormatter();
                     MemoryStream    memoryStream    = new MemoryStream();
                     binaryFormatter.Serialize(memoryStream, graph);
                     byte[] binary = memoryStream.ToArray();
                     memoryStream.Close();
                     Platform.Current.WriteSaveSlot(saveSlot, binary, delegate(bool didSave)
                     {
                         this.HideSaveIcon();
                         callback(didSave);
                     });
                 }
                 else
                 {
                     Platform.Current.WriteSaveSlot(saveSlot, Encoding.UTF8.GetBytes(text), delegate(bool didSave)
                     {
                         this.HideSaveIcon();
                         if (callback != null)
                         {
                             callback(didSave);
                         }
                     });
                 }
             }
             catch (Exception arg)
             {
                 Debug.LogError("GM Save - There was an error saving the game: " + arg);
                 this.HideSaveIcon();
                 if (callback != null)
                 {
                     CoreLoop.InvokeNext(delegate
                     {
                         callback(false);
                     });
                 }
             }
             ModHooks.Instance.OnSavegameSave(saveSlot);
         }
         else
         {
             Debug.Log("Saving game disabled. No save file written.");
             if (callback != null)
             {
                 CoreLoop.InvokeNext(delegate
                 {
                     callback(false);
                 });
             }
         }
     }
     else
     {
         Debug.LogError("Save game slot not valid: " + saveSlot);
         if (callback != null)
         {
             CoreLoop.InvokeNext(delegate
             {
                 callback(false);
             });
         }
     }
 }