cxd4 / zs-flash

N64 flash RAM editor for "Legend of Zelda: Majora's Mask".
Creative Commons Zero v1.0 Universal
11 stars 3 forks source link

zs_data: Lots of repeated code. #2

Closed cxd4 closed 8 years ago

cxd4 commented 9 years ago

We have many functions looking just like this:

int totalday(int optc, char ** optv)
{
    signed long input;
    const size_t file_offset = 0x000018;

    if (optc < 2)
        return show32("totalday", file_offset);
    input = strtol(optv[1], NULL, 0);
    return sendx32(file_offset, input);
}

int player_character(int optc, char ** optv)
{
    unsigned long input;
    const size_t file_offset = 0x000020;

    if (optc < 2)
        return show8("player_character", file_offset);
    input = strtoul(optv[1], NULL, 0);
    return send8(file_offset, input);
}

... and the list goes on.

Combining all of the functions (one for each game data field in the save file) into one function is theoretically possible, though we will need to account for not only the similarities but also the few differences as well (size in bits, signedness, dynamics of the optc counter for certain functions that I made behave differently with required/optional arguments).

There may be some sort of object-oriented approach at this, but in C I know it can be done with an array of structs of some kind enlisting the properties of each game data field into each struct element of said array.

I don't think I'll ever fix this "issue", but felt like filing this issue anyway as a note.

Reason being: While consolidating the shared code makes program binary and source size smaller, whatever approach I use to do it might vary inversely against the flexibility of the functions. Having one single function reading an array of structures is very clean and maintainable, but it might lose maintainability over the power I have with making sure what I want to be done is done in a stable and well-defined manner. This is where having multiple functions with mostly repeated code can have some advantage over the ease of adding new, more complex save editor rules, over having a single function that tries to consolidate most of everything.

Maybe I can care for this issue if the program gets very big from having tens of editor commands.

cxd4 commented 8 years ago

It's not really an issue because it looks like these bodies can be morphed to take advantage of other features in the process. I need to implement detection of missing stray fairy and heart piece locations.