/// <summary> /// Fetches all maps, groups them into mapsets, sets them to allow them to be played. /// </summary> public static void OrderAndSetMapsets() { var maps = FetchAll(); var mapsets = MapsetHelper.ConvertMapsToMapsets(maps); MapManager.Mapsets = MapsetHelper.OrderMapsByDifficulty(MapsetHelper.OrderMapsetsByArtist(mapsets)); }
/// <summary> /// Checks the maps in the database vs. the amount of .chart files on disk. /// If there's a mismatch, it will add any missing ones /// </summary> private static void SyncMissingOrUpdatedFiles(IReadOnlyCollection <string> files) { var maps = FetchAll(); foreach (var map in maps) { var filePath = BackslashToForward($"{GlobalConfig.Load().SongDirectory}/{map.Directory}/{map.Path}"); // Check if the file actually exists. if (files.Any(x => BackslashToForward(x) == filePath)) { // Check if the file was updated. In this case, we check if the last write times are different // BEFORE checking Md5 checksum of the file since it's faster to check if we even need to // bother updating it. if (map.LastFileWrite != File.GetLastWriteTimeUtc(filePath)) { if (map.Md5Checksum == MapsetHelper.GetMd5Checksum(filePath)) { continue; } Map newMap; try { newMap = Map.FromChart(map.LoadChart(false), filePath); } catch (Exception e) { Logger.Log(e); File.Delete(filePath); new SQLiteConnection(DatabasePath).Delete(map); Logger.Log($"Removed {filePath} from the cache, as the file could not be parsed.", LogLevel.Warning); continue; } newMap.CalculateDifficulties(); newMap.Id = map.Id; new SQLiteConnection(DatabasePath).Update(newMap); Logger.Log($"Updated cached map: {newMap.Id}, as the file was updated.", LogLevel.Info); } continue; } // The file doesn't exist, so we can safely delete it from the cache. new SQLiteConnection(DatabasePath).Delete(map); Logger.Log($"Removed {filePath} from the cache, as the file no longer exists", LogLevel.Info); } }
/// <summary> /// Responsible for converting a ChartFile object, to a Map object /// a Map object is one that is stored in the db. /// </summary> /// <param name="chart"></param> /// <param name="path"></param> /// <returns></returns> public static Map FromChart(ChartFile chart, string path, bool skipPathSetting = false) { var map = new Map { Artist = chart.Artist, Title = chart.Title, HighestRank = Grade.None, AudioPath = chart.SoundFile, AudioPreviewTime = chart.PreviewStart, BackgroundPath = "", Description = "", MapId = chart.ID, MapSetId = 0, Creator = chart.Creator, DifficultyName = chart.Difficulty, Source = chart.Source, Tags = chart.Tags, SongLength = 0, NoteCount = 0, SliderCount = 0, SpinnerCount = 0 }; if (!skipPathSetting) { try { map.Md5Checksum = MapsetHelper.GetMd5Checksum(path); map.Directory = new DirectoryInfo(System.IO.Path.GetDirectoryName(path) ?? throw new InvalidOperationException()).Name.Replace("\\", "/"); map.Path = System.IO.Path.GetFileName(path)?.Replace("\\", "/"); map.LastFileWrite = File.GetLastWriteTimeUtc(map.Path); } // ReSharper disable once EmptyGeneralCatchClause catch (Exception) { } } try { map.Bpm = chart.BPM; } catch (Exception) { map.Bpm = 0; } map.DateAdded = DateTime.Now; return(map); }