fvtt-lib-wrapper icon indicating copy to clipboard operation
fvtt-lib-wrapper copied to clipboard

The "Token.prototype._onClickRight2" registration seem not to work

Open p4535992 opened this issue 4 months ago • 4 comments

⚠ Do not open a support ticket using this form unless you are a package developer. We also do not provide support for packages that promote or otherwise endorse piracy. Your issue will be closed as invalid if you do not fulfill these requirements.

The easiest way to find support is to ask the community. The largest community-provided support channels are:


Confirm you have read the above

Yes i think is the right channel for a question.

Describe the question

I used the libwrapper feature many times , but this is the first time i tried to used on the "Token.prototype._onClickRight", and it's seem to not work ?

Everything seems to be in place, but for some strange reason it doesn't "go" inside the registred wrapped code... sorry I don't really know what other details to provide...

Here the piece of code:

 libWrapper.register(
            CONSTANTS.MODULE_ID,
            "Token.prototype._onClickRight2",
            _TokenPrototypeOnClickRight2TokenHandler,
            "MIXED",
        );
    static _TokenPrototypeOnClickRight2TokenHandler(wrapped, ...args) {
        // if ( !this._propagateRightClick(event) ) event.stopPropagation();
        // if ( this.isOwner && game.user.can("TOKEN_CONFIGURE") ) return super._onClickRight2(event);
        // return this.setTarget(!this.targeted.has(game.user), {releaseOthers: !event.shiftKey});
        const token = this;
        const isEi = token.actor?.getFlag(CONSTANTS.MODULE_ID, CONSTANTS.FLAGS.ENABLE) ?? false;
        //  const isItemPiles = game.modules.get("item-piles")?.active && game.itempiles.API.isValidItemPile(token);
        if (isEi && !game.user.isGM) {
            token.actor.choicesPlusExecuteMacro();
        }
        return wrapped(...args);
    }

Additional context

The full example module code is here:

https://github.com/p4535992/foundryvtt-choices-plus/blob/41a9e24f65ee1e3fa2ae45a83943b5b781165a4c/src/scripts/lib/choices-plus-helpers.js#L117

p4535992 avatar Apr 21 '24 11:04 p4535992

Can you confirm the version of Foundry and libWrapper you are using?

Also, does anything show up in console if you set the libWrapper verbosity to Debug in the settings menu?

I'll take a look soon, and try to reproduce.

ruipin avatar Apr 21 '24 13:04 ruipin

All modules disabled (excpet Choices Plus and ColorSettings) The versions are: libwrapper: 1.12.13 Foundry: 11.315

It's seem the wrapper is registered:

libWrapper-api.js:595 [I] libWrapper: Registered a wrapper for 'Token.prototype._onClickRight2' (ID=8) by module choices-plus with type MIXED.

I'll pass to you the full log file:

vnbrs.localhost-1713724843356.log

p4535992 avatar Apr 21 '24 18:04 p4535992

I can't take a better look right now, but I notice from the log you are wrapping this method quite late.

There is a known implementation detail with Foundry where some constructors copy references to callback methods and store them internally as part of a classes' members, rather than actually calling the methods by their full name as necessary. The problem is that if libWrapper hasn't wrapped said method before the reference is copied, those references will always refer to the old (non-libWrapper) method.

The solution for those cases is to ensure that libWrapper tries to wrap this method ASAP, for example in the libWrapper.Ready hook which runs right before init. This ensures that the references taken are to the libWrapper handler.

I suspect this might be the case here - any chance you can try wrapping during the libWrapper ready hook, and check if those wrappers execute?

If that doesn't work, I'll try to take a look tomorrow.

ruipin avatar Apr 21 '24 20:04 ruipin

Sure i'll let you know it

p4535992 avatar Apr 21 '24 21:04 p4535992

Nothing here the code i tried:

Hooks.once("ready", function () {
    Hooks.once("libWrapper.Ready", function () {
        ChoicesPlusHelpers.registerClicks();
    });
});

p4535992 avatar Apr 22 '24 19:04 p4535992

Not ready, that is too late. You need to do it before any Token gets constructed, i.e. at init or (even earlier) libWrapper.ready.

Just the inside portion:

Hooks.once("libWrapper.Ready", function () {
   ChoicesPlusHelpers.registerClicks();
});```

ruipin avatar Apr 22 '24 19:04 ruipin

a ok is worked on the init hook.... from now on i will use the Hooks.once("libWrapper.Ready"... Ty very much for the support

p4535992 avatar Apr 22 '24 19:04 p4535992

No problem. This confirms it is indeed one of the "class instances store callback references" issue. Closing as resolved 😄

ruipin avatar Apr 22 '24 19:04 ruipin