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

Quote and escape ConfigFile keys when necessary #52180

Merged
merged 1 commit into from
Aug 28, 2021

Conversation

timothyqiu
Copy link
Member

Unexpected things happen when keys of ConfigFile contain special characters. For example:

  • When the key is a=b, saving works fine. But it complains about invalid identifier when loading.

    ERROR: ConfigFile parse error at user://test.cfg:2: Unexpected identifier: 'b'..
       at: _parse (core/io/config_file.cpp:280)
    
  • When the key contains non-Ascii characters like Chinese, saving works fine. But it might be decoded incorrectly when loaded again. (静音 will become é��é�³.)

This PR encodes the keys with String::property_name_encode(). This is also how ResourceFormatSaverText encodes property names.

Test Script
extends SceneTree

const CONFIG_PATH := "user://test.cfg"

func _init():
	var config := ConfigFile.new()

	config.set_value("音频", "静音", 42)
	config.set_value("Audio", "a=b", 7)

	var err := config.save(CONFIG_PATH)
	assert(err == OK)

	config.clear()
	err = config.load(CONFIG_PATH)
	assert(err == OK)

	for section in config.get_sections():
		print(section)
		for key in config.get_section_keys(section):
			printt("", key, config.get_value(section, key))

	quit()

@timothyqiu timothyqiu added bug topic:core cherrypick:3.x Considered for cherry-picking into a future 3.x release labels Aug 28, 2021
@timothyqiu timothyqiu added this to the 4.0 milestone Aug 28, 2021
@timothyqiu timothyqiu requested a review from a team as a code owner August 28, 2021 05:11
@Calinou
Copy link
Member

Calinou commented Aug 28, 2021

Could you modify tests/test_config_file.h to add test cases for this in the same commit? Thanks in advance 🙂

@timothyqiu timothyqiu requested a review from a team as a code owner August 28, 2021 10:48
@timothyqiu
Copy link
Member Author

Done. Didn't notice that ConfigFile has a unit test file ;-)

Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Calinou Calinou merged commit 4d08a73 into godotengine:master Aug 28, 2021
@timothyqiu timothyqiu deleted the config-file-prop branch August 29, 2021 01:31
@akien-mga
Copy link
Member

Cherry-picked for 3.4.

@akien-mga akien-mga removed the cherrypick:3.x Considered for cherry-picking into a future 3.x release label Sep 14, 2021
@akien-mga
Copy link
Member

Cherry-picked for 3.3.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants