summaryrefslogtreecommitdiff
path: root/Thronefall_v1.57/Thronefall/I2.Loc/I2BasePersistentStorage.cs
blob: bd4a52cf497b94acb3f95b151264249001f3ff2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Globalization;
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);
				return;
			}
			int num2 = Mathf.CeilToInt((float)length / (float)num);
			for (int i = 0; i < num2; i++)
			{
				int num3 = num * i;
				PlayerPrefs.SetString($"[I2split]{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)
	{
		try
		{
			string text = PlayerPrefs.GetString(key, defaultValue);
			if (!string.IsNullOrEmpty(text) && text.StartsWith("[I2split]", StringComparison.Ordinal))
			{
				int num = int.Parse(text.Substring("[I2split]".Length), CultureInfo.InvariantCulture);
				text = "";
				for (int i = 0; i < num; i++)
				{
					text += PlayerPrefs.GetString($"[I2split]{i}{key}", "");
				}
			}
			return text;
		}
		catch (Exception)
		{
			Debug.LogError("Error loading PlayerPrefs " + key);
			return defaultValue;
		}
	}

	public virtual void DeleteSetting(string key)
	{
		try
		{
			string @string = PlayerPrefs.GetString(key, null);
			if (!string.IsNullOrEmpty(@string) && @string.StartsWith("[I2split]", StringComparison.Ordinal))
			{
				int num = int.Parse(@string.Substring("[I2split]".Length), CultureInfo.InvariantCulture);
				for (int i = 0; i < num; i++)
				{
					PlayerPrefs.DeleteKey($"[I2split]{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)
	{
		switch (fileType)
		{
		case PersistentStorage.eFileType.Persistent:
			fileName = Application.persistentDataPath + "/" + fileName;
			break;
		case PersistentStorage.eFileType.Temporal:
			fileName = Application.temporaryCachePath + "/" + fileName;
			break;
		case PersistentStorage.eFileType.Streaming:
			fileName = Application.streamingAssetsPath + "/" + fileName;
			break;
		}
		return fileName;
	}

	public virtual bool SaveFile(PersistentStorage.eFileType fileType, string fileName, string data, bool logExceptions = true)
	{
		if (!CanAccessFiles())
		{
			return false;
		}
		try
		{
			fileName = UpdateFilename(fileType, fileName);
			File.WriteAllText(fileName, data, Encoding.UTF8);
			return true;
		}
		catch (Exception ex)
		{
			if (logExceptions)
			{
				Debug.LogError("Error saving file '" + fileName + "'\n" + ex);
			}
			return false;
		}
	}

	public virtual string LoadFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
	{
		if (!CanAccessFiles())
		{
			return null;
		}
		try
		{
			fileName = UpdateFilename(fileType, fileName);
			return File.ReadAllText(fileName, Encoding.UTF8);
		}
		catch (Exception ex)
		{
			if (logExceptions)
			{
				Debug.LogError("Error loading file '" + fileName + "'\n" + ex);
			}
			return null;
		}
	}

	public virtual bool DeleteFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
	{
		if (!CanAccessFiles())
		{
			return false;
		}
		try
		{
			fileName = UpdateFilename(fileType, fileName);
			File.Delete(fileName);
			return true;
		}
		catch (Exception ex)
		{
			if (logExceptions)
			{
				Debug.LogError("Error deleting file '" + fileName + "'\n" + ex);
			}
			return false;
		}
	}

	public virtual bool HasFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
	{
		if (!CanAccessFiles())
		{
			return false;
		}
		try
		{
			fileName = UpdateFilename(fileType, fileName);
			return File.Exists(fileName);
		}
		catch (Exception ex)
		{
			if (logExceptions)
			{
				Debug.LogError("Error requesting file '" + fileName + "'\n" + ex);
			}
			return false;
		}
	}
}