-
Notifications
You must be signed in to change notification settings - Fork 0
CompatLib Documentation
CompatLib is designed to streamline mod compatibility by providing a structured approach to detect and manage interactions between various mods. It offers utilities to check for the presence of specific mods, retrieve mod versions, and register compatibility handlers that ensure seamless integration.
To integrate CompatLib into your modding project, follow these steps:
-
Download CompatLib: Get the latest release from the Releases page.
-
Add to Project: Include the CompatLib DLL in your mod's project references.
-
Namespace Inclusion: Add the CompatLib namespace in your code:
using CompatLib;
Initialize CompatLib within your mod's main class by extending ModSystem:
using Vintagestory.API.Common;
namespace YourModNamespace
{
public class YourModSystem : ModSystem
{
public override void Start(ICoreAPI api)
{
base.Start(api);
// Initialize CompatLib
api.Logger.Notification("YourMod initialized with CompatLib support.");
}
}
}Before implementing compatibility features, it's crucial to verify if the target mod is loaded. CompatLib provides the ModChecker utility for this purpose.
using Vintagestory.API.Common;
namespace YourModNamespace
{
public class YourModSystem : ModSystem
{
public override void Start(ICoreAPI api)
{
base.Start(api);
string targetModId = "targetmodid";
if (ModChecker.IsModLoaded(api, targetModId))
{
string modVersion = ModChecker.GetModVersion(api, targetModId);
api.Logger.Notification($"Mod '{targetModId}' is loaded. Version: {modVersion}");
}
else
{
api.Logger.Warning($"Mod '{targetModId}' is not loaded. Compatibility features will be disabled.");
}
}
}
}-
IsModLoadedreturnstrueif the mod is installed. -
GetModVersionretrieves the mod version, returning"unknown"if unavailable.
In this example, replace "targetmodid" with the Mod ID of the mod you want to check. The IsModLoaded method returns a boolean indicating the presence of the specified mod.
CompatLib allows you to register handlers that manage interactions with other mods. These handlers ensure your mod functions correctly alongside others.
using Vintagestory.API.Common;
namespace YourModNamespace
{
public class YourModSystem : ModSystem
{
public override void Start(ICoreAPI api)
{
base.Start(api);
CompatibilityManager.RegisterHandler(
targetModId: "targetmodid",
priority: 100,
handler: () =>
{
// Compatibility logic for the target mod
api.Logger.Notification("Compatibility handler for 'targetmodid' executed.");
},
description: "Ensures compatibility with Target Mod",
mutuallyExclusive: false,
dependencies: new List<string> { "dependencyModId" },
compatibleVersion: "1.0.0"
);
}
}
}-
priority: Defines execution order (higher value = higher priority). -
mutuallyExclusive: Prevents multiple handlers for the same mod from running simultaneously. -
dependencies: Specifies required mods for this handler to activate. -
compatibleVersion: Ensures compatibility with a specific mod version.
In this example:
-
targetModId: The Mod ID of the mod you're targeting. -
100: The priority of the handler. Higher values indicate higher priority. -
The lambda function contains the compatibility logic to be executed if the target mod is present.
-
The description provides context for the handler's purpose.
After registering handlers, CompatLib processes them based on detected mods. This typically occurs in the StartServerSide method.
using Vintagestory.API.Common;
using Vintagestory.API.Server;
namespace YourModNamespace
{
public class YourModSystem : ModSystem
{
public override void StartServerSide(ICoreServerAPI api)
{
base.StartServerSide(api);
var modGroups = CompatibilityManager.GetRegistrations().GroupBy(r => r.TargetModId);
foreach (var group in modGroups)
{
string modid = group.Key;
if (ModChecker.IsModLoaded(api, modid))
{
CompatibilityManager.ProcessHandlers(modid, api);
}
else
{
api.Logger.Notification($"Mod '{modid}' not detected; skipping compatibility handlers.");
}
}
}
}
}- Groups registered handlers by Mod ID.
- Processes handlers if the mod is detected.
You can enable or disable specific handlers dynamically.
CompatibilityManager.SetHandlerEnabled("targetmodid", "Handler for targetmodid", isEnabled: false);- Useful for runtime debugging or selective feature activation.
CompatLib includes AnalyticsManager to log conflicts and errors, helping with debugging and ensuring smooth operation.
AnalyticsManager.LogConflict("Conflicting behavior detected between ModA and ModB.");foreach (var log in AnalyticsManager.GetConflictLogs())
{
api.Logger.Notification(log);
}AnalyticsManager.ExportLogsToJson("path/to/logfile.json");CompatLib provides chat commands to retrieve compatibility logs.
.compatlog
- Displays conflict logs in the chat.
/compatlog
- Shows logs in the server console.
- Modular Handlers: Keep compatibility handlers independent for maintainability.
- Priority Management: Assign priorities strategically to prevent execution conflicts.
- Logging: Use logging features to track compatibility processes.
- Community Collaboration: Work with other modders to enhance compatibility solutions.