//////////////////////////////////////////////////////////////////////////////// // // @module Quick Save for Unity3D // @author Michael Clayton // @support clayton.inds+support@gmail.com // //////////////////////////////////////////////////////////////////////////////// using System; using CI.QuickSave.Core.Serialisers; namespace CI.QuickSave { public class QuickSaveReader : QuickSaveBase { private QuickSaveReader(string root, QuickSaveSettings settings) { _root = root; _settings = settings; } /// /// Creates a QuickSaveReader on the specified root /// /// The root to read from /// A QuickSaveReader instance public static QuickSaveReader Create(string root) { return Create(root, new QuickSaveSettings()); } /// /// Creates a QuickSaveReader on the specified root using the specified settings /// /// The root to read from /// Settings /// A QuickSaveReader instance public static QuickSaveReader Create(string root, QuickSaveSettings settings) { QuickSaveReader quickSaveReader = new QuickSaveReader(root, settings); quickSaveReader.Load(false); return quickSaveReader; } /// /// Reads an object under the specified key /// /// The type of object to read /// The key this object was saved under /// The object that was loaded public T Read(string key) { if (!Exists(key)) { throw new QuickSaveException("Key does not exists"); } try { return JsonSerialiser.DeserialiseKey(key, _items); } catch { throw new QuickSaveException("Deserialisation failed"); } } /// /// Reads an object under the specified key /// /// The type of object to read /// The key this object was saved under /// An action to be called when the read completes /// The QuickSaveReader public QuickSaveReader Read(string key, Action result) { if (!Exists(key)) { throw new QuickSaveException("Key does not exists"); } try { result(JsonSerialiser.DeserialiseKey(key, _items)); } catch { throw new QuickSaveException("Deserialisation failed"); } return this; } /// /// Attempts to read an object under the specified key /// /// The type of object to read /// The key this object was saved under /// The object that was loaded /// Was the read successful public bool TryRead(string key, out T result) { result = default(T); if (!Exists(key)) { return false; } try { result = JsonSerialiser.DeserialiseKey(key, _items); return true; } catch { return false; } } /// /// Reloads data from the root /// public void Reload() { Load(false); } } }