WEBVTT
Kind: captions
Language: en

00:00:02.628 --> 00:00:04.212
Ahh, Minesweeper.

00:00:04.210 --> 00:00:06.500
For many of us, it was a defining game of our childhoods

00:00:06.500 --> 00:00:08.632
alongside Pinball and...

00:00:08.632 --> 00:00:09.536
...Solitaire?

00:00:09.536 --> 00:00:11.720
But funnily enough, as a kid, I never actually

00:00:11.720 --> 00:00:13.592
figured out how to play it.

00:00:13.592 --> 00:00:15.528
I feel like someone did tell me at some point

00:00:15.528 --> 00:00:17.524
what the goal was and what the numbers meant

00:00:17.524 --> 00:00:20.540
but my baby brain probably decided it was too much effort.

00:00:20.540 --> 00:00:22.772
Instead, I'd just set the size as big as possible

00:00:22.772 --> 00:00:24.188
and the mines as low as possible

00:00:24.188 --> 00:00:26.580
and pretend I was a minesweeping god!

00:00:26.580 --> 00:00:29.196
[censored] yeah, ever seen a one second speedrun?!

00:00:29.196 --> 00:00:30.740
But recently, I was playing around with the

00:00:30.740 --> 00:00:33.028
Flash emulator Ruffle and came across this

00:00:33.020 --> 00:00:36.496
gag in an old Flash game called Windows HIV.

00:00:36.496 --> 00:00:38.596
And of course, I'm the kind of person who's like:

00:00:38.596 --> 00:00:39.428
[nose exhale]

00:00:40.080 --> 00:00:41.576
I wonder if you could actually do that?

00:00:41.576 --> 00:00:43.136
I mean, hey, April Fool's was coming up

00:00:43.136 --> 00:00:44.264
so I thought it might make a...

00:00:44.264 --> 00:00:44.772
[nose exhale]

00:00:44.772 --> 00:00:45.980
...worthy prank!

00:00:45.980 --> 00:00:47.804
First, let's make sure I'm not being obtuse

00:00:47.804 --> 00:00:49.196
and you can't just set the mines to a

00:00:49.196 --> 00:00:50.872
high enough number to fill the screen.

00:00:50.872 --> 00:00:53.868
9 times 9 is 81, so let's put that in!

00:00:53.868 --> 00:00:56.944
No, it's limited it to 64.

00:00:56.944 --> 00:01:01.780
And if I choose the expert size, it limits to 435.

00:01:01.780 --> 00:01:03.680
So I guess there's some sort of algorithm in place

00:01:03.680 --> 00:01:06.052
ensuring that the game remains...

00:01:06.052 --> 00:01:08.716
well, I don't want to say "winnable" exactly.

00:01:08.716 --> 00:01:10.196
Well, looks like this is going to require

00:01:10.196 --> 00:01:13.176
some hacking after all, so let's get to it!

00:01:13.170 --> 00:01:15.348
So occasionally people just getting into romhacking

00:01:15.348 --> 00:01:17.548
ask how I know where to start.

00:01:17.548 --> 00:01:20.308
I mean, in a program with thousands or even millions

00:01:20.308 --> 00:01:23.476
of lines of assembly code, how do you find "the thing"?

00:01:23.476 --> 00:01:26.772
Well, the truth is it's mostly educated guesses and luck,

00:01:26.772 --> 00:01:29.468
however on PC, you do get a huge benefit

00:01:29.468 --> 00:01:31.728
in the form of "export symbols".

00:01:31.728 --> 00:01:33.092
What are export symbols?

00:01:33.092 --> 00:01:34.956
They're the main way that a process calls code

00:01:34.956 --> 00:01:37.752
in another library. Libraries like the ones in Windows

00:01:37.752 --> 00:01:39.584
or the standard C library.

00:01:39.584 --> 00:01:41.708
Since all of these libraries are well documented,

00:01:41.708 --> 00:01:44.244
even if we know nothing about the Minesweeper code itself,

00:01:44.244 --> 00:01:45.968
we can still get a good idea what it's doing

00:01:45.968 --> 00:01:48.204
by how it calls these other functions.

00:01:48.204 --> 00:01:51.304
So we want to find whatever code sets the tiles to mines.

00:01:51.300 --> 00:01:52.956
Well, we know it places them randomly

00:01:52.956 --> 00:01:54.584
and unless they went to all the trouble

00:01:54.584 --> 00:01:56.552
of writing their own random generator into the game,

00:01:56.552 --> 00:01:58.760
it probably uses a standard function from

00:01:58.760 --> 00:02:00.516
one of the aforementioned libraries.

00:02:00.516 --> 00:02:02.592
Checking over the symbols, we can see that, yep!

00:02:02.592 --> 00:02:05.712
It imports "rand" from the standard C library.

00:02:05.712 --> 00:02:08.048
I'll put a breakpoint on that, which basically means

00:02:08.048 --> 00:02:10.028
the debugger will pause the game and tell us

00:02:10.028 --> 00:02:11.632
whenever that function is called so we can see

00:02:11.630 --> 00:02:13.104
exactly what's going on.

00:02:13.100 --> 00:02:14.268
And there we go!

00:02:14.268 --> 00:02:15.752
Here's where the game calls "rand".

00:02:15.750 --> 00:02:19.104
The game only uses "rand" once, wrapping it in a small function

00:02:19.104 --> 00:02:21.480
that I'm going to assume is used to limit the result

00:02:21.480 --> 00:02:24.700
since "rand" itself doesn't actually have any options to do that.

00:02:24.700 --> 00:02:27.128
This wrapping function is called twice in the game,

00:02:27.128 --> 00:02:30.288
both in the same loop, effectively making this the only place

00:02:30.288 --> 00:02:33.080
in the game where randomly generated numbers are used.

00:02:33.080 --> 00:02:34.508
Looks like where we want to be!

00:02:34.508 --> 00:02:37.608
But initially, the fact that it was called twice kind of confused me

00:02:37.608 --> 00:02:40.052
because for some reason I'd assumed the way it would work

00:02:40.052 --> 00:02:42.364
is by looping over every tile in the game

00:02:42.364 --> 00:02:45.268
and randomly deciding if it was going to be mine or not.

00:02:45.268 --> 00:02:48.228
I don't know why I thought this because it's objectively

00:02:48.228 --> 00:02:51.216
a pretty bad way of doing that, but eventually I realized

00:02:51.216 --> 00:02:53.784
what it was doing and what actually made a lot more sense.

00:02:53.780 --> 00:02:56.480
It chooses a random X and Y coordinate,

00:02:56.480 --> 00:02:57.796
hence the two random calls,

00:02:57.790 --> 00:03:00.360
and checks if that coordinate is a mine;

00:03:00.360 --> 00:03:02.412
setting it to a mine if it isn't.

00:03:02.412 --> 00:03:04.072
It does this on loop until it's reached

00:03:04.072 --> 00:03:06.184
the number of mines it's trying to make.

00:03:06.184 --> 00:03:07.232
So my first thought was just

00:03:07.232 --> 00:03:09.656
overriding the mine count in this loop.

00:03:09.656 --> 00:03:11.896
Sure, it limits how many mines you can enter,

00:03:11.896 --> 00:03:14.076
but if the loop just happened to make more,

00:03:14.076 --> 00:03:15.656
it probably wouldn't notice.

00:03:15.656 --> 00:03:18.032
I didn't know if this would work, because I mean,

00:03:18.032 --> 00:03:20.804
picking tiles randomly until they're all mines

00:03:20.804 --> 00:03:23.260
is not very efficient at all.

00:03:23.260 --> 00:03:26.864
But I wasn't prepared for what did happen...

00:03:26.864 --> 00:03:27.792
No infinite loop,

00:03:27.792 --> 00:03:29.960
not even any noticeable slowdown,

00:03:29.960 --> 00:03:31.092
just...

00:03:31.992 --> 00:03:33.328
this.

00:03:35.168 --> 00:03:40.576
Soo... yeah, in an attempt to make all of the tiles mines,

00:03:40.576 --> 00:03:44.008
I've made... none of the tiles mines.

00:03:44.008 --> 00:03:47.476
This... made... no sense to me.

00:03:47.470 --> 00:03:48.300
At all.

00:03:48.300 --> 00:03:51.308
And stumped me for quite some time.

00:03:51.308 --> 00:03:53.720
On a whim, I tried some slightly lower numbers

00:03:53.720 --> 00:03:56.020
and it worked pretty much as expected,

00:03:56.020 --> 00:03:59.512
at which point, I gradually figured out what was going on.

00:03:59.512 --> 00:04:01.792
Apparently, it's a common rule in minesweeping games

00:04:01.792 --> 00:04:04.396
that the first click not be a mine.

00:04:04.396 --> 00:04:06.096
I guess that would be pretty unfair.

00:04:06.096 --> 00:04:08.136
So on the off chance that you do click on a mine

00:04:08.136 --> 00:04:11.208
in your first move, the game quietly moves it somewhere else

00:04:11.208 --> 00:04:13.376
so your first click is always safe.

00:04:13.376 --> 00:04:15.980
So what happens when we make it extremely likely

00:04:15.980 --> 00:04:18.448
that your first click, and in fact every subsequent click,

00:04:18.448 --> 00:04:20.408
is going to be on a mine?

00:04:20.408 --> 00:04:23.064
Well, I think what's happening is the game is

00:04:23.064 --> 00:04:24.560
still trying to prevent a "game over",

00:04:24.560 --> 00:04:28.884
but since there really isn't anywhere for it to move mines to

00:04:28.884 --> 00:04:31.872
we end up in this kind of limbo state where all the tiles

00:04:31.872 --> 00:04:34.464
are both mines and not mines.

00:04:34.464 --> 00:04:35.796
And they probably didn't code for this

00:04:35.796 --> 00:04:37.708
sort of scenario because ordinarily the game

00:04:37.708 --> 00:04:40.440
ensures a certain amount of free tiles.

00:04:40.440 --> 00:04:42.468
Well obviously that's not going to work for our gag,

00:04:42.468 --> 00:04:44.396
so how can we get around this?

00:04:44.396 --> 00:04:46.464
I decided to whip out Cheat Engine for this.

00:04:46.464 --> 00:04:49.380
It can be a little better for finding specific values in memory

00:04:49.380 --> 00:04:51.272
and is another good way to find where to start

00:04:51.272 --> 00:04:53.740
if symbols don't happen to help in a situation.

00:04:53.740 --> 00:04:56.256
I made a guess that the value we were looking for

00:04:56.256 --> 00:04:58.916
would either be a boolean, that is, a true or false

00:04:58.916 --> 00:05:01.488
that's set to true when a free tile is unveiled,

00:05:01.488 --> 00:05:04.916
or just a number that counts up how many free tiles are revealed.

00:05:04.916 --> 00:05:07.676
Either way, that value would start at zero upon reset

00:05:07.676 --> 00:05:11.040
and would be greater than zero once a tile was revealed.

00:05:11.040 --> 00:05:14.584
My hunch turned out to be right, and I soon found value where

00:05:14.584 --> 00:05:19.132
if I forced it to one, a beautiful grid of mines appeared

00:05:19.132 --> 00:05:21.368
every single time!

00:05:21.360 --> 00:05:23.304
So now, I just have to find the assembly instruction

00:05:23.304 --> 00:05:26.456
that sets it to zero, and set it to one instead.

00:05:26.450 --> 00:05:29.432
Turns out it was right under my nose the whole time.

00:05:29.432 --> 00:05:31.156
Right below the random placement loop

00:05:31.156 --> 00:05:33.412
was the instruction setting it to zero.

00:05:33.410 --> 00:05:37.372
Great! The only problem now is... replacing it.

00:05:37.372 --> 00:05:39.592
An instruction that sets it to one will require

00:05:39.592 --> 00:05:42.316
more bytes than we have, so we'll have to

00:05:42.316 --> 00:05:44.176
make some more space somehow.

00:05:44.176 --> 00:05:45.216
Here's what we can do.

00:05:45.216 --> 00:05:48.704
You see this other instruction here that also sets something to one?

00:05:48.704 --> 00:05:51.052
Setting to an immediate value such as this "one"

00:05:51.052 --> 00:05:52.936
rather than the value of a register like

00:05:52.936 --> 00:05:54.868
the rest of these instructions takes up a lot more

00:05:54.868 --> 00:05:57.488
bytes of code as you can see over here.

00:05:57.488 --> 00:06:00.184
If I was to set this to use the EDI register

00:06:00.184 --> 00:06:03.168
rather than "one", it frees up quite a few bytes.

00:06:03.168 --> 00:06:05.232
Look at all this room for activities!

00:06:05.232 --> 00:06:07.320
Okay it's only four bytes, but it's enough room

00:06:07.320 --> 00:06:10.380
for an extra instruction that increments EDI by one

00:06:10.380 --> 00:06:12.908
so that now... it equals one.

00:06:12.908 --> 00:06:15.020
Then we can shift the instruction we want down

00:06:15.020 --> 00:06:18.444
so that when it runs, EDI will be set to one for it too,

00:06:18.444 --> 00:06:22.076
effectively setting both of these to one with less bytes than before.

00:06:22.076 --> 00:06:24.472
We even have enough space to decrement it back to zero

00:06:24.472 --> 00:06:26.148
for the rest of the function, though we don't actually

00:06:26.148 --> 00:06:29.168
have to do that because the code overwrites EDI's contents

00:06:29.168 --> 00:06:30.464
shortly after anyway.

00:06:30.464 --> 00:06:34.468
And with that, the game lets us die every single time!

00:06:34.468 --> 00:06:36.804
Excellent, but we're not done yet, are we?

00:06:36.804 --> 00:06:38.896
This is all well and good for the beginner size,

00:06:38.896 --> 00:06:42.140
but since the mine count is now hard-coded to 81,

00:06:42.140 --> 00:06:44.804
for any larger sizes it doesn't work anymore.

00:06:44.804 --> 00:06:46.456
So we're going to have to change the earlier code

00:06:46.456 --> 00:06:49.408
to calculate the amount of mines it needs on-the-fly,

00:06:49.408 --> 00:06:52.472
and once again we're in luck due to some surrounding code.

00:06:52.472 --> 00:06:55.492
It turns out here, register EAX is the width

00:06:55.492 --> 00:06:57.928
and register ECX is the height.

00:06:57.928 --> 00:06:59.872
This is actually really convenient because

00:06:59.872 --> 00:07:03.320
any x86 multiply is done to the EAX register

00:07:03.320 --> 00:07:06.468
so the numbers are already where we want them to be.

00:07:06.468 --> 00:07:09.176
So the simple instruction "mul ecx",

00:07:09.176 --> 00:07:10.944
or multiply by ECX,

00:07:10.944 --> 00:07:13.220
will get the exact number of mines we need

00:07:13.220 --> 00:07:16.820
into the EAX register. Coincidentally, the register that's used

00:07:16.820 --> 00:07:18.704
to set the mine count in memory.

00:07:18.704 --> 00:07:20.252
Again, very convenient!

00:07:20.252 --> 00:07:21.900
So here's what we can do.

00:07:21.900 --> 00:07:23.816
We can naturally nop this instruction that

00:07:23.816 --> 00:07:25.524
loads the mine count from the settings.

00:07:25.524 --> 00:07:27.532
Next, I'll have to shift these instructions around

00:07:27.532 --> 00:07:30.188
because the function call here messes with the registers.

00:07:30.188 --> 00:07:32.660
So now, when the mine count is set from EAX,

00:07:32.660 --> 00:07:35.932
it happens when EAX is equal to the playfield width.

00:07:35.932 --> 00:07:38.564
All we have to do now is throw in a "mul ecx"

00:07:38.564 --> 00:07:41.784
to multiply by the height, and we get the total number of mines

00:07:41.784 --> 00:07:45.664
needed to fill the playfield right into our mine count!

00:07:45.664 --> 00:07:47.444
And there we go!

00:07:47.444 --> 00:07:51.456
No matter what the size, the playfield is always all mines!

00:07:51.456 --> 00:07:53.480
I'm actually pretty happy with this hack!

00:07:53.480 --> 00:07:55.548
It's simple yet effective.

00:07:55.540 --> 00:07:57.588
Sure, it could be cleaner by rewriting the loop

00:07:57.588 --> 00:07:59.876
without any random calls, but eh.

00:07:59.876 --> 00:08:01.352
If it works, it works, right?

00:08:01.352 --> 00:08:02.988
Time to create a patched executable

00:08:02.980 --> 00:08:05.288
and send it to some friends!

00:08:05.288 --> 00:08:08.760
So Matt sent me a file called "aprilfools".

00:08:08.760 --> 00:08:10.268
Yeah, this bodes well.

00:08:10.260 --> 00:08:10.964
Right.

00:08:11.308 --> 00:08:13.328
Winmine-- oh god...

00:08:13.320 --> 00:08:16.128
Better? o- okay...

00:08:16.120 --> 00:08:18.980
This wasn't what I was expect-- oh no...

00:08:18.980 --> 00:08:20.748
Oh no!

00:08:20.740 --> 00:08:22.696
I'm so bad at Minesweeper-- right.

00:08:31.092 --> 00:08:32.960
You know what? Actually this is good.

00:08:32.960 --> 00:08:34.572
This is good, because now...

00:08:35.072 --> 00:08:37.448
I'm at exactly the same level of everyone else

00:08:38.060 --> 00:08:39.488
playing Minesweeper.

00:08:40.796 --> 00:08:41.980
You know what, thanks Matt!

00:08:44.100 --> 00:08:45.388
God damn...

00:08:46.636 --> 00:08:49.232
Now if you want to try this out yourself, there is a download link

00:08:49.232 --> 00:08:51.660
in the description, however the version you're getting

00:08:51.660 --> 00:08:54.080
is going to be slightly different to the one I made in this video

00:08:54.080 --> 00:08:57.192
because the version I hacked was the XP version,

00:08:57.192 --> 00:08:59.364
and it turns out for some reason, it doesn't run

00:08:59.364 --> 00:09:02.780
on Windows 9x, which felt like a bit of a shame.

00:09:02.780 --> 00:09:05.604
Meanwhile, the version that ships with 9x is 16-bit

00:09:05.604 --> 00:09:08.740
so it won't work on most modern computers either.

00:09:08.740 --> 00:09:11.932
However, the version that ships with NT 4

00:09:11.932 --> 00:09:14.628
is both 32-bit, so it will run on modern computers,

00:09:14.628 --> 00:09:17.120
and also will run on 9x.

00:09:17.120 --> 00:09:20.256
So I went ahead and did exactly the same hack to that version

00:09:20.250 --> 00:09:21.720
and that's the one you can download.

00:09:21.720 --> 00:09:25.500
It should work on every version of Windows from 95 onwards.

00:09:25.500 --> 00:09:28.124
Now even though this hack was fairly simple,

00:09:28.124 --> 00:09:31.108
I learned later it actually could have been a lot easier.

00:09:31.108 --> 00:09:34.372
Firstly, you can actually find Minesweeper debug symbols.

00:09:34.372 --> 00:09:37.400
These are like export symbols, but way more OP.

00:09:37.400 --> 00:09:39.456
They provide human-readable names for pretty much

00:09:39.450 --> 00:09:41.848
every function and variable in the program.

00:09:41.848 --> 00:09:44.080
You can see how having actual words instead of

00:09:44.080 --> 00:09:45.480
random memory addresses would have

00:09:45.480 --> 00:09:47.132
gotten rid of a lot of the guesswork.

00:09:47.132 --> 00:09:49.316
Secondly, there's actually a GitHub project called

00:09:49.316 --> 00:09:51.680
ReversingMinesweeper which is apparently a

00:09:51.680 --> 00:09:55.980
largely complete decompilation of Minesweeper back into C++.

00:09:55.980 --> 00:09:59.224
So one could have just edited and recompiled this

00:09:59.220 --> 00:10:01.568
instead of hacking up the assembly code.

00:10:01.568 --> 00:10:03.852
And finally, if you want to be really naughty,

00:10:03.852 --> 00:10:07.804
an old Microsoft leak from 2004 actually contains

00:10:07.804 --> 00:10:10.352
the complete source code for Minesweeper.

00:10:10.352 --> 00:10:12.248
Not only could you have recompiled this,

00:10:12.248 --> 00:10:15.872
you can actually see exactly what code produced this assembly.

00:10:15.872 --> 00:10:17.168
It's pretty cool!

00:10:17.548 --> 00:10:19.988
Uhh, by which I mean, wrong and illegal...

00:10:19.988 --> 00:10:21.496
Please don't remove this video at Microsoft...

00:10:21.496 --> 00:10:23.188
Well, Happy April Fools everyone!

00:10:23.188 --> 00:10:24.700
Thank you so much for watching this video.

00:10:24.700 --> 00:10:25.844
I'll see you in the next one.

00:10:25.844 --> 00:10:26.876
Bye guys!

