Skip to content

Combine MiscTablesModule and ExpandedTablesModule#326

Open
Supremekirb wants to merge 3 commits into
pk-hack:masterfrom
Supremekirb:misc-expanded-tables-merge-but-fixed
Open

Combine MiscTablesModule and ExpandedTablesModule#326
Supremekirb wants to merge 3 commits into
pk-hack:masterfrom
Supremekirb:misc-expanded-tables-merge-but-fixed

Conversation

@Supremekirb
Copy link
Copy Markdown

This PR combines MiscTablesModule and ExpandedTablesModule by implementing the expansion features of the latter into the former. ExpandedTablesModule no longer exists; the new combination retains the MiscTablesModule name.

Under the hood, things are similar to ExpandedTablesModule, but if no list of pointers to patch is provided, then the table will be considered unexpandable, and regular MiscTablesModule behavior will apply.

Additionally, the FREE_RANGES attribute becomes a property that is calculated at runtime. This means that it is no longer necessary to manually calculate and hardcode the space freed up by expanding a table.

This PR doesn't add expansion capabilities to any new tables. That can come later.

Copy link
Copy Markdown
Contributor

@PhoenixBound PhoenixBound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to discuss ideas about the best way to handle "moving" files from one module to another, in the project upgrade process, and pruning out data for redundant modules from the Project.snake. In the meantime, here's what I'd suggest so far.

Thanks again for your work on this.

Comment on lines +72 to +74
if not patches: continue # non-expandable, doesn't free up space
table = eb_table_from_offset(offset)
ranges.append((from_snes_address(offset), from_snes_address(offset+table.size-1)))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less code is needed here if you leave out continue, and it's arguably clearer to read, too:

Suggested change
if not patches: continue # non-expandable, doesn't free up space
table = eb_table_from_offset(offset)
ranges.append((from_snes_address(offset), from_snes_address(offset+table.size-1)))
if patches:
# A non-empty list here means that the table is moved elsewhere.
# Mark the original table's address range as freeable.
table = eb_table_from_offset(offset)
ranges.append((from_snes_address(offset), from_snes_address(offset+table.size-1)))

Comment on lines +13 to +20
# this decorator is used to calculate the free ranges instead of hardcoding them.
# in 3.9 - 3.11, it's possible to combine @property and @classmethod, so we COULD just do that...
# however, for the sake for maintainability, a >3.11-style solution is here.
class classproperty:
def __init__(self, func):
self.fget = func
def __get__(self, instance, owner):
return self.fget(owner)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm misunderstanding something, you should be able to combine @staticmethod and @property, instead of using @classmethod.

This is the only spot in CoilSnake where FREE_RANGES is read from:

for free_range in module_class.FREE_RANGES:
and it's not called on an instance of the class

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been almost a year so I don't recall the details, but I remember this was giving me a headache. That means I probably landed on this for a reason, so a change would need to be tested

Comment on lines +109 to 119
# expanded table - recreate it with our new number of rows
if self.TABLE_OFFSETS[offset]:
log.debug("Reading {}.yml as an expanded table".format(table.name.lower()))
yml_rep = yml_load(f)
num_rows = len(yml_rep)
table.recreate(num_rows=num_rows)
table.from_yml_rep(yml_rep)
# unexpanded table - standard process
else:
table.from_yml_file(f)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick but code comments should apply to the state of the system at the point the comment “executes.” Try moving the comments into the bodies of the if and else:

if self.TABLE_OFFSETS[offset]:
    # expanded table - recreate it with our new number of rows
    log.debug("Reading {}.yml as an expanded table".format(table.name.lower()))
    # ...

I think it reads clearer that way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants