Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AchievementManager.cs
Go to the documentation of this file.
1using System;
3using System.IO;
4using System.Linq;
6using System.Text;
7using Newtonsoft.Json;
8using Newtonsoft.Json.Bson;
9using Newtonsoft.Json.Linq;
10using Terraria.Social;
11using Terraria.UI;
13
14namespace Terraria.Achievements;
15
17{
18 private class StoredAchievement
19 {
20 [JsonProperty]
22 }
23
24 private string _savePath;
25
26 private bool _isCloudSave;
27
29
31
32 private byte[] _cryptoKey;
33
35
36 private static object _ioLock = new object();
37
38 public event Achievement.AchievementCompleted OnAchievementCompleted;
39
41 {
42 //IL_000c: Unknown result type (might be due to invalid IL or missing references)
43 //IL_0016: Expected O, but got Unknown
44 if (SocialAPI.Achievements != null)
45 {
46 _savePath = SocialAPI.Achievements.GetSavePath();
47 _isCloudSave = true;
48 _cryptoKey = SocialAPI.Achievements.GetEncryptionKey();
49 }
50 else
51 {
52 _savePath = Main.SavePath + Path.DirectorySeparatorChar + "achievements.dat";
53 _isCloudSave = false;
54 _cryptoKey = Encoding.ASCII.GetBytes("RELOGIC-TERRARIA");
55 }
56 }
57
58 public void Save()
59 {
61 {
63 });
64 }
65
66 private void Save(string path, bool cloud)
67 {
68 //IL_0047: Unknown result type (might be due to invalid IL or missing references)
69 //IL_004e: Expected O, but got Unknown
71 {
72 if (SocialAPI.Achievements != null)
73 {
74 SocialAPI.Achievements.StoreStats();
75 }
76 try
77 {
81 try
82 {
83 JsonSerializer.Create(_serializerSettings).Serialize((JsonWriter)(object)val, (object)_achievements);
84 ((JsonWriter)val).Flush();
85 cryptoStream.FlushFinalBlock();
87 }
88 finally
89 {
90 ((IDisposable)val)?.Dispose();
91 }
92 }
93 catch (Exception exception)
94 {
96 }
97 }
98 }
99
101 {
102 return _achievements.Values.ToList();
103 }
104
105 public void Load()
106 {
108 }
109
110 private void Load(string path, bool cloud)
111 {
112 //IL_0055: Unknown result type (might be due to invalid IL or missing references)
113 //IL_005c: Expected O, but got Unknown
114 bool flag = false;
115 lock (_ioLock)
116 {
117 if (!FileUtilities.Exists(path, cloud))
118 {
119 return;
120 }
121 byte[] buffer = FileUtilities.ReadAllBytes(path, cloud);
123 try
124 {
128 try
129 {
130 dictionary = JsonSerializer.Create(_serializerSettings).Deserialize<Dictionary<string, StoredAchievement>>((JsonReader)(object)val);
131 }
132 finally
133 {
134 ((IDisposable)val)?.Dispose();
135 }
136 }
137 catch (Exception)
138 {
139 FileUtilities.Delete(path, cloud);
140 return;
141 }
142 if (dictionary == null)
143 {
144 return;
145 }
147 {
149 {
150 _achievements[item.Key].Load(item.Value.Conditions);
151 }
152 }
153 if (SocialAPI.Achievements != null)
154 {
155 foreach (KeyValuePair<string, Achievement> achievement in _achievements)
156 {
157 if (achievement.Value.IsCompleted && !SocialAPI.Achievements.IsAchievementCompleted(achievement.Key))
158 {
159 flag = true;
160 achievement.Value.ClearProgress();
161 }
162 }
163 }
164 }
165 if (flag)
166 {
167 Save();
168 }
169 }
170
171 public void ClearAll()
172 {
173 if (SocialAPI.Achievements != null)
174 {
175 return;
176 }
177 foreach (KeyValuePair<string, Achievement> achievement in _achievements)
178 {
179 achievement.Value.ClearProgress();
180 }
181 Save();
182 }
183
184 private void AchievementCompleted(Achievement achievement)
185 {
186 Save();
187 if (this.OnAchievementCompleted != null)
188 {
189 this.OnAchievementCompleted(achievement);
190 }
191 }
192
193 public void Register(Achievement achievement)
194 {
195 _achievements.Add(achievement.Name, achievement);
196 achievement.OnCompleted += AchievementCompleted;
197 }
198
203
205 {
206 _achievements[achievementName].SetCategory(category);
207 }
208
210 {
212 {
213 return value;
214 }
215 return null;
216 }
217
222
224 {
226 {
227 return value.GetCondition(conditionName);
228 }
229 return null;
230 }
231
232 public int GetIconIndex(string achievementName)
233 {
235 {
236 return value;
237 }
238 return 0;
239 }
240}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
void Add(TKey key, TValue value)
static Encoding ASCII
Definition Encoding.cs:511
void RegisterIconIndex(string achievementName, int iconIndex)
Dictionary< string, int > _achievementIconIndexes
Achievement GetAchievement(string achievementName)
Dictionary< string, Achievement > _achievements
readonly JsonSerializerSettings _serializerSettings
Achievement.AchievementCompleted OnAchievementCompleted
AchievementCondition GetCondition(string achievementName, string conditionName)
void AchievementCompleted(Achievement achievement)
T GetCondition< T >(string achievementName, string conditionName)
void RegisterAchievementCategory(string achievementName, AchievementCategory category)
static Terraria.Social.Base.AchievementsSocialModule Achievements
Definition SocialAPI.cs:16
static void ShowFileSavingFailError(Exception exception, string filePath)
static bool Exists(string path, bool cloud)
static void ProtectedInvoke(Action action)
static byte[] ReadAllBytes(string path, bool cloud)
static void Delete(string path, bool cloud, bool forceDeleteFile=false)
static void WriteAllBytes(string path, byte[] data, bool cloud)