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

gamenet: Add fields to SvGameMsg #90

Closed
wants to merge 2 commits into from

Conversation

ChillerDragon
Copy link
Contributor

@ChillerDragon ChillerDragon commented Feb 12, 2024

Para I-III are not i32 at all times. Some of them might be the team enum. But that depends on the gamemsg field and I do not think the current code supports such logic. I would love to add some if statements to the generated rust code but it seems wrong. Not sure how complex the generation code would be if it would support if statements or field relations. Also might look ugly in the json. So I went with the raw i32 for now.

If you need a refresher on what this message does I fully documented it here https://chillerdragon.github.io/teeworlds-protocol/07/game_messages.html#NETMSGTYPE_SV_GAMEMSG

Here is how it looks in action used by the wireshark dissector.

Before:

Screenshot from 2024-02-12 10-50-54

After:

Screenshot from 2024-02-12 10-50-11

{"name": ["para", "I"], "type": {"kind": "optional", "inner": {"kind": "int32"}}},
{"name": ["para", "II"], "type": {"kind": "optional", "inner": {"kind": "int32"}}},
{"name": ["para", "III"], "type": {"kind": "optional", "inner": {"kind": "int32"}}}
],
Copy link
Owner

Choose a reason for hiding this comment

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

Even this JSON is supposed to be generated by gamenet/generate/loader.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean editing this line?

NetMessage("Sv_GameMsg", []),

That would differ from upstream then 🤔

https://github.com/teeworlds/teeworlds/blob/a1911c8f7d8458fb4076ef8e7651e8ef5e91ab3e/datasrc/network.py#L383

Copy link
Owner

Choose a reason for hiding this comment

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

No, editing this description in loader.py, and hopefully also pull-requesting something to upstream to fix this incorrect protocol description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might have no arguments so not sure how incorrect it is.

Copy link
Owner

Choose a reason for hiding this comment

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

It's incorrect in the sense that it apparently doesn't do what you want it to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay so this one should be added for sure NetIntRange("m_GameMsg", 'GAMEMSG_TEAM_SWAP', 'GAMEMSG_GAME_PAUSED')

But if the parameters are also added there then that would not always be correct. The teeworlds py protocol spec does not support optional types. So they just left it empty and made it optional in c++.

gamenet/generate/spec/teeworlds-0.7.5.json Outdated Show resolved Hide resolved
_p.finish(warn);
result
}
pub fn encode<'d, 's>(&self, mut _p: Packer<'d, 's>) -> Result<&'d [u8], CapacityError> {
assert!(self.para_I.is_some());
assert!(self.para_II.is_some());
assert!(self.para_III.is_some());
Copy link
Owner

Choose a reason for hiding this comment

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

As you can see, the code does not allow for actually optional fields yet, only fields that can be omitted for backward compatibility.

Perhaps the optional code could be replaced by something that allows default values to be specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would consider sending some default like 0 instead of nothing wrong in that case. Those parameters are not always sent. Unused defaults seem misleading at best and break the protocol at worst. Doesn't rust have an optional type? How is the default supposed to be represented in the json?

Copy link
Owner

Choose a reason for hiding this comment

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

I would consider sending some default like 0 instead of nothing wrong in that case. Those parameters are not always sent. Unused defaults seem misleading at best and break the protocol at worst.

I tend to agree.

Doesn't rust have an optional type?

Yes, it does.

How is the default supposed to be represented in the json?

Currently, the JSON description does not support optional fields. It only supports fields that can be omitted for backwards compatibility. If we want to accurately support this 0.7 message, we'd first have to figure out a way to represent this in the JSON and then adapt all users of this JSON to deal with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we'd first have to figure out a way to represent this in the JSON

Yes that is what I am asking about. I will not come up with the format if you have your ideas anyways. I have no good idea so that one is on you.

and then adapt all users of this JSON to deal with it.

By users do you mean humans with their libtw2 based projects? Or parts of the code in libtw2 that consume the json? Either way do you have a list of those users?

Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps we could specify that this is a repetitions of i32s to the end of the message. That sounds kinda sane to me. Perhaps "kind":"array-until-end"?

Parts of libtw2 that consume the JSON, wireshark dissector and code generation for the gamenet crates, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I like the idea sounds good. I sadly already spent way more time on this pr than initially planned. And it turned into something that got way bigger than I have motivation to work on.

I am closing this pr now so it does not get stale.

I would appreciate if one day you add support for the sv game msg to the dissector. Until then I will just use my branch that added this message because it works wonderfully for my use case which is debugging traffic including this very game message.

@heinrich5991
Copy link
Owner

Some of them might be the team enum. But that depends on the gamemsg field and I do not think the current code supports such logic.

Correct.

I would love to add some if statements to the generated rust code but it seems wrong.

Yea, that would be wrong. The JSON is supposed to declaratively describe the protocol.

Not sure how complex the generation code would be if it would support if statements or field relations. Also might look ugly in the json. So I went with the raw i32 for now.

I agree, I think it'd be better to develop protocols where fields don't depend on values. Makes it easier to write parsers.

@ChillerDragon
Copy link
Contributor Author

By the way is this command hidden in some generate_all script?

cd gamenet/generate
python3 serialize.py --version 0.7 raw/teeworlds-0.7.5.py > spec/teeworlds-0.7.5.json

It is quite tedious to type. Shouldn't that be included in here?

https://github.com/heinrich5991/libtw2/blob/b9286674da94d3d45b9c10ffce517af394e2d58c/gamenet/generate_all

@heinrich5991
Copy link
Owner

It is quite tedious to type. Shouldn't that be included in here?

It's kinda intentionally omitted because it's not supposed to be run regularly. I agree that this makes a bad contributor experience. Not sure what to do about it. :(

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