gamedig / rust-gamedig

Game Server Query Library.
https://crates.io/crates/gamedig
MIT License
36 stars 10 forks source link

Reduce game implementation repetition #122

Closed Douile closed 9 months ago

Douile commented 9 months ago

Using macros we can reduce the amount of code we have to write for each game.

Further reduction is likely possible if we wrote a macro that made use of the generic implementation and called the game query function macro generation as well as the game definition generation macro from the same place. But I think a ~500 line reduction is a good start.

These macros have the added nicety of generating doc comments for all the game query function based on the protocol, game name/protocol version, and default port.

Closes #120.

CosminPerRam commented 9 months ago

Great PR, very happy to see that adding a game can be simplified drastically, we can now also add query_with_timeout and the other good stuff.

Further reduction is likely possible if we wrote a macro that made use of the generic implementation and called [...]

For sure, that can be done eventually.

But I think (and want to) further reduce code in a simple way: Remove game files and have a separate file like valve that defines all those games and modify the macro to include a namespace of their id mod game_id { //... }, as I said I don't really know macros, but I think that's possible.

Douile commented 9 months ago

For sure, that can be done eventually. I think (and want to) further reduce code in a simple way: Remove game files and have a separate file like valve that defines all those games and modify the macro to include a namespace of their id mod game_id { //... }, as I said I don't really know macros, but I think that's possible.

That's definitely possible, great idea.

Douile commented 9 months ago

A bit of python magic later, and I've moved them into protocol-specific files.

How I verified no games were deleted ```shell // Get old games version $ git clone https://github.com/gamedig/rust-gamedig.git // Extract game module names $ rg "pub mod" ./rust-gamedig/src/games/mod.rs > games.txt // Filter game mods to just the module name $ cut -d' ' -f3 games.txt | sed 's/;$//' > games-stripped.txt // Tell ripgrep to ignore the definitions file $ echo "definitions.rs" > ignore // Validate all names are in the new build somewhere $ for game in $(cat games-stripped.txt); do rg --ignore-file ignore "$game" ~/Code/Github/rust-gamedig/src/games >/dev/null || echo "Deleted $game" done ```