using System; using System.Collections.Generic; using System.Diagnostics; using SUISS.Storage; using UnityEngine; namespace SUISSEngine { public class IslandState : MonoBehaviour { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event IslandState.ValueChangedHandler ValueChanged; protected virtual void Awake() { if (this.values == null) { this.LoadState(); } } protected virtual void Start() { } protected virtual void OnDestroy() { this.ValueChanged = null; } public static IslandState ParentOf(Component comp) { return comp.GetComponentInParent(); } public virtual T GetValue(string key, T defaultValue) { if (this.values == null) { this.LoadState(); } object obj; if (!this.values.TryGetValue(key, out obj)) { return defaultValue; } if (obj is T) { return (T)((object)obj); } return defaultValue; } public virtual void SetValue(string key, object value) { if (this.isNewGameState) { this.values[key] = value; } else { if (this.values == null) { this.LoadState(); } object obj; if (!this.values.TryGetValue(key, out obj)) { obj = null; } if (!object.Equals(obj, value)) { this.values[key] = value; this.OnValueChanged(key, obj, value); } } } protected virtual void LoadState() { Storage storage = Storage.Get(StorageLifecycle.Game); if (storage.Root.ContainsKey(this.StorageKey)) { this.values = (Dictionary)storage.Root[this.StorageKey]; } else { this.values = new Dictionary(); storage.Root[this.StorageKey] = this.values; this.isNewGameState = true; this.OnNewGameState(); this.isNewGameState = false; } } protected virtual void OnValueChanged(string key, object oldValue, object newValue) { if (this.ValueChanged != null) { this.ValueChanged(key, oldValue, newValue); } } protected virtual void OnNewGameState() { } public string StorageKey; protected Dictionary values; protected bool isNewGameState; public delegate void ValueChangedHandler(string key, object oldValue, object newValue); } }