/// <summary> /// Removes objects of T that match func from file at path; /// </summary> /// <param name="path">File that should be searched.</param> /// <param name="func">Function to use for determing if object should be removed.</param> /// <typeparam name="T">What type of object are stored in this file (has to be the same for each object).</typeparam> public void RemoveFromFile <T>(string path, Func <T, bool> func) { if (FileController.Exists(path)) { var tempName = path + ".tmp"; lock (tempName) { bool any = false; foreach (var item in FileController.ReadLinesAs <T>(path)) { if (func.Invoke(item)) { FileController.AppendLineAs <T>(tempName, item); any = true; } } FileController.Delete(path); if (any) { FileController.Move(tempName, path); } } } }
private static void Load() { if (ValuesController.HasKey("currentUserIdentifier")) { ActiveUserId = ValuesController.GetValue <EntityId>("currentUserIdentifier"); } if (ValuesController.HasKey("deviceId")) { DeviceId = ValuesController.GetValue <EntityId>("deviceId"); } if (ValuesController.HasKey("installId")) { InstallationId = ValuesController.GetValue <EntityId>("installationId"); } if (FileController.Exists("userSettings")) { Users = MessagePackSerializer.Deserialize <List <UserSettings> >(FileController.ReadAllBytes("userSettings")); } else { Users = new List <UserSettings>(); } }
/// <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); }
/// <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))); }