Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: --list-unused-strings to list the unused strings #348

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions nml/grfstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ class Language:
@type strings: C{dict} of
"""

used_strings = []

def __init__(self, default):
self.default = default
self.langid = None
Expand Down Expand Up @@ -985,6 +987,9 @@ def get_string(self, string, lang_id):
assert string_id in self.strings
assert lang_id == self.langid or self.langid == DEFAULT_LANGUAGE

if string_id not in Language.used_strings:
Language.used_strings.append(string_id)

str_type = self.strings[string_id].get_type()
parsed_string = ""
if self.strings[string_id].gender is not None:
Expand Down Expand Up @@ -1291,3 +1296,10 @@ def read_lang_files(lang_dir, default_lang_file):
continue
parse_file(filename, False)
langs.sort()


def list_unused_strings():
for string in default_lang.strings:
if string in Language.used_strings:
continue
generic.print_warning(generic.Warning.GENERIC, 'String "{}" is unused'.format(string))
7 changes: 7 additions & 0 deletions nml/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def parse_cli(argv):
allow_extra_zoom=True,
allow_32bpp=True,
disable_palette_validation=False,
list_unused_strings=False,
)
opt_parser.add_option("-d", "--debug", action="store_true", dest="debug", help="write the AST to stdout")
opt_parser.add_option("-s", "--stack", action="store_true", dest="stack", help="Dump stack when an error occurs")
Expand Down Expand Up @@ -231,6 +232,9 @@ def parse_cli(argv):
dest="disable_palette_validation",
help="Disable palette validation for sprites",
)
opt_parser.add_option(
"--list-unused-strings", action="store_true", dest="list_unused_strings", help="List unused strings"
)

opts, args = opt_parser.parse_args(argv)

Expand Down Expand Up @@ -359,6 +363,9 @@ def main(argv):
)

input.close()

if opts.list_unused_strings:
grfstrings.list_unused_strings()
sys.exit(ret)


Expand Down
Loading