您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

392 行
13 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using CIG.Translation;
  5. using SUISSEngine;
  6. using UnityEngine;
  7. namespace Engine.DependencyTree
  8. {
  9. public abstract class DailyAbstractStateDependency<T> : Dependency where T : DailyAbstractState<T>
  10. {
  11. public DailyAbstractStateDependency(string identifier, Dictionary<string, object> persistentState, Dictionary<string, string> properties, DependencyTree.Communicator communicator) : base(identifier, persistentState, properties, communicator)
  12. {
  13. this._listening = false;
  14. base.SetInternallyAchieved(false);
  15. }
  16. public override void OnStart()
  17. {
  18. base.OnStart();
  19. T state = this.GetState();
  20. if (state != null)
  21. {
  22. string property = base.GetProperty("key");
  23. object startOfDayValue = state.GetStartOfDayValue(property);
  24. object value = state.GetValue(property, null);
  25. this.CheckValue(startOfDayValue, value);
  26. }
  27. else
  28. {
  29. base.SetInternallyAchieved(false);
  30. }
  31. this.BeginListening();
  32. }
  33. public override void OnDestroy()
  34. {
  35. base.OnDestroy();
  36. this.EndListening();
  37. }
  38. protected override void OnReset()
  39. {
  40. base.OnReset();
  41. T state = this.GetState();
  42. if (state != null)
  43. {
  44. string property = base.GetProperty("key");
  45. object startOfDayValue = state.GetStartOfDayValue(property);
  46. object value = state.GetValue(property, null);
  47. this.CheckValue(startOfDayValue, value);
  48. }
  49. else
  50. {
  51. base.SetInternallyAchieved(false);
  52. }
  53. }
  54. public override ILocalizedString[] TitleArguments
  55. {
  56. get
  57. {
  58. ILocalizedString[] titleArguments = base.TitleArguments;
  59. string property = base.GetProperty("min");
  60. string property2 = base.GetProperty("max");
  61. string property3 = base.GetProperty("eq");
  62. ILocalizedString localizedString = (property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Literal(property);
  63. ILocalizedString localizedString2 = (property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Literal(property2);
  64. ILocalizedString localizedString3 = (property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Literal(property3);
  65. T state = this.GetState();
  66. if (state != null)
  67. {
  68. object obj = state.GetValue(base.GetProperty("key"), null);
  69. if (obj == null)
  70. {
  71. obj = state.GetStartOfDayValue(base.GetProperty("key"));
  72. }
  73. if (obj is Currencies)
  74. {
  75. string property4 = base.GetProperty("sub-key");
  76. if (property4.Length > 0)
  77. {
  78. obj = ((Currencies)obj).GetValue(property4);
  79. }
  80. }
  81. if (obj is int)
  82. {
  83. localizedString = ((property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledInt(property)));
  84. localizedString2 = ((property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledInt(property2)));
  85. localizedString3 = ((property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledInt(property3)));
  86. }
  87. else if (obj is long)
  88. {
  89. localizedString = ((property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledLong(property)));
  90. localizedString2 = ((property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledLong(property2)));
  91. localizedString3 = ((property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledLong(property3)));
  92. }
  93. else if (obj is decimal)
  94. {
  95. localizedString = ((property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(decimal.Parse(property, CultureInfo.InvariantCulture)));
  96. localizedString2 = ((property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(decimal.Parse(property2, CultureInfo.InvariantCulture)));
  97. localizedString3 = ((property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(decimal.Parse(property3, CultureInfo.InvariantCulture)));
  98. }
  99. }
  100. List<ILocalizedString> list = new List<ILocalizedString>(titleArguments);
  101. if (localizedString != null && localizedString != Localization.EmptyLocalizedString)
  102. {
  103. list.Add(localizedString);
  104. }
  105. if (localizedString2 != null && localizedString2 != Localization.EmptyLocalizedString)
  106. {
  107. list.Add(localizedString2);
  108. }
  109. if (localizedString3 != null && localizedString3 != Localization.EmptyLocalizedString)
  110. {
  111. list.Add(localizedString3);
  112. }
  113. return list.ToArray();
  114. }
  115. }
  116. public override int ProgressValue
  117. {
  118. get
  119. {
  120. return this._progress;
  121. }
  122. }
  123. public override int ProgressMaximumValue
  124. {
  125. get
  126. {
  127. string property = base.GetProperty("min");
  128. string property2 = base.GetProperty("max");
  129. string property3 = base.GetProperty("eq");
  130. int num;
  131. int num2;
  132. int num3;
  133. try
  134. {
  135. num = ((property.Length <= 0) ? int.MinValue : int.Parse(property, CultureInfo.InvariantCulture));
  136. num2 = ((property2.Length <= 0) ? int.MaxValue : int.Parse(property2, CultureInfo.InvariantCulture));
  137. num3 = ((property3.Length <= 0) ? int.MaxValue : int.Parse(property3, CultureInfo.InvariantCulture));
  138. }
  139. catch
  140. {
  141. return 0;
  142. }
  143. if (num3 != 2147483647)
  144. {
  145. return num3;
  146. }
  147. if (num != -2147483648)
  148. {
  149. return num;
  150. }
  151. if (num2 != 2147483647)
  152. {
  153. return num2;
  154. }
  155. return 0;
  156. }
  157. }
  158. protected abstract T GetState();
  159. private int getInt(string intValue)
  160. {
  161. return int.Parse(intValue, CultureInfo.InvariantCulture);
  162. }
  163. private int getScaledInt(string intValue)
  164. {
  165. return Mathf.FloorToInt((float)this.getInt(intValue) / base.ValueModifier);
  166. }
  167. private long getLong(string longValue)
  168. {
  169. return long.Parse(longValue, CultureInfo.InvariantCulture);
  170. }
  171. private long getScaledLong(string longValue)
  172. {
  173. return (long)((float)this.getLong(longValue) / base.ValueModifier);
  174. }
  175. private void CheckValue(object startOfDayValue, object newValue)
  176. {
  177. string property = base.GetProperty("min");
  178. string property2 = base.GetProperty("max");
  179. string property3 = base.GetProperty("eq");
  180. try
  181. {
  182. bool internallyAchieved;
  183. if (newValue == null)
  184. {
  185. internallyAchieved = false;
  186. }
  187. else if (newValue is int)
  188. {
  189. int num = (int)newValue;
  190. if (startOfDayValue != null)
  191. {
  192. num -= (int)startOfDayValue;
  193. }
  194. int num2 = (property.Length <= 0) ? int.MinValue : int.Parse(property, CultureInfo.InvariantCulture);
  195. int num3 = (property2.Length <= 0) ? int.MaxValue : int.Parse(property2, CultureInfo.InvariantCulture);
  196. int num4 = (property3.Length <= 0) ? int.MaxValue : int.Parse(property3, CultureInfo.InvariantCulture);
  197. if (num4 != 2147483647)
  198. {
  199. internallyAchieved = (num == num4);
  200. }
  201. else
  202. {
  203. internallyAchieved = (num >= num2 && num <= num3);
  204. }
  205. this._progress = num;
  206. }
  207. else if (newValue is long)
  208. {
  209. long num5 = (long)newValue;
  210. if (startOfDayValue != null)
  211. {
  212. num5 -= (long)startOfDayValue;
  213. }
  214. long num6 = (property.Length <= 0) ? long.MinValue : long.Parse(property, CultureInfo.InvariantCulture);
  215. long num7 = (property2.Length <= 0) ? long.MaxValue : long.Parse(property2, CultureInfo.InvariantCulture);
  216. long num8 = (property3.Length <= 0) ? long.MaxValue : long.Parse(property3, CultureInfo.InvariantCulture);
  217. if (num8 != 9223372036854775807L)
  218. {
  219. internallyAchieved = (num5 == num8);
  220. }
  221. else
  222. {
  223. internallyAchieved = (num5 >= num6 && num5 <= num7);
  224. }
  225. this._progress = (int)num5;
  226. }
  227. else if (newValue is float)
  228. {
  229. float num9 = (float)newValue;
  230. if (startOfDayValue != null)
  231. {
  232. num9 -= (float)startOfDayValue;
  233. }
  234. float num10 = (property.Length <= 0) ? float.MinValue : float.Parse(property, CultureInfo.InvariantCulture);
  235. float num11 = (property2.Length <= 0) ? float.MaxValue : float.Parse(property2, CultureInfo.InvariantCulture);
  236. float num12 = (property3.Length <= 0) ? float.NaN : float.Parse(property3, CultureInfo.InvariantCulture);
  237. if (!float.IsNaN(num12))
  238. {
  239. internallyAchieved = Mathf.Approximately(num9, num12);
  240. }
  241. else
  242. {
  243. internallyAchieved = (num9 >= num10 && num9 <= num11);
  244. }
  245. this._progress = (int)num9;
  246. }
  247. else if (newValue is double)
  248. {
  249. double num13 = (double)newValue;
  250. if (startOfDayValue != null)
  251. {
  252. num13 -= (double)startOfDayValue;
  253. }
  254. double num14 = (property.Length <= 0) ? double.MinValue : double.Parse(property, CultureInfo.InvariantCulture);
  255. double num15 = (property2.Length <= 0) ? double.MaxValue : double.Parse(property2, CultureInfo.InvariantCulture);
  256. double num16 = (property3.Length <= 0) ? double.NaN : double.Parse(property3, CultureInfo.InvariantCulture);
  257. if (!double.IsNaN(num16))
  258. {
  259. internallyAchieved = (Math.Abs(num13 - num16) <= 9.8813129168249309E-324);
  260. }
  261. else
  262. {
  263. internallyAchieved = (num13 >= num14 && num13 <= num15);
  264. }
  265. this._progress = (int)num13;
  266. }
  267. else if (newValue is decimal)
  268. {
  269. decimal num17 = (decimal)newValue;
  270. if (startOfDayValue != null)
  271. {
  272. num17 -= (decimal)startOfDayValue;
  273. }
  274. decimal d = (property.Length <= 0) ? decimal.MinValue : decimal.Parse(property, CultureInfo.InvariantCulture);
  275. decimal d2 = (property2.Length <= 0) ? decimal.MaxValue : decimal.Parse(property2, CultureInfo.InvariantCulture);
  276. decimal num18 = (property3.Length <= 0) ? 12345678900000000000000000000m : decimal.Parse(property3, CultureInfo.InvariantCulture);
  277. if (num18 != 12345678900000000000000000000m)
  278. {
  279. internallyAchieved = (Math.Abs(num17 - num18) <= 0.0000000000000000000000000002m);
  280. }
  281. else
  282. {
  283. internallyAchieved = (num17 >= d && num17 <= d2);
  284. }
  285. this._progress = (int)num17;
  286. }
  287. else
  288. {
  289. if (!(newValue is Currencies))
  290. {
  291. throw new InvalidOperationException("GameState property " + base.GetProperty("key") + " is of an unsupported type: " + newValue.GetType().ToString());
  292. }
  293. string property4 = base.GetProperty("sub-key");
  294. if (property4.Length == 0)
  295. {
  296. throw new InvalidOperationException("GameState property " + base.GetProperty("key") + " is of type Currencies, but property 'sub-key' is missing.");
  297. }
  298. decimal num19 = ((Currencies)newValue).GetValue(property4);
  299. if (startOfDayValue != null)
  300. {
  301. num19 -= ((Currencies)startOfDayValue).GetValue(property4);
  302. }
  303. decimal d3 = (property.Length <= 0) ? decimal.MinValue : decimal.Parse(property, CultureInfo.InvariantCulture);
  304. decimal d4 = (property2.Length <= 0) ? decimal.MaxValue : decimal.Parse(property2, CultureInfo.InvariantCulture);
  305. decimal num20 = (property3.Length <= 0) ? decimal.MaxValue : decimal.Parse(property3, CultureInfo.InvariantCulture);
  306. if (num20 != 79228162514264337593543950335m)
  307. {
  308. internallyAchieved = (num19 == num20);
  309. }
  310. else
  311. {
  312. internallyAchieved = (num19 >= d3 && num19 <= d4);
  313. }
  314. this._progress = (int)num19;
  315. }
  316. base.SetInternallyAchieved(internallyAchieved);
  317. }
  318. catch (Exception ex)
  319. {
  320. UnityEngine.Debug.LogWarning("Unable to check GameStateDependency " + base.Identifier + ": " + ex.ToString());
  321. }
  322. this.FireProgressValueChangedEvent(this._progress);
  323. }
  324. private void BeginListening()
  325. {
  326. if (!this._listening)
  327. {
  328. T state = this.GetState();
  329. if (state != null)
  330. {
  331. state.ValueChangedEvent += this.StateValueChanged;
  332. this._listening = true;
  333. }
  334. }
  335. }
  336. private void EndListening()
  337. {
  338. if (this._listening)
  339. {
  340. T state = this.GetState();
  341. if (state != null)
  342. {
  343. state.ValueChangedEvent -= this.StateValueChanged;
  344. }
  345. this._listening = false;
  346. }
  347. }
  348. private void StateValueChanged(string key, object oldValue, object newValue)
  349. {
  350. if (key == base.GetProperty("key"))
  351. {
  352. T state = this.GetState();
  353. this.CheckValue(state.GetStartOfDayValue(key), newValue);
  354. }
  355. }
  356. public const string StateKeyPropertyName = "key";
  357. public const string StateSubKeyPropertyName = "sub-key";
  358. public const string MinValuePropertyName = "min";
  359. public const string MaxValuePropertyName = "max";
  360. public const string EqualsValuePropertyName = "eq";
  361. private const decimal decimalNaN = 12345678900000000000000000000m;
  362. private const decimal decimalEpsilon = 0.0000000000000000000000000001m;
  363. private bool _listening;
  364. private int _progress;
  365. }
  366. }