AchievementManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using I2.Loc;
  5. using UnityEngine;
  6. public class AchievementManager : SingletonMono<AchievementManager>
  7. {
  8. private void Awake()
  9. {
  10. this._originalLanguageInfoDictionary = new Dictionary<string, string>
  11. {
  12. {
  13. "English (United States)",
  14. this._defaultLanguageInfo.text
  15. },
  16. {
  17. "Chinese (Simplified)",
  18. this._simpChineseLanguageInfo.text
  19. },
  20. {
  21. "Chinese (Traditional)",
  22. this._tradChineseLanguageInfo.text
  23. }
  24. };
  25. foreach (KeyValuePair<string, string> keyValuePair in this._originalLanguageInfoDictionary)
  26. {
  27. XmlNodeList xmlInfo = this.GetXmlInfo(keyValuePair.Value);
  28. this._achievementInfoDictionary.Add(keyValuePair.Key, new Dictionary<int, AchievementManager.AchievementInfo>());
  29. this.SetDictionaryInfo(xmlInfo, this._achievementInfoDictionary[keyValuePair.Key]);
  30. }
  31. }
  32. public bool AwardAchievement(int index)
  33. {
  34. if (this.GetAchievementUnlockState(index))
  35. {
  36. return false;
  37. }
  38. this.UnlockAchievement(index);
  39. R.Ui.TrophyNotification.AwardTrophy(this.GetAchievementInfo(index).Name, index.ToString());
  40. this.UnlockHastur();
  41. return true;
  42. }
  43. public void AwardAll()
  44. {
  45. for (int i = 1; i < AchievementManager.TrophyMaxCount; i++)
  46. {
  47. this.AwardAchievement(i);
  48. }
  49. }
  50. public AchievementManager.AchievementInfo GetAchievementInfo(int index)
  51. {
  52. bool flag = this._achievementInfoDictionary.ContainsKey(LocalizationManager.CurrentLanguage);
  53. return this._achievementInfoDictionary[(!flag) ? "English (United States)" : LocalizationManager.CurrentLanguage][index];
  54. }
  55. public bool GetAchievementUnlockState(int index)
  56. {
  57. return R.Settings.AchievementInfo.Contains(index);
  58. }
  59. private void UnlockAchievement(int index)
  60. {
  61. if (!this.GetAchievementUnlockState(index))
  62. {
  63. R.Settings.AchievementInfo.Add(index);
  64. }
  65. R.Settings.Save();
  66. }
  67. private void UnlockHastur()
  68. {
  69. if (R.Settings.AchievementInfo.Count == AchievementManager.TrophyMaxCount - 1)
  70. {
  71. this.AwardAchievement(0);
  72. }
  73. }
  74. private XmlNodeList GetXmlInfo(string text)
  75. {
  76. XmlDocument xmlDocument = new XmlDocument();
  77. xmlDocument.LoadXml(text);
  78. return xmlDocument.SelectNodes("trophyconf/trophy");
  79. }
  80. private void SetDictionaryInfo(XmlNodeList nodeList, Dictionary<int, AchievementManager.AchievementInfo> dictionary)
  81. {
  82. for (int i = 0; i < nodeList.Count; i++)
  83. {
  84. XmlNode xmlNode = nodeList.Item(i);
  85. if (xmlNode != null)
  86. {
  87. dictionary.Add(i, new AchievementManager.AchievementInfo
  88. {
  89. Id = i,
  90. Name = xmlNode.ChildNodes[0].InnerText,
  91. Detail = xmlNode.ChildNodes[1].InnerText
  92. });
  93. }
  94. }
  95. }
  96. private static readonly int TrophyMaxCount = 34;
  97. private readonly Dictionary<string, Dictionary<int, AchievementManager.AchievementInfo>> _achievementInfoDictionary = new Dictionary<string, Dictionary<int, AchievementManager.AchievementInfo>>();
  98. [SerializeField]
  99. private TextAsset _defaultLanguageInfo;
  100. private Dictionary<string, string> _originalLanguageInfoDictionary = new Dictionary<string, string>();
  101. [SerializeField]
  102. private TextAsset _simpChineseLanguageInfo;
  103. [SerializeField]
  104. private TextAsset _tradChineseLanguageInfo;
  105. public struct AchievementInfo
  106. {
  107. public int Id;
  108. public string Name;
  109. public string Detail;
  110. }
  111. }