ISaveStorage

Summary: Implement your own storage backend, for example to save to the cloud.

Description

Implement ISaveStorage to store save data somewhere the built-in backends do not, then pass it to SetStorage. Save System prepares the bytes, including protection, compression and checksums, so the storage only reads and writes them by key.

Storage keys have the form slot/name. Slot metadata uses its own key in the same slot. Store each key exactly as given and return the same bytes on read.

Plain ISaveStorage keeps one copy per key. To keep temporary and backup copies for recovery, also implement IBackupStorage, as MemoryStorage does.

Namespace Gley.SaveSystem.

Declaration

C#
public interface ISaveStorage

Members

MemberTypeDescription
IdentitystringA stable name for this store. Save System queues operations per identity and key, so two instances that share the same underlying data should use the same identity.
RequiresMainThreadboolTrue when the storage must run on Unity’s main thread, for example because it calls Unity APIs. When false, the methods can be called from background threads and must be thread-safe.
TryRead—bool TryRead(string storageKey, out byte[] data)
Returns the stored bytes for a key, or false when the key is missing.
Write—void Write(string storageKey, byte[] data)
Stores the bytes for a key, replacing any earlier data. Throw an exception if the write fails.
Delete—void Delete(string storageKey)
Removes the data for a key. A missing key is not an error.
Exists—bool Exists(string storageKey)
Returns true when data is stored for a key.
List—IReadOnlyList<string> List(string prefix)
Returns every stored key that starts with prefix. Save System uses it to list slots and the saves inside them.

Example

Create UseSessionStorage.cs and attach it to an active GameObject. Enter Play mode to switch the shared service to a storage that keeps saves in a dictionary for the current session. Replace the dictionary with your own service calls to store saves elsewhere.

C#
using System;
using System.Collections.Generic;
using Gley.SaveSystem;
using UnityEngine;

public class UseSessionStorage : MonoBehaviour
{
    private void Awake()
    {
        API.SetStorage(new SessionStorage("session-storage-example"));
        Debug.Log("Save System is using SessionStorage.", this);
    }

    private class SessionStorage : ISaveStorage
    {
        private readonly Dictionary<string, byte[]> _entries = new Dictionary<string, byte[]>(StringComparer.Ordinal);
        private readonly object _gate = new object();

        public string Identity { get; }
        public bool RequiresMainThread => false;

        public SessionStorage(string identity)
        {
            Identity = identity;
        }

        public bool TryRead(string storageKey, out byte[] data)
        {
            lock (_gate)
            {
                if (!_entries.TryGetValue(storageKey, out byte[] stored))
                {
                    data = null;
                    return false;
                }

                data = (byte[])stored.Clone();
                return true;
            }
        }

        public void Write(string storageKey, byte[] data)
        {
            lock (_gate)
            {
                _entries[storageKey] = (byte[])data.Clone();
            }
        }

        public void Delete(string storageKey)
        {
            lock (_gate)
            {
                _entries.Remove(storageKey);
            }
        }

        public bool Exists(string storageKey)
        {
            lock (_gate)
            {
                return _entries.ContainsKey(storageKey);
            }
        }

        public IReadOnlyList<string> List(string prefix)
        {
            List<string> keys = new List<string>();
            lock (_gate)
            {
                foreach (string key in _entries.Keys)
                {
                    if (key.StartsWith(prefix, StringComparison.Ordinal))
                    {
                        keys.Add(key);
                    }
                }
            }

            return keys;
        }
    }
}
On This Page