1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Rework StableInfo into a DI'd data structure

This commit is contained in:
Bartłomiej Dach 2020-06-13 15:05:52 +02:00
parent 5f79feaa8b
commit 1cd96b8002
5 changed files with 67 additions and 60 deletions

View File

@ -5,7 +5,6 @@ using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Microsoft.Win32;
using osu.Framework.Allocation;
using osu.Framework.Logging;
@ -34,14 +33,13 @@ namespace osu.Game.Tournament.IPC
[Resolved]
private LadderInfo ladder { get; set; }
[Resolved]
private StableInfo stableInfo { get; set; }
private int lastBeatmapId;
private ScheduledDelegate scheduled;
private GetBeatmapRequest beatmapLookupRequest;
public StableInfo StableInfo { get; private set; }
public const string STABLE_CONFIG = "tournament/stable.json";
public Storage IPCStorage { get; private set; }
[Resolved]
@ -165,8 +163,8 @@ namespace osu.Game.Tournament.IPC
private string findStablePath()
{
if (!string.IsNullOrEmpty(readStableConfig()))
return StableInfo.StablePath.Value;
if (!string.IsNullOrEmpty(stableInfo.StablePath))
return stableInfo.StablePath;
string stableInstallPath = string.Empty;
@ -204,43 +202,13 @@ namespace osu.Game.Tournament.IPC
if (!ipcFileExistsInDirectory(path))
return false;
StableInfo.StablePath.Value = path;
using (var stream = tournamentStorage.GetStream(STABLE_CONFIG, FileAccess.Write, FileMode.Create))
using (var sw = new StreamWriter(stream))
{
sw.Write(JsonConvert.SerializeObject(StableInfo,
new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
}));
}
stableInfo.StablePath = path;
LocateStableStorage();
stableInfo.SaveChanges();
return true;
}
private string readStableConfig()
{
if (StableInfo == null)
StableInfo = new StableInfo();
if (tournamentStorage.Exists(FileBasedIPC.STABLE_CONFIG))
{
using (Stream stream = tournamentStorage.GetStream(FileBasedIPC.STABLE_CONFIG, FileAccess.Read, FileMode.Open))
using (var sr = new StreamReader(stream))
{
StableInfo = JsonConvert.DeserializeObject<StableInfo>(sr.ReadToEnd());
}
return StableInfo.StablePath.Value;
}
return null;
}
private string findFromEnvVar()
{
try

View File

@ -2,7 +2,9 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
using System.IO;
using Newtonsoft.Json;
using osu.Framework.Platform;
namespace osu.Game.Tournament.Models
{
@ -12,6 +14,43 @@ namespace osu.Game.Tournament.Models
[Serializable]
public class StableInfo
{
public Bindable<string> StablePath = new Bindable<string>(string.Empty);
public string StablePath { get; set; }
public event Action OnStableInfoSaved;
private const string config_path = "tournament/stable.json";
private readonly Storage storage;
public StableInfo(Storage storage)
{
this.storage = storage;
if (!storage.Exists(config_path))
return;
using (Stream stream = storage.GetStream(config_path, FileAccess.Read, FileMode.Open))
using (var sr = new StreamReader(stream))
{
JsonConvert.PopulateObject(sr.ReadToEnd(), this);
}
}
public void SaveChanges()
{
using (var stream = storage.GetStream(config_path, FileAccess.Write, FileMode.Create))
using (var sw = new StreamWriter(stream))
{
sw.Write(JsonConvert.SerializeObject(this,
new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
}));
}
OnStableInfoSaved?.Invoke();
}
}
}

View File

@ -31,6 +31,9 @@ namespace osu.Game.Tournament.Screens
[Resolved]
private MatchIPCInfo ipc { get; set; }
[Resolved]
private StableInfo stableInfo { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
@ -57,6 +60,7 @@ namespace osu.Game.Tournament.Screens
};
api.LocalUser.BindValueChanged(_ => Schedule(reload));
stableInfo.OnStableInfoSaved += () => Schedule(reload);
reload();
}
@ -66,21 +70,13 @@ namespace osu.Game.Tournament.Screens
private void reload()
{
var fileBasedIpc = ipc as FileBasedIPC;
StableInfo stableInfo = fileBasedIpc?.StableInfo;
fillFlow.Children = new Drawable[]
{
new ActionableInfo
{
Label = "Current IPC source",
ButtonText = "Change source",
Action = () =>
{
stableInfo?.StablePath.BindValueChanged(_ =>
{
Schedule(reload);
});
sceneManager?.SetScreen(new StablePathSelectScreen());
},
Action = () => sceneManager?.SetScreen(new StablePathSelectScreen()),
Value = fileBasedIpc?.IPCStorage?.GetFullPath(string.Empty) ?? "Not found",
Failing = fileBasedIpc?.IPCStorage == null,
Description = "The osu!stable installation which is currently being used as a data source. If a source is not found, make sure you have created an empty ipc.txt in your stable cutting-edge installation."

View File

@ -15,34 +15,36 @@ using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
using osu.Game.Tournament.IPC;
using osu.Game.Tournament.Components;
using osu.Game.Tournament.Models;
using osuTK;
namespace osu.Game.Tournament.Screens
{
public class StablePathSelectScreen : TournamentScreen
{
private DirectorySelector directorySelector;
[Resolved]
private MatchIPCInfo ipc { get; set; }
private DialogOverlay overlay;
private GameHost host { get; set; }
[Resolved(canBeNull: true)]
private TournamentSceneManager sceneManager { get; set; }
[Resolved]
private GameHost host { get; set; }
private MatchIPCInfo ipc { get; set; }
[Resolved]
private StableInfo stableInfo { get; set; }
private DirectorySelector directorySelector;
private DialogOverlay overlay;
[BackgroundDependencyLoader(true)]
private void load(Storage storage, OsuColour colours)
{
var fileBasedIpc = ipc as FileBasedIPC;
var initialPath = new DirectoryInfo(storage.GetFullPath(string.Empty)).Parent?.FullName;
if (!string.IsNullOrEmpty(fileBasedIpc?.StableInfo.StablePath.Value))
if (!string.IsNullOrEmpty(stableInfo.StablePath))
{
initialPath = new DirectoryInfo(host.GetStorage(fileBasedIpc.StableInfo.StablePath.Value).GetFullPath(string.Empty)).Parent?.FullName;
initialPath = new DirectoryInfo(host.GetStorage(stableInfo.StablePath).GetFullPath(string.Empty)).Parent?.FullName;
}
AddRangeInternal(new Drawable[]

View File

@ -53,6 +53,8 @@ namespace osu.Game.Tournament
ladder.CurrentMatch.Value = ladder.Matches.FirstOrDefault(p => p.Current.Value);
dependencies.CacheAs(new StableInfo(storage));
dependencies.CacheAs<MatchIPCInfo>(ipc = new FileBasedIPC());
Add(ipc);
}