osu/osu.Game.Tournament/Models/StableInfo.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
2.0 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-06-17 07:37:17 +00:00
#nullable disable
using System;
using System.IO;
using Newtonsoft.Json;
using osu.Framework.Platform;
using osu.Game.Tournament.IO;
namespace osu.Game.Tournament.Models
{
/// <summary>
/// Holds the path to locate the osu! stable cutting-edge installation.
/// </summary>
[Serializable]
public class StableInfo
{
2020-06-13 14:20:59 +00:00
/// <summary>
/// Path to the IPC directory used by the stable (cutting-edge) install.
/// </summary>
public string StablePath { get; set; }
2020-06-13 14:20:59 +00:00
/// <summary>
/// Fired whenever stable info is successfully saved to file.
/// </summary>
public event Action OnStableInfoSaved;
private const string config_path = "stable.json";
2021-04-04 12:31:08 +00:00
private readonly Storage configStorage;
2021-04-04 12:58:25 +00:00
public StableInfo(TournamentStorage storage)
{
2021-04-04 12:58:25 +00:00
configStorage = storage.AllTournaments;
2021-04-04 12:31:08 +00:00
if (!configStorage.Exists(config_path))
return;
2021-04-04 12:31:08 +00:00
using (Stream stream = configStorage.GetStream(config_path, FileAccess.Read, FileMode.Open))
using (var sr = new StreamReader(stream))
{
JsonConvert.PopulateObject(sr.ReadToEnd(), this);
}
}
public void SaveChanges()
{
2022-05-16 09:03:53 +00:00
using (var stream = configStorage.CreateFileSafely(config_path))
using (var sw = new StreamWriter(stream))
{
sw.Write(JsonConvert.SerializeObject(this,
new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
}));
}
OnStableInfoSaved?.Invoke();
}
}
}