using System; using System.Collections.Generic; using System.Globalization; using CIG.Translation; using SUISSEngine; using UnityEngine; namespace Engine.DependencyTree { public abstract class DailyAbstractStateDependency : Dependency where T : DailyAbstractState { public DailyAbstractStateDependency(string identifier, Dictionary persistentState, Dictionary properties, DependencyTree.Communicator communicator) : base(identifier, persistentState, properties, communicator) { this._listening = false; base.SetInternallyAchieved(false); } public override void OnStart() { base.OnStart(); T state = this.GetState(); if (state != null) { string property = base.GetProperty("key"); object startOfDayValue = state.GetStartOfDayValue(property); object value = state.GetValue(property, null); this.CheckValue(startOfDayValue, value); } else { base.SetInternallyAchieved(false); } this.BeginListening(); } public override void OnDestroy() { base.OnDestroy(); this.EndListening(); } protected override void OnReset() { base.OnReset(); T state = this.GetState(); if (state != null) { string property = base.GetProperty("key"); object startOfDayValue = state.GetStartOfDayValue(property); object value = state.GetValue(property, null); this.CheckValue(startOfDayValue, value); } else { base.SetInternallyAchieved(false); } } public override ILocalizedString[] TitleArguments { get { ILocalizedString[] titleArguments = base.TitleArguments; string property = base.GetProperty("min"); string property2 = base.GetProperty("max"); string property3 = base.GetProperty("eq"); ILocalizedString localizedString = (property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Literal(property); ILocalizedString localizedString2 = (property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Literal(property2); ILocalizedString localizedString3 = (property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Literal(property3); T state = this.GetState(); if (state != null) { object obj = state.GetValue(base.GetProperty("key"), null); if (obj == null) { obj = state.GetStartOfDayValue(base.GetProperty("key")); } if (obj is Currencies) { string property4 = base.GetProperty("sub-key"); if (property4.Length > 0) { obj = ((Currencies)obj).GetValue(property4); } } if (obj is int) { localizedString = ((property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledInt(property))); localizedString2 = ((property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledInt(property2))); localizedString3 = ((property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledInt(property3))); } else if (obj is long) { localizedString = ((property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledLong(property))); localizedString2 = ((property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledLong(property2))); localizedString3 = ((property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(this.getScaledLong(property3))); } else if (obj is decimal) { localizedString = ((property.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(decimal.Parse(property, CultureInfo.InvariantCulture))); localizedString2 = ((property2.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(decimal.Parse(property2, CultureInfo.InvariantCulture))); localizedString3 = ((property3.Length <= 0) ? Localization.EmptyLocalizedString : Localization.Integer(decimal.Parse(property3, CultureInfo.InvariantCulture))); } } List list = new List(titleArguments); if (localizedString != null && localizedString != Localization.EmptyLocalizedString) { list.Add(localizedString); } if (localizedString2 != null && localizedString2 != Localization.EmptyLocalizedString) { list.Add(localizedString2); } if (localizedString3 != null && localizedString3 != Localization.EmptyLocalizedString) { list.Add(localizedString3); } return list.ToArray(); } } public override int ProgressValue { get { return this._progress; } } public override int ProgressMaximumValue { get { string property = base.GetProperty("min"); string property2 = base.GetProperty("max"); string property3 = base.GetProperty("eq"); int num; int num2; int num3; try { num = ((property.Length <= 0) ? int.MinValue : int.Parse(property, CultureInfo.InvariantCulture)); num2 = ((property2.Length <= 0) ? int.MaxValue : int.Parse(property2, CultureInfo.InvariantCulture)); num3 = ((property3.Length <= 0) ? int.MaxValue : int.Parse(property3, CultureInfo.InvariantCulture)); } catch { return 0; } if (num3 != 2147483647) { return num3; } if (num != -2147483648) { return num; } if (num2 != 2147483647) { return num2; } return 0; } } protected abstract T GetState(); private int getInt(string intValue) { return int.Parse(intValue, CultureInfo.InvariantCulture); } private int getScaledInt(string intValue) { return Mathf.FloorToInt((float)this.getInt(intValue) / base.ValueModifier); } private long getLong(string longValue) { return long.Parse(longValue, CultureInfo.InvariantCulture); } private long getScaledLong(string longValue) { return (long)((float)this.getLong(longValue) / base.ValueModifier); } private void CheckValue(object startOfDayValue, object newValue) { string property = base.GetProperty("min"); string property2 = base.GetProperty("max"); string property3 = base.GetProperty("eq"); try { bool internallyAchieved; if (newValue == null) { internallyAchieved = false; } else if (newValue is int) { int num = (int)newValue; if (startOfDayValue != null) { num -= (int)startOfDayValue; } int num2 = (property.Length <= 0) ? int.MinValue : int.Parse(property, CultureInfo.InvariantCulture); int num3 = (property2.Length <= 0) ? int.MaxValue : int.Parse(property2, CultureInfo.InvariantCulture); int num4 = (property3.Length <= 0) ? int.MaxValue : int.Parse(property3, CultureInfo.InvariantCulture); if (num4 != 2147483647) { internallyAchieved = (num == num4); } else { internallyAchieved = (num >= num2 && num <= num3); } this._progress = num; } else if (newValue is long) { long num5 = (long)newValue; if (startOfDayValue != null) { num5 -= (long)startOfDayValue; } long num6 = (property.Length <= 0) ? long.MinValue : long.Parse(property, CultureInfo.InvariantCulture); long num7 = (property2.Length <= 0) ? long.MaxValue : long.Parse(property2, CultureInfo.InvariantCulture); long num8 = (property3.Length <= 0) ? long.MaxValue : long.Parse(property3, CultureInfo.InvariantCulture); if (num8 != 9223372036854775807L) { internallyAchieved = (num5 == num8); } else { internallyAchieved = (num5 >= num6 && num5 <= num7); } this._progress = (int)num5; } else if (newValue is float) { float num9 = (float)newValue; if (startOfDayValue != null) { num9 -= (float)startOfDayValue; } float num10 = (property.Length <= 0) ? float.MinValue : float.Parse(property, CultureInfo.InvariantCulture); float num11 = (property2.Length <= 0) ? float.MaxValue : float.Parse(property2, CultureInfo.InvariantCulture); float num12 = (property3.Length <= 0) ? float.NaN : float.Parse(property3, CultureInfo.InvariantCulture); if (!float.IsNaN(num12)) { internallyAchieved = Mathf.Approximately(num9, num12); } else { internallyAchieved = (num9 >= num10 && num9 <= num11); } this._progress = (int)num9; } else if (newValue is double) { double num13 = (double)newValue; if (startOfDayValue != null) { num13 -= (double)startOfDayValue; } double num14 = (property.Length <= 0) ? double.MinValue : double.Parse(property, CultureInfo.InvariantCulture); double num15 = (property2.Length <= 0) ? double.MaxValue : double.Parse(property2, CultureInfo.InvariantCulture); double num16 = (property3.Length <= 0) ? double.NaN : double.Parse(property3, CultureInfo.InvariantCulture); if (!double.IsNaN(num16)) { internallyAchieved = (Math.Abs(num13 - num16) <= 9.8813129168249309E-324); } else { internallyAchieved = (num13 >= num14 && num13 <= num15); } this._progress = (int)num13; } else if (newValue is decimal) { decimal num17 = (decimal)newValue; if (startOfDayValue != null) { num17 -= (decimal)startOfDayValue; } decimal d = (property.Length <= 0) ? decimal.MinValue : decimal.Parse(property, CultureInfo.InvariantCulture); decimal d2 = (property2.Length <= 0) ? decimal.MaxValue : decimal.Parse(property2, CultureInfo.InvariantCulture); decimal num18 = (property3.Length <= 0) ? 12345678900000000000000000000m : decimal.Parse(property3, CultureInfo.InvariantCulture); if (num18 != 12345678900000000000000000000m) { internallyAchieved = (Math.Abs(num17 - num18) <= 0.0000000000000000000000000002m); } else { internallyAchieved = (num17 >= d && num17 <= d2); } this._progress = (int)num17; } else { if (!(newValue is Currencies)) { throw new InvalidOperationException("GameState property " + base.GetProperty("key") + " is of an unsupported type: " + newValue.GetType().ToString()); } string property4 = base.GetProperty("sub-key"); if (property4.Length == 0) { throw new InvalidOperationException("GameState property " + base.GetProperty("key") + " is of type Currencies, but property 'sub-key' is missing."); } decimal num19 = ((Currencies)newValue).GetValue(property4); if (startOfDayValue != null) { num19 -= ((Currencies)startOfDayValue).GetValue(property4); } decimal d3 = (property.Length <= 0) ? decimal.MinValue : decimal.Parse(property, CultureInfo.InvariantCulture); decimal d4 = (property2.Length <= 0) ? decimal.MaxValue : decimal.Parse(property2, CultureInfo.InvariantCulture); decimal num20 = (property3.Length <= 0) ? decimal.MaxValue : decimal.Parse(property3, CultureInfo.InvariantCulture); if (num20 != 79228162514264337593543950335m) { internallyAchieved = (num19 == num20); } else { internallyAchieved = (num19 >= d3 && num19 <= d4); } this._progress = (int)num19; } base.SetInternallyAchieved(internallyAchieved); } catch (Exception ex) { UnityEngine.Debug.LogWarning("Unable to check GameStateDependency " + base.Identifier + ": " + ex.ToString()); } this.FireProgressValueChangedEvent(this._progress); } private void BeginListening() { if (!this._listening) { T state = this.GetState(); if (state != null) { state.ValueChangedEvent += this.StateValueChanged; this._listening = true; } } } private void EndListening() { if (this._listening) { T state = this.GetState(); if (state != null) { state.ValueChangedEvent -= this.StateValueChanged; } this._listening = false; } } private void StateValueChanged(string key, object oldValue, object newValue) { if (key == base.GetProperty("key")) { T state = this.GetState(); this.CheckValue(state.GetStartOfDayValue(key), newValue); } } public const string StateKeyPropertyName = "key"; public const string StateSubKeyPropertyName = "sub-key"; public const string MinValuePropertyName = "min"; public const string MaxValuePropertyName = "max"; public const string EqualsValuePropertyName = "eq"; private const decimal decimalNaN = 12345678900000000000000000000m; private const decimal decimalEpsilon = 0.0000000000000000000000000001m; private bool _listening; private int _progress; } }