123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- using System;
- using System.IO;
- using System.Text;
- using UnityEngine;
- namespace I2.Loc
- {
- public abstract class I2BasePersistentStorage
- {
- public virtual void SetSetting_String(string key, string value)
- {
- try
- {
- int length = value.Length;
- int num = 8000;
- if (length <= num)
- {
- PlayerPrefs.SetString(key, value);
- }
- else
- {
- int num2 = Mathf.CeilToInt((float)length / (float)num);
- for (int i = 0; i < num2; i++)
- {
- int num3 = num * i;
- PlayerPrefs.SetString(string.Format("[I2split]{0}{1}", i, key), value.Substring(num3, Mathf.Min(num, length - num3)));
- }
- PlayerPrefs.SetString(key, "[$I2#@div$]" + num2);
- }
- }
- catch (Exception)
- {
- Debug.LogError("Error saving PlayerPrefs " + key);
- }
- }
- public virtual string GetSetting_String(string key, string defaultValue)
- {
- string result;
- try
- {
- string text = PlayerPrefs.GetString(key, defaultValue);
- if (!string.IsNullOrEmpty(text) && text.StartsWith("[I2split]"))
- {
- int num = int.Parse(text.Substring("[I2split]".Length));
- text = string.Empty;
- for (int i = 0; i < num; i++)
- {
- text += PlayerPrefs.GetString(string.Format("[I2split]{0}{1}", i, key), string.Empty);
- }
- }
- result = text;
- }
- catch (Exception)
- {
- Debug.LogError("Error loading PlayerPrefs " + key);
- result = defaultValue;
- }
- return result;
- }
- public virtual void DeleteSetting(string key)
- {
- try
- {
- string @string = PlayerPrefs.GetString(key, null);
- if (!string.IsNullOrEmpty(@string) && @string.StartsWith("[I2split]"))
- {
- int num = int.Parse(@string.Substring("[I2split]".Length));
- for (int i = 0; i < num; i++)
- {
- PlayerPrefs.DeleteKey(string.Format("[I2split]{0}{1}", i, key));
- }
- }
- PlayerPrefs.DeleteKey(key);
- }
- catch (Exception)
- {
- Debug.LogError("Error deleting PlayerPrefs " + key);
- }
- }
- public virtual void ForceSaveSettings()
- {
- PlayerPrefs.Save();
- }
- public virtual bool HasSetting(string key)
- {
- return PlayerPrefs.HasKey(key);
- }
- public virtual bool CanAccessFiles()
- {
- return true;
- }
- private string UpdateFilename(PersistentStorage.eFileType fileType, string fileName)
- {
- if (fileType != PersistentStorage.eFileType.Persistent)
- {
- if (fileType != PersistentStorage.eFileType.Temporal)
- {
- if (fileType == PersistentStorage.eFileType.Streaming)
- {
- fileName = Application.streamingAssetsPath + "/" + fileName;
- }
- }
- else
- {
- fileName = Application.temporaryCachePath + "/" + fileName;
- }
- }
- else
- {
- fileName = Application.persistentDataPath + "/" + fileName;
- }
- return fileName;
- }
- public virtual bool SaveFile(PersistentStorage.eFileType fileType, string fileName, string data, bool logExceptions = true)
- {
- if (!this.CanAccessFiles())
- {
- return false;
- }
- bool result;
- try
- {
- fileName = this.UpdateFilename(fileType, fileName);
- File.WriteAllText(fileName, data, Encoding.UTF8);
- result = true;
- }
- catch (Exception ex)
- {
- if (logExceptions)
- {
- Debug.LogError(string.Concat(new object[]
- {
- "Error saving file '",
- fileName,
- "'\n",
- ex
- }));
- }
- result = false;
- }
- return result;
- }
- public virtual string LoadFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
- {
- if (!this.CanAccessFiles())
- {
- return null;
- }
- string result;
- try
- {
- fileName = this.UpdateFilename(fileType, fileName);
- result = File.ReadAllText(fileName, Encoding.UTF8);
- }
- catch (Exception ex)
- {
- if (logExceptions)
- {
- Debug.LogError(string.Concat(new object[]
- {
- "Error loading file '",
- fileName,
- "'\n",
- ex
- }));
- }
- result = null;
- }
- return result;
- }
- public virtual bool DeleteFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
- {
- if (!this.CanAccessFiles())
- {
- return false;
- }
- bool result;
- try
- {
- fileName = this.UpdateFilename(fileType, fileName);
- File.Delete(fileName);
- result = true;
- }
- catch (Exception ex)
- {
- if (logExceptions)
- {
- Debug.LogError(string.Concat(new object[]
- {
- "Error deleting file '",
- fileName,
- "'\n",
- ex
- }));
- }
- result = false;
- }
- return result;
- }
- public virtual bool HasFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
- {
- if (!this.CanAccessFiles())
- {
- return false;
- }
- bool result;
- try
- {
- fileName = this.UpdateFilename(fileType, fileName);
- result = File.Exists(fileName);
- }
- catch (Exception ex)
- {
- if (logExceptions)
- {
- Debug.LogError(string.Concat(new object[]
- {
- "Error requesting file '",
- fileName,
- "'\n",
- ex
- }));
- }
- result = false;
- }
- return result;
- }
- }
- }
|