From 7fb4b95c1cdbba402fd32c6ecbf834ee97aa61c8 Mon Sep 17 00:00:00 2001
From: yangzhi <@4F!xZpJwly&KbWq>
Date: Wed, 26 Feb 2020 21:19:10 +0800
Subject: [PATCH] Redirect bug report to our account.
---
src/SMAPI/Constants.cs | 3 +
src/SMAPI/Framework/Patching/GamePatcher.cs | 5 +-
src/SMAPI/Patches/AppCenterReportPatch.cs | 68 +++++++++++++++++++++
src/SMAPI/SMAPI.csproj | 1 +
src/SMAPI/SMainActivity.cs | 7 ++-
5 files changed, 81 insertions(+), 3 deletions(-)
create mode 100644 src/SMAPI/Patches/AppCenterReportPatch.cs
diff --git a/src/SMAPI/Constants.cs b/src/SMAPI/Constants.cs
index 9bceee61..2d390a02 100644
--- a/src/SMAPI/Constants.cs
+++ b/src/SMAPI/Constants.cs
@@ -61,6 +61,9 @@ namespace StardewModdingAPI
/// The absolute path to the folder containing SMAPI's internal files.
internal static readonly string InternalFilesPath = Program.DllSearchPath;
+ /// The app secret from AppCenter.
+ internal static readonly object MicrosoftAppSecret = "79411636-0bc5-41cc-9889-43a4bca83b9d";
+
/// The file path for the SMAPI configuration file.
internal static string ApiConfigPath => Path.Combine(Constants.InternalFilesPath, "config.json");
diff --git a/src/SMAPI/Framework/Patching/GamePatcher.cs b/src/SMAPI/Framework/Patching/GamePatcher.cs
index b7f1f7cd..47a0303a 100644
--- a/src/SMAPI/Framework/Patching/GamePatcher.cs
+++ b/src/SMAPI/Framework/Patching/GamePatcher.cs
@@ -29,7 +29,10 @@ namespace StardewModdingAPI.Framework.Patching
/// The patches to apply.
public void Apply(params IHarmonyPatch[] patches)
{
- HarmonyDetourBridge.Init();
+ if (!HarmonyDetourBridge.Initialized)
+ {
+ HarmonyDetourBridge.Init();
+ }
HarmonyInstance harmony = HarmonyInstance.Create("io.smapi");
foreach (IHarmonyPatch patch in patches)
diff --git a/src/SMAPI/Patches/AppCenterReportPatch.cs b/src/SMAPI/Patches/AppCenterReportPatch.cs
new file mode 100644
index 00000000..c9a5d428
--- /dev/null
+++ b/src/SMAPI/Patches/AppCenterReportPatch.cs
@@ -0,0 +1,68 @@
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+using System.Reflection;
+using System.Reflection.Emit;
+using Android.OS;
+using Harmony;
+using Microsoft.Xna.Framework;
+using MonoMod.RuntimeDetour;
+using StardewModdingAPI.Framework.Patching;
+using StardewValley;
+using StardewValley.Buildings;
+using StardewValley.Characters;
+
+namespace StardewModdingAPI.Patches
+{
+ /// A Harmony patch for to redirect bug report.
+ /// Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments.
+ [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ internal class AppCenterReportPatch : IHarmonyPatch
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// A unique name for this patch.
+ public string Name => nameof(AppCenterReportPatch);
+
+ /// Apply the Harmony patch.
+ /// The Harmony instance.
+ public void Apply(HarmonyInstance harmony)
+ {
+ harmony.Patch(
+ original: AccessTools.Method(typeof(MainActivity), "OnCreate", new System.Type[] { typeof(Android.OS.Bundle)}),
+ transpiler: new HarmonyMethod(this.GetType(), nameof(AppCenterReportPatch.Modify_MainActivity_OnCreate))
+ );
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// The patch logic .
+ private static IEnumerable Modify_MainActivity_OnCreate(IEnumerable instructions)
+ {
+ List codes = new List(instructions);
+ for (int i = 0; i < codes.Count ; i++)
+ {
+ if (codes[i].opcode == OpCodes.Ldstr && (string)codes[i].operand == "5677d40e-f7b3-4ccb-bee4-5dca56d86ade")
+ {
+ codes[i].operand = Constants.MicrosoftAppSecret;
+ break;
+ }
+ }
+ return codes.AsEnumerable();
+ }
+
+ public static void ApplyPatch()
+ {
+ if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
+ {
+ HarmonyDetourBridge.Init();
+ HarmonyInstance harmony = HarmonyInstance.Create("io.smapi.mainactivity");
+ new AppCenterReportPatch().Apply(harmony);
+ }
+ }
+ }
+}
diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj
index c294eac7..8973b366 100644
--- a/src/SMAPI/SMAPI.csproj
+++ b/src/SMAPI/SMAPI.csproj
@@ -361,6 +361,7 @@
+
diff --git a/src/SMAPI/SMainActivity.cs b/src/SMAPI/SMainActivity.cs
index d5f24ed9..3d5ff656 100644
--- a/src/SMAPI/SMainActivity.cs
+++ b/src/SMAPI/SMainActivity.cs
@@ -14,6 +14,7 @@ using StardewValley;
using System.Reflection;
using Android.Content.Res;
using Java.Interop;
+using StardewModdingAPI.Patches;
namespace StardewModdingAPI
{
@@ -79,6 +80,10 @@ namespace StardewModdingAPI
}
this.Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
this.Window.SetFlags(WindowManagerFlags.KeepScreenOn, WindowManagerFlags.KeepScreenOn);
+
+ Program.Main(null);
+ // this patch should apply much earlier
+ AppCenterReportPatch.ApplyPatch();
base.OnCreate(bundle);
this.CheckAppPermissions();
}
@@ -87,8 +92,6 @@ namespace StardewModdingAPI
{
new SGameConsole();
- Program.Main(null);
-
this.core = new SCore(System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "StardewValley/Mods"), false);
this.core.RunInteractively();