Rewrite for Android 11, music fix

This commit is contained in:
ZaneYork 2021-02-05 16:08:15 +08:00
parent e1ad4d7b9c
commit c00f1b3438
3 changed files with 83 additions and 2 deletions

View File

@ -74,7 +74,7 @@ namespace StardewModdingAPI
#if !SMAPI_FOR_MOBILE #if !SMAPI_FOR_MOBILE
public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.7.6"); public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.7.6");
#else #else
public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.7.6"); public static ISemanticVersion ApiVersion { get; } = new Toolkit.SemanticVersion("3.7.6.1", true);
/// <summary>Android SMAPI's current semantic version.</summary> /// <summary>Android SMAPI's current semantic version.</summary>
public static ISemanticVersion AndroidApiVersion { get; } = new Toolkit.SemanticVersion("0.8.8"); public static ISemanticVersion AndroidApiVersion { get; } = new Toolkit.SemanticVersion("0.8.8");

View File

@ -291,12 +291,12 @@ namespace StardewModdingAPI.Framework
new ThreadSilenceExitPatch(this.Monitor), new ThreadSilenceExitPatch(this.Monitor),
new SaveGamePatch(this.Translator, this.Monitor), new SaveGamePatch(this.Translator, this.Monitor),
new OnAppPausePatch(this.Monitor), new OnAppPausePatch(this.Monitor),
new MusicBankPatch(this.Monitor),
#endif #endif
new LoadContextPatch(this.Reflection, this.OnLoadStageChanged), new LoadContextPatch(this.Reflection, this.OnLoadStageChanged),
new LoadErrorPatch(this.Monitor, this.OnSaveContentRemoved), new LoadErrorPatch(this.Monitor, this.OnSaveContentRemoved),
new ScheduleErrorPatch(this.LogManager.MonitorForGame) new ScheduleErrorPatch(this.LogManager.MonitorForGame)
); );
// add exit handler // add exit handler
this.CancellationToken.Token.Register(() => this.CancellationToken.Token.Register(() =>
{ {

View File

@ -0,0 +1,81 @@
#if SMAPI_FOR_MOBILE
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.AppCenter.Crashes;
#if HARMONY_2
using HarmonyLib;
#else
using Harmony;
#endif
using StardewModdingAPI.Framework;
using StardewModdingAPI.Framework.Patching;
using StardewValley;
namespace StardewModdingAPI.Patches
{
internal class MusicBankPatch : IHarmonyPatch
{
/*********
** Accessors
*********/
/// <summary>A unique name for this patch.</summary>
public string Name => $"{nameof(MusicBankPatch)}";
/// <summary>Writes messages to the console and log file.</summary>
private static IMonitor Monitor;
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
public MusicBankPatch(Monitor monitor)
{
MusicBankPatch.Monitor = monitor;
}
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
public void Apply(HarmonyInstance harmony)
{
harmony.Patch(AccessTools.Method(typeof(Game1), "FetchMusicXWBPath"),
new HarmonyMethod(AccessTools.Method(this.GetType(), nameof(MusicBankPatch.Game_FetchMusicXWBPathPrefix))));
}
/*********
** Private methods
*********/
/// <summary>The method to call instead of <see cref="StardewValley.Game1.FetchMusicXWBPath"/>.</summary>
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")]
private static bool Game_FetchMusicXWBPathPrefix(ref string __result)
{
if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.Q)
{
string path = Path.Combine((string) (Java.Lang.Object) Android.OS.Environment.ExternalStorageDirectory ?? string.Empty, "Android", "obb", SMainActivity.instance.PackageName);
string str = string.Empty;
try
{
if (Directory.Exists(path))
{
foreach (string file in Directory.GetFiles(path))
{
if (file.Contains("com.chucklefish.stardewvalley.obb"))
str = file;
}
}
}
catch (System.Exception ex)
{
Microsoft.AppCenter.Crashes.Crashes.TrackError(ex, (IDictionary<string, string>) null, Array.Empty<ErrorAttachmentLog>());
return true;
}
__result = str;
return false;
}
return true;
}
}
}
#endif