/// <summary> /// Adds or update the given profile. /// Updates occur when a profile with the same name and game exist. /// </summary> /// <param name="prof">The profile to add or update</param> public static void AddOrUpdate(ProfileModel prof) { if (prof == null) throw new ArgumentNullException(nameof(prof)); lock (Profiles) { if (!Profiles.Contains(prof)) Profiles.Add(prof); } lock (prof) { // Store the file if (!(prof.GameName?.Length > 1) || !(prof.KeyboardSlug?.Length > 1) || !(prof.Name?.Length > 1)) throw new ArgumentException("Profile is invalid. Name, GameName and KeyboardSlug are required"); var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); string json; // Should saving fail for whatever reason, catch the exception and log it // But DON'T touch the profile file. try { json = JsonConvert.SerializeObject(prof, Formatting.Indented); } catch (Exception e) { Logger.Error(e, "Couldn't save profile '{0}.json'", prof.Name); return; } File.WriteAllText(path + $@"\{prof.Name}.json", json); } }
protected bool Equals(ProfileModel other) { return(string.Equals(Slug, other.Slug) && string.Equals(KeyboardSlug, other.KeyboardSlug) && string.Equals(GameName, other.GameName)); }
protected bool Equals(ProfileModel other) { return string.Equals(Name, other.Name) && string.Equals(KeyboardSlug, other.KeyboardSlug) && string.Equals(GameName, other.GameName); }
/// <summary> /// Adds a new profile to the current game and keyboard /// </summary> public async void AddProfile() { if (_mainManager.DeviceManager.ActiveKeyboard == null) { DialogService.ShowMessageBox("Cannot add profile.", "To add a profile, please select a keyboard in the options menu first."); return; } var name = await DialogService.ShowInputDialog("Add new profile", "Please provide a profile name unique to this game and keyboard."); // Null when the user cancelled if (name == null) return; if (name.Length < 2) { DialogService.ShowMessageBox("Invalid profile name", "Please provide a valid profile name"); return; } var profile = new ProfileModel { Name = name, KeyboardSlug = _mainManager.DeviceManager.ActiveKeyboard.Slug, Width = _mainManager.DeviceManager.ActiveKeyboard.Width, Height = _mainManager.DeviceManager.ActiveKeyboard.Height, GameName = _gameModel.Name }; if (ProfileProvider.GetAll().Contains(profile)) { var overwrite = await DialogService.ShowQuestionMessageBox("Overwrite existing profile", "A profile with this name already exists for this game. Would you like to overwrite it?"); if (!overwrite.Value) return; } ProfileProvider.AddOrUpdate(profile); LoadProfiles(); SelectedProfile = profile; }
public static void DeleteProfile(ProfileModel prof) { // Remove from datastore lock (Profiles) { // Get the profile from the datastore instead of just the provided value, to be certain it is removed var dsProfile = Profiles.FirstOrDefault(p => p.GameName == prof.GameName && p.Name == prof.Name && p.KeyboardSlug == prof.KeyboardSlug); if (dsProfile != null) Profiles.Remove(dsProfile); } // Remove the file var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}\{prof.Name}.json"; if (File.Exists(path)) File.Delete(path); }
/// <summary> /// Renames the profile on the model and filesystem /// </summary> /// <param name="profile">The profile to rename</param> /// <param name="name">The new name</param> public static void RenameProfile(ProfileModel profile, string name) { if (string.IsNullOrEmpty(name)) return; // Remove the old profile DeleteProfile(profile); // Update the profile, creating a new file profile.Name = name; AddOrUpdate(profile); }
/// <summary> /// Exports the given profile to the provided path in XML /// </summary> /// <param name="prof">The profile to export</param> /// <param name="path">The path to save the profile to</param> public static void ExportProfile(ProfileModel prof, string path) { var json = JsonConvert.SerializeObject(prof); File.WriteAllText(path, json); }