forked from Rhekar/CCCaster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_protocol
executable file
·138 lines (102 loc) · 4.13 KB
/
make_protocol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
# Library folder
DIR=lib
# Protocol match regex
REGEX="^(struct|class) [A-Za-z0-9]+ .+ public Serializable[A-Z]"
#######################################################################################################################
# Scan for all protocol classes, sorted alphabetically
grep --extended-regexp "$REGEX" "$@" \
| sed --regexp-extended 's/^.+\.hpp:[a-z]+ ([A-Za-z0-9]+) .+$$/\1,/' \
| sort \
| uniq \
> $DIR/tmp_all
# Use a sorted list of the current enums
grep --invert-match --fixed-strings ' // Deleted message' $DIR/ProtocolEnums.hpp \
| sort \
> $DIR/tmp_enums
# Check for changes to the new list of enums
diff $DIR/tmp_all $DIR/tmp_enums 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
echo Protocol changed
# Separate unchanged, new, and deleted messages.
# Deleted messages should be kept in the list of enums, to keep maintain numbering.
# When the number of messages grows too large, this list should be pruned, which will break protocol.
comm $DIR/tmp_enums $DIR/tmp_all \
| sed --regexp-extended 's/^([A-Za-z0-9]+,)$/\1 \/\/ Deleted message/' \
| sed --regexp-extended 's/^\t([A-Za-z0-9]+,)$/\1 \/\/ New message/' \
| sed --regexp-extended 's/^\t\t([A-Za-z0-9]+,)$/\1/' \
> $DIR/tmp_changes
# Filter out deleted messages
grep --fixed-strings ' // Deleted message' $DIR/tmp_changes \
| sed --regexp-extended 's/^([A-Za-z0-9]+,) \/\/.+$/\1/' \
| uniq \
> $DIR/tmp_deleted
# Filter out new messages
grep --fixed-strings ' // New message' $DIR/tmp_changes \
| sed --regexp-extended 's/^([A-Za-z0-9]+,) \/\/.+$/\1/' \
| uniq \
> $DIR/tmp_new
# Regenerate enums with commented deleted messages
rm -f $DIR/tmp_enums
# Iterate through each message
while read message
do
# Check if the message is deleted
grep > /dev/null "^$message$" $DIR/tmp_deleted
if [ $? -eq 0 ]; then
echo "$message // Deleted message" >> $DIR/tmp_enums
else
echo $message >> $DIR/tmp_enums
fi
done < "$DIR/ProtocolEnums.hpp"
# Append new messages at the end
cat $DIR/tmp_new >> $DIR/tmp_enums
# Check for changes to the final list of enums
diff $DIR/tmp_enums $DIR/ProtocolEnums.hpp 1> /dev/null 2>&1
# Regenerate protocol only if the final list is different
if [ $? -ne 0 ]; then
SHOULD_REGEN=1
fi
# Use the final list of enums
mv -f $DIR/tmp_enums $DIR/ProtocolEnums.hpp
fi
rm -f $DIR/tmp*
#######################################################################################################################
# Check if we should regenerate protocol
if [ "$SHOULD_REGEN" = "1" ] || [ ! -f "$DIR/Protocol.include.hpp" ] \
|| [ ! -f "$DIR/Protocol.inlineimpl.hpp" ] \
|| [ ! -f "$DIR/Protocol.switchdecode.hpp" ] \
|| [ ! -f "$DIR/Protocol.switchstring.hpp" ]; then
echo Regenerating protocol
grep --extended-regexp "$REGEX" "$@" \
| sed --regexp-extended \
's/^(.+\.hpp):[a-z]+ ([A-Za-z0-9]+) .+$$/#include "\1"/' \
| sort \
| uniq \
> $DIR/Protocol.include.hpp
rm -f $DIR/Protocol.inlineimpl.hpp
grep --extended-regexp "$REGEX" "$@" | grep --invert-match "no-clone" \
| sed --regexp-extended \
's/^(.+\.hpp):[a-z]+ ([A-Za-z0-9]+) .+$$/\
inline MsgPtr \2::clone() const { MsgPtr msg ( new \2 ( *this ) ); msg->invalidate(); return msg; }/' \
| sort \
| uniq \
>> $DIR/Protocol.inlineimpl.hpp
grep --extended-regexp "$REGEX" "$@" | grep --invert-match "no-type" \
| sed --regexp-extended \
's/^(.+\.hpp):[a-z]+ ([A-Za-z0-9]+) .+$$/\
inline MsgType \2::getMsgType() const { return MsgType::\2; }/' \
| sort \
| uniq \
>> $DIR/Protocol.inlineimpl.hpp
grep --extended-regexp "$REGEX" "$@" \
| sed --regexp-extended \
's/^.+\.hpp:[a-z]+ ([A-Za-z0-9]+) .+$$/case MsgType::\1: msg.reset ( new \1() ); break;/' \
| sort \
> $DIR/Protocol.switchdecode.hpp
grep --extended-regexp "$REGEX" "$@" \
| sed --regexp-extended \
's/^.+\.hpp:[a-z]+ ([A-Za-z0-9]+) .+$$/case MsgType::\1: return ( os << "\1" );/' \
| sort \
> $DIR/Protocol.switchstring.hpp
fi