Basic SWF Code Injection Tutorial
As we've found out, it's possible to execute arbitrary code in Super Mario 63. This means we can modify SM63 by injecting code. While I'm more interested in exploring and sharing the possibilities of this in custom levels, I'm putting this in Super Mario 63 General because it affects the game as a whole.
For this tutorial, we'll do something super simple that only requires one line of code. Remember though, I'm no Flash expert, I'm still learning how things work, so don't rely on this tutorial that much.
It all starts from the level designer. You should all know that signs (and titles) in Super Mario 63 accept HTML code, that includes <img> tags, but if you link to an SWF file instead of an image, you can get a secondary SWF running in a sign!
However, keep in mind this trick does NOT work on the Newgrounds or LD-only versions, so you might wanna download the full offline version here: viewtopic.php?f=83&t=50885
Since the secondary SWF shares variables with Super Mario 63, we can change variables, arrays, and even functions by writing code in this SWF that does specifically that, and load it in a sign/title to modify the game. Don't worry though, most of these changes aren't permanent and will be gone as soon as you close/restart the game.
The only changes that stay are those affecting save files, obviously.
To do this, we'll need a Flash decompiler such as FFDec. I'm sure there are other decompilers out there, but for this tutorial we'll use FFDec, since that's the decompiler I'm using.
Download and install FFDec: https://free-decompiler.com/flash/
Save this template SWF file too, we'll be using this in the tutorial: http://jhynjhiruu.uk/etc/template.zip
Please extract "template.swf" to the folder where your Super Mario 63 EXE is.
Once you have everything ready, run FFDec and open template.swf in it.
You should see a list on the left side. Expand "scripts", click on "frame 1" and "DoAction". You should now see a code editor in the middle. This is the code we'll be loading in a sign.
Again, for this tutorial, we're sticking with something simple: we'll change Mario's jump height via a sign.
To do that, click the "Edit ActionScript" button below the code editor, and you should now be in edit mode.
Write this line of code:
- Code: Select all
_root.Mariojumpspeed = 24;
When you click on that button, it should tell you "Code successfully saved!". Now click on the other "Save" button on the upper left.
Now that we've created an SWF file with our code, we'll need to load this in a sign.
Start up Super Mario 63 and open the level designer. For this test, we'll just use a local link in a sign. So place a sign in the level and write this message:
- Code: Select all
<img src="/template.swf">
If you've done everything correctly, you should now be able to jump higher than usual. Congrats, you've just successfully modified a variable with code injection!
That's just the basic idea on how it's done. Obviously, we're loading code from a local SWF file, which means it'll only work for you and not others if you publish a level with that. Thus, we'll have to host our SWF files on the internet, then we'll update the signs to point to those SWF files.
Example:
- Code: Select all
<img src="http://example.org/swf/template.swf">
But, one more thing... How do you find the game's variables? I've only demonstrated modifying Mario's jump height, but not anything else. Well, Jhynjhiruu is working on a documentation that lists useful functions/variables/redefinition codes, but it's currently WIP and doesn't have much yet.
Alternatively, since you have FFDec installed, and possibly the SWF of Super Mario 63 (if you don't have it, you can get it here), you can just open that in FFDec and view Super Mario 63's code. You'll find all the variables and functions there.
I'll let you know where to look though. Under "scripts", ignore all the "DefineSprites", and just scroll to the very bottom where you should find 17 frames. Frame 3 contains the initialization for most gameplay-related stuff, so you can find most of the useful _root variables there (like the jump speed we used above).
I'll put a few examples here, I figured I should demonstrate a few other things like redefining a function.
Displaying a message
- Code: Select all
_root.Mariojumpspeed = 24;
_root.AreaTextClipF("Jump height increased!",0);
For some reason, sometimes the function won't work, so you'll have to restart SM63 if that happens.
Changing the music
- Code: Select all
_root.SongIntro = "HazyMazeCave-Intro";
_root.SongRepeat = "HazyMazeCave-Rock";
_root.PlayMusicAndIntro();
Making Mario (or Luigi) spin indefinitely
- Code: Select all
_root.KeySPIN = function()
{
return true;
}
Writing code that runs every frame in-game
- Code: Select all
_root.KeyPlus = function()
{
_root.Coins++;
if(Key.isDown(33) || Key.isDown(187))
{
return true;
}
return false;
};
So there you have it, those are the basics of SWF code injection. I'd love to share code that modifies objects in custom levels (such as water and door destinations). However, those are a little more complicated so I'm planning to put them in a later topic/post.
If you have just basic code knowledge, or if you're a pro at ActionScript, you should be able to find things out on your own. As far as I know, it's pretty basic stuff, as I hardly know anything about ActionScript myself.