/// <summary> /// Gets the string. /// </summary> /// <returns>The string found under that key.</returns> /// <param name="key">Key.</param> public static string GetString(string key) { try { return(Encoding.UTF8.GetString(FileController.ReadAllBytes(SaveKeyWithPrefix(key)))); } catch (FileNotFoundException) { return(null); } }
/// <summary> /// Gets an int. /// </summary> /// <returns>The int if found or -1 if an error occured.</returns> /// <param name="key">Key.</param> public static int GetInt(string key) { try { return(BitConverter.ToInt32(FileController.ReadAllBytes("data" + key), 0)); } catch (FileNotFoundException) { return(-1); } }
/// <summary> /// Loads data from disc and tries to decrypt and deserialize it. /// </summary> /// <returns>The loaded object.</returns> /// <param name="relativePath">Path relative to the data folder.</param> /// <param name="createNew">Function to create a new instance of the object when not found on disc.</param> /// <typeparam name="T">Type to deserialize to.</typeparam> public T LoadObject <T>(string relativePath, Func <T> createNew = null) { if (!FileController.Exists(relativePath)) { if (createNew != null) { return(createNew()); } else { return((T)Activator.CreateInstance(typeof(T))); } } return(MessagePackSerializer.Deserialize <T>(LoadData(relativePath))); }
/// <summary> /// Tries to load the reference from disc. /// </summary> /// <returns><c>true</c>, if load resource was tryed, <c>false</c> otherwise.</returns> /// <param name="id">Identifier.</param> /// <param name="data">Data.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public bool TryLoadReference(EntityId id, out InnerReference <Entity> data, bool cache = true) { var path = $"res/{id.ToString()}"; if (!FileController.Exists(path)) { data = null; return(false); } var bytes = DataController.Instance.LoadData(path); var result = MessagePack.MessagePackSerializer.Typeless.Deserialize(bytes) as InnerReference <Entity>; if (cache) { references[id] = result; } data = result; return(true); }
public void Save() { FileController.SaveAs("coflnet_keyPairs", keys); FileController.SaveAs("signingKeys", signingKeyPairs); }
/// <summary> /// Sets the value. /// Objects save this way have to have DataContract or <see cref="MessagePack.MessagePackObjectAttribute"/> attribute /// </summary> /// <param name="key">Key.</param> /// <param name="value">Value.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static void SetValue <T>(string key, T value) { FileController.SaveAs(SaveKeyWithPrefix(key), value); }
/// <summary> /// Deletes the key. /// </summary> /// <param name="key">Key.</param> public static void DeleteKey(string key) { FileController.Delete(SaveKeyWithPrefix(key)); }
/// <summary> /// Sets an int. /// </summary> /// <param name="key">Key under which to save the int.</param> /// <param name="value">The int which to save.</param> public static void SetInt(string key, int value) { FileController.WriteAllBytes(SaveKeyWithPrefix(key), BitConverter.GetBytes(value)); }
/// <summary> /// Saves a string to disc /// </summary> /// <param name="key">Key under which to save the string.</param> /// <param name="value">Value.</param> public static void SetString(string key, string value) { FileController.WriteAllBytes(SaveKeyWithPrefix(key), Encoding.UTF8.GetBytes(value)); }
/// <summary> /// Hases the key. /// </summary> /// <returns><c>true</c>, if key exists, <c>false</c> otherwise.</returns> /// <param name="key">Key to search for.</param> public static bool HasKey(string key) { return(FileController.Exists(SaveKeyWithPrefix(key))); }
/// <summary> /// Gets the value. /// </summary> /// <returns>The value.</returns> /// <param name="key">Key.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T GetValue <T>(string key) { return(FileController.LoadAs <T>(SaveKeyWithPrefix(key))); }
/// <summary> /// Saves the data after encryption /// </summary> /// <param name="path">Path relative to the data folder.</param> /// <param name="data">Data.</param> public void SaveData(string path, byte[] data) { FileController.WriteAllBytes(path, Encrypt(data)); }
/// <summary> /// Loads data from disc and tries to decrypt it. /// </summary> /// <returns>The decrypted data.</returns> /// <param name="relativePath">Relative path.</param> public byte[] LoadData(string relativePath) { return(Decrypt(FileController.ReadAllBytes(relativePath))); }