detect use of 'dynamic' in mod code

This commit is contained in:
Jesse Plamondon-Willard 2017-10-02 21:39:51 -04:00
parent bd4ed43829
commit 365da8e6e4
5 changed files with 24 additions and 4 deletions

View File

@ -4,7 +4,7 @@
For players:
* SMAPI now alerts you when mods have new versions available.
* SMAPI now warns you about mods which may impact game stability.
* SMAPI now warns you about mods which may impact game stability or compatibility.
* The console is now simpler and easier to read, and adjusts its colors to fit your terminal background color.
* Renamed installer folder to avoid confusion.
* Updated compatibility list.

View File

@ -25,6 +25,9 @@ namespace StardewModdingAPI.Framework.ModLoading
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;
/// <summary>Whether to enable developer mode logging.</summary>
private readonly bool IsDeveloperMode;
/*********
** Public methods
@ -32,9 +35,11 @@ namespace StardewModdingAPI.Framework.ModLoading
/// <summary>Construct an instance.</summary>
/// <param name="targetPlatform">The current game platform.</param>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
public AssemblyLoader(Platform targetPlatform, IMonitor monitor)
/// <param name="isDeveloperMode">Whether to enable developer mode logging.</param>
public AssemblyLoader(Platform targetPlatform, IMonitor monitor, bool isDeveloperMode)
{
this.Monitor = monitor;
this.IsDeveloperMode = isDeveloperMode;
this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform);
// generate type => assembly lookup for types which should be rewritten
@ -276,6 +281,17 @@ namespace StardewModdingAPI.Framework.ModLoading
this.Monitor.LogOnce(loggedMessages, $"{mod.DisplayName} seems to change the save serialiser. It may change your saves in such a way that they won't work without this mod in the future.", LogLevel.Warn);
break;
case InstructionHandleResult.DetectedDynamic:
this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected 'dynamic' keyword ({handler.NounPhrase}) in assembly {filename}.");
this.Monitor.LogOnce(loggedMessages, $"{mod.DisplayName} uses the 'dynamic' keyword, which isn't compatible with Stardew Valley on Linux or Mac.",
#if SMAPI_FOR_WINDOWS
this.IsDeveloperMode ? LogLevel.Warn : LogLevel.Debug
#else
LogLevel.Warn
#endif
);
break;
case InstructionHandleResult.None:
break;

View File

@ -16,6 +16,9 @@ namespace StardewModdingAPI.Framework.ModLoading
DetectedGamePatch,
/// <summary>The instruction is compatible, but affects the save serializer in a way that may make saves unloadable without the mod.</summary>
DetectedSaveSerialiser
DetectedSaveSerialiser,
/// <summary>The instruction is compatible, but uses the <c>dynamic</c> keyword which won't work on Linux/Mac.</summary>
DetectedDynamic
}
}

View File

@ -71,6 +71,7 @@ namespace StardewModdingAPI.Metadata
** detect code which may impact game stability
****/
new TypeFinder("Harmony.HarmonyInstance", InstructionHandleResult.DetectedGamePatch),
new TypeFinder("System.Runtime.CompilerServices.CallSite", InstructionHandleResult.DetectedDynamic),
new FieldFinder(typeof(SaveGame).FullName, nameof(SaveGame.serializer), InstructionHandleResult.DetectedSaveSerialiser),
new FieldFinder(typeof(SaveGame).FullName, nameof(SaveGame.farmerSerializer), InstructionHandleResult.DetectedSaveSerialiser),
new FieldFinder(typeof(SaveGame).FullName, nameof(SaveGame.locationSerializer), InstructionHandleResult.DetectedSaveSerialiser),

View File

@ -626,7 +626,7 @@ namespace StardewModdingAPI
{
void TrackSkip(IModMetadata mod, string reasonPhrase) => skippedMods[mod] = reasonPhrase;
AssemblyLoader modAssemblyLoader = new AssemblyLoader(Constants.TargetPlatform, this.Monitor);
AssemblyLoader modAssemblyLoader = new AssemblyLoader(Constants.TargetPlatform, this.Monitor, this.Settings.DeveloperMode);
AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => modAssemblyLoader.ResolveAssembly(e.Name);
foreach (IModMetadata metadata in mods)
{