Welcome back to Objects and Methods.
This is the last section in our huge smiley chapter right now.
And it's also the most amazing part because objects and methods gives us the possibility to mod our
code, to modify it so we can insert complete new code into our application.
I mean, okay, we have done this regarding to overriding single registers, but now we can add additional
code and we will use this to do a lot of amazing stuff.
For example, we insert code that prints out passwords or crypto keys.
We can also set some print statements to print out values of registers, for example, or regarding
to checks.
So if this check has been set to true or false, so we can do a lot of stuff.
Okay, so let's simply just dive into this and you will see how amazing this is.
All right.
So we start with a simple example on the left.
Let me get this mouse.
There we go.
We've got our check if we lose function.
And this one here got some local variable which is named collision.
We are creating a new player and this player calls now this collision function and this collision returns
a boolean value, true or false?
And then we return this value over here.
And if this value is, for example, true, then we lose.
Otherwise, this game can move on.
All right?
This time we are not that interested in this function over here.
We are mostly interested in this one, the player.
So how does this object get created?
And if you take a look on the right, we've got the smiley code and we can see we've got a new instance
we zero.
Then we've got the reference to this instance and this is the class reference to player.
We can see this at the ending, at the semicolon over here.
Then we have an invoke direct method call and now we also need to take care about details like this
brackets.
This bill invoked this method on this object, and the method we are going to invoke is our class reference
player dot in it.
Init is a constructor so we didn't write in constructor or we didn't define in constructor over here
in the player class.
I mean, you cannot see this, but at least there will be a default constructor.
We will see this if we code this later on real quick.
Okay.
And this year this constructor gets called and it returns void and we can now call methods on this object,
which is super amazing.
So let's say, yeah, display object got some different methods.
We can now extend this code by calling additional methods over here.
Super nice.
All right, we can see.
Okay, this we zero becomes now a local variable which is player.
So now we've got our player variable.
But the most important part are these two instruction nil instance and invoke direct.
All right then we've got this one over here.
Here we are calling a method on this object.
Let's see how it looks like.
Okay.
This time we've got invoke virtual.
This just means we've got a public method invoke direct.
You remember this was regarding to constructors and private methods.
Invoke virtual is regarding to public methods.
Okay then we've got our brackets and we've got our zero and then we can see it once again.
Class Reference Player Collision.
This is how we can call a method on this object.
So the approach is always the same regarding to objects.
You also learn about static methods.
This is a little bit different.
It's more easier.
But for now we are just looking on objects over here.
All right?
And then, yeah, the rest is well known.
We move the result into V one, which means our B one register now holds the return value of this method
player that collision.
And then we simply return this v one and we can also see this one over here, which is the, the local
variable collision.
So it gets assigned just before we return this value.
But that's not really important.
All right.
And that's all about our function here.
So if you take a look at the left side in the Java code, we can see there's a lot of stuff going on
and also on the right side, of course.
But now, now we are able to understand what's going on.
So regarding to this new instance in and invoke direct, okay, we have to learn this first, but we
will do this by practicing.
But after all, I mean, this one is pretty identical to this one.
This is just another method, a different ball.
This is the constructor and this is a collision method.
So if you want to create a new player object, we simply can take this one over here.
And all we have to do is we have to assign it to a new register.
So we use, for example, we want for this and also here and then we can create a new player.
We will do this in the space planning application later on.
But first of all let's switch to our Ubuntu VM and code this application real quick and then we can
practice and play around a little bit.
All right.
Okay, here we are now the Ubuntu VM.
Let's write this application.
So we open the android studio.
Okay.
So here we create a new project now empty activity.
And we name this Smiley three Shabba, and we go back to Marshmallow.
All right.
So let's wait until the Gradle build has been finished.
This might take some minutes, so I'll see you in a few moments.
Alrighty.
The build has been finished.
This is our application.
Now let's create a new class.
And you know what?
We name this player once again.
So it's really impressive how much diversity I bring into this right now.
But having this class name, I mean, this is the best example, in my opinion, to practice this.
So at this time let's make an yeah, let's make it a little bit different.
So now we've got a level completely different and we start with level one, but we still get the power
level and I guess we got a little bit stronger.
So now our power level is now let's say 5050 school.
And you know what, this time you also create and constructor.
So I guess that's the first time we, we create creating constructor over here.
So I guess we cannot complain anymore about some diversity in our application here.
We print out that we have created a new object and that's all.
Okay, then we have two methods in public void.
Let's see, we've got level up method here, level of this code and this increase our level by one.
And we also print this out our new level here.
All right, then we've got a new a different method, another method which is increased power.
And we've got the parameter of power and we increase our power by this.
And we also print this out.
All right.
This looks good.
The code regarding to this will be massive.
So we play it on Somali file.
It will be no fun to look at this right now.
We will learn how to do this, but not yet.
What we are interested in is what we are in.
Interested in.
Yeah, I guess that's that's correct.
Is how we can create a new object.
So let's see, we go into our main and here we create now a new player
and because some methods.
So let's call.
Increase power and we provide a new one which is 900 OC.
We do not call the other method, but this is.
Yeah, that's good for us.
Alright.
We built this application and then we install it.
We have a quick look into this, then we decompile it and we analyze this magic.
So the approach is pretty the same and this time we try to insert some new code over here, which is
yeah, which will be super amazing.
So let's see, we've got our app and let's put this into an extra directory.
So we've got apps.
And we've got absolutely no structure, or at least I've got absolutely no structure in there.
So let's call this Smiley three in case you did.
Better yet, please write me a message on how to organize this a little bit.
Maybe I will adjust this in the future.
So we insert it and then we have it compile.
Now let's install it first.
So let's see how how it works.
Yes.
ADB is missing.
Okay.
Maybe we have to first connect.
We are connecting first.
Now we're installing this app.
We get the screen and we also get a new terminal to fetch the output.
So in the past we have seen the Lochhead output is really massive.
Therefore we filter for this right now and we can do this with grep i for ignore k sensitive.
And we are looking for system out instructions.
We open our app.
There we go.
Yeah, we've got a hello world, but this is what we are interested in.
A new player object has been created.
No type.
Nice new power level is 900.
I guess the sentence could have been grammatically a little bit better, but it's okay for us.
So yeah, we have created a new player object and we have increased the power level.
So let's have look into the multi code and let's see how this part looks like and therefore I get rid
of all these windows.
And we decompile the asset and we are opening it with the results to the code editor classes to.
All right, come up.
Let's have a look into this place, Smiley.
So let's see.
Okay.
Let's start at the beginning.
So we've got our public class, our player class.
We do not extend a certain special class.
So we are having a super class of Java, an object.
This is the source.
We've got two instance fields over here and we haven't seen this quite a while, but it's nice to see
it.
Then we've got our constructor and now we've got some code in our constructor over here and here we
can see, well, okay, it didn't expect it to be that massive, but this looks really hard right now.
So, I mean, increase power.
Let's have a look.
All right.
It's just this one, these these two lines over here, and these two lines generate this amount of smiley
code.
We will learn how to deal with this.
And it won't be or it will not be that complex.
But for now, we get this also the level up.
Okay.
Let's have a look into something more smooth.
So the main activity, those two windows gets even more.
I've got this feeling.
Okay.
So here we have our main activity and we are only interested in this part over here.
So this by the way, this is here.
If we look at this.
So this is the zero we are at.
We are getting this value and to be zero.
And here we have invoke virtual.
So we are we are invoking this this method code and this one has an integer parameter.
So this is referenced as an integer.
But yeah, it's some kind of reference.
So just if you are curious and would like to know what this one is.
So this is the parameter of set content view, which is.
And this one over here.
So this ID.
All right.
So at least not a reference.
This is an ID over here.
Sorry, not an address.
This is an ID.
Okay.
But this is not something we are interested right now, so.
And let's skip this.
And yeah, we now focus only on this part right now.
So I get out my graphic tablet and then we discuss these few lines.
Okay, guys, let's have a look into this code over here.
All right.
The first line is already a new instruction.
Let's see what this does.
New instance.
Then we have a register with zero.
We've got a type reference capital until player and then we have our semicolon.
So we've got the player type.
Okay, let's get out our cheat sheet or yeah, the smiley up code cheat sheet and have a look.
Okay, so let's look this instruction up.
I hope you have your cheat sheet next to you because we will need this a lot during this chapter and
also within the last couple of videos.
So so this cheat sheet is really important for us.
Okay.
So we are looking for new instance.
New instance.
Okay.
We have.
We have only those two.
Okay, that's good.
New instance, v x and then we have comma type, instantiate an object type and puts the reference of
the newly created instance into the.
Well, okay, this is a lot of stuff going on in this instruction over here.
Let's read this once again, but let's close it over here.
Instances, it's an object type, so okay, this is our object type and puts the reference of the newly
created instance.
We are creating a new instance and we put the reference into v x.
Okay, so let's have look into the example.
Maybe we understand this a little bit better over here.
So we've got new instance we zero and then we've got Java IO file input stream.
So we are creating a new instance of Java IO file input stream and put this into V zero and this is
super amazing.
Why?
Because we have now an instance of file input stream.
We can, for example, also use file output stream and we can use this instance now to log sensitive
information.
Let's assume we've gotten A's encrypt and decrypt method.
We can now use this instance over here which is thought and with zero to lock out sensitive information
like the password, the initialization vector to encrypt it, or maybe also the decrypted message.
We can also use this to create new code in our application so we can create new instance of whatever
we like.
And this one is super amazing because we will use this to insert debug statements to print out register
values, to check which if statement we are taking, which path we are taking in this application and
many, many amazing stuff.
Okay.
But first, we have to understand how we can work with this.
All right.
So let's step back a little bit.
So let's get back into our code.
Okay.
So this one over here is our object type.
Object type.
So we are instantiating a new object type.
So player.
And we are putting the reference of this newly created instance.
So we are having a reference.
Of this newly created instance.
And we are putting this into v zero.
This player gets now put into v zero.
All right.
This was a short sentence, but a lot of stuff was thought in there.
This means we are having this line, our object type.
We are creating a new instance and we put the reference into v zero.
Okay.
But we do not do more.
So in this case, we cannot use something like for example, player dot power.
This is not possible.
Why?
Because we haven't called the constructor yet.
We only have a reference of this object.
So this is just a reference, nothing more.
This is not invalid object or instance right now.
Okay.
So I guess it will become one in maybe the next line.
Let's see.
So we are here.
So we know invoke direct regarding to private methods or constructors.
Okay.
Invoke direct.
Then we've got this curly brackets over here, which is interesting because we haven't seen them here,
for example.
It seems to be new invoke direct.
Then we've got our type reference or in this case we also have our instance reference because we have
this error over here.
This means this is a dot.
So our B zero needs to be our player.
So player dot in it in this case.
But before we do too much guessing, let's look this up.
What this instruction does.
We are looking for in bulk direct.
Okay, we've got six hits.
So invoke direct parameters.
Method call.
Invoke direct slash range and invoke direct empty.
Okay.
So this one is definitely what we're looking for.
Invoke direct.
Then we have parameters and we have a method called invokes a method with parameters without virtual
method resolution.
Okay.
So if you do not care about this one over here, what we do right now, then we simply just invoke a
method with parameters.
That sounds not that hard, but let's have a look at the example.
Invoke direct v one.
Then we've got Java Lang object and also this constructor of Java Lang object this in it.
But we can see here if this is the method, then it's a little bit strange because we do not have parameter
in there and here it tells us, invokes a method with parameters, but here we can see no parameters
are in there.
So let's read a little bit further.
We've got this strange looking method.
Eight invokes the eight method in the method table, so I'm really not sure what this is about.
So we simply ignore this with just one parameter and we want is the this instance are okay.
So this means we want is our parameter, but we want is our this instance.
So therefore we do not have a parameter over here.
This is now like the P zero register.
If you would have in parameter over here we would have a second register in there.
So the first register within this curly brackets of invoke direct means this is the this instance our
this object.
And afterwards there will be all the other parameters regarding to the function call.
But this is now empty because we only have all this object over here.
This is pretty similar to the P zero register right now.
Okay.
So it's absolutely normal if you are now a little bit confused.
But if you do our own method call or the next method call, you will see what I mean.
Okay, so then let's get back enable our mail, written text here.
I mean, it's getting a little bit off, but here it's okay.
So this means invoke direct.
We are invoking the constructor.
This one here is our this object.
So.
This this means our this object is the player because it's v zero.
Therefore I write this down as follow player and then we have a dot and then this init method.
But this init method is within this curly brackets and this means it's not an yeah in real method in
our java code.
So if you take back a look into this, if you create a new player object, it will be only this line
here.
And yeah, that's also what we've got in our smiley part at the first line.
But within our player object, we also got this one over here.
This is now our constructor that prints out a new player object has been created.
And this constructor right now is exactly what's happening in this line.
Therefore, this was the reason why I been so, so precise on reading this Mali instruction.
At this point we do not have our System.out.println.
This happens at this point right now, but this is not a Java instruction or Java function we can see
in our code.
This is hidden.
So if you take a look back into main activity, we do not have this over here.
There we've got we've got it included in this one, but there's no certain instruction for this.
In Somali, we can see this.
So this one over here, it's like in In the Matrix where we can see the code.
What's really happening in this case, we can see, okay, we are creating a new instance and tree accordingly
constructor of this instance.
And yeah, it's a little bit confusing, but this part is always the same.
We are creating a new instance and then we invoke direct on the constructor in case the constructor
has some parameters in there.
They will be, of course, included over here.
But let's do this step by step and not making it too complex right now.
Okay, so we get back on this one.
This means we have our player in it, but this one is not mentioned in the Java code.
So we do not see this.
At least we are in the matrix over here and in the Somali part.
Then we are able to see everything.
Okay.
So this line does not have a certain Java value over here.
So these two will end up in this line.
Okay.
So we've got this one and we've got this one.
Then clean this up next.
This one is familiar.
Locally, zero means we have a label for our register.
Right now.
It becomes a variable or an object in this case, and this object is named player.
But we are super smart and already named this as player, so we are also done on this one.
Okay, so these three lines are clear right now.
The next one also we have con slash 16 V one and then this value.
So at this point we are writing B one or we are we are writing this value into v10x384.
And this is the value.
Let's do the math.
Okay.
It's 900.
I really hope it's 900.
Otherwise, I have to record this video once again.
All right, so let's see.
Yeah.
We also got this line over here.
Okay, next, we have invoked virtual and this means we are invoking a virtual method, which is a public
method.
So we have invoked direct regarding to private methods and constructors.
We have invoked virtual regarding to public methods and then we have invoked static.
So static methods are pretty similar regarding to both of them, but a static method does not have this
arrow over here which signals this is now an instance field or instance method because the static method
does not have an instance.
We will talk about static methods in a separate section, but these are our three types we have to deal
with.
And it's not that complicated.
It's only these three invoke direct, invoke virtual and invoke static.
So let's see, invoke virtual.
We are having a public method.
We know this one here is our this object.
This means we have now a parameter over here.
Let's see.
We've got increase power.
This is the method or the instance method.
And yes, we've gotten parameter over here from type integer.
Okay, we are returning void.
That's nice to know.
So let's get through this once again.
Invoke virtual.
We are calling a public method.
We have we have our this object or this object is from type player.
So, yeah, we do not have to write this down.
We also gotten parameter in our method invocation and this is we want and we want is being passed as
parameter over here.
So I clean this up real quick.
Now we can see this a little bit better.
This is our we want, but to be sure.
Anyway, let's look this up.
So we are looking for invoke better.
Okay.
Here we can see we've got eight hits so invoke virtual parameters in curly brackets.
Method code.
This looks super promising, but nevertheless, let's check the others two range OC quick and quick
range.
All right.
This one is our method.
Code invokes a virtual method with parameters.
Nice.
This description is super awesome.
I mean, it tells us everything which we can understand.
It just invokes a better method with parameters, that's all.
But let's have a look at the description.
Invokes a virtual method.
Here we have before zero one, two, three.
Okay, this means we have one, two, three, four, five parameters and here we can see.
Okay, we've got test two.
This is our instance and method five is the method we are calling and here we can see we have one,
two, three, four.
Okay.
At the beginning we would have been confused, but now we know the first register is our this object.
Let's read this up.
Invokes the sixth method and the method table.
Okay.
We ignored us with the following arguments before.
Yes, it's the this instance and we zero.
We want v two and b three are the method parameters.
The method has five arguments and force four most significant bits.
Whatever.
All right, these are our arguments.
Sorry.
These are our parameters.
And we have five arguments, including our this OC.
I hope I didn't make it more complex.
So let's see.
We enable our.
Ooh.
It looks a little bit messy right now.
Was it all the time?
Right.
This maybe.
Okay, so we've got our this.
This is now our parameter.
And we are invoking increased power.
So this means we have player.
Dot.
Decrease.
Power
we want as a parameter and we want as 900.
All right.
So at this point, we create a new player object.
Let me clean this up a little bit.
Okay.
Way better.
So we create a new player object over here.
And this is done by calling new instance and invoke direct these two.
Then we are writing one or this value into v one.
We also address our label for our register.
I mean, these are not regarding to objects right now, so we ignore them.
And then next we call a method, this one over here, and we call this method with invoke virtual.
Then we provide the this object and we zero this register.
Then we provide our parameters and then we have our type reference of this object which ends at the
semicolon.
Then we've got our error, which signals.
Now we have our instance method and the instance method is called increased.
Power has a parameter over here and returns void.
And this is exactly what this line over here does.
All right.
So these two lines play a new player and player increase power regarding to the following three smiley
lines.
This one.
This one and this one.
So this is how we can create a new object and also call a new method.
So let's recap this real quick on the slides and then we will yeah, we will write our first smiley
code because our player instance has another method, this one over here, and this method is never
be used in our code.
We are not calling it in our app, but we will patch the application that we call this method.
So we will write our first real smiley line based on inserting or executing a new function call in our
app.
Okay.
But first, let's go back to the slides.
Okay.
So this is a different function, but it doesn't really matter.
So the approach of creating a new instance and calling an instance method is always the same.
Here we can see this method check if we lose returns and boolean value and does not have parameter.
Okay then we are creating a new instance over here.
New instance also from type player.
Okay.
So we also have this sentence over here copied from this awesome site instantiate an object type and
puts the reference of the newly created instance into VIX OC.
Our V0 is now holding our reference to this instance.
Next we've got invoke direct invokes a method with parameters without the virtual method resolution.
We ignore everything after without and we have a look into this invoke direct then curly brackets,
parameters and our method core.
Okay, so to create a new object, we simply need the following two lines.
These create a new object and they store it in v zero.
Now we have to be zero register.
And if we want to invoke a method on this object, we can use the next one to invoke virtual on our
object, which is v zero.
And then we simply have to write L for the type reference of what this object is.
So how are we zero this this object then semicolon arrow and the method name.
If you would have some parameters we need to declare them in invoke virtual at this point right here.
But in this case we can see no parameters are required.
So I would suggest let's go back into our code and here let's write our first line of Smiley.
So we want to call a new instance method.
Let's see if we can do this.
So we have our code over here and now we want to write it in the following way.
Player dot level up.
We want to have this code afterwards in our application.
Okay, we can now write this build is application and then we are done.
Yeah.
So let's assume we do not have the source code over here and we cannot simply add Java code.
Therefore we need to go back on our smiley pad.
All right, so let's see, let's make some notes.
Our method is level up, it does not have parameters and we are returning a void.
So this is our function definition.
Let's see.
So the first thing we need to do is we need to create a new instance.
But in this case we already have our instance because these two lines here has created our player object
and stored this in v zero.
It hasn't been overwritten yet, so we simply can use this one.
Okay.
If you want to call a new method, then we have to use.
Yeah.
Invoke virtual for public methods.
Maybe we have to also declared over here.
Let's do it in this way.
Okay.
So invoke virtual.
Okay.
Then we can see we do not have any parameters in there, but we need our this object and our this object
is being stored in v zero.
Okay, so we have one parameter which is V zero, then we've got a comma and now we need to type reference
of our v zero type reference starts with this capital L and then we have to get here the type reference
of this player class so we can fetch this from here.
Okay.
The dots needs to be replaced with slashes because within Smiley, these are directories.
Okay.
So we have our player and the type reference and ends sorry at the semicolon.
Then we want to call an instance method, which means we have to add our error over here and now we
simply have to add the function definition of our method.
We want to call we can see this one here is called level up.
All right, level up.
We can see we do not have parameters and we are returning.
What?
That's all.
This line will execute and level up on our player object.
And this is nice because our player would otherwise not have been the possibility to level up.
And this is something, of course, we do not want to have.
We want a well level player right now.
So let's see, let's play this application back and install it on the device.
Okay, so here we have back.
I have built and signed this application.
Here's our device and we can also see whoops.
We've got the old application installed.
So by opening this we create a new play object and we have a power level of 900 now.
All right, so let's uninstall this and yeah, install our new version.
Let's put some dramatic countdown on this to see if our player gets some level up.
So I will open this application in 3 to 1.
Oh, and never mind.
I just open it.
Yes.
A new player object has been created.
New power level is 900.
Our player is now level two.
We hadn't the ability to level up in this application, but now we introduced it and you can imagine
how many stuff you can do right now.
But please do not use this to cheat on online games or other games or applications where do not have
the permission to do so.
So I'm not responsible if you misuse your newly gained power.
All right.
In this case, I give you the permission to hack this application.
So we've got level up and we are now level two.
But let's assume this is a function that's exactly doing the same in some other applications, or it
increases our power, our gold, our life, our health, whatever.
So why do we call it just once?
Wouldn't it be more fun regarding to this app right now if we call this multiple times?
So regarding to the Java code, let's say instead of doing this and let's do this.
So in this way, we will level up a lot more.
Faster, right.
Let's see how we can do this.
We go back in our smiley coat.
And if you take a look into this, we can see we do not have certain parameters or certain conditions
over here.
We call this instance method level up, no parameters.
And what that's all within this method.
We go into our player object and it does the rest.
So therefore, let's simply copy and paste this line.
So I copy it and now we paste it a little more often.
Okay.
So let's play this application back again and see how it works.
So here we are back again.
I have built this new version.
We can see the old one is still running.
Let's let's uninstall this ADB uninstall app hacking smartly.
Three ADB install odd apk and the background.
You can see the code or smiley code with all these level apps.
Should we put some dramatic camera on this once again?
Let's do this.
Three, two, one, go.
And there we go.
We've got now a lot of more level ups, which is super amazing.
So this is how we have to deal with objects and instance methods.
To recap this real quick, let's have a look.
The things we need to do is we need to create a new instance.
Then we need to invoke the constructor of this instance.
And this is within this brackets over here.
If the constructor has parameters, constructor, constructor, constructor, if the constructor has
some parameters, we have to declare them over here.
Let's say our constructor has an integer.
Then we need.
Of course.
Then we need to introduce a new register over here.
For example, b two.
This one is now ten.
And then we also have known parameter.
Sorry with B two.
All right.
So in case it does not have any parameters, then we can stick to this approach.
Okay, so let's get cleaned on this.
So, new instance, invoke direct and then we have this object on zero.
If you want to call a method, a public method, we can use invoke virtual amount of parameters.
Object reference.
Our arrow, for instance, method, the instance method and the prototype.
And that's it.
These three steps.
So let me show you this in this way.
These three steps are required to do this.
Okay.
All right.
So that's all for this video right now.
In the next video, we take a look at static methods.
And this will be super amazing because we get back on our space ping application and we will do a lot
of funny stuff over there.
Okay.
So I'm looking forward to see you there.