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

String encodings continue to be one of the hardest things #103

Merged
merged 3 commits into from
Jan 8, 2024

Conversation

ceejbot
Copy link
Owner

@ceejbot ceejbot commented Jan 8, 2024

String encodings continue to be one of the hardest things

When we get a string for an item name from the game, it might be
in one of several encodings:

  • ascii
  • Latin-1 1252, aka ISO-8859 most likely the 9 variation
  • UCS2, which is almost like UTF-16 LE except it's fixed width

We must take this varied input and turn it into valid UTF-8 strings
for Rust usage & logging, and for display in the HUD via Imgui. We
must also not disturb any already-valid strings by mangling them while
attempting to decode them into a modern encoding.

So, we do the following. First, use chardet::detect() to guess
at an encoding. If it's utf-8 already or one of the ISO-8859s,
we can decode immediately and return. If it is not one of those,
we make the assumption that it is UCS2. The string might be
almost-correctly detected as UTF-16le if it uses the second byte
for any characters, but if the characters it is holding are plain
old ascii, it'll be reported as ASCII and that is wrong. So we guess,
and then decode the UCS2.

Broke the string manglers out into their own file and wrote some
basic tests. Changed the helper wrappers to use cstr null-terminated
version for item name handling, since those come from the game with
null termination. If I discover the null termination is also
double-width I guess I'll have a bug to fix.

Put mcm-meta-helper to work on the task for which it was invented.

Added better logging for when fmtstr fails on huditems.

When we get a string for an item name from the game, it might be
in one of several encodings:
- ascii
- Latin-1 1252, aka ISO-8859 most likely the 9 variation
- UCS2, which is almost like UTF-16 LE except it's fixed width

We must take this varied input and turn it into valid UTF-8 strings
for Rust usage & logging, and for display in the HUD via Imgui. We
must also not disturb any already-valid strings by mangling them while
attempting to decode them into a modern encoding.

So, we do the following. First, use `chardet::detect()` to guess
at an encoding. If it's utf-8 already or one of the ISO-8859s,
we can decode immediately and return. If it is not one of those,
we make the _assumption_ that it is UCS2. The string might be
almost-correctly detected as UTF-16le if it uses the second byte
for any characters, but if the characters it is holding are plain
old ascii, it'll be reported as ASCII and that is wrong. So we guess,
and then decode the UCS2.

Broke the string manglers out into their own file and wrote some
basic tests. Changed the helper wrappers to use cstr null-terminated
version for item name handling, since those come from the game with
null termination. If I discover the null termination is also
double-width I guess I'll have a bug to fix.

Put mcm-meta-helper to work on the task for which it was invented.

Time to test on Windows now.
@ceejbot ceejbot merged commit 672bec4 into latest Jan 8, 2024
1 check passed
@ceejbot ceejbot deleted the ceej/encodings-again branch January 8, 2024 04:41
ceejbot added a commit that referenced this pull request Jan 8, 2024
When we get a string for an item name from the game, it might be
in one of several encodings:
- ascii
- Latin-1 1252, aka ISO-8859 most likely the 9 variation
- UCS2, which is almost like UTF-16 LE except it's fixed width

We must take this varied input and turn it into valid UTF-8 strings
for Rust usage & logging, and for display in the HUD via Imgui. We
must also not disturb any already-valid strings by mangling them while
attempting to decode them into a modern encoding.

So, we do the following. First, use `chardet::detect()` to guess
at an encoding. If it's utf-8 already or one of the ISO-8859s,
we can decode immediately and return. If it is not one of those,
we make the _assumption_ that it is UCS2. The string might be
almost-correctly detected as UTF-16le if it uses the second byte
for any characters, but if the characters it is holding are plain
old ascii, it'll be reported as ASCII and that is wrong. So we guess,
and then decode the UCS2.

Broke the string manglers out into their own file and wrote some
basic tests. Changed the helper wrappers to use cstr null-terminated
version for item name handling, since those come from the game with
null termination. If I discover the null termination is also
double-width I guess I'll have a bug to fix.

Put mcm-meta-helper to work on the task for which it was invented.

Added better logging for when fmtstr fails on huditems.
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.

1 participant