{"id":82,"date":"2014-04-06T17:59:34","date_gmt":"2014-04-06T17:59:34","guid":{"rendered":"https:\/\/blogs.scummvm.org\/josejx\/?p=82"},"modified":"2022-05-21T17:59:50","modified_gmt":"2022-05-21T17:59:50","slug":"making-patches-with-diffr-and-patchr","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/josejx\/2014\/04\/06\/making-patches-with-diffr-and-patchr\/","title":{"rendered":"Making Patches with diffr and patchr"},"content":{"rendered":"<p>ResidualVM comes with two tools we can use to modify scripts while running a game in the engine. These tools are <i>diffr<\/i> and <i>patchr<\/i>, and can be found in the <a href=\"https:\/\/github.com\/residualvm\/residualvm-tools\" target=\"_blank\" rel=\"noopener\">residual-tools<\/a> repository.<\/p>\n<p><i>diffr<\/i> takes two files, and prepares a binary difference patch between them, outputting the result into a <i>.patchr<\/i> file. <i>patchr<\/i> takes the original file and the patch, and outputs the patched file. Let&#8217;s say we&#8217;ve made a fix to the game logic that we&#8217;d like to ship with ResidualVM. In this example, I&#8217;m replacing the call to <i>ReadRegistryValue<\/i> in the EMI Demo so that instead of trying to read the value <i>SfxVolume<\/i> from a non-existent registry and segfaulting (<a href=\"http:\/\/diggingemi.blogspot.com\/2014\/03\/detour-fixing-segfault.html\" target=\"_blank\" rel=\"noopener\">remember that bug?<\/a>) the script instead reads from <i>system_prefs.cfg.sfxvolume<\/i>, which is implemented properly in ResidualVM for EMI.<\/p>\n<p>Once we have made the desired change to our script, the first step is to compile a new script. If we can, it&#8217;s desirable to base this script on the previous script to reduce the amount of binary changes for <i>patchr<\/i>. Note that the luac referenced here is the one distributed with <i>residualvm-tools<\/i>, a regular copy won&#8217;t work!<\/p>\n<ul>\n<li>luac -b &lt;old compiled script&gt; -o &lt;new file output&gt; &lt;script to compile&gt;<\/li>\n<\/ul>\n<p>With the compiled script, we&#8217;ll now ask diffr to make a patchr file for us, describing the binary differences between the two files:<\/p>\n<ul>\n<li>diffr &lt;old compiled script&gt; &lt;new compiled script&gt; &lt;patchr file&gt;<\/li>\n<\/ul>\n<p>Now, we need to add this to the file containing the patches used at runtime. In the source directory, there is a folder that contains the existing patches. It can be found here for EMI&#8217;s patches: <i>dists\/engine-data\/patches\/emi<\/i>. Once we have generated a patchr file, move it into this directory. Then, from <i>dists\/engine-data<\/i> we&#8217;ll generate a new copy of <i>residualvm-emi-patch.m4b<\/i> containing our patch:<\/p>\n<ul>\n<li>mklab &#8211;emi patches\/emi residualvm-emi-patch.m4b<\/li>\n<\/ul>\n<p>Now, what happens if someone has already made a patchr for the file we&#8217;d like to change? In this case, we&#8217;ll patch the binary file using patchr, then delua it like normal so we can make our changes:<\/p>\n<ul>\n<li>patchr &lt;old file&gt; &lt;new output file&gt; &lt;patchr file&gt;<\/li>\n<\/ul>\n<p>Remember that when building the new file and generating the patchr file, the old file is the original file from EMI, not the one we generated using patchr.<\/p>\n<p>In this way, we preserved the changes already made, and can safely overwrite the patchr file when making our modified <i>residualvm-emi-patch.m4b<\/i>.<\/p>\n<p>As an added bonus, I&#8217;ve also written a <a href=\"https:\/\/github.com\/JoseJX\/random_tools\/blob\/master\/patchr_diff\" target=\"_blank\" rel=\"noopener\">script<\/a> to let you get a regular old patch file from a patchr file. Simply pass the file to be patched and the patchr file, and the script will spit out a text patch that describes the changes made. Obviously, this only works if your patchr file is patching a Lua script. Here&#8217;s how to use it:<\/p>\n<ul>\n<li>patchr_diff _options.lua _options.lua_1.patchr<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>ResidualVM comes with two tools we can use to modify scripts while running a game in the engine. These tools are diffr and patchr, and can be found in the residual-tools repository. diffr takes two files, and prepares a binary difference patch between them, outputting the result into a .patchr file. patchr takes the original [&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-82","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/82","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=82"}],"version-history":[{"count":2,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":84,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/posts\/82\/revisions\/84"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/josejx\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}