Repository navigation
Add an option to preserve value whitespace when loading .ini files - #9
Open
DeathFishAtEase wants to merge 1 commit into
Open
DeathFishAtEase wants to merge 1 commit into
DeathFishAtEase wants to merge 1 commit into
Conversation
The ini parser used by SadPencil.Ra2CsfFile (ini-parser-netstandard 2.5.2) trims the leading and the trailing whitespace characters of every value, and that fork exposes no configuration switch to turn the trimming off. As a result, a line like 'Value= a' was always loaded as 'a', so a .csf file whose label values start with spaces could not be generated. Add an advanced-mode option, off by default, that keeps the raw value of every line and restores it right after the official helper has loaded the file. The official helper still owns the header parsing, the label discovery and the validation, so the loaded structure is not affected at all.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The .csf format stores each label value as a length-prefixed character
sequence, so it performs no whitespace handling at all: a value starting with
spaces is perfectly valid, and it round-trips through
CsfFile.WriteCsfFile()->
CsfFile.LoadFromCsfFile()unchanged.This tool, however, cannot produce such a file from an .ini source. A line like
is always loaded as
a, so the generated .csf containsainstead ofa.This blocks mods that need leading spaces (indented multi-line strings, column
alignment in in-game dialogs and sidebars).
Relationship with engine-side support
This PR only solves the authoring half of the problem: it makes it possible
to author strings with leading/trailing whitespace and to have them survive
into the generated .csf file byte-for-byte.
The rendering half is a separate concern that this PR cannot address. The
vanilla game engine trims such whitespace when displaying strings, so enabling
the option below changes nothing visible in-game on its own.
Runtime support requires the engine extension in
Phobos-developers/Phobos#2479, which is what makes the preserved
whitespace render as intended.
In short:
Users should enable this option only if they ship that engine extension
too; otherwise the generated .csf differs from what the game actually displays.
Root cause
CsfFileIniHelper.LoadFromIniFile()parses the file withini-parser-netstandard2.5.2, which trims the leading and the trailingwhitespace of every parsed value. Upstream ini-parser exposes a
TrimPropertiesswitch for this, but that netstandard fork dropped it -- itsIniParserConfigurationhas noTrim*member at all, so the trimming cannotbe turned off from the outside.
Approach
Add an advanced-mode option, off by default, and only act when it is on:
CsfFileIniHelper.LoadFromIniFile()parse it as usual. The official helper stays in charge of the header
parsing/validation, the label discovery, the language and the key-case
correction, so the resulting structure is identical to before.
and restore those values. The scanner only ever overrides values of
labels that the helper already found, for exactly as many lines as the
helper reported, so it can neither add labels nor change the structure.
Keeping it opt-in avoids silently changing the output for existing users. With
the option off, the generated .csf files are byte-for-byte what the current
release produces. Given that the vanilla engine does not render these spaces
anyway, defaulting to off is also the safer choice for users who do not use
the engine extension.
Notes
because the .ini file is meant to be a lossless representation of the string
table.
values) because the .ini representation of this format never contains those
constructs -- the files are written by the library itself.
Testing
dotnet build Ra2CsfToolsGUI.sln -c Release: 0 warnings, 0 errors.SadPencil.Ra2CsfFile2.2.5 thatAddLabel("TEST", " a\n b")->WriteCsfFile()->LoadFromCsfFile()returns
" a\n b"unchanged, confirming the .csf format itself is notthe blocker.
enabled, an .ini file containing
Value= aproduces a .csf file whoselabel value starts with spaces; with the option disabled, the output is
unchanged. Values without leading/trailing whitespace are unaffected either
way.
Alternatives considered
Adding a
TrimProperties-like option toCsfFileOptionsinSadPencil.Ra2CsfFilewould let this workaround be removed entirely. Thatrequires upstream cooperation and a coordinated dependency bump, so this PR
keeps the change contained to this repository instead.