{"id":36,"date":"2014-03-21T16:27:38","date_gmt":"2014-03-21T16:27:38","guid":{"rendered":"https:\/\/blogs.scummvm.org\/josejx\/?p=36"},"modified":"2022-05-21T17:01:06","modified_gmt":"2022-05-21T17:01:06","slug":"identifying-variables-and-functions","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/josejx\/2014\/03\/21\/identifying-variables-and-functions\/","title":{"rendered":"Identifying Variables and Functions"},"content":{"rendered":"<p><i>Continued from the <a href=\"https:\/\/blogs.scummvm.org\/josejx\/2014\/03\/20\/working-on-understanding-the-function-from-the-other-side\/\">previous entry<\/a><\/i><\/p>\n<p>In this post, we&#8217;ll be focusing on variables, function calls and the structure of the decompiled function, <i>SetActorLocalAlpha<\/i>.<\/p>\n<p>In the previous entry, we examined the Lua script that calls this function and identified its arguments. Applying that knowledge to the disassembled code indicates that the first 4 calls to <i>lua_lua2C<\/i> are actually calls to get the parameters for the function. As such, this code can be re-written with descriptive variable names. Additionally, the types suggested by these parameters suggest similarities with code that&#8217;s already been written.<\/p>\n<p>Starting with the first parameter, we see that in the script, the function is called by itself, with no colon or period operator. This indicates that the function is standalone and not a member of any class. Next, we see that the first parameter is <i>self.hActor<\/i>, as seen in the call to <i>SetActorLocalAlpha<\/i> in the previous post. Since we know that the variable is a member of the &#8220;self&#8221; object, we need to identify what this variable is used for. Often, it&#8217;s possible to tell the object&#8217;s type by looking at what sets the member variable. Searching through the scripts for &#8220;<i>hActor =<\/i>&#8221; will identify where the <i>hActor<\/i> variable was set, and we&#8217;re in luck! The file <i>_actors.lua<\/i> is the only file in the scripts directory that matches this criteria. Let&#8217;s take a look at where it&#8217;s used.<\/p>\n<p>The first hit we get is in the <i>actorTemplate<\/i>, a structure that is used in the Lua script as a template for all new actor objects. While this is useful for identifying members of the Actor class, this doesn&#8217;t help with identifying the type for <i>hActor<\/i>. Let&#8217;s move on to the next instance.<\/p>\n<p>In this function, <i>Actor.create<\/i>, we see that the <i>actorTemplate<\/i> is copied into the variable <i>local1. <\/i>The variable later has the <i>hActor<\/i> member set by saving the return value of the function <i>LoadActor<\/i>. From the source for <i>LoadActor<\/i> found in <i><a href=\"https:\/\/github.com\/residualvm\/residualvm\/blob\/master\/engines\/grim\/lua_v1_actor.cpp#L37\">engines\/grim\/lua_v1_actor.cpp<\/a><\/i> (line 37), we can see that this function creates a new Actor, and therefore, the type for <i>hActor<\/i> is most likely an Actor. While this might seem a bit obvious, when things are less obvious, you&#8217;ll still follow the same basic steps.<\/p>\n<p>With the knowledge that this variable is an Actor object, we can improve our translated code by naming the variable that we&#8217;ve saved the 1st parameter <i>actorObj<\/i>. We can also continue through the code and simplify functions that use this a parameter. It is also a good idea to compare other uses of Actor objects to see if already rewritten code matches what has been found so far. In this case, we see a call to lua_isuserdata and lua_tag:<\/p>\n<figure id=\"attachment_39\" aria-describedby=\"caption-attachment-39\" style=\"width: 468px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Userdata_and_Tag.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-39\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Userdata_and_Tag.png\" alt=\"\" width=\"468\" height=\"417\" srcset=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Userdata_and_Tag.png 468w, https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/Userdata_and_Tag-300x267.png 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/a><figcaption id=\"caption-attachment-39\" class=\"wp-caption-text\">lua_userdata and lua_tag<\/figcaption><\/figure>\n<p>The call to <i>lua_isuserdata<\/i> is checking that the 1st parameter contains an object with the type <i>UserData<\/i>. If the 1st parameter doesn&#8217;t have a <i>UserData<\/i> object, the whole function will just return. In the second box, the call to <i>lua_tag<\/i> is comparing the UserData&#8217;s tag with the number 52544341h. While this might seem like a random number, if we interpret this value as a string of four characters, they spell out &#8216;RTCA&#8217;, or &#8216;ACTR&#8217; in little endian.<\/p>\n<p>Before we continue, let&#8217;s look at what we mean by Lua UserData and tags. In the <a href=\"http:\/\/wiki.residualvm.org\/index.php\/GrimE\">ResidualVM wiki<\/a> we see that in the modified version of Lua used in this engine, variables are saved in a pool and are identified by their pool id number and tagged with an identifier. The id number is used to retrieve the data from the pool, while the tag is used to identify the type of the object. Going back to <i><a href=\"https:\/\/github.com\/residualvm\/residualvm\/blob\/master\/engines\/grim\/lua_v1_actor.cpp#L37\">engines\/grim\/lua_v1_actor.cpp<\/a><\/i>, when the engine loads the Actor, it creates a new Actor object instance, then adds this instance into the pool. It is also applying the tag using a macro: <i>MKTAG<\/i>.<\/p>\n<p>So, applying this information, we interpret these two lines of code as checking to see if the variable is <i>UserData<\/i>, and if so, does it have the tag &#8216;ACTR&#8217;. If not, the code will return. This bit of assembly can now be converted into C++:<\/p>\n<figure id=\"attachment_40\" aria-describedby=\"caption-attachment-40\" style=\"width: 527px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/actorObj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-40\" src=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/actorObj.png\" alt=\"\" width=\"527\" height=\"105\" srcset=\"https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/actorObj.png 527w, https:\/\/blogs.scummvm.org\/josejx\/wp-content\/uploads\/sites\/23\/2014\/03\/actorObj-300x60.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/a><figcaption id=\"caption-attachment-40\" class=\"wp-caption-text\">The C++ code, translated from assembly<\/figcaption><\/figure>\n<p>To this point, we haven&#8217;t really added anything to the project yet since this code had already been worked out by one of the previous developers. In the next post, we&#8217;ll start working on filling in the missing parts of the code with the information that we&#8217;ve learned so far.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continued from the previous entry In this post, we&#8217;ll be focusing on variables, function calls and the structure of the decompiled function, SetActorLocalAlpha. In the previous entry, we examined the Lua script that calls this function and identified its arguments. Applying that knowledge to the disassembled code indicates that the first 4 calls to lua_lua2C [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":3,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":41,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/36\/revisions\/41"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}