Esempio n. 1
0
        /// <summary>
        /// Collects, serializes and stores the state of all ScriptableObject instances currently in the list.
        /// Also persists previously saved data that is not currently used by any of the ScriptableObject instances.
        /// </summary>
        public void SaveData()
        {
            ValidatePersistenceMechanism();

            if (_persistenceList.Length == 0)
            {
                return;
            }

            // Overwrite existing entry for given persistable with fresh data, or - if entry didn't exist - create it.
            // Preserves old entries (which are not connected to any current persistable) to avoid data loss.
            foreach (var persistable in _persistenceList)
            {
                if (persistable == null)
                {
                    continue;
                }

                var id = GetID(persistable);
                _dataEntities[id] = new DataEntity()
                {
                    EntityId = id,
                    JsonData = JsonUtility.ToJson(persistable)
                };
            }

            DataEntitiesContainer container = new DataEntitiesContainer()
            {
                // Convert the values in the dictionary to an array. Dictionary enumeration is slow, but Count is expected to be negligible.
                DataEntities = _dataEntities.Select((kvp, _) => kvp.Value).ToArray()
            };

            // Push the container into the persistence mechanism for storage, under the given key.
            _persistenceMechanism.Save(_persistentContainerKey, container);
        }
Esempio n. 2
0
 public Repository()
 {
     context = new DataEntitiesContainer();
 }