Remove Linq patch,which may cause some error
This commit is contained in:
parent
a24d5157e4
commit
b4a7fb8f6a
|
@ -273,7 +273,6 @@ namespace StardewModdingAPI.Framework
|
|||
new ScheduleErrorPatch(this.MonitorForGame),
|
||||
new SaveBackupPatch(this.EventManager),
|
||||
new JunimoHarvesterPatch(this.Monitor),
|
||||
new LinqEnumerablePatch(this.Monitor),
|
||||
new SpriteFontPatch(this.Monitor)
|
||||
);
|
||||
|
||||
|
|
|
@ -1,212 +0,0 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using Harmony;
|
||||
using Microsoft.Xna.Framework;
|
||||
using StardewModdingAPI.Framework.Patching;
|
||||
using StardewValley;
|
||||
using StardewValley.Buildings;
|
||||
using StardewValley.Characters;
|
||||
|
||||
namespace StardewModdingAPI.Patches
|
||||
{
|
||||
/// <summary>A Harmony patch for <see cref="StardewValley.Character.JunimoHarvesterPatch"/> which intercepts crashes due to invalid schedule data.</summary>
|
||||
/// <remarks>Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments.</remarks>
|
||||
[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 LinqEnumerablePatch : IHarmonyPatch
|
||||
{
|
||||
/*********
|
||||
** Fields
|
||||
*********/
|
||||
|
||||
/*********
|
||||
** Fields
|
||||
*********/
|
||||
/// <summary>Writes messages to the console and log file.</summary>
|
||||
private static IMonitor Monitor;
|
||||
|
||||
/*********
|
||||
** Accessors
|
||||
*********/
|
||||
/// <summary>A unique name for this patch.</summary>
|
||||
public string Name => nameof(LinqEnumerablePatch);
|
||||
|
||||
|
||||
/*********
|
||||
** Public methods
|
||||
*********/
|
||||
/// <summary>Construct an instance.</summary>
|
||||
/// <param name="monitorForGame">Writes messages to the console and log file on behalf of the game.</param>
|
||||
public LinqEnumerablePatch(IMonitor monitor)
|
||||
{
|
||||
Monitor = monitor;
|
||||
}
|
||||
|
||||
/// <summary>Apply the Harmony patch.</summary>
|
||||
/// <param name="harmony">The Harmony instance.</param>
|
||||
public void Apply(HarmonyInstance harmony)
|
||||
{
|
||||
MethodInfo methodInfo = AccessTools.FirstMethod(typeof(System.Linq.Enumerable), method => method.Name == "FirstOrDefault" && method.GetParameters().Length == 2).MakeGenericMethod(new Type[] { typeof(Item) });
|
||||
harmony.Patch(
|
||||
original: methodInfo,
|
||||
prefix: new HarmonyMethod(this.GetType(), nameof(LinqEnumerablePatch.Before_LinqEnumerablePatch_FirstOrDefault))
|
||||
);
|
||||
methodInfo = AccessTools.FirstMethod(typeof(System.Linq.Enumerable), method => method.Name == "Any" && method.GetParameters().Length == 2).MakeGenericMethod(new Type[] { typeof(Item) });
|
||||
harmony.Patch(
|
||||
original: methodInfo,
|
||||
prefix: new HarmonyMethod(this.GetType(), nameof(LinqEnumerablePatch.Before_LinqEnumerablePatch_Any))
|
||||
);
|
||||
methodInfo = AccessTools.FirstMethod(typeof(System.Linq.Enumerable), method => method.Name == "TryGetFirst" && method.GetParameters().Length == 3).MakeGenericMethod(new Type[] { typeof(Item) });
|
||||
harmony.Patch(
|
||||
original: methodInfo,
|
||||
prefix: new HarmonyMethod(this.GetType(), nameof(LinqEnumerablePatch.Before_LinqEnumerablePatch_TryGetFirst))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*********
|
||||
** Private methods
|
||||
*********/
|
||||
/// <summary>The method to call instead of <see cref="System.Linq.Enumerable.FirstOrDefault"/>.</summary>
|
||||
/// <param name="__instance">The instance being patched.</param>
|
||||
/// <param name="__originalMethod">The method being wrapped.</param>
|
||||
/// <returns>Returns whether to execute the original method.</returns>
|
||||
private static bool Before_LinqEnumerablePatch_FirstOrDefault(IEnumerable<object> source, Func<object, bool> predicate, ref object __result)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentNullException("source");
|
||||
}
|
||||
if (predicate == null)
|
||||
{
|
||||
throw new ArgumentNullException("predicate");
|
||||
}
|
||||
foreach (object local in source)
|
||||
{
|
||||
if (local == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (predicate(local))
|
||||
{
|
||||
__result = local;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(local is StardewValley.Item))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (predicate(local))
|
||||
{
|
||||
__result = local;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
__result = default(Item);
|
||||
return false;
|
||||
}
|
||||
private static bool Before_LinqEnumerablePatch_Any(IEnumerable<object> source, Func<object, bool> predicate, ref object __result)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentNullException("source");
|
||||
}
|
||||
if (predicate == null)
|
||||
{
|
||||
throw new ArgumentNullException("predicate");
|
||||
}
|
||||
foreach (object local in source)
|
||||
{
|
||||
if (local == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (predicate(local))
|
||||
{
|
||||
__result = local;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(local is StardewValley.Item))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (predicate(local))
|
||||
{
|
||||
__result = local;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
__result = null;
|
||||
return false;
|
||||
}
|
||||
private static bool Before_LinqEnumerablePatch_TryGetFirst(IEnumerable<object> source, Func<object, bool> predicate, ref bool found, ref object __result, MethodBase __originalMethod)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentNullException("source");
|
||||
}
|
||||
if (predicate == null)
|
||||
{
|
||||
throw new ArgumentNullException("predicate");
|
||||
}
|
||||
foreach (object local in source)
|
||||
{
|
||||
if (local == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (predicate(local))
|
||||
{
|
||||
__result = local;
|
||||
found = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(local is StardewValley.Item))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (predicate(local))
|
||||
{
|
||||
__result = local;
|
||||
found = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
found = false;
|
||||
__result = default(Item);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -355,7 +355,6 @@
|
|||
<Compile Include="Patches\DialogueErrorPatch.cs" />
|
||||
<Compile Include="Patches\EventErrorPatch.cs" />
|
||||
<Compile Include="Patches\SpriteFontPatch.cs" />
|
||||
<Compile Include="Patches\LinqEnumerablePatch.cs" />
|
||||
<Compile Include="Patches\LoadContextPatch.cs" />
|
||||
<Compile Include="Patches\LoadErrorPatch.cs" />
|
||||
<Compile Include="Patches\ObjectErrorPatch.cs" />
|
||||
|
|
Loading…
Reference in New Issue