/// <summary>Saves a value to a key in this ES3File.</summary> /// <param name="key">The key we want to use to identify our value in the file.</param> /// <param name="value">The value we want to save.</param> public void Save <T>(string key, object value) { using (var stream = new MemoryStream(settings.bufferSize)) { var unencryptedSettings = (ES3Settings)settings.Clone(); unencryptedSettings.encryptionType = ES3.EncryptionType.None; var es3Type = ES3TypeMgr.GetOrCreateES3Type(typeof(T)); using (var baseWriter = ES3Writer.Create(stream, unencryptedSettings, false, false)) baseWriter.Write(value, es3Type); cache[key] = new ES3Data(es3Type, stream.ToArray()); } }
internal static void CacheFile(ES3Settings settings) { // If we're still using cached settings, default to file. if (settings.location == ES3.Location.Cache) { settings = (ES3Settings)settings.Clone(); settings.location = ES3.Location.File; } if (!ES3.FileExists(settings)) { return; } cachedFiles[settings.path] = new ES3File(ES3.LoadRawBytes(settings)); }
internal static void Store(ES3Settings settings) { ES3File cachedFile; if (!cachedFiles.TryGetValue(settings.path, out cachedFile)) { throw new FileNotFoundException("The file '" + settings.path + "' could not be stored because it could not be found in the cache."); } if (settings.location == ES3.Location.Cache) { settings = (ES3Settings)settings.Clone(); settings.location = ES3.Location.File; ES3Debug.LogWarning("Location is set to 'Cache' when trying to store a cached file, but this should be set to a location in persistent storage (e.g. File, PlayerPrefs). Easy Save will store this cached file to ES3.Location.File."); } cachedFile.Sync(settings); }
/// <summary>Saves a value to a key in this ES3File.</summary> /// <param name="key">The key we want to use to identify our value in the file.</param> /// <param name="value">The value we want to save.</param> public void Save <T>(string key, T value) { var unencryptedSettings = (ES3Settings)settings.Clone(); unencryptedSettings.encryptionType = ES3.EncryptionType.None; unencryptedSettings.compressionType = ES3.CompressionType.None; // If T is object, use the value to get it's type. Otherwise, use T so that it works with inheritence. cache[key] = new ES3Data(ES3TypeMgr.GetOrCreateES3Type(typeof(T)), ES3.Serialize(value, unencryptedSettings)); }
// Explicit Stream Methods. internal static ES3Writer Create(Stream stream, ES3Settings settings, bool writeHeaderAndFooter, bool overwriteKeys) { if (stream.GetType() == typeof(MemoryStream)) { settings = (ES3Settings)settings.Clone(); settings.location = ES3.Location.InternalMS; } // Get the baseWriter using the given Stream. if (settings.format == ES3.Format.JSON) { return(new ES3JSONWriter(stream, settings, writeHeaderAndFooter, overwriteKeys)); } else { return(null); } }
/// <summary>Merges the data specified by the bytes parameter into this ES3File.</summary> /// <param name="bytes">The bytes we want to merge with this ES3File.</param> /// <param name="settings">The settings we want to use to override the default settings.</param> public void SaveRaw(byte[] bytes, ES3Settings settings) { // Type checking must be enabled when syncing. var settingsWithTypeChecking = (ES3Settings)settings.Clone(); settingsWithTypeChecking.typeChecking = true; using (var reader = ES3Reader.Create(bytes, settingsWithTypeChecking)) { if (reader == null) { return; } foreach (KeyValuePair <string, ES3Data> kvp in reader.RawEnumerator) { cache [kvp.Key] = kvp.Value; } } }
internal static void CacheFile(ES3Settings settings) { // If we're still using cached settings, default to file. if (settings.location == ES3.Location.Cache) { settings = (ES3Settings)settings.Clone(); settings.location = ES3.Location.File; } if (!ES3.FileExists(settings)) { return; } // Disable compression when loading the raw bytes, and the ES3File constructor will expect compressed bytes. var loadSettings = (ES3Settings)settings.Clone(); loadSettings.compressionType = ES3.CompressionType.None; cachedFiles[settings.path] = new ES3File(ES3.LoadRawBytes(loadSettings), settings); }
/// <summary>Creates a new ES3File and loads the specified file into the ES3File if there is data to load.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> /// <param name="syncWithFile">Whether we should sync this ES3File with the one in storage immediately after creating it.</param> public ES3File(ES3Settings settings, bool syncWithFile) { this.settings = settings; this.syncWithFile = syncWithFile; if (syncWithFile) { // Type checking must be enabled when syncing. var settingsWithTypeChecking = (ES3Settings)settings.Clone(); settingsWithTypeChecking.typeChecking = true; using (var reader = ES3Reader.Create(settingsWithTypeChecking)) { if (reader == null) { return; } foreach (KeyValuePair <string, ES3Data> kvp in reader.RawEnumerator) { cache[kvp.Key] = kvp.Value; } } } }