public void Import(string path) { try { Import(ArchiveReader.GetReader(Storage, path)); // We may or may not want to delete the file depending on where it is stored. // e.g. reconstructing/repairing database with beatmaps from default storage. // Also, not always a single file, i.e. for LegacyFilesystemReader // TODO: Add a check to prevent files from storage to be deleted. try { File.Delete(path); } catch (Exception e) { Logger.Error(e, $@"Could not delete file at {path}"); } } catch (Exception e) { e = e.InnerException ?? e; Logger.Error(e, @"Could not import beatmap set"); } }
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet) { if (string.IsNullOrEmpty(beatmapSet.Path)) return null; return ArchiveReader.GetReader(storage, beatmapSet.Path); }
public void Import(params string[] paths) { foreach (string p in paths) { var path = p; string hash = null; BeatmapMetadata metadata; using (var reader = ArchiveReader.GetReader(storage, path)) metadata = reader.ReadMetadata(); if (connection.Table <BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) { return; // TODO: Update this beatmap instead } if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader { using (var md5 = MD5.Create()) using (var input = storage.GetStream(path)) { hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant(); input.Seek(0, SeekOrigin.Begin); path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); using (var output = storage.GetStream(path, FileAccess.Write)) input.CopyTo(output); } } var beatmapSet = new BeatmapSetInfo { BeatmapSetID = metadata.BeatmapSetID, Beatmaps = new List <BeatmapInfo>(), Path = path, Hash = hash, Metadata = metadata }; using (var reader = ArchiveReader.GetReader(storage, path)) { string[] mapNames = reader.ReadBeatmaps(); foreach (var name in mapNames) { using (var stream = new StreamReader(reader.GetStream(name))) { var decoder = BeatmapDecoder.GetDecoder(stream); Beatmap beatmap = decoder.Decode(stream); beatmap.BeatmapInfo.Path = name; // TODO: Diff beatmap metadata with set metadata and leave it here if necessary beatmap.BeatmapInfo.Metadata = null; beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } } } Import(new[] { beatmapSet }); } }
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet) { if (string.IsNullOrEmpty(beatmapSet.Path)) { return(null); } return(ArchiveReader.GetReader(Storage, beatmapSet.Path)); }
/// <summary> /// Duplicates content from <paramref name="path"/> to storage and returns a representing <see cref="BeatmapSetInfo"/>. /// </summary> /// <param name="path">Content location</param> /// <returns><see cref="BeatmapSetInfo"/></returns> private BeatmapSetInfo getBeatmapSet(string path) { string hash = null; BeatmapMetadata metadata; using (var reader = ArchiveReader.GetReader(storage, path)) metadata = reader.ReadMetadata(); if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader { using (var input = storage.GetStream(path)) { hash = input.GetMd5Hash(); input.Seek(0, SeekOrigin.Begin); path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); if (!storage.Exists(path)) { using (var output = storage.GetStream(path, FileAccess.Write)) input.CopyTo(output); } } } var existing = connection.Table <BeatmapSetInfo>().FirstOrDefault(b => b.Hash == hash); if (existing != null) { if (existing.DeletePending) { existing.DeletePending = false; Update(existing, false); BeatmapSetAdded?.Invoke(existing); } return(existing); } var beatmapSet = new BeatmapSetInfo { OnlineBeatmapSetID = metadata.OnlineBeatmapSetID, Beatmaps = new List <BeatmapInfo>(), Path = path, Hash = hash, Metadata = metadata }; using (var archive = ArchiveReader.GetReader(storage, path)) { string[] mapNames = archive.BeatmapFilenames; foreach (var name in mapNames) { using (var raw = archive.GetStream(name)) using (var ms = new MemoryStream()) //we need a memory stream so we can seek and shit using (var sr = new StreamReader(ms)) { raw.CopyTo(ms); ms.Position = 0; var decoder = BeatmapDecoder.GetDecoder(sr); Beatmap beatmap = decoder.Decode(sr); beatmap.BeatmapInfo.Path = name; beatmap.BeatmapInfo.Hash = ms.GetMd5Hash(); // TODO: Diff beatmap metadata with set metadata and leave it here if necessary beatmap.BeatmapInfo.Metadata = null; beatmap.BeatmapInfo.StarDifficulty = beatmap.CalculateStarDifficulty(); beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } } beatmapSet.StoryboardFile = archive.StoryboardFilename; } return(beatmapSet); }
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet) { return(ArchiveReader.GetReader(storage, beatmapSet.Path)); }
/// <summary> /// Duplicates content from <paramref name="path"/> to storage and returns a representing <see cref="BeatmapSetInfo"/>. /// </summary> /// <param name="path">Content location</param> /// <returns><see cref="BeatmapSetInfo"/></returns> private BeatmapSetInfo getBeatmapSet(string path) { string hash = null; BeatmapMetadata metadata; using (var reader = ArchiveReader.GetReader(storage, path)) metadata = reader.ReadMetadata(); if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader { using (var md5 = MD5.Create()) using (var input = storage.GetStream(path)) { hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant(); input.Seek(0, SeekOrigin.Begin); path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); if (!storage.Exists(path)) { using (var output = storage.GetStream(path, FileAccess.Write)) input.CopyTo(output); } } } var existing = connection.Table <BeatmapSetInfo>().FirstOrDefault(b => b.Hash == hash); if (existing != null) { if (existing.DeletePending) { existing.DeletePending = false; Update(existing, false); BeatmapSetAdded?.Invoke(existing); } return(existing); } var beatmapSet = new BeatmapSetInfo { OnlineBeatmapSetID = metadata.OnlineBeatmapSetID, Beatmaps = new List <BeatmapInfo>(), Path = path, Hash = hash, Metadata = metadata }; using (var reader = ArchiveReader.GetReader(storage, path)) { string[] mapNames = reader.BeatmapFilenames; foreach (var name in mapNames) { using (var stream = new StreamReader(reader.GetStream(name))) { var decoder = BeatmapDecoder.GetDecoder(stream); Beatmap beatmap = decoder.Decode(stream); beatmap.BeatmapInfo.Path = name; // TODO: Diff beatmap metadata with set metadata and leave it here if necessary beatmap.BeatmapInfo.Metadata = null; beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } } beatmapSet.StoryboardFile = reader.StoryboardFilename; } return(beatmapSet); }
private BeatmapSetInfo getBeatmapSet(ArchiveReader archiveReader) { BeatmapMetadata metadata; using (var stream = new StreamReader(archiveReader.GetStream(archiveReader.BeatmapFilenames[0]))) metadata = BeatmapDecoder.GetDecoder(stream).Decode(stream).Metadata; string hash; string path; using (var input = archiveReader.GetUnderlyingStream()) { hash = input.GetMd5Hash(); input.Seek(0, SeekOrigin.Begin); path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); if (!Storage.Exists(path)) { using (var output = Storage.GetStream(path, FileAccess.Write)) input.CopyTo(output); } } var existing = Connection.Table <BeatmapSetInfo>().FirstOrDefault(b => b.Hash == hash); if (existing != null) { GetChildren(existing); if (existing.DeletePending) { existing.DeletePending = false; Update(existing, false); BeatmapSetAdded?.Invoke(existing); } return(existing); } var beatmapSet = new BeatmapSetInfo { OnlineBeatmapSetID = metadata.OnlineBeatmapSetID, Beatmaps = new List <BeatmapInfo>(), Path = path, Hash = hash, Metadata = metadata }; using (var archive = ArchiveReader.GetReader(Storage, path)) { string[] mapNames = archive.BeatmapFilenames; foreach (var name in mapNames) { using (var raw = archive.GetStream(name)) using (var ms = new MemoryStream()) //we need a memory stream so we can seek and shit using (var sr = new StreamReader(ms)) { raw.CopyTo(ms); ms.Position = 0; var decoder = BeatmapDecoder.GetDecoder(sr); Beatmap beatmap = decoder.Decode(sr); beatmap.BeatmapInfo.Path = name; beatmap.BeatmapInfo.Hash = ms.GetMd5Hash(); // TODO: Diff beatmap metadata with set metadata and leave it here if necessary beatmap.BeatmapInfo.Metadata = null; // TODO: this should be done in a better place once we actually need to dynamically update it. beatmap.BeatmapInfo.Ruleset = rulesets.Query <RulesetInfo>().FirstOrDefault(r => r.ID == beatmap.BeatmapInfo.RulesetID); beatmap.BeatmapInfo.StarDifficulty = rulesets.Query <RulesetInfo>().FirstOrDefault(r => r.ID == beatmap.BeatmapInfo.RulesetID)?.CreateInstance()?.CreateDifficultyCalculator(beatmap).Calculate() ?? 0; beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } } beatmapSet.StoryboardFile = archive.StoryboardFilename; } return(beatmapSet); }
/// <summary> /// Duplicates content from <paramref name="path"/> to storage and returns a representing <see cref="BeatmapSetInfo"/>. /// </summary> /// <param name="path">Content location</param> /// <returns><see cref="BeatmapSetInfo"/></returns> private BeatmapSetInfo getBeatmapSet(string path) => getBeatmapSet(ArchiveReader.GetReader(Storage, path));