Skip to content

Commit

Permalink
Suppress harmony warnings when checking for optional methods and types
Browse files Browse the repository at this point in the history
  • Loading branch information
toebeann committed Oct 2, 2024
1 parent 64ecc68 commit 4002276
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Plugin/ExtensionMethods/TraverseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using HarmonyLib;
using System;
using Tobey.UnityAudio.Utilities;

namespace Tobey.UnityAudio.ExtensionMethods;
internal static class TraverseExtensions
{
public static Traverse OptionalMethod(this Traverse traverse, string name, params object[] arguments) =>
TraverseHelper.SuppressHarmonyWarnings(() => traverse.Method(name, arguments));

public static Traverse OptionalMethod(this Traverse traverse, string name, Type[] paramTypes, params object[] arguments) =>
TraverseHelper.SuppressHarmonyWarnings(() => traverse.Method(name, paramTypes, arguments));
}
20 changes: 12 additions & 8 deletions Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Tobey.UnityAudio.ExtensionMethods;
using Tobey.UnityAudio.Utilities;
using UnityEngine;

namespace Tobey.UnityAudio;
Expand Down Expand Up @@ -110,7 +112,6 @@ void logFailure(string missingMemberName, string missingMemberType) =>
}

Logger.LogMessage("Attempting to determine the runtime state of Unity Audio...");
Logger.LogInfo("You may see warnings from AccessTools about being unable to find methods or types etc. during this operation.");

var path = Path.GetFullPath(Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, audioFilePath.Value));
if (!File.Exists(path))
Expand All @@ -120,45 +121,48 @@ void logFailure(string missingMemberName, string missingMemberType) =>

// in newer versions of Unity, UnityWebRequest.GetAudioClip is deprecated in favour of UnityWebRequestMultimedia.GetAudioClip,
// so use the newer method where available
var unityWebRequestMultimedia = new[] { "UnityWebRequestMultimedia", "UnityWebRequest" }.Select(Traverse.CreateWithType).FirstOrDefault(t => t.TypeExists());
var unityWebRequestMultimedia = new[] { "UnityWebRequestMultimedia", "UnityWebRequest" }
.Select(TraverseHelper.CreateWithOptionalType)
.FirstOrDefault(t => t.TypeExists());

if (unityWebRequestMultimedia is null)
{
logFailure("UnityEngine.Networking.UnityWebRequest", "type");
yield break;
}

// we need the type of UnityWebRequest on all versions, as this is the type that will be passed to DownloadHandlerAudioClip.GetContent
var unityWebRequest = Traverse.CreateWithType("UnityWebRequest");
var unityWebRequest = TraverseHelper.CreateWithOptionalType("UnityWebRequest");
if (!unityWebRequest.TypeExists())
{
logFailure("UnityEngine.Networking.UnityWebRequest", "type");
yield break;
}
Type unityWebRequestType = unityWebRequest.GetValue<Type>();

var audioType = Traverse.CreateWithType("AudioType");
var audioType = TraverseHelper.CreateWithOptionalType("AudioType");
if (!audioType.TypeExists())
{
logFailure("UnityEngine.AudioType", "type");
yield break;
}
Type audioTypeType = audioType.GetValue<Type>();

var downloadHandlerAudioClip = Traverse.CreateWithType("DownloadHandlerAudioClip");
var downloadHandlerAudioClip = TraverseHelper.CreateWithOptionalType("DownloadHandlerAudioClip");
if (!downloadHandlerAudioClip.TypeExists())
{
logFailure("UnityEngine.Networking.DownloadHandlerAudioClip", "type");
yield break;
}

var getContent = downloadHandlerAudioClip.Method("GetContent", new[] { unityWebRequestType });
var getContent = downloadHandlerAudioClip.OptionalMethod("GetContent", new[] { unityWebRequestType });
if (!getContent.MethodExists())
{
logFailure($"{downloadHandlerAudioClip}:GetContent", "static method");
yield break;
}

var getAudioClip = unityWebRequestMultimedia.Method("GetAudioClip", new[] { typeof(string), audioTypeType });
var getAudioClip = unityWebRequestMultimedia.OptionalMethod("GetAudioClip", new[] { typeof(string), audioTypeType });
if (!getAudioClip.MethodExists())
{
logFailure($"{unityWebRequestMultimedia}:GetAudioClip", "static method");
Expand All @@ -167,7 +171,7 @@ void logFailure(string missingMemberName, string missingMemberType) =>

using var disposableRequest = getAudioClip.GetValue<IDisposable>($"file:///{path}", 20); // AudioType.WAV = 20
var request = Traverse.Create(disposableRequest);
var sendWebRequest = new[] { "SendWebRequest", "Send" }.Select(name => request.Method(name)).FirstOrDefault(m => m.MethodExists());
var sendWebRequest = new[] { "SendWebRequest", "Send" }.Select(name => request.OptionalMethod(name)).FirstOrDefault(m => m.MethodExists());
if (sendWebRequest is null)
{
logFailure($"{unityWebRequestMultimedia}:SendWebRequest", "instance method");
Expand Down
24 changes: 24 additions & 0 deletions Plugin/Utilities/TraverseHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using HarmonyLib;
using HarmonyLib.Tools;
using System;

namespace Tobey.UnityAudio.Utilities;
internal static class TraverseHelper
{
public static T SuppressHarmonyWarnings<T>(Func<T> fn)
{
var initialFilter = Logger.ChannelFilter;
Logger.ChannelFilter &= ~Logger.LogChannel.Warn;
try
{
return fn();
}
finally
{
Logger.ChannelFilter = initialFilter;
}
}

public static Traverse CreateWithOptionalType(string name) =>
SuppressHarmonyWarnings(() => Traverse.CreateWithType(name));
}

0 comments on commit 4002276

Please sign in to comment.