Skip to content

Commit

Permalink
Revamp the error/result code module. (nh-server#830)
Browse files Browse the repository at this point in the history
* Prepare for error code rewrite
-removed err.py
-removed nxerr.py
-edited kurisu.py to account for this

* Add results cog
It unifies `nxerr` and `err` together with a cleaner codebase.
Its supporting code properly parses errors whenever possible and is
mostly complete at this point. There are still some missing codes from
all consoles, but I plan on adding more as time goes on.

* Add types.py
This file is the backbone of the result codes built on top of it.
It provides the two basic types necessary for the result cog to function.

* Add ctr.py
This file contains all currently known 3DS error codes, and helper funcs to
get information about them.

* Add cafe.py
This file contains all currently known Wii U error codes, and helper funcs to
get information about them.

* Add nx.py
This file contains all currently known Switch error codes, and helper funcs
to get information about them.

* Rename files for less confusion
nx.py to switch.py
cafe.py to wiiu.py

* Update cog code to account for renamed files

* Add Wii U error code 115-5004.
Closes issue nh-server#825.

* Fix indentation of description embed field

* Add comments to most of the files to help explain what they do.
Also how to add new modules and error codes.

* Adjust some comments in the cog.

* Make a note that a number of 3DS result codes were left out.
This wasn't completely intentional, but adding literally everything
would have delayed this even longer than it's already taken (months of off and on work).
I will add them in eventually.

* Credit switchbrew, 3dbrew, and Atmosphere for much of the information.

* Address pyflakes's complaints.
They're actually valid for once. I missed a few things.

* Add meme support
Not bothering to obfuscate them anymore, because everyone knows about them already.

* Add several more missing 3DS error codes.
NIM-specifics are still NYI.

* Adjust ban result codes to have a flag instead
Keeps the descriptions much shorter.

* Add more missing Wii U results

* Add some missing nx support pages and errors

* Implement err2hex, hex2err
And also add a couple more errors...

* Fixup the input for hex2err & err2hex

* Prefer `is not None` when possible

* Support showing both hex and human-readable results in the embed

* Add basic support for Wii U hexadecimal error codes
-Removed err2hex for 3ds & wiiu, it's not possible to do this

* Revise QoL hex/err display logic

* Shorten an extremely long line of code

* Fix pyflakes issues

* Two changes
-Catch bad error or hex codes passed to err2hex and hex2err
-Simplify the err_disp logic as much as possible

* Remove debug prints

* Fix small bug in wiiu.get

Actually fix the bug

* Add support for suppressing err2hex unsupported messages.
It helps keep other code sane. Hopefully this is the LAST time I have to touch the error code display logic. I'm sick of this.

Also change the embed colour to the warning colour when the result is a ban.

* Add 3DS nim-specific error handling.
It feels like a hack but it is what it is.

* Add original notes from the nim-specific stuff in the old err cog
Edited slightly since the first line had redundancy.

* Don't need an f-string there

* Fix indentation

* Add a bit more info to one error code

* Reorder 3ds modules based on number

* Reorder Switch modules based on number

* wiiu: Add missing parenthesis, remove old TODO

* Fix various style issues (mainly spacing).
Ignored error about := spacing, because pep8 doesn't know what it wants.
Ignored line length limits, we don't follow that either.

* Remove excess newlines at end of files
Gedit has some kind of a bug in it, or it automatically adds newlines
regardless of what you do? Unsure, don't care.

* Remove unused and/or redundant things
  • Loading branch information
thedax authored Sep 27, 2020
1 parent b6df797 commit 7da7ef6
Show file tree
Hide file tree
Showing 8 changed files with 2,944 additions and 1,350 deletions.
522 changes: 0 additions & 522 deletions cogs/err.py

This file was deleted.

826 changes: 0 additions & 826 deletions cogs/nxerr.py

This file was deleted.

138 changes: 138 additions & 0 deletions cogs/results/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import discord
from discord.ext import commands

from . import switch, wiiu, ctr, types


class Results(commands.Cog):
"""
Parses game console result codes.
"""
def fetch(self, error):
if ctr.is_valid(error):
return ctr.get(error)
if wiiu.is_valid(error):
return wiiu.get(error)
if switch.is_valid(error):
return switch.get(error)

# Console name, module name, result, color
return None, None, None, types.WARNING_COLOR

def err2hex(self, error, suppress_error=False):
# If it's already hex, just return it.
if self.is_hex(error):
return error

# Only Switch is supported. The other two can only give nonsense results.
if switch.is_valid(error):
return switch.err2hex(error, suppress_error)

if not suppress_error:
return 'Invalid or unsupported error code format. \
Only Nintendo Switch XXXX-YYYY formatted error codes are supported.'

def hex2err(self, error):
# Don't bother processing anything if it's not hex.
if self.is_hex(error):
if ctr.is_valid(error):
return ctr.hex2err(error)
if wiiu.is_valid(error):
return wiiu.hex2err(error)
if switch.is_valid(error):
return switch.hex2err(error)
return 'This isn\'t a hexadecimal value!'

def fixup_input(self, user_input):
# Truncate input to 16 chars so as not to create a huge embed or do
# eventual regex on a huge string. If we add support for consoles that
# that have longer error codes, adjust accordingly.
user_input = user_input[:16]

# Fix up hex input if 0x was omitted. It's fine if it doesn't convert.
try:
user_input = hex(int(user_input, 16))
except ValueError:
pass

return user_input

def is_hex(self, user_input):
try:
user_input = hex(int(user_input, 16))
except ValueError:
return False
return True

def check_meme(self, err: str) -> str:
memes = {
'0xdeadbeef': 'you sure you want to eat that?',
'0xdeadbabe': 'i think you have bigger problems if that\'s the case',
'0x8badf00d': 'told you not to eat it'
}
return memes.get(err.casefold())

@commands.command(aliases=['nxerr', 'serr', 'err', 'res'])
async def result(self, ctx, err: str):
"""
Displays information on game console result codes, with a fancy embed.
0x prefix is not required for hex input.
Examples:
.err 0xD960D02B
.err D960D02B
.err 022-2634
.err 102-2804
.err 2168-0002
.err 2-ARVHA-0000
"""
err = self.fixup_input(err)
if (meme := self.check_meme(err)) is not None:
return await ctx.send(meme)

system_name, module_name, error, color = self.fetch(err)

if error:
if self.is_hex(err):
err_str = self.hex2err(err)
else:
err_str = self.err2hex(err, True)

err_disp = f'{err}{"/" + err_str if err_str else ""}'
embed = discord.Embed(title=f"{err_disp} ({system_name})")
embed.add_field(name="Module", value=module_name, inline=False)

if error.summary is not None:
embed.add_field(name="Summary", value=error.summary, inline=False)
if error.level is not None:
embed.add_field(name="Level", value=error.level, inline=False)

embed.add_field(name="Description", value=error.description, inline=False)

if error.support_url:
embed.add_field(name="Further information", value=error.support_url, inline=False)

if error.is_ban:
embed.add_field(
name="Console, account and game bans",
value="Nintendo Homebrew does not provide support \
for unbanning. Please do not ask for further assistance with this.")
embed.color = color if not error.is_ban else types.WARNING_COLOR
await ctx.send(embed=embed)
else:
await ctx.send(f'{ctx.author.mention}, the code you entered is \
invalid or is for a system I don\'t have support for.')

@commands.command(name='err2hex')
async def cmderr2hex(self, ctx, error: str):
error = self.fixup_input(error)
return await ctx.send(self.err2hex(error))

@commands.command(name='hex2err')
async def cmdhex2err(self, ctx, error: str):
error = self.fixup_input(error)
return await ctx.send(self.hex2err(error))


def setup(bot):
bot.add_cog(Results(bot))
Loading

0 comments on commit 7da7ef6

Please sign in to comment.