using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; using System.Runtime.CompilerServices; using CIG.Translation; using SparkLinq; using SUISSEngine; using UnityEngine; namespace Engine.DependencyTree { public class Dependency { public Dependency(string identifier, Dictionary persistentState, Dictionary properties, DependencyTree.Communicator communicator) { if (communicator == null) { throw new ArgumentNullException("communicator"); } this._state = DependencyState.Created; this._attachedToTree = (identifier == "root"); this._identifier = identifier; this._persistentState = persistentState; this._properties = properties; this._communicator = communicator; this._parent = null; this._childIndex = -1; this._children = new List(); this._readOnlyChildren = this._children.AsReadOnly(); this._internallyAchieved = true; this._achieved = true; this._autoGenState = int.MinValue; if (this._properties != null && this._properties.ContainsKey("requires-ads") && "true".Equals(this._properties["requires-ads"])) { this._persistentState["IsActive"] = true; } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Dependency.AchievedChangedEventHandler AchievedChangedEvent; //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Dependency.ProgressValueChangedEventHandler ProgressValueChangedEvent; //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Dependency.ActiveChangedEventHandler ActiveChangedEvent; //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Dependency.RewardClaimedEventHandler RewardClaimedEvent; public virtual void OnStart() { if (this._state != DependencyState.Created) { throw new InvalidOperationException("Dependency can only be started when its in the Created state."); } this._state = DependencyState.Started; } public virtual void OnDestroy() { if (this._state == DependencyState.Destroyed) { throw new InvalidOperationException("Dependency is already destroyed."); } this._state = DependencyState.Destroyed; } public virtual void OnBeginMutating() { } public virtual void OnEndMutating() { if (this._achieved && this.StayAchieved && this.AchievedCount == 0) { this.AchievedCount = 1; } int peristentInt = this.GetPeristentInt("AutoGenerated", int.MinValue); if (peristentInt != -2147483648) { int autoGenCount = this.AutoGenCount; string autoGenTemplate = this.AutoGenTemplate; int autoGenIterationStart = this.AutoGenIterationStart; int autoGenIterationStep = this.AutoGenIterationStep; int autoGenIterationEnd = this.AutoGenIterationEnd; if (autoGenCount <= 0 || string.IsNullOrEmpty(autoGenTemplate) || peristentInt < autoGenIterationStart) { if (this._persistentState != null) { this._persistentState.Remove("AutoGenerated"); } } else { int num = autoGenIterationStart; while (num <= peristentInt && num <= autoGenIterationEnd) { if (num > this._autoGenState) { Dependency dependency = this.AutoGenerateChild(num); if (dependency == null) { break; } this.AddChild(dependency); this._autoGenState = num; } num += autoGenIterationStep; } this.SetPersistentInt("AutoGenerated", this._autoGenState); } } this.CheckAutoGeneratedChildren(); } protected virtual void OnReset() { } public DependencyState State { get { return this._state; } } public bool IsAttachedToTree { get { return this._attachedToTree; } } public string Identifier { get { return this._identifier; } } public Dependency Parent { get { return this._parent; } } public int ChildIndex { get { return this._childIndex; } } public ReadOnlyCollection Children { get { return this._readOnlyChildren; } } public bool IsInternallyAchieved { get { return this._internallyAchieved; } } public bool IsAchieved { get { return this._achieved; } } public int MinChildrenAchieved { get { string property = this.GetProperty("min-children-achieved"); int maxValue; if (property.Length == 0 || !int.TryParse(property, out maxValue)) { maxValue = int.MaxValue; } return maxValue; } } public int MaxChildrenAchieved { get { string property = this.GetProperty("max-children-achieved"); int maxValue; if (property.Length == 0 || !int.TryParse(property, out maxValue)) { maxValue = int.MaxValue; } return maxValue; } } public bool OrderedChildren { get { string property = this.GetProperty("ordered-children"); bool result; if (property.Length == 0 || !bool.TryParse(property, out result)) { result = false; } return result; } } public bool StayAchieved { get { string property = this.GetProperty("stay-achieved"); bool result; if (property.Length == 0 || !bool.TryParse(property, out result)) { result = true; } return result; } } public float ValueModifier { get { string property = this.GetProperty("value-modifier"); float result; if (property.Length == 0 || !float.TryParse(property, out result)) { result = 1f; } return result; } } public Currencies Reward { get { string property = this.GetProperty("reward"); Currencies zero; if (property.Length == 0 || !Currencies.TryParse(property, out zero)) { zero = Currencies.Zero; } return zero; } } public bool IsActive { get { return this.GetPeristentBool("IsActive", true); } set { if (value != this.IsActive) { this.SetPersistentBool("IsActive", value); this._communicator.NotifyActiveChanged(this, value); this.FireActiveChangedEvent(value); } } } public bool RewardClaimed { get { return this.GetPeristentBool("RewardClaimed", false); } set { if (value != this.RewardClaimed) { this.SetPersistentBool("RewardClaimed", value); this._communicator.NotifyRewardClaimed(this, value); this.FireRewardClaimedEvent(value); } } } public bool HasTitle { get { string title = this.Title; string titleSingular = this.TitleSingular; string titlePlural = this.TitlePlural; bool flag = title != null; bool flag2 = titleSingular != null; bool flag3 = titlePlural != null; return flag || flag2 || flag3; } } public bool IsQuantityTitle { get { string title = this.Title; string titleSingular = this.TitleSingular; string titlePlural = this.TitlePlural; bool flag = title != null; bool flag2 = titleSingular != null; bool flag3 = titlePlural != null; if (!flag && !flag2 && !flag3) { return false; } if (flag) { if (flag2 || flag3) { UnityEngine.Debug.LogWarning(string.Concat(new string[] { "Dependency ", this._identifier, " has a non-quantity string as a title (", title, "), but also defines a quantity string as title. Please check your DependencyTree xml-file for mistakes." })); } return false; } if (!flag2) { 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."); } if (!flag3) { 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."); } return true; } } public string Title { get { string property = this.GetProperty("title"); if (string.IsNullOrEmpty(property)) { return null; } return property; } } public string TitleSingular { get { string property = this.GetProperty("title-singular"); if (string.IsNullOrEmpty(property)) { return null; } return property; } } public string TitlePlural { get { string property = this.GetProperty("title-plural"); if (string.IsNullOrEmpty(property)) { return null; } return property; } } public virtual ILocalizedString[] TitleArguments { get { string property = this.GetProperty("title-arguments"); ILocalizedString[] result; if (string.IsNullOrEmpty(property)) { result = new ILocalizedString[0]; } else { string[] array = property.Split(new char[] { '|' }); if (Dependency._003C_003Ef__mg_0024cache0 == null) { Dependency._003C_003Ef__mg_0024cache0 = new Func(Localization.Literal); } result = array.ConvertAll(Dependency._003C_003Ef__mg_0024cache0); } return result; } } public string SpriteName { get { string property = this.GetProperty("sprite"); if (string.IsNullOrEmpty(property)) { return null; } return property; } } public string AchievedSpriteName { get { string property = this.GetProperty("achieved-sprite"); if (string.IsNullOrEmpty(property)) { return null; } return property; } } public int AutoGenCount { get { string property = this.GetProperty("auto-gen-count"); int result; if (int.TryParse(property, out result)) { return result; } return 0; } } public string AutoGenTemplate { get { string property = this.GetProperty("auto-gen-template"); if (string.IsNullOrEmpty(property)) { return null; } return property; } } public int AutoGenIterationStart { get { string property = this.GetProperty("auto-gen-iteration-start"); int result; if (int.TryParse(property, out result)) { return result; } return 0; } } public int AutoGenIterationStep { get { string property = this.GetProperty("auto-gen-iteration-step"); int result; if (int.TryParse(property, out result)) { return result; } return 1; } } public int AutoGenIterationEnd { get { string property = this.GetProperty("auto-gen-iteration-end"); int result; if (int.TryParse(property, out result)) { return result; } return int.MaxValue; } } public virtual int ProgressValue { get { return 0; } } public float ScaledProgressValue { get { return (float)this.ProgressValue / this.ValueModifier; } } public virtual int ProgressMaximumValue { get { return 0; } } public float ScaledProgressMaximumValue { get { return (float)this.ProgressMaximumValue / this.ValueModifier; } } protected Dictionary PersistentState { get { return this._persistentState; } } protected Dictionary Properties { get { return this._properties; } } protected DependencyTree.Communicator Communicator { get { return this._communicator; } } private int AchievedCount { get { return this.GetPeristentInt("AchievedCount", 0); } set { this.SetPersistentInt("AchievedCount", value); } } public void Traverse(Dependency.Traverser traverser) { traverser(this); for (int i = 0; i < this._children.Count; i++) { Dependency dependency = this._children[i]; dependency.Traverse(traverser); } } public bool AddChild(Dependency child) { return this.InsertChild(this._children.Count, child); } public bool InsertChild(int index, Dependency child) { if (index < 0 || index > this._children.Count) { throw new IndexOutOfRangeException(string.Concat(new object[] { "Child insertion index ", index, " is out of range (", this._children.Count, " children)." })); } if (child == null) { throw new ArgumentNullException("child"); } if (child._parent != null) { throw new ArgumentException("Child dependency already has a parent.", "child"); } if (child._communicator.Tree != this._communicator.Tree) { throw new ArgumentException("The specified child was not created for this Dependency's DependencyTree."); } this._children.Insert(index, child); child.Traverse(delegate(Dependency c) { c._attachedToTree = this._attachedToTree; }); child._parent = this; child._childIndex = index; for (int i = index + 1; i < this._children.Count; i++) { this._children[i]._childIndex = i; } try { if (this._attachedToTree) { this._communicator.NotifySubTreeAttached(this, child); } } catch (Exception ex) { this._children.Remove(child); child.Traverse(delegate(Dependency c) { c._attachedToTree = false; }); child._parent = null; child._childIndex = -1; for (int j = index; j < this._children.Count; j++) { this._children[j]._childIndex = j; } throw ex; } child.OnAttachedToParent(this); this.OnChildAttached(child); return true; } public Dependency GetChild(Predicate predicate) { return this._children.Find(predicate); } public bool RemoveChild(Dependency child) { if (child == null || child._parent != this) { return false; } if (this._children.Remove(child)) { int childIndex = child._childIndex; child.Traverse(delegate(Dependency c) { c._attachedToTree = false; }); child._parent = null; child._childIndex = -1; for (int i = childIndex; i < this._children.Count; i++) { this._children[i]._childIndex = i; } try { if (this._attachedToTree) { this._communicator.NotifySubTreeDetached(this, child); } } catch (Exception ex) { this._children.Insert(childIndex, child); child.Traverse(delegate(Dependency c) { c._attachedToTree = this._attachedToTree; }); child._parent = this; child._childIndex = childIndex; for (int j = childIndex + 1; j < this._children.Count; j++) { this._children[j]._childIndex = j; } throw ex; } child.OnDetachedFromParent(this); this.OnChildDetached(child); return true; } return false; } public void Reset(Dictionary overriddenProperties) { if (!this._communicator.Tree.IsMutating) { throw new InvalidOperationException("Can't reset dependency while tree is not mutating."); } if (this._persistentState != null) { this._persistentState.Clear(); if (overriddenProperties != null) { Dictionary dictionary = new Dictionary(); foreach (KeyValuePair keyValuePair in overriddenProperties) { dictionary[keyValuePair.Key] = keyValuePair.Value; } this._persistentState["OverriddenProperties"] = dictionary; } } else if (overriddenProperties != null) { UnityEngine.Debug.LogWarning("You are trying to override properties for a dependency, but the dependency has no persistent state."); } this.OnReset(); this.CheckAchieved(); } public string GetProperty(string key) { object obj; object obj2; if (this._persistentState != null && this._persistentState.TryGetValue("OverriddenProperties", out obj) && ((Dictionary)obj).TryGetValue(key, out obj2)) { if (obj2 is string) { return (string)obj2; } return string.Empty; } else { string text; if (this._properties == null || !this._properties.TryGetValue(key, out text)) { return string.Empty; } if (text == null) { return string.Empty; } return text; } } public Dependency GetFirstIncompletedChild() { Dependency dependency = null; int count = this.Children.Count; for (int i = 0; i < count; i++) { Dependency dependency2 = this.Children[i]; if (!dependency2.IsAchieved || !dependency2.RewardClaimed) { dependency = dependency2; break; } } if (dependency == null && count > 0) { dependency = this.Children[count - 1]; } return dependency; } protected void SetInternallyAchieved(bool internallyAchieved) { if (internallyAchieved != this._internallyAchieved) { this._internallyAchieved = internallyAchieved; this.CheckAchieved(); } } protected virtual void OnAttachedToParent(Dependency parent) { if (parent.OrderedChildren) { bool achieved = this._achieved; this.CheckAchieved(); if (achieved == this._achieved && this._childIndex + 1 < this._parent._children.Count) { this._parent.Children[this._childIndex + 1].OnPrecedingSiblingAchievedChanged(this, this._achieved); } } } protected virtual void OnDetachedFromParent(Dependency parent) { if (parent.OrderedChildren) { this.CheckAchieved(); } } protected virtual void OnChildAttached(Dependency child) { this.CheckAchieved(); } protected virtual void OnChildDetached(Dependency child) { this.CheckAchieved(); } protected virtual void OnChildAchievedChanged(Dependency child, bool achieved) { this.CheckAutoGeneratedChildren(); this.CheckAchieved(); } protected virtual void OnParentOrderedChildrenChanged(Dependency parent, bool orderedChildren) { this.CheckAchieved(); } protected virtual void OnPrecedingSiblingAchievedChanged(Dependency sibling, bool achieved) { this.CheckAchieved(); } protected virtual void FireAchievedChangedEvent(bool achieved) { if (this.AchievedChangedEvent != null) { this.AchievedChangedEvent(this, achieved); } } protected virtual void FireProgressValueChangedEvent(int progress) { if (this.ProgressValueChangedEvent != null) { this.ProgressValueChangedEvent(this, progress); } } protected virtual void FireActiveChangedEvent(bool active) { if (this.ActiveChangedEvent != null) { this.ActiveChangedEvent(this, active); } } protected virtual void FireRewardClaimedEvent(bool claimed) { if (this.RewardClaimedEvent != null) { this.RewardClaimedEvent(this, claimed); } } protected int GetPeristentInt(string key, int defaultValue) { object obj; if (this._persistentState != null && this._persistentState.TryGetValue(key, out obj) && obj is int) { return (int)obj; } return defaultValue; } protected void SetPersistentInt(string key, int value) { if (this._persistentState != null) { this._persistentState[key] = value; } else { UnityEngine.Debug.LogWarning("Trying to set persistent property '" + key + "', but the dependency has no persistent state."); } } protected string GetPeristentString(string key, string defaultValue) { object obj; if (this._persistentState != null && this._persistentState.TryGetValue(key, out obj) && obj is string) { return (string)obj; } return defaultValue; } protected void SetPersistentString(string key, string value) { if (this._persistentState != null) { this._persistentState[key] = value; } else { UnityEngine.Debug.LogWarning("Trying to set persistent property '" + key + "', but the dependency has no persistent state."); } } protected bool GetPeristentBool(string key, bool defaultValue) { object obj; if (this._persistentState != null && this._persistentState.TryGetValue(key, out obj) && obj is bool) { return (bool)obj; } return defaultValue; } protected void SetPersistentBool(string key, bool value) { if (this._persistentState != null) { this._persistentState[key] = value; } else { UnityEngine.Debug.LogWarning("Trying to set persistent property '" + key + "', but the dependency has no persistent state."); } } private void CheckAchieved() { bool flag = this._internallyAchieved; if (this.StayAchieved && this.AchievedCount > 0) { flag = true; } else { if (flag) { int num = 0; foreach (Dependency dependency in this._children) { if (dependency.IsAchieved) { num++; } } int minChildrenAchieved = this.MinChildrenAchieved; if (minChildrenAchieved == 2147483647) { if (num < this._children.Count) { flag = false; } } else if (num < minChildrenAchieved) { flag = false; } int maxChildrenAchieved = this.MaxChildrenAchieved; if (maxChildrenAchieved == 2147483647) { if (num > this._children.Count) { flag = false; } } else if (num > maxChildrenAchieved) { flag = false; } } if (flag && this._parent != null && this._parent.OrderedChildren && this._childIndex > 0 && !this._parent._children[this._childIndex - 1].IsAchieved) { flag = false; } } if (flag != this._achieved) { this._achieved = flag; if (!this._communicator.Tree.IsMutating) { string str = (this._identifier != null) ? this._identifier : "(anonymous)"; if (this._achieved) { UnityEngine.Debug.Log("Dependency " + str + " is now achieved."); } else { UnityEngine.Debug.Log("Dependency " + str + " is no longer achieved."); } } if (this._achieved && !this._communicator.Tree.IsMutating) { this.AchievedCount++; } if (this._parent != null) { this._parent.OnChildAchievedChanged(this, this._achieved); if (this._parent.OrderedChildren && this._childIndex + 1 < this._parent._children.Count) { this._parent.Children[this._childIndex + 1].OnPrecedingSiblingAchievedChanged(this, this._achieved); } } this.FireAchievedChangedEvent(this._achieved); this._communicator.NotifySubTreeAchievedChanged(this, this._achieved); } } private void CheckAutoGeneratedChildren() { int num = 0; foreach (Dependency dependency in this._children) { if (dependency.IsAchieved) { num++; } } int autoGenCount = this.AutoGenCount; int autoGenIterationStart = this.AutoGenIterationStart; int autoGenIterationStep = this.AutoGenIterationStep; int autoGenIterationEnd = this.AutoGenIterationEnd; if (this._children.Count - num < autoGenCount) { bool flag = false; if (!this._communicator.Tree.IsMutating) { this._communicator.Tree.BeginMutating(); flag = true; } while (this._children.Count - num < autoGenCount) { int num2 = (this._autoGenState < autoGenIterationStart) ? autoGenIterationStart : (this._autoGenState + autoGenIterationStep); if (num2 > autoGenIterationEnd) { break; } Dependency dependency2 = this.AutoGenerateChild(num2); if (dependency2 == null) { break; } this.AddChild(dependency2); this._autoGenState = num2; this.SetPersistentInt("AutoGenerated", this._autoGenState); } if (flag) { this._communicator.Tree.EndMutating(); } } } private Dependency AutoGenerateChild(int value) { string autoGenTemplate = this.AutoGenTemplate; if (string.IsNullOrEmpty(autoGenTemplate)) { UnityEngine.Debug.LogWarning("Can't auto generate a child for dependency '" + this._identifier + "' because it has no template ID"); return null; } DependencyTemplate dependencyTemplate = null; if (!this._communicator.Tree.TryGetTemplate(autoGenTemplate, out dependencyTemplate)) { UnityEngine.Debug.LogWarning("Could not find a template with ID '" + autoGenTemplate + "'."); return null; } string newValue = value.ToString(CultureInfo.InvariantCulture); string identifier = dependencyTemplate.Identifier.Replace("#", newValue); Dictionary dictionary = new Dictionary(); if (dependencyTemplate.Properties != null) { foreach (KeyValuePair keyValuePair in dependencyTemplate.Properties) { dictionary[keyValuePair.Key] = keyValuePair.Value.Replace("[#]", newValue); } } return this._communicator.Tree.CreateDependency(identifier, dictionary, dependencyTemplate.ClassName); } public const int AllChildrenAchieved = 2147483647; public const string AchievedCountStateKey = "AchievedCount"; public const string IsActiveStateKey = "IsActive"; public const string RewardClaimedStateKey = "RewardClaimed"; public const string AutoGeneratedStateKey = "AutoGenerated"; public const string OverriddenPropertiesStateKey = "OverriddenProperties"; public const string MinChildrenAchievedPropertyName = "min-children-achieved"; public const string MaxChildrenAchievedPropertyName = "max-children-achieved"; public const string OrderedChildrenPropertyName = "ordered-children"; public const string StayAchievedPropertyName = "stay-achieved"; public const string ValueModifierPropertyName = "value-modifier"; public const string RewardPropertyName = "reward"; public const string TitlePropertyName = "title"; public const string TitleSingularPropertyName = "title-singular"; public const string TitlePluralPropertyName = "title-plural"; public const string TitleArgumentsPropertyName = "title-arguments"; public const string SpritePropertyName = "sprite"; public const string AchievedSpritePropertyName = "achieved-sprite"; public const string AutoGenCountPropertyName = "auto-gen-count"; public const string AutoGenTemplatePropertyName = "auto-gen-template"; public const string AutoGenIterationStartPropertyName = "auto-gen-iteration-start"; public const string AutoGenIterationStepPropertyName = "auto-gen-iteration-step"; public const string AutoGenIterationEndPropertyName = "auto-gen-iteration-end"; private DependencyState _state; private bool _attachedToTree; private string _identifier; private Dictionary _persistentState; private Dictionary _properties; private DependencyTree.Communicator _communicator; private Dependency _parent; private int _childIndex; private List _children; private ReadOnlyCollection _readOnlyChildren; private bool _internallyAchieved; private bool _achieved; private int _autoGenState; [CompilerGenerated] private static Func _003C_003Ef__mg_0024cache0; public delegate void Traverser(Dependency dependency); public delegate void AchievedChangedEventHandler(Dependency dependency, bool achieved); public delegate void ProgressValueChangedEventHandler(Dependency dependency, int progress); public delegate void ActiveChangedEventHandler(Dependency dependency, bool active); public delegate void RewardClaimedEventHandler(Dependency dependency, bool claimed); } }