Skip to content

Commit

Permalink
wip: dynamic stub tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 31, 2024
1 parent 2ae20b3 commit a2c2bc7
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 48 deletions.
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/BeatmapMirror/EnableOsuDirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Other;
using Osu.Utils.Extensions;
using static System.Reflection.Emit.OpCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Framework;
using Osu.Stubs.GameModes;
using Osu.Stubs.Graphics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using Osu.Performance;
using Osu.Stubs;
using Osu.Stubs.GameModes;
using Osu.Stubs.Other;
using Osu.Stubs.Scoring;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Osu.Stubs;
using Osu.Stubs.Framework;

namespace Osu.Patcher.Hook.Patches.LivePerformance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Performance;
using Osu.Stubs;
using Osu.Stubs.Rulesets;
using Osu.Stubs.Scoring;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Rulesets;

namespace Osu.Patcher.Hook.Patches.LivePerformance;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/AllowPlayModeReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Patcher.Hook.Patches.UI;
using Osu.Stubs;
using Osu.Stubs.Other;
using Osu.Utils.Extensions;
using static System.Reflection.Emit.OpCodes;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/DisableErrorReporting.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Helpers;

namespace Osu.Patcher.Hook.Patches.Misc;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/FixDoubleSkipping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.GameModes;
using Osu.Stubs.Other;

Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/LogOsuLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Helpers;

namespace Osu.Patcher.Hook.Patches.Misc;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/Misc/LogSoftErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Other;

namespace Osu.Patcher.Hook.Patches.Misc;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/Mods/PatchSuddenDeathAutoRetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Rulesets;
using static System.Reflection.Emit.OpCodes;

Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/UI/AllowOpenOptionsInGameplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Other;
using Osu.Utils.Extensions;
using static System.Reflection.Emit.OpCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.SongSelect;
using Osu.Utils.Extensions;
using static System.Reflection.Emit.OpCodes;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/UI/RevertSortWhenNoGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Reflection.Emit;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.Helpers;
using Osu.Stubs.SongSelect;
using Osu.Utils.Extensions;
Expand Down
1 change: 0 additions & 1 deletion Osu.Patcher.Hook/Patches/UI/ShowModsInGameplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using Osu.Stubs;
using Osu.Stubs.GameModes;
using static System.Reflection.Emit.OpCodes;

Expand Down
37 changes: 37 additions & 0 deletions Osu.Stubs.Tests/Osu.Stubs.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Build">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>Osu.Stubs.Tests</RootNamespace>
<TargetFramework>net462</TargetFramework>
<PlatformTarget>x86</PlatformTarget> <!-- Running in 32bit is necessary for loading in osu! -->
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Net.Http"/>
<ProjectReference Include="../Osu.Stubs/Osu.Stubs.csproj"/>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="all"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="NSubstitute" Version="5.1.0"/>
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
79 changes: 79 additions & 0 deletions Osu.Stubs.Tests/OsuApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace Osu.Stubs.Tests;

[PublicAPI]
public static class OsuApi
{
[PublicAPI]
public enum ReleaseStream
{
CuttingEdge,
Stable40,
Beta40,
}

private static readonly HttpClient Http = new();

/// <summary>
/// Gets the latest release files for a specific release stream.
/// </summary>
public static async Task<List<OsuUpdateFile>> GetReleaseFiles(ReleaseStream stream)
{
Console.WriteLine("Fetching latest osu! update info");

var url = $"https://osu.ppy.sh/web/check-updates.php" +
$"?action=check" +
$"&stream={stream.ToString().ToLower()}" +
$"&time={DateTime.Now.Ticks}";

using var response = await Http.GetAsync(url);
response.EnsureSuccessStatusCode();

var bodyText = await response.Content.ReadAsStringAsync();
if (bodyText == null) throw new Exception("Response returned no body");

var releaseFiles = JsonConvert.DeserializeObject<List<OsuUpdateFile>>(bodyText);
if (releaseFiles == null) throw new Exception("Failed to deserialize update files");

return releaseFiles;
}

/// <summary>
/// Downloads the full osu! update file list to a specific directory.
/// </summary>
/// <param name="dir">An empty directory.</param>
/// <param name="stream">The release stream to download.</param>
public static async Task DownloadOsu(string dir, ReleaseStream stream = ReleaseStream.Stable40)
{
var updateFiles = await GetReleaseFiles(ReleaseStream.Stable40);

foreach (var updateFile in updateFiles)
{
Console.WriteLine($"Downloading {updateFile.FileName}");

await DownloadFile(
updateFile.DownloadUrl,
Path.Combine(dir, updateFile.FileName)
);
}
}

private static async Task DownloadFile(string url, string path)
{
using var response = await Http.GetAsync(url);
response.EnsureSuccessStatusCode();

var bodyStream = await response.Content.ReadAsStreamAsync();
if (bodyStream == null) throw new Exception("Response returned no body");

using var file = File.Create(path);
await bodyStream.CopyToAsync(file);
}
}
30 changes: 30 additions & 0 deletions Osu.Stubs.Tests/OsuUpdateFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;

namespace Osu.Stubs.Tests;

public class OsuUpdateFile
{
[JsonRequired]
[JsonProperty("file_version")]
public int FileVersion { get; set; }

[JsonRequired]
[JsonProperty("filesize")]
public int FileSize { get; set; }

[JsonRequired]
[JsonProperty("filename")]
public string FileName { get; set; } = null!;

[JsonRequired]
[JsonProperty("file_hash")]
public string FileHash { get; set; } = null!;

[JsonRequired]
[JsonProperty("timestamp")]
public string Timestamp { get; set; } = null!;

[JsonRequired]
[JsonProperty("url_full")]
public string DownloadUrl { get; set; } = null!;
}
49 changes: 49 additions & 0 deletions Osu.Stubs.Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Osu.Utils.Extensions;
using Osu.Utils.Lazy;

#pragma warning disable CS0618 // Type or member is obsolete

namespace Osu.Stubs.Tests;

internal static class Program
{
private static async Task Main()
{
var osuDir = Path.Combine(Environment.CurrentDirectory, "osu!");
var osuExe = Path.Combine(osuDir, "osu!.exe");

if (!Directory.Exists(osuDir))
{
Directory.CreateDirectory(osuDir);
await OsuApi.DownloadOsu(osuDir);
}

AppDomain.CurrentDomain.AppendPrivatePath(osuDir);
Assembly.LoadFile(osuExe);

foreach (var type in Assembly.GetAssembly(typeof(Stub)).GetTypes())
{
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static))
{
if (field.GetCustomAttribute(typeof(Stub)) == null)
continue;

var lazy = field.GetValue<ILazy<MemberInfo>>(null);

try
{
Console.WriteLine($"{lazy.Name} -> {lazy.Reference}");
}
catch (Exception e)
{
Console.WriteLine($"{lazy.Name} -> [Failure]");
Console.WriteLine(e);
}
}
}
}
}
2 changes: 1 addition & 1 deletion Osu.Stubs/Stub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Osu.Stubs;

/// <summary>
/// Stub marker for a type extending <see cref="LazyInfo{T}" />.
/// Stub marker for a type extending <see cref="ILazy{T}" />.
/// Used for testing stubs on new versions of osu!.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
Expand Down
Loading

0 comments on commit a2c2bc7

Please sign in to comment.