Skip to content

Commit

Permalink
Move get entry assembly api to RuntimeInfo
Browse files Browse the repository at this point in the history
A better place for it, as the entry assembly is queried in non-"debug" situations.
  • Loading branch information
Susko3 committed Jan 31, 2024
1 parent 50f9880 commit b620e6f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CodeAnalysis/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ M:System.Char.ToLower(System.Char);char.ToLower() changes behaviour depending on
M:System.Char.ToUpper(System.Char);char.ToUpper() changes behaviour depending on CultureInfo.CurrentCulture. Use char.ToUpperInvariant() instead. If wanting culture-sensitive behaviour, explicitly provide CultureInfo.CurrentCulture.
M:System.String.ToLower();string.ToLower() changes behaviour depending on CultureInfo.CurrentCulture. Use string.ToLowerInvariant() instead. If wanting culture-sensitive behaviour, explicitly provide CultureInfo.CurrentCulture or use LocalisableString.
M:System.String.ToUpper();string.ToUpper() changes behaviour depending on CultureInfo.CurrentCulture. Use string.ToUpperInvariant() instead. If wanting culture-sensitive behaviour, explicitly provide CultureInfo.CurrentCulture or use LocalisableString.
M:System.Reflection.Assembly.GetEntryAssembly();Use osu.Framework.Development.DebugUtils.GetEntryAssembly() instead
M:System.Reflection.Assembly.GetEntryAssembly();Use osu.Framework.RuntimeInfo.EntryAssembly instead
5 changes: 3 additions & 2 deletions osu.Framework.Android/AndroidGameActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using ManagedBass;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using DebugUtils = osu.Framework.Development.DebugUtils;
using Debug = System.Diagnostics.Debug;

namespace osu.Framework.Android
{
Expand Down Expand Up @@ -65,7 +65,8 @@ public override void OnTrimMemory([GeneratedEnum] TrimMemory level)

protected override void OnCreate(Bundle? savedInstanceState)
{
DebugUtils.EntryAssembly = GetType().Assembly;
Debug.Assert(RuntimeInfo.EntryAssembly.IsNull(), "RuntimeInfo.EntryAssembly should be null on Android and therefore needs to be manually updated.");
RuntimeInfo.EntryAssembly = GetType().Assembly;

// The default current directory on android is '/'.
// On some devices '/' maps to the app data directory. On others it maps to the root of the internal storage.
Expand Down
4 changes: 2 additions & 2 deletions osu.Framework.Tests/Visual/Platform/TestSceneDebugUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void LogStatics()
textFlow.Clear();
log(DebugUtils.IsNUnitRunning);
log(DebugUtils.IsDebugBuild);
log(DebugUtils.GetEntryAssembly());
log(RuntimeInfo.EntryAssembly);
#pragma warning disable RS0030
log(Assembly.GetEntryAssembly());
#pragma warning restore RS0030
Expand Down Expand Up @@ -69,7 +69,7 @@ public void TestIsDebugBuild()
[Test]
public void TestEntryAssembly()
{
AddAssert("check GetEntryAssembly()", () => DebugUtils.GetEntryAssembly().FullName, () => Does.StartWith("osu.Framework.Tests"));
AddAssert("check RuntimeInfo.EntryAssembly", () => RuntimeInfo.EntryAssembly.FullName, () => Does.StartWith("osu.Framework.Tests"));
}

/// <summary>
Expand Down
34 changes: 6 additions & 28 deletions osu.Framework/Development/DebugUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public static class DebugUtils

private static readonly Lazy<bool> is_nunit_running = new Lazy<bool>(() =>
{
var entry = EntryAssembly;
#pragma warning disable RS0030
var entry = Assembly.GetEntryAssembly();
#pragma warning restore RS0030

string? assemblyName = entry?.GetName().Name;

Expand All @@ -35,6 +37,8 @@ public static class DebugUtils
}
);

internal static Assembly NUnitTestAssembly => nunit_test_assembly.Value;

private static readonly Lazy<Assembly> nunit_test_assembly = new Lazy<Assembly>(() =>
{
Debug.Assert(IsNUnitRunning);
Expand All @@ -44,20 +48,10 @@ public static class DebugUtils
}
);

#pragma warning disable RS0030
/// <summary>
/// The entry assembly.
/// </summary>
/// <remarks>
/// Usually obtained via <see cref="Assembly.GetEntryAssembly"/>, but can be set by a different mechanism on platforms that don't support that.
/// </remarks>
internal static Assembly? EntryAssembly { private get; set; } = Assembly.GetEntryAssembly();
#pragma warning restore RS0030

public static bool IsDebugBuild => is_debug_build.Value;

private static readonly Lazy<bool> is_debug_build = new Lazy<bool>(() =>
isDebugAssembly(typeof(DebugUtils).Assembly) || isDebugAssembly(GetEntryAssembly())
isDebugAssembly(typeof(DebugUtils).Assembly) || isDebugAssembly(RuntimeInfo.EntryAssembly)
);

/// <summary>
Expand All @@ -68,21 +62,5 @@ public static class DebugUtils

// https://stackoverflow.com/a/2186634
private static bool isDebugAssembly(Assembly? assembly) => assembly?.GetCustomAttributes(false).OfType<DebuggableAttribute>().Any(da => da.IsJITTrackingEnabled) ?? false;

/// <summary>
/// Gets the entry assembly.
/// When running under NUnit, the assembly of the current test will be returned instead.
/// </summary>
/// <returns>The entry assembly.</returns>
public static Assembly GetEntryAssembly()
{
if (IsNUnitRunning)
return nunit_test_assembly.Value;

// can only be null when running under nunit, and that case is covered above.
Debug.Assert(EntryAssembly != null);

return EntryAssembly;
}
}
}
4 changes: 1 addition & 3 deletions osu.Framework/Graphics/Video/VideoDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Development;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Logging;
using osu.Framework.Platform;
Expand Down Expand Up @@ -830,7 +828,7 @@ protected virtual FFmpegFuncs CreateFuncs()
throw new ArgumentOutOfRangeException(nameof(RuntimeInfo.OS), RuntimeInfo.OS, null);
}

return NativeLibrary.Load(libraryName, DebugUtils.GetEntryAssembly().AsNonNull(), DllImportSearchPath.UseDllDirectoryForDependencies | DllImportSearchPath.SafeDirectories);
return NativeLibrary.Load(libraryName, RuntimeInfo.EntryAssembly, DllImportSearchPath.UseDllDirectoryForDependencies | DllImportSearchPath.SafeDirectories);
};

return new FFmpegFuncs
Expand Down
4 changes: 1 addition & 3 deletions osu.Framework/Platform/GameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,8 @@ public void Run(Game game)
Trace.Listeners.Clear();
Trace.Listeners.Add(new ThrowingTraceListener());

var assembly = DebugUtils.GetEntryAssembly();

Logger.GameIdentifier = Name;
Logger.VersionIdentifier = assembly.GetName().Version?.ToString() ?? Logger.VersionIdentifier;
Logger.VersionIdentifier = RuntimeInfo.EntryAssembly.GetName().Version?.ToString() ?? Logger.VersionIdentifier;

Dependencies.CacheAs(this);
Dependencies.CacheAs(Storage = game.CreateStorage(this, GetDefaultGameStorage()));
Expand Down
13 changes: 13 additions & 0 deletions osu.Framework/RuntimeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Diagnostics;
using System.Reflection;
using osu.Framework.Development;
using osu.Framework.Extensions.ObjectExtensions;

namespace osu.Framework
{
Expand All @@ -25,6 +27,17 @@ public static string GetFrameworkAssemblyPath()
return assembly.Location;
}

/// <summary>
/// Gets the entry assembly.
/// When running under NUnit, the assembly of the current test will be returned instead.
/// </summary>
/// <returns>The entry assembly.</returns>
public static Assembly EntryAssembly { get; internal set; } = DebugUtils.IsNUnitRunning
? DebugUtils.NUnitTestAssembly
#pragma warning disable RS0030
: Assembly.GetEntryAssembly().AsNonNull();
#pragma warning restore RS0030

public static Platform OS { get; }

public static bool IsUnix => OS != Platform.Windows;
Expand Down

0 comments on commit b620e6f

Please sign in to comment.