Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
OriginalCommunityContentManager.cs
浏览该文件的文档.
1using System.Globalization;
2using System.Security.Cryptography;
3using System.Text;
4using System.Xml.Linq;
5using Engine;
6using Game;
7using XmlUtilities;
8
9public static class OriginalCommunityContentManager {
10
11 public static Dictionary<string, string> m_idToAddressMap = new();
12
13 public static Dictionary<string, bool> m_feedbackCache = new();
14
15 public const string fName1 = "CommunityContentManager";
16
17 public static void Initialize() {
18 Load();
19 WorldsManager.WorldDeleted += delegate(string path) { m_idToAddressMap.Remove(MakeContentIdString(ExternalContentType.World, path)); };
20 BlocksTexturesManager.BlocksTextureDeleted += delegate(string path) {
21 m_idToAddressMap.Remove(MakeContentIdString(ExternalContentType.BlocksTexture, path));
22 };
23 CharacterSkinsManager.CharacterSkinDeleted += delegate(string path) {
24 m_idToAddressMap.Remove(MakeContentIdString(ExternalContentType.CharacterSkin, path));
25 };
26 FurniturePacksManager.FurniturePackDeleted += delegate(string path) {
27 m_idToAddressMap.Remove(MakeContentIdString(ExternalContentType.FurniturePack, path));
28 };
29 Window.Deactivated += delegate { Save(); };
30 }
31
32 public static string GetDownloadedContentAddress(ExternalContentType type, string name) {
33 m_idToAddressMap.TryGetValue(MakeContentIdString(type, name), out string value);
34 return value;
35 }
36
37 public static bool IsContentRated(string address, string userId) {
38 string key = MakeFeedbackCacheKey(address, "Rating", userId);
39 return m_feedbackCache.ContainsKey(key);
40 }
41
42 public static void List(string cursor,
43 string userId,
44 string userUrl,
45 string type,
46 string moderation,
47 string search,
48 string sortOrder,
49 CancellableProgress progress,
50 Action<List<OriginalCommunityContentEntry>, string> success,
51 Action<Exception> failure) {
52 progress = progress ?? new CancellableProgress();
54 failure(new InvalidOperationException(LanguageControl.Get(fName1, "1")));
55 return;
56 }
57 Dictionary<string, string> dictionary = new();
58 dictionary.Add("Action", "list");
59 dictionary.Add("Cursor", cursor ?? string.Empty);
60 dictionary.Add("UserId", userId ?? string.Empty);
61 dictionary.Add("UserUrl", userUrl ?? string.Empty);
62 dictionary.Add("Type", type ?? string.Empty);
63 dictionary.Add("Moderation", moderation ?? string.Empty);
64 dictionary.Add("Search", search ?? string.Empty);
65 dictionary.Add("SortOrder", sortOrder ?? string.Empty);
66 dictionary.Add("Platform", VersionsManager.PlatformString);
67 dictionary.Add("Version", VersionsManager.Version);
70 null,
71 null,
73 progress,
74 delegate(byte[] result) {
75 try {
76 XElement xElement = XmlUtils.LoadXmlFromString(Encoding.UTF8.GetString(result, 0, result.Length), true);
77 string attributeValue = XmlUtils.GetAttributeValue<string>(xElement, "NextCursor");
79 string downloadString = LanguageControl.Get("OriginalCommunityContentScreen", "10");
80 foreach (XElement item in xElement.Elements()) {
81 try {
82 list.Add(
84 Type = XmlUtils.GetAttributeValue(item, "Type", ExternalContentType.Unknown),
85 Name = XmlUtils.GetAttributeValue<string>(item, "Name"),
86 Url = XmlUtils.GetAttributeValue<string>(item, "Url"),
87 Version = XmlUtils.GetAttributeValue(item, "Version", new Version(0, 0)),
88 Size = XmlUtils.GetAttributeValue<long>(item, "Size"),
89 ExtraText = XmlUtils.GetAttributeValue(item, "ExtraText", string.Empty).Replace("downloads", downloadString),
90 RatingsAverage = XmlUtils.GetAttributeValue(item, "RatingsAverage", 0f)
91 }
92 );
93 }
94 catch (Exception) {
95 // ignored
96 }
97 }
98 success(list, attributeValue);
99 }
100 catch (Exception obj) {
101 failure(obj);
102 }
103 },
104 delegate(Exception error) { failure(error); }
105 );
106 }
107
108 public static void Download(string address,
109 string name,
111 string userId,
112 CancellableProgress progress,
113 Action success,
114 Action<Exception> failure) {
115 progress = progress ?? new CancellableProgress();
117 failure(new InvalidOperationException(LanguageControl.Get(fName1, "1")));
118 return;
119 }
121 address,
122 null,
123 null,
124 progress,
125 delegate(byte[] data) {
126 string hash = CalculateContentHashString(data);
128 new MemoryStream(data),
129 type,
130 name,
131 delegate(string downloadedName) {
132 m_idToAddressMap[MakeContentIdString(type, downloadedName)] = address;
133 Feedback(
134 address,
135 "Success",
136 null,
137 hash,
138 data.Length,
139 userId,
140 progress,
141 delegate { },
142 delegate { }
143 );
144 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Download Success", new AnalyticsParameter("Name", name));
145 success();
146 },
147 delegate(Exception error) {
148 Feedback(
149 address,
150 "ImportFailure",
151 null,
152 hash,
153 data.Length,
154 userId,
155 null,
156 delegate { },
157 delegate { }
158 );
159 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Import Failure", new AnalyticsParameter("Name", name), new AnalyticsParameter("Error", error.Message.ToString()));
160 failure(error);
161 }
162 );
163 },
164 delegate(Exception error) {
165 Feedback(
166 address,
167 "DownloadFailure",
168 null,
169 null,
170 0L,
171 userId,
172 null,
173 delegate { },
174 delegate { }
175 );
176 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Download Failure", new AnalyticsParameter("Name", name), new AnalyticsParameter("Error", error.Message.ToString()));
177 failure(error);
178 }
179 );
180 }
181
182 public static void Publish(string address,
183 string name,
185 string userId,
186 CancellableProgress progress,
187 Action success,
188 Action<Exception> failure) {
189 progress = progress ?? new CancellableProgress();
191 failure(new InvalidOperationException(LanguageControl.Get(fName1, "2")));
192 return;
193 }
195 failure(new InvalidOperationException(LanguageControl.Get(fName1, "1")));
196 return;
197 }
199 address,
200 name,
201 type,
202 progress,
203 delegate(byte[] data) {
204 string value = CalculateContentHashString(data);
207 null,
208 null,
210 new Dictionary<string, string> {
211 { "Action", "publish" },
212 { "UserId", userId },
213 { "Name", name },
214 { "Url", address },
215 { "Type", type.ToString() },
216 { "Hash", value },
217 { "Size", data.Length.ToString(CultureInfo.InvariantCulture) },
218 { "Platform", VersionsManager.PlatformString },
219 { "Version", VersionsManager.Version }
220 }
221 ),
222 progress,
223 delegate {
224 success();
225 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Publish Success", new AnalyticsParameter("Name", name), new AnalyticsParameter("Type", type.ToString()), new AnalyticsParameter("Size", data.Length.ToString()), new AnalyticsParameter("User", userId));
226 },
227 delegate(Exception error) {
228 failure(error);
229 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Publish Failure", new AnalyticsParameter("Name", name), new AnalyticsParameter("Type", type.ToString()), new AnalyticsParameter("Size", data.Length.ToString()), new AnalyticsParameter("User", userId), new AnalyticsParameter("Error", error.Message.ToString()));
230 }
231 );
232 },
233 failure
234 );
235 }
236
237 public static void Delete(string address, string userId, CancellableProgress progress, Action success, Action<Exception> failure) {
238 progress = progress ?? new CancellableProgress();
240 failure(new InvalidOperationException(LanguageControl.Get(fName1, "1")));
241 return;
242 }
243 Dictionary<string, string> dictionary = new();
244 dictionary.Add("Action", "delete");
245 dictionary.Add("UserId", userId);
246 dictionary.Add("Url", address);
247 dictionary.Add("Platform", VersionsManager.PlatformString);
248 dictionary.Add("Version", VersionsManager.Version);
251 null,
252 null,
254 progress,
255 delegate {
256 success();
257 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Delete Success", new AnalyticsParameter("Name", address), new AnalyticsParameter("User", userId));
258 },
259 delegate(Exception error) {
260 failure(error);
261 //AnalyticsManager.LogEvent("[OriginalCommunityContentManager] Delete Failure", new AnalyticsParameter("Name", address), new AnalyticsParameter("User", userId), new AnalyticsParameter("Error", error.Message.ToString()));
262 }
263 );
264 }
265
266 public static void Rate(string address, string userId, int rating, CancellableProgress progress, Action success, Action<Exception> failure) {
267 rating = Math.Clamp(rating, 1, 5);
268 Feedback(
269 address,
270 "Rating",
271 rating.ToString(CultureInfo.InvariantCulture),
272 null,
273 0L,
274 userId,
275 progress,
276 success,
277 failure
278 );
279 }
280
281 public static void Report(string address, string userId, string report, CancellableProgress progress, Action success, Action<Exception> failure) {
282 Feedback(
283 address,
284 "Report",
285 report,
286 null,
287 0L,
288 userId,
289 progress,
290 success,
291 failure
292 );
293 }
294
295 public static void SendPlayTime(string address,
296 string userId,
297 double time,
298 CancellableProgress progress,
299 Action success,
300 Action<Exception> failure) {
301 Feedback(
302 address,
303 "PlayTime",
304 Math.Round(time).ToString(CultureInfo.InvariantCulture),
305 null,
306 0L,
307 userId,
308 progress,
309 success,
310 failure
311 );
312 }
313
314 // ReSharper disable UnusedParameter.Local
315 static void VerifyLinkContent(string address,
316 string name,
318 CancellableProgress progress,
319 Action<byte[]> success,
320 Action<Exception> failure)
321 // ReSharper restore UnusedParameter.Local
322 {
323 progress = progress ?? new CancellableProgress();
325 address,
326 null,
327 null,
328 progress,
329 delegate(byte[] data) {
331 new MemoryStream(data),
332 type,
333 "__Temp",
334 delegate(string downloadedName) {
335 ExternalContentManager.DeleteExternalContent(type, downloadedName);
336 success(data);
337 },
338 failure
339 );
340 },
341 failure
342 );
343 }
344
345 static void Feedback(string address,
346 string feedback,
347 string feedbackParameter,
348 string hash,
349 long size,
350 string userId,
351 CancellableProgress progress,
352 Action success,
353 Action<Exception> failure) {
354 progress = progress ?? new CancellableProgress();
356 failure(new InvalidOperationException(LanguageControl.Get(fName1, "1")));
357 return;
358 }
359 string key = MakeFeedbackCacheKey(address, feedback, userId);
360 if (!m_feedbackCache.TryAdd(key, true)) {
361 Task.Run(
362 delegate {
363 Task.Delay(1500).Wait();
364 failure(new InvalidOperationException(LanguageControl.Get(fName1, "3")));
365 }
366 );
367 return;
368 }
369 Dictionary<string, string> dictionary = new();
370 dictionary.Add("Action", "feedback");
371 dictionary.Add("Feedback", feedback);
372 if (feedbackParameter != null) {
373 dictionary.Add("FeedbackParameter", feedbackParameter);
374 }
375 dictionary.Add("UserId", userId);
376 if (address != null) {
377 dictionary.Add("Url", address);
378 }
379 if (hash != null) {
380 dictionary.Add("Hash", hash);
381 }
382 if (size > 0) {
383 dictionary.Add("Size", size.ToString(CultureInfo.InvariantCulture));
384 }
385 dictionary.Add("Platform", VersionsManager.PlatformString);
386 dictionary.Add("Version", VersionsManager.Version);
389 null,
390 null,
392 progress,
393 delegate { success(); },
394 delegate(Exception error) { failure(error); }
395 );
396 }
397
398 static string CalculateContentHashString(byte[] data) => Convert.ToBase64String(SHA1.HashData(data));
399
400 static string MakeFeedbackCacheKey(string address, string feedback, string userId) => $"{address}\n{feedback}\n{userId}";
401
402 static string MakeContentIdString(ExternalContentType type, string name) => $"{type}:{name}";
403
404 static void Load() {
405 try {
407 return;
408 }
410 XElement xElement = XmlUtils.LoadXmlFromStream(stream, null, true);
411 IEnumerable<XElement> feedbackElements = xElement.Element("Feedback")?.Elements();
412 if (feedbackElements != null) {
413 foreach (XElement item in feedbackElements) {
414 string attributeValue = XmlUtils.GetAttributeValue<string>(item, "Key");
415 m_feedbackCache[attributeValue] = true;
416 }
417 }
418 IEnumerable<XElement> contentElements = xElement.Element("Content")?.Elements();
419 if (contentElements != null) {
420 foreach (XElement item2 in contentElements) {
421 string attributeValue2 = XmlUtils.GetAttributeValue<string>(item2, "Path");
422 string attributeValue3 = XmlUtils.GetAttributeValue<string>(item2, "Address");
423 m_idToAddressMap[attributeValue2] = attributeValue3;
424 }
425 }
426 }
427 catch (Exception e) {
429 }
430 }
431
432 static void Save() {
433 try {
434 XElement xElement = new("Cache");
435 XElement xElement2 = new("Feedback");
436 xElement.Add(xElement2);
437 foreach (string key in m_feedbackCache.Keys) {
438 XElement xElement3 = new("Item");
439 XmlUtils.SetAttributeValue(xElement3, "Key", key);
440 xElement2.Add(xElement3);
441 }
442 XElement xElement4 = new("Content");
443 xElement.Add(xElement4);
444 foreach (KeyValuePair<string, string> item in m_idToAddressMap) {
445 XElement xElement5 = new("Item");
446 XmlUtils.SetAttributeValue(xElement5, "Path", item.Key);
447 XmlUtils.SetAttributeValue(xElement5, "Address", item.Value);
448 xElement4.Add(xElement5);
449 }
451 XmlUtils.SaveXmlToStream(xElement, stream, null, true);
452 }
453 catch (Exception e) {
455 }
456 }
457}
static Stream OpenFile(string path, OpenFileMode openFileMode)
static bool FileExists(string path)
static Action Deactivated
static Action< string > BlocksTextureDeleted
static Action< string > CharacterSkinDeleted
static void ReportExceptionToUser(string additionalMessage, Exception e)
static void ImportExternalContent(Stream stream, ExternalContentType type, string name, Action< string > success, Action< Exception > failure)
static void DeleteExternalContent(ExternalContentType type, string name)
static Action< string > FurniturePackDeleted
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static void Post(string address, Dictionary< string, string > parameters, Dictionary< string, string > headers, Stream data, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)
static bool IsInternetConnectionAvailable()
static MemoryStream UrlParametersToStream(Dictionary< string, string > values)
static void Get(string address, Dictionary< string, string > parameters, Dictionary< string, string > headers, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)
static Action< string > WorldDeleted
static string OriginalCommunityContentCachePath
static void SendPlayTime(string address, string userId, double time, CancellableProgress progress, Action success, Action< Exception > failure)
static void Download(string address, string name, ExternalContentType type, string userId, CancellableProgress progress, Action success, Action< Exception > failure)
static string MakeContentIdString(ExternalContentType type, string name)
static Dictionary< string, string > m_idToAddressMap
static bool IsContentRated(string address, string userId)
static string GetDownloadedContentAddress(ExternalContentType type, string name)
static void Delete(string address, string userId, CancellableProgress progress, Action success, Action< Exception > failure)
static void VerifyLinkContent(string address, string name, ExternalContentType type, CancellableProgress progress, Action< byte[]> success, Action< Exception > failure)
static string MakeFeedbackCacheKey(string address, string feedback, string userId)
static void Feedback(string address, string feedback, string feedbackParameter, string hash, long size, string userId, CancellableProgress progress, Action success, Action< Exception > failure)
static void Rate(string address, string userId, int rating, CancellableProgress progress, Action success, Action< Exception > failure)
static void Publish(string address, string name, ExternalContentType type, string userId, CancellableProgress progress, Action success, Action< Exception > failure)
static void List(string cursor, string userId, string userUrl, string type, string moderation, string search, string sortOrder, CancellableProgress progress, Action< List< OriginalCommunityContentEntry >, string > success, Action< Exception > failure)
static void Report(string address, string userId, string report, CancellableProgress progress, Action success, Action< Exception > failure)
static Dictionary< string, bool > m_feedbackCache
static void SetAttributeValue(XElement node, string attributeName, object value)
static object GetAttributeValue(XElement node, string attributeName, Type type)
static XElement LoadXmlFromStream(Stream stream, Encoding encoding, bool throwOnError)
static XElement LoadXmlFromString(string data, bool throwOnError)
static void SaveXmlToStream(XElement node, Stream stream, Encoding encoding, bool throwOnError)