Jump to content

Issue with multiple mods replacing delegates of buttons


Recommended Posts

I ran into this problem a month ago, and first spent several hours isolating it down to two specific mods, and then today I spent a number of hours debugging it.  What made it strange was that the two mods both worked perfectly by themselves, it was only when both were installed that the problem showed up.

It was very strange., you can read about the issue here: 

 

The problem of multiple buttons when both Fill-It-Up and Progressive-Colonization are present (and most likely Werner's Checker as well) arises because both mods are replacing the listener of the EditorLogic.fetch.launchBtn.onClick.  Then, when the button is clicked, BOTH replacements are called, 
and then both of them are invoking the EditorLogic.fetch.launchVessel

This isn't a bug in either mod, it's a bug in the implementation since there is no way for a mod to get a delegate of a button which is an unknown method.


While it's possible to remove all listeners, the only one this would work for would be the last mod initialized, since it would remove all others.

Ideally, what should happen is one of the following (for the purposes of this discussion, I'll refer to both mods via abbreviations, and also assume there are only two mods, but this applies to any number of mods):

  1. The new UnityAction which each mod adds would be called simultaneously, and then only when both have finished, the original delegate (EditorLogic.fetch.launchVessel) would be called.
  2. The first mod initialized would be called first, and when it was done the second mod would be called, and finally the original delegate would be called.
  3. A mod will specify a priority when initializing .


This can't be solved by one mod, a solution needs to be implemented and used by all mods which replaces the delegate(s) of this button.

I am working on a solution for this because:

  1. I found the problem
  2. I have at least two mods affected by this problem

I'm writing a tiny mod which will be used by any other mod to control access to this button and it's delegates:

class ButtonManager
{
    void InitializeListener(UnityEngine.UI.Button button,  UnityAction delegate);
    int AddListener(UnityEngine.UI.Button button,  UnityAction delegate, int priority = 5);
    void RemoveListener(UnityEngine.UI.Button button,  UnityAction delegate);
    void RemoveListener(int listenerId);

    int GetListenerCount(UnityEngine.UI.Button button);
    void RemoveAllListeners(UnityEngine.UI.Button button);

    void InvokeNextDelegate(int currentDelegateId);

    void SetModeSequential();
    void SetModeSimultaneous();
}

The priority will be used to determine the order of listeners to be called.  Higher priorities will be run first.  For the Sequential mode, mods with the same priority will be run in the order initialized.

 

InitializeListener     Initializes the mod to work with the specified button.  The delegate should be the same as what the default it, it will have the lowest priority
AddListener     Adds a delegate to a button with a specified priority.  Returns a listenerId, used below
RemoveListener     Removes a delegate from a button
GetListenerCount     Gets a count of all the listeners
RemoveAllListeners   Removes all except the default listener
InvokeNextDelegate   Tells the mod to call the next delegate on the list

SetModeSequential  

SetModeSimultaneous  

These two can set or change the mode, sequential will run one delegate at a time, simultaneous will run all of the same priority at the same time, and then the next lower, etc
   

 

 

 

 

 

 

 

 

 

I hope to have this finished in the next few days, and will first implement it in my mods.

Edited by linuxgurugamer
Link to comment
Share on other sites

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...