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

C# Array<String> Export Support Enum(String) #51835

Merged
merged 2 commits into from
Sep 17, 2021

Conversation

magian1127
Copy link
Contributor

@magian1127 magian1127 commented Aug 18, 2021

()Y$(4G5$@ {F3RSM~{O2 H

asdasd

After searching for a long time and not finding anything relevant, I modified it myself.
So far I have tested it without problems. Testing in 3.x
搜索很久没找到相关的, 我就自己修改了一下.
目前测试下来没有遇到问题. 测试于3.x

@magian1127 magian1127 requested a review from a team as a code owner August 18, 2021 12:31
@YuriSizov YuriSizov added this to the 4.0 milestone Aug 18, 2021
@neikeq
Copy link
Contributor

neikeq commented Aug 20, 2021

Do we support enum strings at all in C#? Does this work already?:

[Export(PropertyHint.Enum, "A,B,C")]
string field;

If it does work, then I understand making it work for arrays as well.
If it doesn't work I don't know if it's a good idea to support it. What does it solve that's not possible with real enums?

@magian1127
Copy link
Contributor Author

magian1127 commented Aug 20, 2021

Do we support enum strings at all in C#? Does this work already?:

[Export(PropertyHint.Enum, "A,B,C")]
string field;

If it does work, then I understand making it work for arrays as well.
If it doesn't work I don't know if it's a good idea to support it. What does it solve that's not possible with real enums?

Yes already supported.
godot many places the parameters passed are string.
If you use an enumeration, you still need to convert it.
enum strings I've been using it for a while now, and I haven't encountered any bugs.

是,已经支持了.
godot 很多地方的传的参数是字符串.
如果用枚举,那还需要转换一下.
enum strings 我已经用了一段时间了,也没遇到什么BUG.

1
2

Comment on lines 2873 to 2879
MonoObject *attr = p_member->get_attribute(CACHED_CLASS(ExportAttribute));
if (PropertyHint(CACHED_FIELD(ExportAttribute, hint)->get_int_value(attr)) == PROPERTY_HINT_ENUM) {
r_hint_string = itos(elem_variant_type) + "/" + itos(PROPERTY_HINT_ENUM) + ":" + CACHED_FIELD(ExportAttribute, hintString)->get_string_value(attr);
} else {
// Format: type/hint:hint_string
r_hint_string = itos(elem_variant_type) + "/" + itos(elem_hint) + ":" + elem_hint_string;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you make it check that the element type is indeed String? Something like this:

Suggested change
MonoObject *attr = p_member->get_attribute(CACHED_CLASS(ExportAttribute));
if (PropertyHint(CACHED_FIELD(ExportAttribute, hint)->get_int_value(attr)) == PROPERTY_HINT_ENUM) {
r_hint_string = itos(elem_variant_type) + "/" + itos(PROPERTY_HINT_ENUM) + ":" + CACHED_FIELD(ExportAttribute, hintString)->get_string_value(attr);
} else {
// Format: type/hint:hint_string
r_hint_string = itos(elem_variant_type) + "/" + itos(elem_hint) + ":" + elem_hint_string;
}
bool preset_hint = false;
if (elem_variant_type == Variant::STRING) {
MonoObject *attr = p_member->get_attribute(CACHED_CLASS(ExportAttribute));
if (PropertyHint(CACHED_FIELD(ExportAttribute, hint)->get_int_value(attr)) == PROPERTY_HINT_ENUM) {
r_hint_string = itos(elem_variant_type) + "/" + itos(PROPERTY_HINT_ENUM) + ":" + CACHED_FIELD(ExportAttribute, hintString)->get_string_value(attr);
preset_hint = true;
}
}
if (!preset_hint) {
// Format: type/hint:hint_string
r_hint_string = itos(elem_variant_type) + "/" + itos(elem_hint) + ":" + elem_hint_string;
}

This way we also make sure not to do extra work if not needed. Additionally we could also avoid the _try_get_member_export_hint call right above this if not needed:

		PropertyHint elem_hint = PROPERTY_HINT_NONE;
		String elem_hint_string;

		ERR_FAIL_COND_V_MSG(elem_variant_type == Variant::NIL, -1, "Unknown array element type.");

		bool preset_hint = false;
		if (elem_variant_type == Variant::STRING) {
			MonoObject *attr = p_member->get_attribute(CACHED_CLASS(ExportAttribute));
			if (PropertyHint(CACHED_FIELD(ExportAttribute, hint)->get_int_value(attr)) == PROPERTY_HINT_ENUM) {
				r_hint_string = itos(elem_variant_type) + "/" + itos(PROPERTY_HINT_ENUM) + ":" + CACHED_FIELD(ExportAttribute, hintString)->get_string_value(attr);
				preset_hint = true;
			}
		}
		
		if (!preset_hint) {
			int hint_res = _try_get_member_export_hint(p_member, elem_type, elem_variant_type, /* allow_generics: */ false, elem_hint, elem_hint_string);

			ERR_FAIL_COND_V_MSG(hint_res == -1, -1, "Error while trying to determine information about the array element type.");

			// Format: type/hint:hint_string
			r_hint_string = itos(elem_variant_type) + "/" + itos(elem_hint) + ":" + elem_hint_string;
		}

		r_hint = PROPERTY_HINT_TYPE_STRING;

Copy link
Contributor Author

@magian1127 magian1127 Aug 20, 2021

Choose a reason for hiding this comment

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

I've changed it, just according to you.

@magian1127 magian1127 requested a review from neikeq August 20, 2021 14:32
@akien-mga akien-mga merged commit ab544a2 into godotengine:master Sep 17, 2021
@akien-mga
Copy link
Member

Thanks!

@magian1127 magian1127 deleted the patch1 branch September 17, 2021 15:25
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