Skip to content

Commit

Permalink
feat(Settings): save config to disk periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Apr 16, 2024
1 parent 22237a1 commit 60310e8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Osu.Patcher.Hook/Patches/PatchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public abstract class PatchOptions
/// </summary>
public abstract void Load(Settings config);

/// <summary>
/// When called, save current internal state to the target config to be saved.
/// </summary>
public abstract void Save(Settings config);

/// <summary>
/// Finds and initializes an instance of every single type extending <see cref="PatchOptions" />.
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions Osu.Patcher.Hook/Patches/PatcherOptions/SaveOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO;
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs.Helpers;
using Osu.Utils;

namespace Osu.Patcher.Hook.Patches.PatcherOptions;

/// <summary>
/// Hooks osu!'s datastore saving mechanism to save our own options every so often.
/// </summary>
[OsuPatch]
[HarmonyPatch]
[UsedImplicitly]
internal static class SaveOptions
{
[UsedImplicitly]
[HarmonyTargetMethod]
private static MethodBase Target() => DataStoreUpdater.Save.Reference;

[UsedImplicitly]
[HarmonyPostfix]
internal static void Save()
{
var osuDir = Path.GetDirectoryName(OsuAssembly.Assembly.Location)!;
var settings = new Settings();

// Write settings to this instance
Hook.PatchOptions.Do(opts => opts.Save(settings));

Settings.WriteToDisk(settings, osuDir);
}
}
23 changes: 23 additions & 0 deletions Osu.Patcher.Hook/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using Osu.Patcher.Hook.Patches.LivePerformance;

namespace Osu.Patcher.Hook;

Expand Down Expand Up @@ -46,6 +47,28 @@ public static Settings ReadFromDisk(string osuDir)
}
}

/// <summary>
/// Handles writing the patcher options to disk.
/// </summary>
public static void WriteToDisk(Settings settings, string osuDir)
{
Debug.WriteLine("Writing patcher config to disk");

Directory.CreateDirectory(osuDir);

var file = Path.Combine(osuDir, OptionsFilename);
using var fs = File.OpenWrite(file);

try
{
Serializer.Serialize(fs, settings);
}
catch (Exception e)
{
Console.WriteLine($"Failed to write config to disk: {e}");
}
}

#region Options

#endregion
Expand Down
38 changes: 38 additions & 0 deletions Osu.Stubs/Helpers/DataStoreUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using JetBrains.Annotations;
using Osu.Utils.Lazy;
using static System.Reflection.Emit.OpCodes;

namespace Osu.Stubs.Helpers;

/// <summary>
/// Original: <c>osu.Helpers.DataStoreUpdater</c>
/// </summary>
[PublicAPI]
public class DataStoreUpdater
{
/// <summary>
/// Original: <c>Save(bool final)</c>
/// </summary>
[Stub]
public static readonly LazyMethod<bool> Save = LazyMethod<bool>.ByPartialSignature(
"osu.Helpers.DataStoreUpdater::Save(bool)",
[
Ldc_I4_0,
Stloc_0,
Leave_S,
Ldarg_0,
Ldsfld,
Stfld,
Ldarg_0,
Ldc_I4_0,
Stfld,
Ldarg_0,
Ldfld,
Callvirt,
Pop,
Endfinally,
Ldloc_0,
Ret,
]
);
}

0 comments on commit 60310e8

Please sign in to comment.