Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

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