Exemplo n.º 1
0
    /// <summary>Loads the value from this ES3File with the given key into an existing object.</summary>
    /// <param name="key">The key which identifies the value we want to load.</param>
    /// <param name="obj">The object we want to load the value into.</param>
    public void LoadInto <T>(string key, T obj) where T : class
    {
        ES3Data es3Data;

        if (!cache.TryGetValue(key, out es3Data))
        {
            throw new KeyNotFoundException("Key \"" + key + "\" was not found in this ES3File. Use Load<T>(key, defaultValue) if you want to return a default value if the key does not exist.");
        }

        var settings = (ES3Settings)this.settings.Clone();

        settings.encryptionType  = ES3.EncryptionType.None;
        settings.compressionType = ES3.CompressionType.None;

        using (var ms = new System.IO.MemoryStream(es3Data.bytes, false))
        {
            using (var reader = ES3Reader.Create(ms, settings, false))
            {
                if (typeof(T) == typeof(object))
                {
                    reader.ReadInto <T>(obj, es3Data.type);
                }
                else
                {
                    reader.ReadInto <T>(obj, ES3TypeMgr.GetOrCreateES3Type(typeof(T)));
                }
            }
        }
    }
Exemplo n.º 2
0
    /// <summary>Loads the value from this ES3File with the given key.</summary>
    /// <param name="key">The key which identifies the value we want to load.</param>
    /// <param name="defaultValue">The value we want to return if the key does not exist in this ES3File.</param>
    public T Load <T>(string key, T defaultValue)
    {
        ES3Data es3Data;

        if (!cache.TryGetValue(key, out es3Data))
        {
            return(defaultValue);
        }
        var settings = (ES3Settings)this.settings.Clone();

        settings.encryptionType  = ES3.EncryptionType.None;
        settings.compressionType = ES3.CompressionType.None;

        using (var ms = new System.IO.MemoryStream(es3Data.bytes, false))
        {
            using (var reader = ES3Reader.Create(ms, settings, false))
            {
                if (typeof(T) == typeof(object))
                {
                    return(reader.Read <T>(es3Data.type));
                }
                else
                {
                    return(reader.Read <T>(ES3TypeMgr.GetOrCreateES3Type(typeof(T))));
                }
            }
        }
    }
Exemplo n.º 3
0
 /*
  *  Merges the contents of the non-temporary file with this ES3Writer,
  *  ignoring any keys which are marked for deletion.
  */
 protected void Merge()
 {
     using (var reader = ES3Reader.Create(settings))
     {
         if (reader == null)
         {
             return;
         }
         Merge(reader);
     }
 }
Exemplo n.º 4
0
 /// <summary>Checks whether a key exists in a file.</summary>
 /// <param name="key">The key we want to check the existence of.</param>
 /// <param name="settings">The settings we want to use to override the default settings.</param>
 /// <returns>True if the file exists, otherwise False.</returns>
 public static bool KeyExists(string key, ES3Settings settings)
 {
     using (var reader = ES3Reader.Create(settings))
     {
         if (reader == null)
         {
             return(false);
         }
         return(reader.Goto(key));
     }
 }
Exemplo n.º 5
0
 /// <summary>Loads the value from a file with the given key.</summary>
 /// <param name="key">The key which identifies the value we want to load.</param>
 /// <param name="defaultValue">The value we want to return if the file or key does not exist.</param>
 /// <param name="settings">The settings we want to use to override the default settings.</param>
 public static T Load <T>(string key, T defaultValue, ES3Settings settings)
 {
     using (var reader = ES3Reader.Create(settings))
     {
         if (reader == null)
         {
             return(defaultValue);
         }
         return(reader.Read <T>(key, defaultValue));
     }
 }
Exemplo n.º 6
0
 /// <summary>Loads the value from a file with the given key.</summary>
 /// <param name="key">The key which identifies the value we want to load.</param>
 /// <param name="settings">The settings we want to use to override the default settings.</param>
 public static T Load <T>(string key, ES3Settings settings)
 {
     using (var reader = ES3Reader.Create(settings))
     {
         if (reader == null)
         {
             throw new System.IO.FileNotFoundException("File \"" + settings.FullPath + "\" could not be found.");
         }
         return(reader.Read <T>(key));
     }
 }
Exemplo n.º 7
0
 public static string LoadStr(string key, string defaultValue)            // 不添加有歧义
 {
     using (var reader = ES3Reader.Create(new ES3Settings()))
     {
         if (reader == null)
         {
             return(defaultValue);
         }
         return(reader.Read(key, defaultValue));
     }
 }
Exemplo n.º 8
0
    /// <summary>Gets an array of all of the key names in a file.</summary>
    /// <param name="settings">The settings we want to use to override the default settings.</param>
    public static string[] GetKeys(ES3Settings settings)
    {
        var keys = new List <string>();

        using (var reader = ES3Reader.Create(settings))
        {
            foreach (string key in reader.Properties)
            {
                keys.Add(key);
                reader.Skip();
            }
        }
        return(keys.ToArray());
    }
Exemplo n.º 9
0
 /// <summary>Loads the value from a file with the given key into an existing object, rather than creating a new instance.</summary>
 /// <param name="key">The key which identifies the value we want to load.</param>
 /// <param name="obj">The object we want to load the value into.</param>
 /// <param name="settings">The settings we want to use to override the default settings.</param>
 public static void LoadInto <T>(string key, T obj, ES3Settings settings) where T : class
 {
     if (settings == null)
     {
         settings = new ES3Settings();
     }
     using (var reader = ES3Reader.Create(settings))
     {
         if (reader == null)
         {
             throw new System.IO.FileNotFoundException("File \"" + settings.FullPath + "\" could not be found.");
         }
         reader.ReadInto <T>(key, obj);
     }
 }
Exemplo n.º 10
0
    /// <summary>Loads the value from this ES3File with the given key.</summary>
    /// <param name="key">The key which identifies the value we want to load.</param>
    /// <param name="defaultValue">The value we want to return if the key does not exist in this ES3File.</param>
    public T Load <T>(string key, T defaultValue)
    {
        ES3Data es3Data;

        if (!cache.TryGetValue(key, out es3Data))
        {
            return(defaultValue);
        }
        var settings = new ES3Settings();

        settings.encryptionType = ES3.EncryptionType.None;

        using (var ms = new System.IO.MemoryStream(es3Data.bytes, false))
            using (var reader = ES3Reader.Create(ms, settings, false))
                return(reader.Read <T>(es3Data.type));
    }
Exemplo n.º 11
0
    /* Standard load methods */

    /// <summary>Loads the value from this ES3File with the given key.</summary>
    /// <param name="key">The key which identifies the value we want to load.</param>
    public T Load <T>(string key)
    {
        ES3Data es3Data;

        if (!cache.TryGetValue(key, out es3Data))
        {
            throw new KeyNotFoundException("Key \"" + key + "\" was not found in this ES3File. Use Load<T>(key, defaultValue) if you want to return a default value if the key does not exist.");
        }

        var settings = (ES3Settings)this.settings.Clone();

        settings.encryptionType = ES3.EncryptionType.None;

        using (var ms = new System.IO.MemoryStream(es3Data.bytes, false))
            using (var reader = ES3Reader.Create(ms, settings, false))
                return(reader.Read <T>(ES3TypeMgr.GetOrCreateES3Type(typeof(T))));
    }
Exemplo n.º 12
0
    /// <summary>Loads the value from this ES3File with the given key into an existing object.</summary>
    /// <param name="key">The key which identifies the value we want to load.</param>
    /// <param name="obj">The object we want to load the value into.</param>
    public void LoadInto <T>(string key, T obj) where T : class
    {
        ES3Data es3Data;

        if (!cache.TryGetValue(key, out es3Data))
        {
            throw new KeyNotFoundException("Key \"" + key + "\" was not found in this ES3File. Use Load<T>(key, defaultValue) if you want to return a default value if the key does not exist.");
        }

        var settings = new ES3Settings();

        settings.encryptionType = ES3.EncryptionType.None;

        using (var ms = new System.IO.MemoryStream(es3Data.bytes, false))
            using (var reader = ES3Reader.Create(ms, settings, false))
                reader.ReadInto <T>(obj, es3Data.type);
    }
Exemplo n.º 13
0
    /// <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;
            }
        }
    }
Exemplo n.º 14
0
 /// <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)
     {
         using (var reader = ES3Reader.Create(settings))
         {
             if (reader == null)
             {
                 return;
             }
             foreach (KeyValuePair <string, ES3Data> kvp in reader.RawEnumerator)
             {
                 cache[kvp.Key] = kvp.Value;
             }
         }
     }
 }
Exemplo n.º 15
0
    /// <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;
                }
            }
        }
    }
        private ES3Reader CreateReader(string key)
        {
            var settings = GetGeneralEasySave3Settings();

            return(ES3Reader.Create(key, settings));
        }