/// <summary>Synchronises this ES3File with a file in storage.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> public void Sync(ES3Settings settings = null) { if (settings == null) { settings = new ES3Settings(); } ES3.DeleteFile(settings); if (cache.Count == 0) { return; } using (var baseWriter = ES3Writer.Create(settings, true, !syncWithFile, false)) { foreach (var kvp in cache) { // If we change the name of a type, the type may be null. // In this case, use System.Object as the type. Type type; if (kvp.Value.type == null) { type = typeof(System.Object); } else { type = kvp.Value.type.type; } baseWriter.Write(kvp.Key, type, kvp.Value.bytes); } baseWriter.Save(!syncWithFile); } }
/* * Same as LoadRawString, except it will return an encrypted/compressed file if these are enabled. */ internal byte[] GetBytes(ES3Settings settings = null) { if (cache.Count == 0) { return(new byte[0]); } if (settings == null) { settings = this.settings; } using (var ms = new System.IO.MemoryStream()) { var memorySettings = (ES3Settings)settings.Clone(); memorySettings.location = ES3.Location.InternalMS; // Ensure we return unencrypted bytes. memorySettings.encryptionType = ES3.EncryptionType.None; memorySettings.compressionType = ES3.CompressionType.None; using (var baseWriter = ES3Writer.Create(ES3Stream.CreateStream(ms, memorySettings, ES3FileMode.Write), memorySettings, true, false)) { foreach (var kvp in cache) { baseWriter.Write(kvp.Key, kvp.Value.type.type, kvp.Value.bytes); } baseWriter.Save(false); } return(ms.ToArray()); } }
public override void WritePropertyByRef(string name, UnityEngine.Object value) { if (SerializationDepthLimitExceeded()) { return; } StartWriteProperty(name); using (var ms = new MemoryStream()) { using (var writer = ES3Writer.Create(ms, new ES3Settings(ES3.EncryptionType.None, ES3.CompressionType.None, ES3.Format.Binary_Alpha), false, false)) { if (value == null) { WriteNull(); return; } ; writer.StartWriteObject(name); writer.WriteRef(value); writer.EndWriteObject(name); } var bytes = ms.ToArray(); Write7BitEncodedInt(bytes.Length); baseWriter.Write(bytes); } EndWriteProperty(name); }
/// <summary>Saves the value to a file with the given key.</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> /// <param name="settings">The settings we want to use to override the default settings.</param> public static void Save <T>(string key, object value, ES3Settings settings) { using (var writer = ES3Writer.Create(settings)) { writer.Write <T>(key, value); writer.Save(); } }
/// <summary>Deletes a key in the file specified by the ES3Settings object.</summary> /// <param name="key">The key we want to delete.</param> /// <param name="settings">The settings we want to use to override the default settings.</param> public static void DeleteKey(string key, ES3Settings settings) { if (settings.location == Location.Resources) { throw new System.NotSupportedException("Modifying files in Resources is not allowed."); } using (var writer = ES3Writer.Create(settings)) { writer.MarkKeyForDeletion(key); writer.Save(); } }
public void Save(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(value.GetType()); using (var baseWriter = ES3Writer.Create(stream, unencryptedSettings, false, false)) baseWriter.Write(value, es3Type); cache[key] = new ES3Data(es3Type, stream.ToArray()); } }
/// <summary>Synchronises this ES3File with a file in storage.</summary> /// <param name="settings">The settings we want to use to override the default settings.</param> public void Sync(ES3Settings settings) { if (cache.Count == 0) { return; } using (var baseWriter = ES3Writer.Create(settings, true, !syncWithFile, false)) { foreach (var kvp in cache) { baseWriter.Write(kvp.Key, kvp.Value.type.type, kvp.Value.bytes); } baseWriter.Save(!syncWithFile); } }
internal override void Write(string key, Type type, byte[] value) { StartWriteProperty(key); using (var ms = new MemoryStream()) { using (var writer = ES3Writer.Create(ms, new ES3Settings(ES3.EncryptionType.None, ES3.CompressionType.None, ES3.Format.Binary_Alpha), false, false)) { writer.StartWriteObject(key); writer.WriteType(type); writer.WriteRawProperty("value", value); writer.EndWriteObject(key); } var bytes = ms.ToArray(); Write7BitEncodedInt(bytes.Length); baseWriter.Write(bytes); } EndWriteProperty(key); }
public override void Write(Type type, string key, object value) { StartWriteProperty(key); using (var ms = new MemoryStream()) { using (var writer = ES3Writer.Create(ms, new ES3Settings(ES3.EncryptionType.None, ES3.CompressionType.None, ES3.Format.Binary_Alpha), false, false)) { writer.StartWriteObject(key); writer.WriteType(type); writer.WriteProperty("value", value, ES3TypeMgr.GetOrCreateES3Type(type), settings.referenceMode); writer.EndWriteObject(key); } var bytes = ms.ToArray(); Write7BitEncodedInt(bytes.Length); baseWriter.Write(bytes); } EndWriteProperty(key); MarkKeyForDeletion(key); }
/// <summary>Loads the ES3File as a raw byte array.</summary> public byte[] LoadRawBytes() { if (cache.Count == 0) { return(new byte[0]); } using (var ms = new System.IO.MemoryStream()) { using (var baseWriter = ES3Writer.Create(ms, settings, false, false)) { foreach (var kvp in cache) { baseWriter.Write(kvp.Key, kvp.Value.type.type, kvp.Value.bytes); } baseWriter.Save(false); } return(ms.ToArray()); } }
/// <summary>Loads the ES3File as a raw byte array.</summary> public byte[] LoadRawBytes() { if (cache.Count == 0) { return(new byte[0]); } using (var ms = new System.IO.MemoryStream()) { var memorySettings = (ES3Settings)settings.Clone(); memorySettings.location = ES3.Location.Memory; using (var baseWriter = ES3Writer.Create(ms, memorySettings, true, false)) { foreach (var kvp in cache) { baseWriter.Write(kvp.Key, kvp.Value.type.type, kvp.Value.bytes); } baseWriter.Save(false); } return(ms.ToArray()); } }
/// <summary>Writes a field or property to the writer. Note that this should only be called within an ES3Type.</summary> /// <param name="name">The name of the field or property.</param> /// <param name="value">The value we want to write.</param> /// <param name="memberReferenceMode">Whether we want to write the property by reference, by value, or both.</param> public override void WriteProperty(string name, object value, ES3.ReferenceMode memberReferenceMode) { if (SerializationDepthLimitExceeded()) { return; } StartWriteProperty(name); using (var ms = new MemoryStream()) { using (var writer = ES3Writer.Create(ms, new ES3Settings(ES3.EncryptionType.None, ES3.CompressionType.None, ES3.Format.Binary_Alpha), false, false)) { writer.Write(value, memberReferenceMode); } var bytes = ms.ToArray(); Write7BitEncodedInt(bytes.Length); baseWriter.Write(bytes); } EndWriteProperty(name); }
private ES3Writer CreateWriter(string key) { var settings = GetGeneralEasySave3Settings(); return(ES3Writer.Create(key, settings)); }