File: cmpgraph.pl

package info (click to toggle)
0ad 0.0.23.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,412 kB
  • sloc: cpp: 245,162; ansic: 200,249; javascript: 19,244; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (187 lines) | stat: -rw-r--r-- 4,945 bytes parent folder | download | duplicates (9)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/perl

# Generates a graph of the interdependencies between simulation components

use strict;
use warnings;

use File::Find;
use Data::Dumper;

my %implements;
my %implementedby;
my %subscribes;
my %queries;
my %posts;
my %native;

for (<../../simulation2/components/CCmp*>)
{
    next if /CCmpTest/;
    parse_ctype_cpp($_);
}

for (<../../../binaries/data/mods/public/simulation/components/*.js>)
{
    parse_ctype_js($_);
}

parse_helper_js("CommandQueue", "../../../binaries/data/mods/public/simulation/helpers/Commands.js");

#parse_ctype_design("components.txt");

# Add one that the parser misses
$posts{RangeManager}{RangeUpdate} = 1;

dump_stats();

use Data::Dumper; print Dumper \%queries;

dump_graph();
system("dot -Tpng -o components.png components.dot");

sub parse_ctype_cpp
{
    my ($fn) = @_;
    print "$fn ...\n";
    open my $f, $fn or die "can't open $fn: $!";
    my $cmp;
    if ($fn =~ /CCmpPathfinder/) { $cmp = 'Pathfinder'; } # because it's split into multiple .cpp files
    while (<$f>) {
        if (/class CCmp(\S+) : public ICmp(\S+)/) {
            $implements{$1} = $2;
            $implementedby{$2}{$1} = 1;
            $native{$1} = 1;
            $cmp = $1;
        } elsif (/Subscribe(Globally)?ToMessageType\(MT_(\S+)\)/) {
            $subscribes{$2}{$cmp} = 1;
        } elsif (/CmpPtr<ICmp(\S+)>/) {
            $queries{$cmp}{$1} = 1;
        } elsif (/^\s*CMessage(\S+)/) {
            $posts{$cmp}{$1} = 1;
        }
    }
}

sub parse_ctype_js
{
    my ($fn) = @_;
    print "$fn ...\n";
    open my $f, $fn or die "can't open $fn: $!";
    my $cmp;
    while (<$f>) {
        if (/^\s*function (\S+)\s*\(\)\s*\{\s*\}\s*$/) {
            $cmp = $1;
        } elsif ($cmp and /\s*$cmp\.prototype\.On(?:Global)?(\S+)\s*=/) {
            $subscribes{$1}{$cmp} = 1;
        } elsif (/Engine\.QueryInterface\(.*?,\s*IID_(\S+?)\)/) {
            $queries{$cmp}{$1} = 1;
        } elsif (/Engine\.RegisterComponentType\(IID_(\S+), "(\S+)", (\S+)\);/) {
            die unless $2 eq $cmp;
            die unless $3 eq $cmp;
            $implements{$cmp} = $1;
            $implementedby{$1}{$cmp} = 1;
        } elsif (/Engine\.(?:Post|Broadcast)Message\(\S+, MT_(\S+),/) {
            $posts{$cmp}{$1} = 1;
        }
    }
}

sub parse_helper_js
{
    my ($cmp, $fn) = @_;
    open my $f, $fn or die "can't open $fn: $!";
    while (<$f>) {
        if (/Engine\.QueryInterface\(.*?,\s*IID_(\S+)\)/) {
            $queries{$cmp}{$1} = 1;
        }
        # TODO: check for message sending
    }
}

sub parse_ctype_design
{
    my ($fn) = @_;
    open my $f, $fn or die "can't open $fn: $!";
    my $cmp;
    while (<$f>) {
        s/\s*#.*//;
        next unless /\S/;
        if (/^component (\S+)(?: : (\S+))?$/) {
            $implements{$1} = ($2 || $1);
            $implementedby{$2 || $1}{$1} = 1;
            $cmp = $1;
        } elsif (/^native$/) {
            $native{$cmp} = 1;
        } elsif (/^subscribe (\S+)$/) {
            $subscribes{$1}{$cmp} = 1;
        } elsif (/^query (\S+)$/) {
            $queries{$cmp}{$1} = 1;
        } elsif (/^post (\S+)$/) {
            $posts{$cmp}{$1} = 1;
        } else {
            die "Invalid input line: $_ in $fn.";
        }
    }
}

sub dump_graph
{
    open my $f, '>', 'components.dot' or die $!;
    print $f <<EOF;
digraph g {
    graph [ranksep=1 nodesep=0.1 fontsize=10 compound=true];
    node [fontsize=10];
    edge [fontsize=8];
EOF

#     for my $c (sort keys %implements) {
#         print $f "$c;\n";
#     }

    for my $i (sort keys %implementedby) {
        print $f "subgraph cluster_ifc_$i {\n";
        print $f "label=\"$i\";\n";
        for my $c (sort keys %{$implementedby{$i}}) {
            my $col = ($native{$c} ? "green" : "black");
            print $f "$c [color=$col];\n";
        }
        print $f "}\n";
    }

    print $f qq{node [color=gray fontcolor=gray];\n};

    print $f qq{edge [color=blue fontcolor=blue];\n};

    for my $c (sort keys %queries) {
        next if $c eq 'GuiInterface' or $c eq 'CommandQueue'; # these make the graph messy
        for my $t (sort keys %{$queries{$c}}) {
            my $tc = (sort keys %{$implementedby{$t}})[0];
            print $f qq{$c -> $tc [lhead=cluster_ifc_$t];\n};
        }
    }

    print $f qq{edge [color=red fontcolor=red weight=0.9];\n};

    for my $c (sort keys %posts) {
        for my $m (sort keys %{$posts{$c}}) {
            for my $t (sort keys %{$subscribes{$m}}) {
                print $f qq{$c -> $t [label="$m"];\n};
            }
        }
    }

    print $f <<EOF;
}
EOF
}

sub dump_stats
{
    my ($native, $scripted) = (0, 0);
    for my $c (keys %implements) {
        if ($native{$c}) { ++$native; } else { ++$scripted; }
    }
    printf "Native components: %d\nScripted components: %d\nTotal components: %d\n", $native, $scripted, $native+$scripted;
    printf "Interfaces: %d\n", (scalar keys %implementedby);
}