/// <summary> /// rename game and forece-update its data /// </summary> /// <param name="gameId"></param> /// <param name="newGameTitle"></param> /// <param name="gameChanged"></param> public static void RenameGame(string gameId, string newGameTitle, Action <string> gameChanged) { var oldGameTitle = Games[gameId].Title; Log.WriteLine($"RenameGame({ gameId }, { oldGameTitle } -> { newGameTitle })"); // check if new title already exists if (string.IsNullOrEmpty(newGameTitle) || (newGameTitle != oldGameTitle && Games.Any(g => g.Value.Title.ToLower() == newGameTitle.ToLower()))) { gameChanged(null); return; } if (Games.TryRemove(gameId, out GameInfo oldGame)) { if (oldGame.Shortcut != null && !File.Exists(oldGame.Shortcut)) { oldGame.Shortcut = null; } var newShortcut = oldGame.Shortcut; if (oldGame.Shortcut != null) { newShortcut = Path.Combine(GamesDirectory, $"{ newGameTitle }{ new FileInfo(oldGame.Shortcut).Extension }"); File.Move(oldGame.Shortcut, newShortcut); } var newGame = new GameInfo { Added = oldGame.Added, Completed = oldGame.Completed, TimeStamps = oldGame.TimeStamps, Title = newGameTitle, Shortcut = newShortcut, Data = new GameData[] { }, LastDataUpdate = new ConcurrentDictionary <string, DateTime>(), }; var newGameId = GetGameId(newGame); Games.AddOrUpdate(newGameId, newGame, (id, oldValue) => newGame); Threading.ThreadAndForget(() => { UpdateGame(newGameId, true); Save(); gameChanged(newGameId); }); } }
public void AddOrUpdate(DatabaseEntry entry) { Games.AddOrUpdate(entry.Id, entry, (key, oldEntry) => oldEntry.MergeIn(entry)); }
/// <summary> /// load game library from xml and add new games /// </summary> /// <returns>games to update (without images)</returns> static bool LoadGames(string libraryToLoad = null) { using (var tb = new TimedBlock($"LoadGames({ libraryToLoad })")) { try { Games.Clear(); // deserialize library DeserializeLibrary(libraryToLoad); if (!string.IsNullOrEmpty(GamesDirectory)) { // add new games from current games folder foreach (var gameShortcut in Directory.GetFiles(GamesDirectory, "*.*", SearchOption.AllDirectories)) { var fileInfo = new FileInfo(gameShortcut); if (!SupportedGameExtensions.Contains(fileInfo.Extension.ToLower())) { continue; } var gameTitle = fileInfo.Name.Replace(fileInfo.Extension, string.Empty); var gameId = GetGameId(gameTitle); if (!Games.ContainsKey(gameId)) { var newGameInfo = new GameInfo { Title = gameTitle, Shortcut = gameShortcut, Added = DateTime.Now, }; // add new game Games.AddOrUpdate(gameId, newGameInfo, (key, oldGameInfo) => newGameInfo); } // update game shortcut everytime Games[gameId].Shortcut = gameShortcut; } } // mark games with not-existing shortcuts as removed foreach (var game in Games.Where(g => g.Value.Shortcut != null && !File.Exists(g.Value.Shortcut))) { game.Value.Shortcut = null; } return(true); } catch (Exception ex) { Log.WriteLine(ex.ToString()); } } return(false); }