From 2a553ed9af337bf74efb5974b4d8e94e50249fe2 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Tue, 15 Nov 2022 09:36:44 +0100 Subject: [PATCH] First working version of cli packet generator --- scripts/bin/twnet | 27 ++++++++++++++++++++++++++- scripts/packet_template.rb | 3 +-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/scripts/bin/twnet b/scripts/bin/twnet index 32cf21d..565a29f 100755 --- a/scripts/bin/twnet +++ b/scripts/bin/twnet @@ -355,11 +355,36 @@ function action_generate() { hashs=' {' for field_name in "${!fields[@]}" do - hashs+="\n $field_name = @$field_name," + hashs+="\n $field_name: @$field_name," done hashs="${hashs::-1}" hashs+="\n }" replace_line "$tmpfile" "{ foo: @foo," "${hashs:2}" + + local packs='' + for field_name in "${!fields[@]}" + do + packs+=" " + field_type="${fields[$field_name]}" + if [ "$field_type" == "raw" ] + then + packs+="Packer.pack_raw(@$field_name) +\n" + elif [ "$field_type" == "int" ] + then + packs+="Packer.pack_int(@$field_name) +\n" + else # string or other + packs+="Packer.pack_str(@$field_name) +\n" + fi + done + replace_line "$tmpfile" Packer.pack_int "${packs:2:-4}" + local destfile + destfile="lib/models/$(basename "$tmpfile")" + if [ -f "$destfile" ] + then + echo "Error: file already exists '$destfile'" + exit 1 + fi + mv "$tmpfile" "$destfile" } function parse_args() { diff --git a/scripts/packet_template.rb b/scripts/packet_template.rb index 8b435fc..7e0a75c 100644 --- a/scripts/packet_template.rb +++ b/scripts/packet_template.rb @@ -32,8 +32,7 @@ class PacketName # basically to_network # int array the SENDER sends to the RECEIVER def to_a - Packer.pack_int(@foo) + - Packer.pack_str(@bar) + Packer.pack_int(@foo) + Packer.pack_str(@bar) end def to_s