{"id":4482,"date":"2016-08-15T22:52:09","date_gmt":"2016-08-16T05:52:09","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/?p=4482"},"modified":"2016-08-25T08:22:21","modified_gmt":"2016-08-25T15:22:21","slug":"approaches-to-modifying-game-resource-files","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/approaches-to-modifying-game-resource-files\/","title":{"rendered":"Approaches To Modifying Game Resource Files"},"content":{"rendered":"<p>I have been assisting <a href=\"http:\/\/multimedia.cx\/eggs\/subtitling-sierra-vmd-files\/\">The Translator<\/a> in the translation of another mid-1990s adventure game. This one isn&#8217;t quite as multimedia-heavy as the last title, and the challenges are a bit different. I wanted to compose this post in order to describe my thought process and mental model in approaching this problem. Hopefully, this will help some others understand my approach since what I&#8217;m doing here often appears as magic to some of my correspondents.<\/p>\n<p><strong>High Level Model<\/strong><br \/>\nAt the highest level, it is valuable to understand the code and the data at play. The code is the game&#8217;s engine and the data refers to the collection of resources that comprise the game&#8217;s graphics, sound, text, and other assets.<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/game-engine-high-level.png\" alt=\"High-level game engine model\" width=\"378\" height=\"225\" class=\"aligncenter size-full wp-image-4485\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/game-engine-high-level.png 378w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/game-engine-high-level-300x179.png 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><br \/>\n<em>Simplistic high-level game engine model<\/em><br \/>\n<\/center><\/p>\n<p>Ideally, we want to change the data in such a way that the original game engine adopts it as its own because it has the same format as the original data. It is very undesirable to have to modify the binary engine executable in any way.<\/p>\n<p><strong>Modifying The Game Data Directly<\/strong><br \/>\nHow to modify the data? If we modify the text strings for the sake of language translation, one approach might be to search for strings within the game data files and change them directly. This model assumes that the text strings are stored in a plain, uncompressed format. Some games might store these strings in a text format which can be easily edited with any text editor. Other games will store them as binary data.<br \/>\n<!--more--><\/p>\n<p>In the latter situation, a game hacker can scan through data files with utilities like Unix &#8216;strings&#8217; to find the resources with the desired strings. Then, use a hex editor to edit the strings directly. For example, change &#8220;Original String&#8221;&#8230;<\/p>\n<pre>\r\n0098F800   00 00 00 00  00 00 00 4F  72 69 67 69  6E 61 6C 20  .......Original \r\n0098F810   53 74 72 69  6E 67 00 00  00 00 00 00  00 00 00 00  String..........\r\n<\/pre>\n<p>&#8230;to &#8220;Short String&#8221; and pad the difference in string lengths using spaces (0x20):<\/p>\n<pre>\r\n0098F800   00 00 00 00  00 00 00 53  68 6F 72 74  20 53 74 72  .......Short Str\r\n0098F810   69 6E 67 20  20 20 00 00  00 00 00 00  00 00 00 00  ing   ..........\r\n<\/pre>\n<p>This has some obvious problems. First, translated strings need to be of equal our smaller length compared to the original. What if we want to encode &#8220;Much Longer String&#8221;?<\/p>\n<pre>\r\n0098F800   00 00 00 00  00 00 00 4D  75 63 68 20  4C 6F 6E 67  .......Much Long\r\n0098F810   65 72 20 53  74 72 00 00  00 00 00 00  00 00 00 00  er Str..........\r\n<\/pre>\n<p>It won&#8217;t fit. The second problem pertains to character set limitations. If the font in use was only designed for ASCII, it&#8217;s going to be inadequate for expressing nearly any other language.<\/p>\n<p>So a better approach is needed. <\/p>\n<p><strong>Understanding The Data Structures<\/strong><br \/>\nAn alternative to the approach outlined above is to understand the game&#8217;s resources so they can be modified at a deeper level. Here&#8217;s a model to motivate this investigation:<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/resource-archive-model.png\" alt=\"Model of the game resource archive model\" width=\"203\" height=\"339\" class=\"size-full wp-image-4489\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/resource-archive-model.png 203w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/resource-archive-model-180x300.png 180w\" sizes=\"auto, (max-width: 203px) 100vw, 203px\" \/><br \/>\n<em>Model of the game resource archive format<\/em><br \/>\n<\/center><\/p>\n<p>This is a very common layout for such formats: there is a file header, a sequence of resource blocks, and a trailing index which describes the locations and types of the foregoing blocks.<\/p>\n<p>What use is understanding the data structures? In doing so, it becomes possible to write new utilities that disassemble the data into individual pieces, modify the necessary pieces, and then reassemble them into a form that the original game engine likes.<\/p>\n<p>It&#8217;s important to take a careful, experimental approach to this since mistakes can be ruthlessly difficult to debug (unless you relish the thought of debugging the control flow through an opaque DOS executable). Thus, the very first goal in all of this is to create a program that can disassemble and reassemble the resource, thus creating an identical resource file. This diagram illustrates this complex initial process:<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/resource-copy.png\" alt=\"Rewriting the game resource file\" width=\"454\" height=\"346\" class=\"aligncenter size-full wp-image-4491\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/resource-copy.png 454w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/resource-copy-300x229.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><br \/>\n<em>Rewriting the game resource file<\/em><br \/>\n<\/center><\/p>\n<p>So, yeah, this is one of the most complicated &#8220;copy file&#8221; operations that I can possibly code. But it forms an important basis, since the next step is to carefully replace one piece at a time.<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/modify-game-resource.png\" alt=\"Modifying a specific game resource\" width=\"540\" height=\"221\" class=\"aligncenter size-full wp-image-4493\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/modify-game-resource.png 540w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2016\/08\/modify-game-resource-300x123.png 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><br \/>\n<em>Modifying a specific game resource<\/em><br \/>\n<\/center><\/p>\n<p>This diagram shows a simplistic model of a resource block that contains a series of message strings. The header contains pointers to each of the strings within the block. Instead of copying this particular resource block directly to the new file, a proposed modification utility will intercept it and rewrite the entire thing, writing new strings of arbitrary length and creating an adjusted header which will correctly point to the start of each new string. Thus, translated strings can be longer than the original strings.<\/p>\n<p><strong>Further Work<\/strong><br \/>\nExploiting this same approach, we can intercept and modify other game resources including fonts, images, and anything else that might need to be translated. I will explore specific examples in a later blog post.<\/p>\n<p><strong>Followup<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/multimedia.cx\/eggs\/translating-return-to-ringworld\/\">Translating Return to Ringworld<\/a>, in which I apply the ideas expressed in this post.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Want to hack a game but don&#8217;t know how to start? This post can help you develop a mental model for approaching the task<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-4482","post","type-post","status-publish","format-standard","hentry","category-game-hacking"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/4482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=4482"}],"version-history":[{"count":14,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/4482\/revisions"}],"predecessor-version":[{"id":4513,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/4482\/revisions\/4513"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=4482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=4482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=4482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}