using System;
using Mono.Cecil;
using StardewModdingAPI.Framework.ModLoading.Finders;
using StardewModdingAPI.Framework.ModLoading.Framework;
namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{
/// Rewrites Harmony 1.x assembly references to work with Harmony 2.x.
internal class Harmony1AssemblyRewriter : BaseTypeReferenceRewriter
{
/*********
** Fields
*********/
/// The full assembly name to which to find references.
private const string FromAssemblyName = "0Harmony";
/// The main Harmony type.
private readonly Type HarmonyType = typeof(HarmonyLib.Harmony);
/*********
** Accessors
*********/
/// A brief noun phrase indicating what the rewriter matches.
public const string DefaultNounPhrase = "Harmony 1.x";
/*********
** Public methods
*********/
/// Construct an instance.
public Harmony1AssemblyRewriter()
: base(new TypeAssemblyFinder(Harmony1AssemblyRewriter.FromAssemblyName, InstructionHandleResult.None), Harmony1AssemblyRewriter.DefaultNounPhrase)
{ }
/*********
** Private methods
*********/
/// Change a type reference if needed.
/// The assembly module containing the instruction.
/// The type to replace if it matches.
/// Assign the new type reference.
protected override bool RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action set)
{
bool rewritten = false;
// current type
if (type.Scope.Name == Harmony1AssemblyRewriter.FromAssemblyName && type.Scope is AssemblyNameReference assemblyScope && assemblyScope.Version.Major == 1)
{
Type targetType = this.GetMappedType(type);
set(module.ImportReference(targetType));
return true;
}
// recurse into generic arguments
if (type is GenericInstanceType genericType)
{
for (int i = 0; i < genericType.GenericArguments.Count; i++)
rewritten |= this.RewriteIfNeeded(module, genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef);
}
// recurse into generic parameters (e.g. constraints)
for (int i = 0; i < type.GenericParameters.Count; i++)
rewritten |= this.RewriteIfNeeded(module, type.GenericParameters[i], typeRef => type.GenericParameters[i] = new GenericParameter(typeRef));
return rewritten;
}
/// Get an equivalent Harmony 2.x type.
/// The Harmony 1.x method.
private Type GetMappedType(TypeReference type)
{
// main Harmony object
if (type.FullName == "Harmony.HarmonyInstance")
return this.HarmonyType;
// other objects
string fullName = type.FullName.Replace("Harmony.", "HarmonyLib.");
string targetName = this.HarmonyType.AssemblyQualifiedName.Replace(this.HarmonyType.FullName, fullName);
return Type.GetType(targetName, throwOnError: true);
}
}
}