1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-06 05:33:22 +08:00
osu-lazer/osu.Game/Database/DatabaseBackedStore.cs

50 lines
1.5 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using osu.Framework.Logging;
using osu.Framework.Platform;
namespace osu.Game.Database
{
public abstract class DatabaseBackedStore
{
protected readonly Storage Storage;
2017-10-17 14:50:42 +08:00
protected readonly Func<OsuDbContext> GetContext;
2017-10-17 14:50:42 +08:00
protected DatabaseBackedStore(Func<OsuDbContext> getContext, Storage storage = null)
{
Storage = storage;
2017-10-17 14:50:42 +08:00
GetContext = getContext;
try
{
Prepare();
}
catch (Exception e)
{
2017-04-17 16:43:48 +08:00
Logger.Error(e, $@"Failed to initialise the {GetType()}! Trying again with a clean database...");
2017-04-17 18:44:03 +08:00
Prepare(true);
}
}
/// <summary>
/// Perform any common clean-up tasks. Should be run when idle, or whenever necessary.
/// </summary>
public virtual void Cleanup()
{
}
/// <summary>
/// Prepare this database for use. Tables should be created here.
/// </summary>
2017-04-17 18:44:03 +08:00
protected abstract void Prepare(bool reset = false);
/// <summary>
/// Reset this database to a default state. Undo all changes to database and storage backings.
/// </summary>
2017-04-17 18:44:03 +08:00
public void Reset() => Prepare(true);
}
}