Esempio n. 1
0
 public SaveStats GetSaveStatsForSlot(int saveSlot)
 {
     if (saveSlot > 0)
     {
         string saveFilename = this.GetSaveFilename(saveSlot);
         if (!string.IsNullOrEmpty(saveFilename) && File.Exists(Application.persistentDataPath + saveFilename))
         {
             try
             {
                 string          toDecrypt       = string.Empty;
                 string          json            = string.Empty;
                 BinaryFormatter binaryFormatter = new BinaryFormatter();
                 FileStream      fileStream      = File.Open(Application.persistentDataPath + saveFilename, FileMode.Open);
                 if (this.gameConfig.useSaveEncryption)
                 {
                     toDecrypt = (string)binaryFormatter.Deserialize(fileStream);
                 }
                 else
                 {
                     json = (string)binaryFormatter.Deserialize(fileStream);
                 }
                 fileStream.Close();
                 if (this.gameConfig.useSaveEncryption)
                 {
                     json = StringEncrypt.DecryptData(toDecrypt);
                 }
                 SaveGameData       saveGameData = JsonUtility.FromJson <SaveGameData>(json);
                 global::PlayerData playerData   = saveGameData.playerData;
                 SaveStats          stats        = new SaveStats(playerData.maxHealthBase, playerData.geo, playerData.mapZone,
                                                                 playerData.playTime, playerData.MPReserveMax, playerData.permadeathMode,
                                                                 playerData.completionPercentage, playerData.unlockedCompletionRate)
                 {
                     Name       = saveGameData.Name,
                     LoadedMods = saveGameData.LoadedMods
                 };
                 return(stats);
             }
             catch (Exception ex)
             {
                 Debug.LogError(string.Concat(new object[]
                 {
                     "Error while loading save file for slot ",
                     saveSlot,
                     " Exception: ",
                     ex
                 }));
                 return(null);
             }
         }
         return(null);
     }
     Debug.LogError("Save game slot not valid: " + saveSlot);
     return(null);
 }
Esempio n. 2
0
        public SaveStats GetSaveStatsForSlot(int saveSlot)
        {
            if (!Platform.IsSaveSlotIndexValid(saveSlot))
            {
                Debug.LogErrorFormat("Cannot get save stats for invalid slot {0}", new object[]
                {
                    saveSlot
                });
                return(null);
            }
            if (!Platform.Current.IsSaveSlotInUse(saveSlot))
            {
                return(null);
            }
            SaveStats result;

            try
            {
                bool   flag = this.gameConfig.useSaveEncryption && !Platform.Current.IsFileSystemProtected;
                string json;
                if (flag)
                {
                    BinaryFormatter binaryFormatter     = new BinaryFormatter();
                    MemoryStream    serializationStream = new MemoryStream(Platform.Current.ReadSaveSlot(saveSlot));
                    string          encryptedString     = (string)binaryFormatter.Deserialize(serializationStream);
                    json = Encryption.Decrypt(encryptedString);
                }
                else
                {
                    json = Encoding.UTF8.GetString(Platform.Current.ReadSaveSlot(saveSlot));
                }
                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.completionPercentage, playerData.unlockedCompletionRate)
                {
                    Name       = saveGameData.Name,
                    LoadedMods = saveGameData.LoadedMods
                };
                result = saveStats;
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Concat(new object[]
                {
                    "Error while loading save file for slot ",
                    saveSlot,
                    " Exception: ",
                    ex
                }));
                result = null;
            }
            return(result);
        }
Esempio n. 3
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); });
                    }
                }
            }
            );
        }
 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);
                 });
             }
         }
     });
 }