Basic SWF Code Injection Tutorial

Discuss the campaign of SM63 or other aspects of SM63 here that do not pertain to the Level Designer.

Moderator: Level Moderator

Basic SWF Code Injection Tutorial

Thumbs up x1

Postby Forgotten » October 30th, 2017, 2:11 pm



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


How It Works

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.


Getting the Tools

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.


Editing/Making the SWF

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;
That will change Mario's jump speed/height to 24 (Luigi has his own variable, _root.Luigijumpspeed). DON'T forget to click the "Save" button on the bottom!
When you click on that button, it should tell you "Code successfully saved!". Now click on the other "Save" button on the upper left.


Making the Level/Loading the SWF

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">
Make sure you have template.swf in the same folder as Super Mario 63, and do not rename it!
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!


Hosting the SWF Files

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 where do we host the files? Well, we still haven't decided on a standard, reliable site for hosting SWF files yet, so you'll probably have to find a website yourself... for now. Personally, I'm using my old forums' filemanager.


Doing Other Stuff

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


Examples

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);
This is the same code with the AreaTextClipF function added. This will display a title message like the level title that appears when you begin a level, saying "Jump height increased!". The second parameter (0) determines the message's color.
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();
The above will change the music to Hazy Maze Cave and play it. I might have to make a list of every track's names, but you should be able to find them in SM63's SWF using FFDec's search functions.

Making Mario (or Luigi) spin indefinitely
Code: Select all
_root.KeySPIN = function()
{
   return true;
}
Originally, the KeySPIN function checks if the X key is being pressed. The function is used in a condition check, and if it returns true, Mario spins. This code here redefines the function to always return true, making Mario spin indefinitely.

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;
};
Because the functions KeyPlus and KeyMinus are called every frame during gameplay, we can take advantage of that by inserting code in them, while still keeping their original code intact. The above code will constantly increase the coins counter once it's run.


THE END

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.
User avatar
Forgotten
As Ninja As Myst

 
Posts: 128
Joined: August 10th, 2015, 8:19 pm
Location: I forgot...

Art Contest Winner
8th Art Contest - Nature

Thumbs Up given: 19 times
Thumbs Up received: 28 times

Return to Super Mario 63 General