mirror of
https://github.com/osukey/osukey.git
synced 2025-05-28 00:47:28 +09:00
74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
using System;
|
|
using System.IO;
|
|
using osu.Framework.Logging;
|
|
using osu.Framework.Platform;
|
|
using osu.Game.Beatmaps;
|
|
using osu.Game.Database;
|
|
using osu.Game.IPC;
|
|
using osu.Game.Rulesets.Scoring.Legacy;
|
|
|
|
namespace osu.Game.Rulesets.Scoring
|
|
{
|
|
public class ScoreStore : DatabaseBackedStore, ICanAcceptFiles
|
|
{
|
|
private readonly Storage storage;
|
|
|
|
private readonly BeatmapManager beatmaps;
|
|
private readonly RulesetStore rulesets;
|
|
|
|
private const string replay_folder = @"replays";
|
|
|
|
public event Action<Score> ScoreImported;
|
|
|
|
// ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised)
|
|
private ScoreIPCChannel ipc;
|
|
|
|
public ScoreStore(Storage storage, DatabaseContextFactory factory, IIpcHost importHost = null, BeatmapManager beatmaps = null, RulesetStore rulesets = null) : base(factory)
|
|
{
|
|
this.storage = storage;
|
|
this.beatmaps = beatmaps;
|
|
this.rulesets = rulesets;
|
|
|
|
if (importHost != null)
|
|
ipc = new ScoreIPCChannel(importHost, this);
|
|
}
|
|
|
|
public string[] HandledExtensions => new[] { ".osr" };
|
|
|
|
public void Import(params string[] paths)
|
|
{
|
|
foreach (var path in paths)
|
|
{
|
|
var score = ReadReplayFile(path);
|
|
if (score != null)
|
|
ScoreImported?.Invoke(score);
|
|
}
|
|
}
|
|
|
|
public Score ReadReplayFile(string replayFilename)
|
|
{
|
|
Stream stream;
|
|
if (File.Exists(replayFilename))
|
|
{
|
|
// Handle replay with File since it is outside of storage
|
|
stream = File.OpenRead(replayFilename);
|
|
}
|
|
else if (storage.Exists(Path.Combine(replay_folder, replayFilename)))
|
|
{
|
|
stream = storage.GetStream(Path.Combine(replay_folder, replayFilename));
|
|
}
|
|
else
|
|
{
|
|
Logger.Log($"Replay file {replayFilename} cannot be found", LoggingTarget.Information, LogLevel.Error);
|
|
return null;
|
|
}
|
|
|
|
using (stream)
|
|
return new DatabasedLegacyScoreParser(rulesets, beatmaps).Parse(stream);
|
|
}
|
|
}
|
|
}
|