ruffle-rs / ruffle

A Flash Player emulator written in Rust
https://ruffle.rs
Other
15.58k stars 811 forks source link

If a Window function exists in other scripts, it will be overwritten by Ruffle's Window function. #14319

Open road0001 opened 10 months ago

road0001 commented 10 months ago

Describe the bug

I use ruffle in RPGMaker MV, it a html5 game engine. My swf load success and playing good, but the game throws error. After analyzing the error log, I found that Window function in the game script was overwritten to a native code, therefore the game error. The original Window function like this:

function Window() {
    this.initialize.apply(this, arguments);
}

Window.prototype = Object.create(PIXI.Container.prototype);
Window.prototype.constructor = Window;

If I load swf, the function Window will be overwritten by Ruffle, it becomes native code.

Expected behavior

Ruffle use independent function name substitute Window, like "WindowRuffle", avoid duplicate name conflicts. I try to change ruffle's "Window" to "WindowRuffle", it works good and game never throw errors. But I don't know if there are any potential problems after this.

Content Location

-

Affected platform

Self-hosted version

Operating system

Windows 10

Browser

NW.JS 0.72.0 Chromium 109

Additional information

No response

n0samu commented 10 months ago

Hi, please try removing the polyfillWindow(); call here: https://github.com/ruffle-rs/ruffle/blob/master/web/packages/core/src/js-polyfills.ts#L164 Then recompile Ruffle and let us know if the problem is fixed. Or share a link to the page affected by this issue so we can test it ourselves. Thanks!

road0001 commented 10 months ago

Hi, please try removing the polyfillWindow(); call here: https://github.com/ruffle-rs/ruffle/blob/master/web/packages/core/src/js-polyfills.ts#L164 Then recompile Ruffle and let us know if the problem is fixed. Or share a link to the page affected by this issue so we can test it ourselves. Thanks!

I uploaded a demo to present this bug. In this page, there are two buttons. If you click Window button first, it will alert your cilck count. If you click SWF button first, the swf will be loaded and play, but now click Window button will error. swftest.zip

n0samu commented 10 months ago

Thanks for the sample. Ruffle's Window polyfill was introduced in #2156, and according to Toad06's comments, Ruffle would crash during initialization if we did not restore the value of Window to native window constructor. I just tested it myself and confirmed this. So if possible, you need to modify your content to use a different name for its Window function, for example "MyWindow". Otherwise you will need to isolate Ruffle and your HTML5 game in separate frames to prevent this conflict from occurring.

road0001 commented 10 months ago

Thanks for the sample. Ruffle's Window polyfill was introduced in #2156, and according to Toad06's comments, Ruffle would crash during initialization if we did not restore the value of Window to native window constructor. I just tested it myself and confirmed this. So if possible, you need to modify your content to use a different name for its Window function, for example "MyWindow". Otherwise you will need to isolate Ruffle and your HTML5 game in separate frames to prevent this conflict from occurring.

Thank you for giving the solution! But to use a different name for Window function is a hard working, not only the game scripts, but a lot of 3rd plugins writting for this game engine. The Window function with its prototypes in game is for many of important feature, it’s hard to imagine how difficult it would be to modify them.

I tried to modify Ruffle, re-name Window to WindowRuffle in ruffle.js and the two core.xxx.js, like this:

After I modified Ruffle's scripts, my flash works good in game, and never cause game errors. But I don't know what will happen if I modify it like this, maybe everything is ok, maybe a lot of errors in other environment.

n0samu commented 10 months ago

That sounds like a fine solution - I'm glad to hear that you got it working. I believe the code you modified is generated by wasm-bindgen, so a proper fix along these lines would need to be done by them. Perhaps we should make an issue for it as Mike suggested in https://github.com/ruffle-rs/ruffle/issues/2156#issuecomment-752297118.