1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 20:53:04 +08:00

Fix issues with beatmap import file contention

This commit is contained in:
Drew DeVault 2016-10-19 11:00:11 -04:00
parent c41b3d92c6
commit 641855c790
2 changed files with 25 additions and 22 deletions

View File

@ -19,13 +19,15 @@ namespace osu.Game.Beatmaps.IO
}); });
OsuLegacyDecoder.Register(); OsuLegacyDecoder.Register();
} }
private ZipFile archive { get; set; } private Stream archiveStream;
private string[] beatmaps { get; set; } private ZipFile archive;
private Beatmap firstMap { get; set; } private string[] beatmaps;
private Beatmap firstMap;
public OszArchiveReader(Stream archiveStream) public OszArchiveReader(Stream archiveStream)
{ {
this.archiveStream = archiveStream;
archive = ZipFile.Read(archiveStream); archive = ZipFile.Read(archiveStream);
beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu")) beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu"))
.Select(e => e.FileName).ToArray(); .Select(e => e.FileName).ToArray();
@ -59,6 +61,7 @@ namespace osu.Game.Beatmaps.IO
public override void Dispose() public override void Dispose()
{ {
archive.Dispose(); archive.Dispose();
archiveStream.Dispose();
} }
} }
} }

View File

@ -34,8 +34,9 @@ namespace osu.Game.Database
public void ImportBeatmap(string path) public void ImportBeatmap(string path)
{ {
string hash = null; string hash = null;
var reader = ArchiveReader.GetReader(storage, path); BeatmapMetadata metadata;
var metadata = reader.ReadMetadata(); using (var reader = ArchiveReader.GetReader(storage, path))
metadata = reader.ReadMetadata();
if (connection.Table<BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) if (connection.Table<BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
return; // TODO: Update this beatmap instead return; // TODO: Update this beatmap instead
if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader
@ -50,32 +51,31 @@ namespace osu.Game.Database
input.CopyTo(output); input.CopyTo(output);
} }
} }
string[] mapNames = reader.ReadBeatmaps();
var beatmapSet = new BeatmapSetInfo var beatmapSet = new BeatmapSetInfo
{ {
BeatmapSetID = metadata.BeatmapSetID, BeatmapSetID = metadata.BeatmapSetID,
Path = path, Path = path,
Hash = hash, Hash = hash,
}; };
beatmapSet.Metadata = metadata;
var maps = new List<BeatmapInfo>(); var maps = new List<BeatmapInfo>();
foreach (var name in mapNames) using (var reader = ArchiveReader.GetReader(storage, path))
{ {
using (var stream = new StreamReader(reader.ReadFile(name))) string[] mapNames = reader.ReadBeatmaps();
foreach (var name in mapNames)
{ {
var decoder = BeatmapDecoder.GetDecoder(stream); using (var stream = new StreamReader(reader.ReadFile(name)))
Beatmap beatmap = decoder.Decode(stream); {
beatmap.BeatmapInfo.Path = name; var decoder = BeatmapDecoder.GetDecoder(stream);
// TODO: Diff beatmap metadata with set metadata and insert if necessary Beatmap beatmap = decoder.Decode(stream);
beatmap.BeatmapInfo.Metadata = null; beatmap.BeatmapInfo.Path = name;
maps.Add(beatmap.BeatmapInfo); // TODO: Diff beatmap metadata with set metadata and leave it here if necessary
connection.Insert(beatmap.BeatmapInfo.BaseDifficulty); beatmap.BeatmapInfo.Metadata = null;
connection.Insert(beatmap.BeatmapInfo); maps.Add(beatmap.BeatmapInfo);
connection.UpdateWithChildren(beatmap.BeatmapInfo); }
} }
} }
connection.Insert(beatmapSet); connection.InsertWithChildren(beatmapSet);
beatmapSet.BeatmapMetadataID = connection.Insert(metadata);
connection.UpdateWithChildren(beatmapSet);
BeatmapSetAdded?.Invoke(beatmapSet); BeatmapSetAdded?.Invoke(beatmapSet);
} }
@ -127,4 +127,4 @@ namespace osu.Game.Database
connection.Update(record); connection.Update(record);
} }
} }
} }