Jump to content

KSPAddon bug causes mod incompatibilities


Majiir

Recommended Posts

This issue has been resolved and the fix is no longer necessary.

---------------

Hi folks,

If you're having issues with mod compatibility where one seems to "disable" the other, you may be a victim of a KSPAddon bug. I discovered this while hunting down the cause of the Kethane and UniverseReplacer conflict.

What is KSPAddon?

This feature was added in the KSP 0.20 update. A class tagged with KSPAddon can start without an associated part, which is useful for mods that add behavior at a higher level. Some mods that have part functionality also use KSPAddon to run background tasks or handle plugin-wide data.

What's the problem?

There's a bug in the addon loader (in the stock game) that can cause mod compatibility issues. KSPAddon has a "once" parameter which, if set to true will cause that addon to only be started once (until the game is restarted). Once an addon starts, the addon loader adds it to a list of addons to skip for next time. The problem is in how entries in this list are compared; the addon loader only looks at the two attribute parameters. So, if you have two addons from two different mods that are both configured to start at the main menu and start only once, only one addon will ever start because when the loader sees the second one, it thinks it's already been loaded and skips it.

How do I fix it?

Until this is fixed in the base game, you have two options. You can set the "once" parameter to false and refactor your code to support that, or you can use my KSPAddonFixed attribute. This requires an additional type parameter which should equal the type you're applying the attribute to.

Example usage:

[KSPAddonFixed(KSPAddon.Startup.MainMenu, true, typeof(MyAddon))]
public class MyAddon : MonoBehaviour
{
// ...
}

KSPAddonFixed source (under CC0 Public Domain Dedication):

/// <summary>
/// KSPAddon with equality checking using an additional type parameter. Fixes the issue where AddonLoader prevents multiple start-once addons with the same start scene.
/// </summary>
public class KSPAddonFixed : KSPAddon, IEquatable<KSPAddonFixed>
{
private readonly Type type;

public KSPAddonFixed(KSPAddon.Startup startup, bool once, Type type)
: base(startup, once)
{
this.type = type;
}

public override bool Equals(object obj) {
if (obj.GetType() != this.GetType()) { return false; }
return Equals((KSPAddonFixed)obj);
}

public bool Equals(KSPAddonFixed other)
{
if (this.once != other.once) { return false; }
if (this.startup != other.startup) { return false; }
if (this.type != other.type) { return false; }
return true;
}

public override int GetHashCode()
{
return this.startup.GetHashCode() ^ this.once.GetHashCode() ^ this.type.GetHashCode();
}
}

I advise including the KSPAddonFixed source in your plugin. You could reference another plugin using it, but that's a hassle; multiple definitions with the same name won't cause any harm (since they'll be in different namespaces).

[EDIT] On the KSP issue tracker: http://bugs.kerbalspaceprogram.com/issues/1176

Edited by Majiir
Link to comment
Share on other sites

  • 10 months later...
  • 1 month later...
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...