UnderminersTeam / UndertaleModTool

The most complete tool for modding, decompiling and unpacking Undertale (and other Game Maker: Studio games!)
GNU General Public License v3.0
1.07k stars 206 forks source link

Feature: Have option to reduce redundant code #1466

Open OliveIsAWord opened 10 months ago

OliveIsAWord commented 10 months ago

Which component should be improved?

Decompiler

Describe your feature suggestion in more detail

I found this tool when trying to extract assets from Fazbear Entertainment: Storage and was impressed with the capabilities of the decompilation tool. However, the decompiled code was filled with dead code such as empty if blocks, redundant with blocks, and values assigned to a temporary variable before being immediately read once and then forgotten. This becomes a major problem for larger Codes, both in decompilation performance and readability of the decompiled code.

In the Code gml_Object_Obj_tbmover_Alarm_1 (which controls Toy Bonnie's movement), the disassembly contains 1263 SLoC, but the decompilation takes multiple seconds and produces over 280,000 (!!) SLoC, many sections of which are indented so far that the lines wrap around the editor. I suspect that most of this indentation and perhaps a majority of these lines could be removed without major overhauls or major feature additions to decompilation.

As a shorter example, the Code gml_Object_Obj_MenuBG_Alarm_1 currently decompiles to this:

var __b__;
with (Obj_NightSetter)
{
    __b__ = action_if_variable(SAVE, 0, 0)
    if __b__
    {
    }
    if __b__
        scr_loadgame()
    with (Obj_NightSetter)
    {
        __b__ = action_if_variable(SAVE, 1, 0)
        if __b__
        {
        }
        if __b__
            scr_savegame()
    }
}

If the dead code mentioned above could be algorithmically removed, this snippet might instead decompile to this much nicer code:

with (Obj_NightSetter)
{
    if action_if_variable(SAVE, 0, 0)
        scr_loadgame()
    if action_if_variable(SAVE, 1, 0)
        scr_savegame()
}

It's entirely possible that I'm missing obvious code refactors, or any of my suggested refactors are incorrect; this tool has been my near only exposure to GML.

Dobby233Liu commented 10 months ago

FYI I think these stuff were generated automatically by gm from Drag and Drop code. Due to the excessive use of action_ functions

Jacky720 commented 4 months ago

I'd like to mark this as won't fix on account of it being an accurate decompilation that is necessary for a 1:1 rewrite. Any attempt at optimizing out empty drag'n'drop should be made optional if possible, IMO.