Skip to content

feat(gametext): decode UTF-8 in plain-text .str files - #3676

Open
dogefly233 wants to merge 1 commit into
TheSuperHackers:mainfrom
dogefly233:feat/gametext-utf8-str
Open

dogefly233 wants to merge 1 commit into
TheSuperHackers:mainfrom
dogefly233:feat/gametext-utf8-str

Conversation

@dogefly233

@dogefly233 dogefly233 commented Nov 10, 2026 •

Copy link
Copy Markdown

Problem

In release builds the game prefers the plain-text Data\Generals.str over the compiled generals.csf (m_useStringFile = TRUE, g_strFile = "data/Generals.str"). That path is the most convenient one for translators and modders because it is plain text, but its conversion to wide characters, GameTextManager::translateCopy, copies exactly one byte per character:

*outbuf++ = *inbuf & 0x00FF;

Anything outside Latin-1 therefore cannot survive it. A UTF-8 Chinese .str, for example, renders as åä°æ... in the UI. This has been worked around for years by a runtime hook shipped as a Miles proxy DLL (mss32.dll); that workaround only works while the engine loads Miles, and it cannot apply to builds or ports that do not.

Change

  • Validate the whole string once: every non-ASCII byte must form a well-formed UTF-8 sequence, and at least one multi-byte sequence must be present.
  • When valid, decode the multi-byte sequences in translateCopy; otherwise keep the exact legacy byte copy, so existing ANSI strings keep their current output.
  • Non-BMP code points are written as a surrogate pair when WideChar is 16-bit.
  • No new includes or dependencies, and the code is VC6-compatible.

parseMapStringFile calls the same function, so map .str files benefit as well.

Compatibility

  • ASCII-only strings: identical output; the decoder is never entered.
  • ANSI/Latin-1 strings that are not valid UTF-8: identical output; validation fails and the legacy path runs.
  • A Latin-1 string that happens to be valid UTF-8 (for example ä) is now decoded as UTF-8. That is the intended reading for .str text, and distinguishing the two would require an explicit encoding setting; I can add one if you prefer.

Testing

  • Windows build with Data\Generals.str saved as UTF-8 (Chinese text): menu and label text render correctly. Verified locally, screenshots in the comment above.
  • The same file saved as ANSI/Western: text output is unchanged from before.
  • Existing English .str files still parse and display identically.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

translateCopy now unescapes text into a translation buffer and converts valid UTF-8 to wide characters. For invalid UTF-8, it copies each byte as an unsigned value. The function no longer masks ordinary or default-escape characters with 0x00FF.

Changes

UTF-8 text conversion

Layer / File(s) Summary
Unescape and convert text
Core/GameEngine/Source/GameClient/GameText.cpp
Adds and initializes a translation buffer. translateCopy unescapes text into the buffer, converts valid UTF-8 to wide characters, and uses unsigned-byte copying for invalid UTF-8. It removes the prior 0x00FF masking for ordinary and default-escape characters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Possibly related PRs

Suggested reviewers: xezon, bobtista

Merge Risk: Minimal · up to 12552

This change decodes UTF-8 in plain-text .str files and keeps the legacy behavior for ASCII and non-UTF-8 input. No concrete merge-blocking risk was identified. The PR's own test checklist is still unchecked, so those checks should be completed in the usual way before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: UTF-8 decoding for plain-text .str files in the GameText code. The changeset adds UTF-8 validation and decoding in GameTextManager::translateCopy, which matches the t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is specific and related to the changeset. It explains the problem with translateCopy copying one byte per character (the 0x00FF mask), and it describes the change: UTF-8 validation, de…
Full details: Title check

Explanation

The title states the main change: UTF-8 decoding for plain-text .str files in the GameText code. The changeset adds UTF-8 validation and decoding in GameTextManager::translateCopy, which matches the title. The title is short and specific, and it names the affected file type.

Full details: Description check

Explanation

The description is specific and related to the changeset. It explains the problem with translateCopy copying one byte per character (the 0x00FF mask), and it describes the change: UTF-8 validation, decoding of multi-byte sequences, the legacy fallback for ANSI strings, surrogate pairs for non-BMP code points when WideChar is 16-bit, and the VC6 compatibility. It also lists compatibility cases and testing status. It matches the GameText.cpp summary, so the check passes.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 789e3869-63d7-4aee-a516-185a989d86dc
Commits

Reviewing files that changed from the base of the PR and between e9acf8d and 12552c4.

Files selected for processing: 1

  • Core/GameEngine/Source/GameClient/GameText.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues remain.

Summary

Adds UTF-8 decoding to plain-text game and map strings through the existing shared helper.

  • Plain-text game strings now decode valid UTF-8.

dogefly233 accepts decoding legacy bytes that also form valid UTF-8 because distinguishing the encodings would require an explicit setting.

Reviews (3) · Last reviewed commit: "feat(gametext): decode UTF-8 in plain-te..." · Reviewed by Greptile

@dogefly233
dogefly233 force-pushed the feat/gametext-utf8-str branch from 781859c to e9acf8d Compare November 10, 2026 12:06
translateCopy now unescapes into a scratch buffer and converts the result with Utf8_To_Wide_Len/Utf8_To_Wide, falling back to the previous 1:1 byte cast when the text is not valid UTF-8, matching UnicodeString::translate.
@dogefly233
dogefly233 force-pushed the feat/gametext-utf8-str branch from e9acf8d to 12552c4 Compare November 12, 2026 05:24
@dogefly233

Copy link
Copy Markdown
Author

Testing update: I built this PR locally (VS 2022 Build Tools, win32 preset) and ran it against a Steam Zero Hour install with a UTF-8 Data\Generals.str.

Before (unpatched build, same .str file, same game data):
before

After (this PR):
after

Only GameText.cpp differs between the two builds. The window title shows the same difference (???? before, correct Chinese after), so the text is decoded before it reaches GDI rather than being fixed up at draw time.

Repro: drop a UTF-8 Generals.str next to the game (it takes precedence over generals.csf in release builds), run generalszh.exe, and compare the main menu.

@dogefly233

Copy link
Copy Markdown
Author

@bobtista thanks a lot for the suggestion — I have updated the code exactly as you described. translateCopy now unescapes into a scratch buffer and converts the result once with Utf8_To_Wide_Len / Utf8_To_Wide, falling back to the 1:1 byte cast when the text is not valid UTF-8, the same way UnicodeString::translate does it. The two hand-written helpers are gone, and the escape handling is unchanged.

I also built it locally (VS 2022 Build Tools, win32 preset) and tested with a UTF-8 Data\Generals.str against a Steam Zero Hour install; before/after screenshots are in the comment above. Really appreciate the pointer — it made the change much smaller and more consistent with the rest of the codebase.

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