Skip to content

Provides many common compatibility methods, anywhere from checking if another mod is active to getting a consistent method from another mod.

License

Notifications You must be signed in to change notification settings

pointfeev/CompatUtils

Repository files navigation

Usage

All methods can be accessed via the CompatUtils.Compatibility class:

using CompatUtils;
bool combatExtendedActive = Compatibility.IsModActive("ceteam.combatextended");

Methods

bool IsModActive(string modPackageId)

Summary:
    Goes through all installed mods checking for a mod with the specified PackageID and returns whether the mod is active.

Returns:
    A bool representing whether or not a mod with the specified PackageID is active.
string GetModName(string modPackageId)

Summary:
    Goes through all installed mods checking for a mod with the specified PackageID and returns that mod's name if found.

Returns:
    A string representing the name of the mod with the specified PackageID, or null if no mod with the specified PackageID is active.
MethodInfo GetMethod(string className, string methodName, Type[] parameters = null, Type[] generics = null)

Summary:
    Uses Harmony's AccessTools to get the reflection information for the specified method.

Returns:
    The MethodInfo of the specified method if found, otherwise null.
MethodInfo GetMethod(string typeColonMethodName, Type[] parameters = null, Type[] generics = null)

Summary:
    Uses Harmony's AccessTools to get the reflection information for the specified method.

Returns:
    The MethodInfo of the specified method if found, otherwise null.
bool IsMethodConsistent(MethodInfo methodInfo, Type[] correctMethodTypes, bool logError = false, string modPackageIdForLog = null)

Summary:
    Checks all of the specified method's types in order to make sure the method hasn't been changed from your current implementation of it.
    May print a detailed error message to RimWorld's console if logError is set to true.
    The detailed error message will also give the name of the mod at fault if modPackageIdForLog is specified.

Returns:
    A bool representing whether or not the specified method's types are all the same.
MethodInfo GetConsistentMethod(string modPackageId, string className, string methodName, Type[] correctMethodTypes, bool logError = false)

Summary:
    Checks whether a mod is active, and if so gets the specified method and runs IsMethodConsistent on it.
    May print a detailed error message to RimWorld's console if logError is set to true.

Returns:
    If the specified mod and method both exist and the specified method is consistent, returns the MethodInfo of the specified method; otherwise returns null.
MethodInfo GetConsistentMethod(string modPackageId, string typeColonMethodName, Type[] correctMethodTypes, bool logError = false)

Summary:
    Checks whether a mod is active, and if so gets the specified method and runs IsMethodConsistent on it.
    May print a detailed error message to RimWorld's console if logError is set to true.

Returns:
    If the specified mod and method both exist and the specified method is consistent, returns the MethodInfo of the specified method; otherwise returns null.

Examples

public static bool combatExtendedActive = Compatibility.IsModActive("ceteam.combatextended");
public static MethodInfo combatExtendedHasAmmoMethod = Compatibility.GetMethod("CombatExtended.CE_Utility", "HasAmmo");

public static bool isMethodConsistent = Compatibility.IsMethodConsistent(combatExtendedHasAmmoMethod, new Type[] {
    typeof(ThingWithComps)
}, logError: true, modPackageIdForLog: "ceteam.combatextended");
public static MethodInfo combatExtendedHasAmmoMethod = Compatibility.GetConsistentMethod("ceteam.combatextended", "CombatExtended.CE_Utility", "HasAmmo", new Type[] {
    typeof(ThingWithComps)
}, logError: true);
public static bool HasAmmo(ThingWithComps thingWithComps)
{
    return combatExtendedHasAmmoMethod is null || (bool)combatExtendedHasAmmoMethod.Invoke(null, new object[] { thingWithComps });
}

About

Provides many common compatibility methods, anywhere from checking if another mod is active to getting a consistent method from another mod.

Topics

Resources

License

Stars

Watchers

Forks

Languages