using System; using System.Collections.Generic; using System.Diagnostics; using SUISS.Core; using SUISSEngine; using UnityEngine; [RequireComponent(typeof(Serializing))] public sealed class CIGPurchasedBuildingsManager : SingletonMonobehaviour { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event CIGPurchasedBuildingsManager.BuildingCountChangedHandler BuildingCountChangedEvent; private void FireBuildingCountChangedEvent() { if (this.BuildingCountChangedEvent != null) { this.BuildingCountChangedEvent(); } } public void AddBuilding(CIGBuilding building) { this.AddPurchasedCount(building.CachedName, 1); this._serializing.Serialize(); this.FireBuildingCountChangedEvent(); } public void ConsumeBuilding(CIGBuilding building) { this.AddConsumedCount(building.CachedName, 1); this._serializing.Serialize(); this.FireBuildingCountChangedEvent(); } public bool IsUnconsumed(CIGBuilding building) { int num; if (this._purchasedBuildings.TryGetValue(building.CachedName, out num)) { int num2; if (!this._consumedBuildings.TryGetValue(building.CachedName, out num2)) { num2 = 0; } return num > num2; } return false; } public Dictionary GetUnconsumedBuildings() { Dictionary dictionary = new Dictionary(); foreach (KeyValuePair keyValuePair in this._purchasedBuildings) { int num; if (!this._consumedBuildings.TryGetValue(keyValuePair.Key, out num)) { num = 0; } if (num < keyValuePair.Value) { dictionary.Add(keyValuePair.Key, keyValuePair.Value - num); } } return dictionary; } public int TotalUnconsumedBuildings { get { int num = 0; int num2 = 0; foreach (KeyValuePair keyValuePair in this._purchasedBuildings) { if (keyValuePair.Value > 0) { num2 += keyValuePair.Value; } } foreach (KeyValuePair keyValuePair2 in this._consumedBuildings) { if (keyValuePair2.Value > 0) { num += keyValuePair2.Value; } } return Math.Max(0, num2 - num); } } private void AddPurchasedCount(string buildingName, int amount) { int num; if (!this._purchasedBuildings.TryGetValue(buildingName, out num)) { num = amount; } else { num += amount; } this._purchasedBuildings[buildingName] = Mathf.Max(0, num); } private void AddConsumedCount(string buildingName, int amount) { int num; if (!this._consumedBuildings.TryGetValue(buildingName, out num)) { num = amount; } else { num += amount; } this._consumedBuildings[buildingName] = Mathf.Max(0, num); } private void OnSerialize(Dictionary values) { this.SaveDictionary(values, "PurchasedBuildings", this._purchasedBuildings); this.SaveDictionary(values, "ConsumedBuildings", this._consumedBuildings); } private void OnDeserialize(Dictionary values) { this._purchasedBuildings = this.LoadDictionary(values, "PurchasedBuildings"); this._consumedBuildings = this.LoadDictionary(values, "ConsumedBuildings"); } private void OnDeserialized() { if (this._purchasedBuildings == null) { this._purchasedBuildings = new Dictionary(); } if (this._consumedBuildings == null) { this._consumedBuildings = new Dictionary(); } } private void SaveDictionary(Dictionary storage, string key, Dictionary values) { Dictionary dictionary = new Dictionary(); foreach (KeyValuePair keyValuePair in values) { dictionary.Add(keyValuePair.Key, keyValuePair.Value); } storage[key] = dictionary; } private Dictionary LoadDictionary(Dictionary storage, string key) { if (!storage.ContainsKey(key)) { return new Dictionary(); } if (!(storage[key] is Dictionary)) { throw new InvalidOperationException(string.Format("Found {0} at storage[{1}]. Expecting {2}", storage[key].GetType().Name, key, typeof(Dictionary).Name)); } Dictionary dictionary = new Dictionary(); Dictionary dictionary2 = (Dictionary)storage[key]; foreach (KeyValuePair keyValuePair in dictionary2) { if (!(dictionary2[keyValuePair.Key] is T)) { throw new InvalidOperationException(string.Format("Found {0} at storage[{1}]. Expecting {2}", storage[key].GetType().Name, key, typeof(T).Name)); } dictionary.Add(keyValuePair.Key, (T)((object)dictionary2[keyValuePair.Key])); } return dictionary; } private const string PurchasedBuildingsKey = "PurchasedBuildings"; private const string ConsumedBuildingsKey = "ConsumedBuildings"; [SerializeField] private Serializing _serializing; private Dictionary _purchasedBuildings; private Dictionary _consumedBuildings; public delegate void BuildingCountChangedHandler(); }