You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1186 lines
28 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.Runtime.CompilerServices;
  7. using CIG.Translation;
  8. using SparkLinq;
  9. using SUISSEngine;
  10. using UnityEngine;
  11. namespace Engine.DependencyTree
  12. {
  13. public class Dependency
  14. {
  15. public Dependency(string identifier, Dictionary<string, object> persistentState, Dictionary<string, string> properties, DependencyTree.Communicator communicator)
  16. {
  17. if (communicator == null)
  18. {
  19. throw new ArgumentNullException("communicator");
  20. }
  21. this._state = DependencyState.Created;
  22. this._attachedToTree = (identifier == "root");
  23. this._identifier = identifier;
  24. this._persistentState = persistentState;
  25. this._properties = properties;
  26. this._communicator = communicator;
  27. this._parent = null;
  28. this._childIndex = -1;
  29. this._children = new List<Dependency>();
  30. this._readOnlyChildren = this._children.AsReadOnly();
  31. this._internallyAchieved = true;
  32. this._achieved = true;
  33. this._autoGenState = int.MinValue;
  34. if (this._properties != null && this._properties.ContainsKey("requires-ads") && "true".Equals(this._properties["requires-ads"]))
  35. {
  36. this._persistentState["IsActive"] = true;
  37. }
  38. }
  39. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  40. public event Dependency.AchievedChangedEventHandler AchievedChangedEvent;
  41. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  42. public event Dependency.ProgressValueChangedEventHandler ProgressValueChangedEvent;
  43. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  44. public event Dependency.ActiveChangedEventHandler ActiveChangedEvent;
  45. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  46. public event Dependency.RewardClaimedEventHandler RewardClaimedEvent;
  47. public virtual void OnStart()
  48. {
  49. if (this._state != DependencyState.Created)
  50. {
  51. throw new InvalidOperationException("Dependency can only be started when its in the Created state.");
  52. }
  53. this._state = DependencyState.Started;
  54. }
  55. public virtual void OnDestroy()
  56. {
  57. if (this._state == DependencyState.Destroyed)
  58. {
  59. throw new InvalidOperationException("Dependency is already destroyed.");
  60. }
  61. this._state = DependencyState.Destroyed;
  62. }
  63. public virtual void OnBeginMutating()
  64. {
  65. }
  66. public virtual void OnEndMutating()
  67. {
  68. if (this._achieved && this.StayAchieved && this.AchievedCount == 0)
  69. {
  70. this.AchievedCount = 1;
  71. }
  72. int peristentInt = this.GetPeristentInt("AutoGenerated", int.MinValue);
  73. if (peristentInt != -2147483648)
  74. {
  75. int autoGenCount = this.AutoGenCount;
  76. string autoGenTemplate = this.AutoGenTemplate;
  77. int autoGenIterationStart = this.AutoGenIterationStart;
  78. int autoGenIterationStep = this.AutoGenIterationStep;
  79. int autoGenIterationEnd = this.AutoGenIterationEnd;
  80. if (autoGenCount <= 0 || string.IsNullOrEmpty(autoGenTemplate) || peristentInt < autoGenIterationStart)
  81. {
  82. if (this._persistentState != null)
  83. {
  84. this._persistentState.Remove("AutoGenerated");
  85. }
  86. }
  87. else
  88. {
  89. int num = autoGenIterationStart;
  90. while (num <= peristentInt && num <= autoGenIterationEnd)
  91. {
  92. if (num > this._autoGenState)
  93. {
  94. Dependency dependency = this.AutoGenerateChild(num);
  95. if (dependency == null)
  96. {
  97. break;
  98. }
  99. this.AddChild(dependency);
  100. this._autoGenState = num;
  101. }
  102. num += autoGenIterationStep;
  103. }
  104. this.SetPersistentInt("AutoGenerated", this._autoGenState);
  105. }
  106. }
  107. this.CheckAutoGeneratedChildren();
  108. }
  109. protected virtual void OnReset()
  110. {
  111. }
  112. public DependencyState State
  113. {
  114. get
  115. {
  116. return this._state;
  117. }
  118. }
  119. public bool IsAttachedToTree
  120. {
  121. get
  122. {
  123. return this._attachedToTree;
  124. }
  125. }
  126. public string Identifier
  127. {
  128. get
  129. {
  130. return this._identifier;
  131. }
  132. }
  133. public Dependency Parent
  134. {
  135. get
  136. {
  137. return this._parent;
  138. }
  139. }
  140. public int ChildIndex
  141. {
  142. get
  143. {
  144. return this._childIndex;
  145. }
  146. }
  147. public ReadOnlyCollection<Dependency> Children
  148. {
  149. get
  150. {
  151. return this._readOnlyChildren;
  152. }
  153. }
  154. public bool IsInternallyAchieved
  155. {
  156. get
  157. {
  158. return this._internallyAchieved;
  159. }
  160. }
  161. public bool IsAchieved
  162. {
  163. get
  164. {
  165. return this._achieved;
  166. }
  167. }
  168. public int MinChildrenAchieved
  169. {
  170. get
  171. {
  172. string property = this.GetProperty("min-children-achieved");
  173. int maxValue;
  174. if (property.Length == 0 || !int.TryParse(property, out maxValue))
  175. {
  176. maxValue = int.MaxValue;
  177. }
  178. return maxValue;
  179. }
  180. }
  181. public int MaxChildrenAchieved
  182. {
  183. get
  184. {
  185. string property = this.GetProperty("max-children-achieved");
  186. int maxValue;
  187. if (property.Length == 0 || !int.TryParse(property, out maxValue))
  188. {
  189. maxValue = int.MaxValue;
  190. }
  191. return maxValue;
  192. }
  193. }
  194. public bool OrderedChildren
  195. {
  196. get
  197. {
  198. string property = this.GetProperty("ordered-children");
  199. bool result;
  200. if (property.Length == 0 || !bool.TryParse(property, out result))
  201. {
  202. result = false;
  203. }
  204. return result;
  205. }
  206. }
  207. public bool StayAchieved
  208. {
  209. get
  210. {
  211. string property = this.GetProperty("stay-achieved");
  212. bool result;
  213. if (property.Length == 0 || !bool.TryParse(property, out result))
  214. {
  215. result = true;
  216. }
  217. return result;
  218. }
  219. }
  220. public float ValueModifier
  221. {
  222. get
  223. {
  224. string property = this.GetProperty("value-modifier");
  225. float result;
  226. if (property.Length == 0 || !float.TryParse(property, out result))
  227. {
  228. result = 1f;
  229. }
  230. return result;
  231. }
  232. }
  233. public Currencies Reward
  234. {
  235. get
  236. {
  237. string property = this.GetProperty("reward");
  238. Currencies zero;
  239. if (property.Length == 0 || !Currencies.TryParse(property, out zero))
  240. {
  241. zero = Currencies.Zero;
  242. }
  243. return zero;
  244. }
  245. }
  246. public bool IsActive
  247. {
  248. get
  249. {
  250. return this.GetPeristentBool("IsActive", true);
  251. }
  252. set
  253. {
  254. if (value != this.IsActive)
  255. {
  256. this.SetPersistentBool("IsActive", value);
  257. this._communicator.NotifyActiveChanged(this, value);
  258. this.FireActiveChangedEvent(value);
  259. }
  260. }
  261. }
  262. public bool RewardClaimed
  263. {
  264. get
  265. {
  266. return this.GetPeristentBool("RewardClaimed", false);
  267. }
  268. set
  269. {
  270. if (value != this.RewardClaimed)
  271. {
  272. this.SetPersistentBool("RewardClaimed", value);
  273. this._communicator.NotifyRewardClaimed(this, value);
  274. this.FireRewardClaimedEvent(value);
  275. }
  276. }
  277. }
  278. public bool HasTitle
  279. {
  280. get
  281. {
  282. string title = this.Title;
  283. string titleSingular = this.TitleSingular;
  284. string titlePlural = this.TitlePlural;
  285. bool flag = title != null;
  286. bool flag2 = titleSingular != null;
  287. bool flag3 = titlePlural != null;
  288. return flag || flag2 || flag3;
  289. }
  290. }
  291. public bool IsQuantityTitle
  292. {
  293. get
  294. {
  295. string title = this.Title;
  296. string titleSingular = this.TitleSingular;
  297. string titlePlural = this.TitlePlural;
  298. bool flag = title != null;
  299. bool flag2 = titleSingular != null;
  300. bool flag3 = titlePlural != null;
  301. if (!flag && !flag2 && !flag3)
  302. {
  303. return false;
  304. }
  305. if (flag)
  306. {
  307. if (flag2 || flag3)
  308. {
  309. UnityEngine.Debug.LogWarning(string.Concat(new string[]
  310. {
  311. "Dependency ",
  312. this._identifier,
  313. " has a non-quantity string as a title (",
  314. title,
  315. "), but also defines a quantity string as title. Please check your DependencyTree xml-file for mistakes."
  316. }));
  317. }
  318. return false;
  319. }
  320. if (!flag2)
  321. {
  322. UnityEngine.Debug.LogWarning("Dependency " + this._identifier + " has a quantity string as a title, but has no value for the singular variant. Please check your DependencyTree xml-file for mistakes.");
  323. }
  324. if (!flag3)
  325. {
  326. UnityEngine.Debug.LogWarning("Dependency " + this._identifier + " has a quantity string as a title, but has no value for the plural variant. Please check your DependencyTree xml-file for mistakes.");
  327. }
  328. return true;
  329. }
  330. }
  331. public string Title
  332. {
  333. get
  334. {
  335. string property = this.GetProperty("title");
  336. if (string.IsNullOrEmpty(property))
  337. {
  338. return null;
  339. }
  340. return property;
  341. }
  342. }
  343. public string TitleSingular
  344. {
  345. get
  346. {
  347. string property = this.GetProperty("title-singular");
  348. if (string.IsNullOrEmpty(property))
  349. {
  350. return null;
  351. }
  352. return property;
  353. }
  354. }
  355. public string TitlePlural
  356. {
  357. get
  358. {
  359. string property = this.GetProperty("title-plural");
  360. if (string.IsNullOrEmpty(property))
  361. {
  362. return null;
  363. }
  364. return property;
  365. }
  366. }
  367. public virtual ILocalizedString[] TitleArguments
  368. {
  369. get
  370. {
  371. string property = this.GetProperty("title-arguments");
  372. ILocalizedString[] result;
  373. if (string.IsNullOrEmpty(property))
  374. {
  375. result = new ILocalizedString[0];
  376. }
  377. else
  378. {
  379. string[] array = property.Split(new char[]
  380. {
  381. '|'
  382. });
  383. if (Dependency._003C_003Ef__mg_0024cache0 == null)
  384. {
  385. Dependency._003C_003Ef__mg_0024cache0 = new Func<string, ILocalizedString>(Localization.Literal);
  386. }
  387. result = array.ConvertAll(Dependency._003C_003Ef__mg_0024cache0);
  388. }
  389. return result;
  390. }
  391. }
  392. public string SpriteName
  393. {
  394. get
  395. {
  396. string property = this.GetProperty("sprite");
  397. if (string.IsNullOrEmpty(property))
  398. {
  399. return null;
  400. }
  401. return property;
  402. }
  403. }
  404. public string AchievedSpriteName
  405. {
  406. get
  407. {
  408. string property = this.GetProperty("achieved-sprite");
  409. if (string.IsNullOrEmpty(property))
  410. {
  411. return null;
  412. }
  413. return property;
  414. }
  415. }
  416. public int AutoGenCount
  417. {
  418. get
  419. {
  420. string property = this.GetProperty("auto-gen-count");
  421. int result;
  422. if (int.TryParse(property, out result))
  423. {
  424. return result;
  425. }
  426. return 0;
  427. }
  428. }
  429. public string AutoGenTemplate
  430. {
  431. get
  432. {
  433. string property = this.GetProperty("auto-gen-template");
  434. if (string.IsNullOrEmpty(property))
  435. {
  436. return null;
  437. }
  438. return property;
  439. }
  440. }
  441. public int AutoGenIterationStart
  442. {
  443. get
  444. {
  445. string property = this.GetProperty("auto-gen-iteration-start");
  446. int result;
  447. if (int.TryParse(property, out result))
  448. {
  449. return result;
  450. }
  451. return 0;
  452. }
  453. }
  454. public int AutoGenIterationStep
  455. {
  456. get
  457. {
  458. string property = this.GetProperty("auto-gen-iteration-step");
  459. int result;
  460. if (int.TryParse(property, out result))
  461. {
  462. return result;
  463. }
  464. return 1;
  465. }
  466. }
  467. public int AutoGenIterationEnd
  468. {
  469. get
  470. {
  471. string property = this.GetProperty("auto-gen-iteration-end");
  472. int result;
  473. if (int.TryParse(property, out result))
  474. {
  475. return result;
  476. }
  477. return int.MaxValue;
  478. }
  479. }
  480. public virtual int ProgressValue
  481. {
  482. get
  483. {
  484. return 0;
  485. }
  486. }
  487. public float ScaledProgressValue
  488. {
  489. get
  490. {
  491. return (float)this.ProgressValue / this.ValueModifier;
  492. }
  493. }
  494. public virtual int ProgressMaximumValue
  495. {
  496. get
  497. {
  498. return 0;
  499. }
  500. }
  501. public float ScaledProgressMaximumValue
  502. {
  503. get
  504. {
  505. return (float)this.ProgressMaximumValue / this.ValueModifier;
  506. }
  507. }
  508. protected Dictionary<string, object> PersistentState
  509. {
  510. get
  511. {
  512. return this._persistentState;
  513. }
  514. }
  515. protected Dictionary<string, string> Properties
  516. {
  517. get
  518. {
  519. return this._properties;
  520. }
  521. }
  522. protected DependencyTree.Communicator Communicator
  523. {
  524. get
  525. {
  526. return this._communicator;
  527. }
  528. }
  529. private int AchievedCount
  530. {
  531. get
  532. {
  533. return this.GetPeristentInt("AchievedCount", 0);
  534. }
  535. set
  536. {
  537. this.SetPersistentInt("AchievedCount", value);
  538. }
  539. }
  540. public void Traverse(Dependency.Traverser traverser)
  541. {
  542. traverser(this);
  543. for (int i = 0; i < this._children.Count; i++)
  544. {
  545. Dependency dependency = this._children[i];
  546. dependency.Traverse(traverser);
  547. }
  548. }
  549. public bool AddChild(Dependency child)
  550. {
  551. return this.InsertChild(this._children.Count, child);
  552. }
  553. public bool InsertChild(int index, Dependency child)
  554. {
  555. if (index < 0 || index > this._children.Count)
  556. {
  557. throw new IndexOutOfRangeException(string.Concat(new object[]
  558. {
  559. "Child insertion index ",
  560. index,
  561. " is out of range (",
  562. this._children.Count,
  563. " children)."
  564. }));
  565. }
  566. if (child == null)
  567. {
  568. throw new ArgumentNullException("child");
  569. }
  570. if (child._parent != null)
  571. {
  572. throw new ArgumentException("Child dependency already has a parent.", "child");
  573. }
  574. if (child._communicator.Tree != this._communicator.Tree)
  575. {
  576. throw new ArgumentException("The specified child was not created for this Dependency's DependencyTree.");
  577. }
  578. this._children.Insert(index, child);
  579. child.Traverse(delegate(Dependency c)
  580. {
  581. c._attachedToTree = this._attachedToTree;
  582. });
  583. child._parent = this;
  584. child._childIndex = index;
  585. for (int i = index + 1; i < this._children.Count; i++)
  586. {
  587. this._children[i]._childIndex = i;
  588. }
  589. try
  590. {
  591. if (this._attachedToTree)
  592. {
  593. this._communicator.NotifySubTreeAttached(this, child);
  594. }
  595. }
  596. catch (Exception ex)
  597. {
  598. this._children.Remove(child);
  599. child.Traverse(delegate(Dependency c)
  600. {
  601. c._attachedToTree = false;
  602. });
  603. child._parent = null;
  604. child._childIndex = -1;
  605. for (int j = index; j < this._children.Count; j++)
  606. {
  607. this._children[j]._childIndex = j;
  608. }
  609. throw ex;
  610. }
  611. child.OnAttachedToParent(this);
  612. this.OnChildAttached(child);
  613. return true;
  614. }
  615. public Dependency GetChild(Predicate<Dependency> predicate)
  616. {
  617. return this._children.Find(predicate);
  618. }
  619. public bool RemoveChild(Dependency child)
  620. {
  621. if (child == null || child._parent != this)
  622. {
  623. return false;
  624. }
  625. if (this._children.Remove(child))
  626. {
  627. int childIndex = child._childIndex;
  628. child.Traverse(delegate(Dependency c)
  629. {
  630. c._attachedToTree = false;
  631. });
  632. child._parent = null;
  633. child._childIndex = -1;
  634. for (int i = childIndex; i < this._children.Count; i++)
  635. {
  636. this._children[i]._childIndex = i;
  637. }
  638. try
  639. {
  640. if (this._attachedToTree)
  641. {
  642. this._communicator.NotifySubTreeDetached(this, child);
  643. }
  644. }
  645. catch (Exception ex)
  646. {
  647. this._children.Insert(childIndex, child);
  648. child.Traverse(delegate(Dependency c)
  649. {
  650. c._attachedToTree = this._attachedToTree;
  651. });
  652. child._parent = this;
  653. child._childIndex = childIndex;
  654. for (int j = childIndex + 1; j < this._children.Count; j++)
  655. {
  656. this._children[j]._childIndex = j;
  657. }
  658. throw ex;
  659. }
  660. child.OnDetachedFromParent(this);
  661. this.OnChildDetached(child);
  662. return true;
  663. }
  664. return false;
  665. }
  666. public void Reset(Dictionary<string, string> overriddenProperties)
  667. {
  668. if (!this._communicator.Tree.IsMutating)
  669. {
  670. throw new InvalidOperationException("Can't reset dependency while tree is not mutating.");
  671. }
  672. if (this._persistentState != null)
  673. {
  674. this._persistentState.Clear();
  675. if (overriddenProperties != null)
  676. {
  677. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  678. foreach (KeyValuePair<string, string> keyValuePair in overriddenProperties)
  679. {
  680. dictionary[keyValuePair.Key] = keyValuePair.Value;
  681. }
  682. this._persistentState["OverriddenProperties"] = dictionary;
  683. }
  684. }
  685. else if (overriddenProperties != null)
  686. {
  687. UnityEngine.Debug.LogWarning("You are trying to override properties for a dependency, but the dependency has no persistent state.");
  688. }
  689. this.OnReset();
  690. this.CheckAchieved();
  691. }
  692. public string GetProperty(string key)
  693. {
  694. object obj;
  695. object obj2;
  696. if (this._persistentState != null && this._persistentState.TryGetValue("OverriddenProperties", out obj) && ((Dictionary<string, object>)obj).TryGetValue(key, out obj2))
  697. {
  698. if (obj2 is string)
  699. {
  700. return (string)obj2;
  701. }
  702. return string.Empty;
  703. }
  704. else
  705. {
  706. string text;
  707. if (this._properties == null || !this._properties.TryGetValue(key, out text))
  708. {
  709. return string.Empty;
  710. }
  711. if (text == null)
  712. {
  713. return string.Empty;
  714. }
  715. return text;
  716. }
  717. }
  718. public Dependency GetFirstIncompletedChild()
  719. {
  720. Dependency dependency = null;
  721. int count = this.Children.Count;
  722. for (int i = 0; i < count; i++)
  723. {
  724. Dependency dependency2 = this.Children[i];
  725. if (!dependency2.IsAchieved || !dependency2.RewardClaimed)
  726. {
  727. dependency = dependency2;
  728. break;
  729. }
  730. }
  731. if (dependency == null && count > 0)
  732. {
  733. dependency = this.Children[count - 1];
  734. }
  735. return dependency;
  736. }
  737. protected void SetInternallyAchieved(bool internallyAchieved)
  738. {
  739. if (internallyAchieved != this._internallyAchieved)
  740. {
  741. this._internallyAchieved = internallyAchieved;
  742. this.CheckAchieved();
  743. }
  744. }
  745. protected virtual void OnAttachedToParent(Dependency parent)
  746. {
  747. if (parent.OrderedChildren)
  748. {
  749. bool achieved = this._achieved;
  750. this.CheckAchieved();
  751. if (achieved == this._achieved && this._childIndex + 1 < this._parent._children.Count)
  752. {
  753. this._parent.Children[this._childIndex + 1].OnPrecedingSiblingAchievedChanged(this, this._achieved);
  754. }
  755. }
  756. }
  757. protected virtual void OnDetachedFromParent(Dependency parent)
  758. {
  759. if (parent.OrderedChildren)
  760. {
  761. this.CheckAchieved();
  762. }
  763. }
  764. protected virtual void OnChildAttached(Dependency child)
  765. {
  766. this.CheckAchieved();
  767. }
  768. protected virtual void OnChildDetached(Dependency child)
  769. {
  770. this.CheckAchieved();
  771. }
  772. protected virtual void OnChildAchievedChanged(Dependency child, bool achieved)
  773. {
  774. this.CheckAutoGeneratedChildren();
  775. this.CheckAchieved();
  776. }
  777. protected virtual void OnParentOrderedChildrenChanged(Dependency parent, bool orderedChildren)
  778. {
  779. this.CheckAchieved();
  780. }
  781. protected virtual void OnPrecedingSiblingAchievedChanged(Dependency sibling, bool achieved)
  782. {
  783. this.CheckAchieved();
  784. }
  785. protected virtual void FireAchievedChangedEvent(bool achieved)
  786. {
  787. if (this.AchievedChangedEvent != null)
  788. {
  789. this.AchievedChangedEvent(this, achieved);
  790. }
  791. }
  792. protected virtual void FireProgressValueChangedEvent(int progress)
  793. {
  794. if (this.ProgressValueChangedEvent != null)
  795. {
  796. this.ProgressValueChangedEvent(this, progress);
  797. }
  798. }
  799. protected virtual void FireActiveChangedEvent(bool active)
  800. {
  801. if (this.ActiveChangedEvent != null)
  802. {
  803. this.ActiveChangedEvent(this, active);
  804. }
  805. }
  806. protected virtual void FireRewardClaimedEvent(bool claimed)
  807. {
  808. if (this.RewardClaimedEvent != null)
  809. {
  810. this.RewardClaimedEvent(this, claimed);
  811. }
  812. }
  813. protected int GetPeristentInt(string key, int defaultValue)
  814. {
  815. object obj;
  816. if (this._persistentState != null && this._persistentState.TryGetValue(key, out obj) && obj is int)
  817. {
  818. return (int)obj;
  819. }
  820. return defaultValue;
  821. }
  822. protected void SetPersistentInt(string key, int value)
  823. {
  824. if (this._persistentState != null)
  825. {
  826. this._persistentState[key] = value;
  827. }
  828. else
  829. {
  830. UnityEngine.Debug.LogWarning("Trying to set persistent property '" + key + "', but the dependency has no persistent state.");
  831. }
  832. }
  833. protected string GetPeristentString(string key, string defaultValue)
  834. {
  835. object obj;
  836. if (this._persistentState != null && this._persistentState.TryGetValue(key, out obj) && obj is string)
  837. {
  838. return (string)obj;
  839. }
  840. return defaultValue;
  841. }
  842. protected void SetPersistentString(string key, string value)
  843. {
  844. if (this._persistentState != null)
  845. {
  846. this._persistentState[key] = value;
  847. }
  848. else
  849. {
  850. UnityEngine.Debug.LogWarning("Trying to set persistent property '" + key + "', but the dependency has no persistent state.");
  851. }
  852. }
  853. protected bool GetPeristentBool(string key, bool defaultValue)
  854. {
  855. object obj;
  856. if (this._persistentState != null && this._persistentState.TryGetValue(key, out obj) && obj is bool)
  857. {
  858. return (bool)obj;
  859. }
  860. return defaultValue;
  861. }
  862. protected void SetPersistentBool(string key, bool value)
  863. {
  864. if (this._persistentState != null)
  865. {
  866. this._persistentState[key] = value;
  867. }
  868. else
  869. {
  870. UnityEngine.Debug.LogWarning("Trying to set persistent property '" + key + "', but the dependency has no persistent state.");
  871. }
  872. }
  873. private void CheckAchieved()
  874. {
  875. bool flag = this._internallyAchieved;
  876. if (this.StayAchieved && this.AchievedCount > 0)
  877. {
  878. flag = true;
  879. }
  880. else
  881. {
  882. if (flag)
  883. {
  884. int num = 0;
  885. foreach (Dependency dependency in this._children)
  886. {
  887. if (dependency.IsAchieved)
  888. {
  889. num++;
  890. }
  891. }
  892. int minChildrenAchieved = this.MinChildrenAchieved;
  893. if (minChildrenAchieved == 2147483647)
  894. {
  895. if (num < this._children.Count)
  896. {
  897. flag = false;
  898. }
  899. }
  900. else if (num < minChildrenAchieved)
  901. {
  902. flag = false;
  903. }
  904. int maxChildrenAchieved = this.MaxChildrenAchieved;
  905. if (maxChildrenAchieved == 2147483647)
  906. {
  907. if (num > this._children.Count)
  908. {
  909. flag = false;
  910. }
  911. }
  912. else if (num > maxChildrenAchieved)
  913. {
  914. flag = false;
  915. }
  916. }
  917. if (flag && this._parent != null && this._parent.OrderedChildren && this._childIndex > 0 && !this._parent._children[this._childIndex - 1].IsAchieved)
  918. {
  919. flag = false;
  920. }
  921. }
  922. if (flag != this._achieved)
  923. {
  924. this._achieved = flag;
  925. if (!this._communicator.Tree.IsMutating)
  926. {
  927. string str = (this._identifier != null) ? this._identifier : "(anonymous)";
  928. if (this._achieved)
  929. {
  930. UnityEngine.Debug.Log("Dependency " + str + " is now achieved.");
  931. }
  932. else
  933. {
  934. UnityEngine.Debug.Log("Dependency " + str + " is no longer achieved.");
  935. }
  936. }
  937. if (this._achieved && !this._communicator.Tree.IsMutating)
  938. {
  939. this.AchievedCount++;
  940. }
  941. if (this._parent != null)
  942. {
  943. this._parent.OnChildAchievedChanged(this, this._achieved);
  944. if (this._parent.OrderedChildren && this._childIndex + 1 < this._parent._children.Count)
  945. {
  946. this._parent.Children[this._childIndex + 1].OnPrecedingSiblingAchievedChanged(this, this._achieved);
  947. }
  948. }
  949. this.FireAchievedChangedEvent(this._achieved);
  950. this._communicator.NotifySubTreeAchievedChanged(this, this._achieved);
  951. }
  952. }
  953. private void CheckAutoGeneratedChildren()
  954. {
  955. int num = 0;
  956. foreach (Dependency dependency in this._children)
  957. {
  958. if (dependency.IsAchieved)
  959. {
  960. num++;
  961. }
  962. }
  963. int autoGenCount = this.AutoGenCount;
  964. int autoGenIterationStart = this.AutoGenIterationStart;
  965. int autoGenIterationStep = this.AutoGenIterationStep;
  966. int autoGenIterationEnd = this.AutoGenIterationEnd;
  967. if (this._children.Count - num < autoGenCount)
  968. {
  969. bool flag = false;
  970. if (!this._communicator.Tree.IsMutating)
  971. {
  972. this._communicator.Tree.BeginMutating();
  973. flag = true;
  974. }
  975. while (this._children.Count - num < autoGenCount)
  976. {
  977. int num2 = (this._autoGenState < autoGenIterationStart) ? autoGenIterationStart : (this._autoGenState + autoGenIterationStep);
  978. if (num2 > autoGenIterationEnd)
  979. {
  980. break;
  981. }
  982. Dependency dependency2 = this.AutoGenerateChild(num2);
  983. if (dependency2 == null)
  984. {
  985. break;
  986. }
  987. this.AddChild(dependency2);
  988. this._autoGenState = num2;
  989. this.SetPersistentInt("AutoGenerated", this._autoGenState);
  990. }
  991. if (flag)
  992. {
  993. this._communicator.Tree.EndMutating();
  994. }
  995. }
  996. }
  997. private Dependency AutoGenerateChild(int value)
  998. {
  999. string autoGenTemplate = this.AutoGenTemplate;
  1000. if (string.IsNullOrEmpty(autoGenTemplate))
  1001. {
  1002. UnityEngine.Debug.LogWarning("Can't auto generate a child for dependency '" + this._identifier + "' because it has no template ID");
  1003. return null;
  1004. }
  1005. DependencyTemplate dependencyTemplate = null;
  1006. if (!this._communicator.Tree.TryGetTemplate(autoGenTemplate, out dependencyTemplate))
  1007. {
  1008. UnityEngine.Debug.LogWarning("Could not find a template with ID '" + autoGenTemplate + "'.");
  1009. return null;
  1010. }
  1011. string newValue = value.ToString(CultureInfo.InvariantCulture);
  1012. string identifier = dependencyTemplate.Identifier.Replace("#", newValue);
  1013. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  1014. if (dependencyTemplate.Properties != null)
  1015. {
  1016. foreach (KeyValuePair<string, string> keyValuePair in dependencyTemplate.Properties)
  1017. {
  1018. dictionary[keyValuePair.Key] = keyValuePair.Value.Replace("[#]", newValue);
  1019. }
  1020. }
  1021. return this._communicator.Tree.CreateDependency(identifier, dictionary, dependencyTemplate.ClassName);
  1022. }
  1023. public const int AllChildrenAchieved = 2147483647;
  1024. public const string AchievedCountStateKey = "AchievedCount";
  1025. public const string IsActiveStateKey = "IsActive";
  1026. public const string RewardClaimedStateKey = "RewardClaimed";
  1027. public const string AutoGeneratedStateKey = "AutoGenerated";
  1028. public const string OverriddenPropertiesStateKey = "OverriddenProperties";
  1029. public const string MinChildrenAchievedPropertyName = "min-children-achieved";
  1030. public const string MaxChildrenAchievedPropertyName = "max-children-achieved";
  1031. public const string OrderedChildrenPropertyName = "ordered-children";
  1032. public const string StayAchievedPropertyName = "stay-achieved";
  1033. public const string ValueModifierPropertyName = "value-modifier";
  1034. public const string RewardPropertyName = "reward";
  1035. public const string TitlePropertyName = "title";
  1036. public const string TitleSingularPropertyName = "title-singular";
  1037. public const string TitlePluralPropertyName = "title-plural";
  1038. public const string TitleArgumentsPropertyName = "title-arguments";
  1039. public const string SpritePropertyName = "sprite";
  1040. public const string AchievedSpritePropertyName = "achieved-sprite";
  1041. public const string AutoGenCountPropertyName = "auto-gen-count";
  1042. public const string AutoGenTemplatePropertyName = "auto-gen-template";
  1043. public const string AutoGenIterationStartPropertyName = "auto-gen-iteration-start";
  1044. public const string AutoGenIterationStepPropertyName = "auto-gen-iteration-step";
  1045. public const string AutoGenIterationEndPropertyName = "auto-gen-iteration-end";
  1046. private DependencyState _state;
  1047. private bool _attachedToTree;
  1048. private string _identifier;
  1049. private Dictionary<string, object> _persistentState;
  1050. private Dictionary<string, string> _properties;
  1051. private DependencyTree.Communicator _communicator;
  1052. private Dependency _parent;
  1053. private int _childIndex;
  1054. private List<Dependency> _children;
  1055. private ReadOnlyCollection<Dependency> _readOnlyChildren;
  1056. private bool _internallyAchieved;
  1057. private bool _achieved;
  1058. private int _autoGenState;
  1059. [CompilerGenerated]
  1060. private static Func<string, ILocalizedString> _003C_003Ef__mg_0024cache0;
  1061. public delegate void Traverser(Dependency dependency);
  1062. public delegate void AchievedChangedEventHandler(Dependency dependency, bool achieved);
  1063. public delegate void ProgressValueChangedEventHandler(Dependency dependency, int progress);
  1064. public delegate void ActiveChangedEventHandler(Dependency dependency, bool active);
  1065. public delegate void RewardClaimedEventHandler(Dependency dependency, bool claimed);
  1066. }
  1067. }