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 @@ public static void Register()
});
OsuLegacyDecoder.Register();
}
private ZipFile archive { get; set; }
private string[] beatmaps { get; set; }
private Beatmap firstMap { get; set; }
private Stream archiveStream;
private ZipFile archive;
private string[] beatmaps;
private Beatmap firstMap;
public OszArchiveReader(Stream archiveStream)
{
this.archiveStream = archiveStream;
archive = ZipFile.Read(archiveStream);
beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu"))
.Select(e => e.FileName).ToArray();
@ -59,6 +61,7 @@ public override BeatmapMetadata ReadMetadata()
public override void Dispose()
{
archive.Dispose();
archiveStream.Dispose();
}
}
}

View File

@ -34,8 +34,9 @@ public BeatmapDatabase(BasicStorage storage)
public void ImportBeatmap(string path)
{
string hash = null;
var reader = ArchiveReader.GetReader(storage, path);
var metadata = reader.ReadMetadata();
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
@ -50,32 +51,31 @@ public void ImportBeatmap(string path)
input.CopyTo(output);
}
}
string[] mapNames = reader.ReadBeatmaps();
var beatmapSet = new BeatmapSetInfo
{
BeatmapSetID = metadata.BeatmapSetID,
Path = path,
Hash = hash,
};
beatmapSet.Metadata = metadata;
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);
Beatmap beatmap = decoder.Decode(stream);
beatmap.BeatmapInfo.Path = name;
// TODO: Diff beatmap metadata with set metadata and insert if necessary
beatmap.BeatmapInfo.Metadata = null;
maps.Add(beatmap.BeatmapInfo);
connection.Insert(beatmap.BeatmapInfo.BaseDifficulty);
connection.Insert(beatmap.BeatmapInfo);
connection.UpdateWithChildren(beatmap.BeatmapInfo);
using (var stream = new StreamReader(reader.ReadFile(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;
maps.Add(beatmap.BeatmapInfo);
}
}
}
connection.Insert(beatmapSet);
beatmapSet.BeatmapMetadataID = connection.Insert(metadata);
connection.UpdateWithChildren(beatmapSet);
connection.InsertWithChildren(beatmapSet);
BeatmapSetAdded?.Invoke(beatmapSet);
}
@ -127,4 +127,4 @@ public void Update<T>(T record, bool cascade = true) where T : class
connection.Update(record);
}
}
}
}