|
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using CIG.Translation;
- using SUISSEngine;
- using UnityEngine;
-
- namespace Engine.DependencyTree
- {
- public abstract class AbstractStateDependency<T> : Dependency where T : AbstractState<T>
- {
- public AbstractStateDependency(string identifier, Dictionary<string, object> persistentState, Dictionary<string, string> 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)
- {
- object value = state.GetValue(base.GetProperty("key"), null);
- this.CheckValue(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)
- {
- object value = state.GetValue(base.GetProperty("key"), null);
- this.CheckValue(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 value = state.GetValue(base.GetProperty("key"), null);
- if (value 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 (value 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)));
- }
- }
- List<ILocalizedString> list = new List<ILocalizedString>(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 : this.getInt(property));
- num2 = ((property2.Length <= 0) ? int.MaxValue : this.getInt(property2));
- num3 = ((property3.Length <= 0) ? int.MaxValue : this.getInt(property3));
- }
- 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 float getFloat(string floatValue)
- {
- return float.Parse(floatValue, CultureInfo.InvariantCulture);
- }
-
- private double getDouble(string doubleValue)
- {
- return double.Parse(doubleValue, CultureInfo.InvariantCulture);
- }
-
- private decimal getDecimal(string decimalValue)
- {
- return decimal.Parse(decimalValue, CultureInfo.InvariantCulture);
- }
-
- private void CheckValue(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 = (property.Length <= 0) ? int.MinValue : this.getInt(property);
- int num2 = (property2.Length <= 0) ? int.MaxValue : this.getInt(property2);
- int num3 = (property3.Length <= 0) ? int.MaxValue : this.getInt(property3);
- if (num3 != 2147483647)
- {
- internallyAchieved = ((int)newValue == num3);
- }
- else
- {
- internallyAchieved = ((int)newValue >= num && (int)newValue <= num2);
- }
- this._progress = (int)newValue;
- }
- else if (newValue is long)
- {
- long num4 = (property.Length <= 0) ? long.MinValue : this.getLong(property);
- long num5 = (property2.Length <= 0) ? long.MaxValue : this.getLong(property2);
- long num6 = (property3.Length <= 0) ? long.MaxValue : this.getLong(property3);
- if (num6 != 9223372036854775807L)
- {
- internallyAchieved = ((long)newValue == num6);
- }
- else
- {
- internallyAchieved = ((long)newValue >= num4 && (long)newValue <= num5);
- }
- this._progress = (int)((long)newValue);
- }
- else if (newValue is float)
- {
- float num7 = (property.Length <= 0) ? float.MinValue : this.getFloat(property);
- float num8 = (property2.Length <= 0) ? float.MaxValue : this.getFloat(property2);
- float num9 = (property3.Length <= 0) ? float.NaN : this.getFloat(property3);
- if (!float.IsNaN(num9))
- {
- internallyAchieved = Mathf.Approximately((float)newValue, num9);
- }
- else
- {
- internallyAchieved = ((float)newValue >= num7 && (float)newValue <= num8);
- }
- this._progress = (int)((float)newValue);
- }
- else if (newValue is double)
- {
- double num10 = (property.Length <= 0) ? double.MinValue : this.getDouble(property);
- double num11 = (property2.Length <= 0) ? double.MaxValue : this.getDouble(property2);
- double num12 = (property3.Length <= 0) ? double.NaN : this.getDouble(property3);
- if (!double.IsNaN(num12))
- {
- internallyAchieved = (Math.Abs((double)newValue - num12) <= 9.8813129168249309E-324);
- }
- else
- {
- internallyAchieved = ((double)newValue >= num10 && (double)newValue <= num11);
- }
- this._progress = (int)((double)newValue);
- }
- else if (newValue is decimal)
- {
- decimal d = (property.Length <= 0) ? decimal.MinValue : this.getDecimal(property);
- decimal d2 = (property2.Length <= 0) ? decimal.MaxValue : this.getDecimal(property2);
- decimal num13 = (property3.Length <= 0) ? 12345678900000000000000000000m : this.getDecimal(property3);
- if (num13 != 12345678900000000000000000000m)
- {
- internallyAchieved = (Math.Abs((decimal)newValue - num13) <= 0.0000000000000000000000000002m);
- }
- else
- {
- internallyAchieved = ((decimal)newValue >= d && (decimal)newValue <= d2);
- }
- this._progress = (int)((decimal)newValue);
- }
- else if (newValue is bool)
- {
- bool flag = bool.Parse(property3);
- internallyAchieved = ((bool)newValue == flag);
- this._progress = ((!(bool)newValue) ? 0 : 1);
- }
- 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 value = ((Currencies)newValue).GetValue(property4);
- decimal d3 = (property.Length <= 0) ? decimal.MinValue : this.getDecimal(property);
- decimal d4 = (property2.Length <= 0) ? decimal.MaxValue : this.getDecimal(property2);
- decimal num14 = (property3.Length <= 0) ? decimal.MaxValue : this.getDecimal(property3);
- if (num14 != 79228162514264337593543950335m)
- {
- internallyAchieved = (value == num14);
- }
- else
- {
- internallyAchieved = (value >= d3 && value <= d4);
- }
- this._progress = (int)value;
- }
- 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"))
- {
- this.CheckValue(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;
- }
- }
|