1#!/usr/bin/perl
2#
3#   Copyright (C) 2007 Mark Wedel & Crossfire Development Team
4#
5#   This program is free software; you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation; either version 2 of the License, or
8#   (at your option) any later version.
9#
10#   This program is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14#
15#   You should have received a copy of the GNU General Public License
16#   along with this program; if not, write to the Free Software
17#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18#
19#   The author can be reached via e-mail to crossfire-devel@real-time.com
20#
21
22# $ARGV[0] is the source directory. We need to know it for out of tree builds.
23
24use strict;
25
26open(IN,"<$ARGV[0]/shared/newclient.h") || die("Can not open newclient.h file\n");
27open(OUT,">msgtypes.h") || die("Can not open msgtypes.h file\n");
28
29print OUT "/* This file is automatically generated. Editing by hand is strongly */\n";
30print OUT "/* discouraged.  Look at the newclient.h file and the msgtypes.pl conversion */\n";
31print OUT "/* script. */\n";
32
33print OUT "\nconst Msg_Type_Names msg_type_names[] = {\n";
34print OUT "{0, 0, \"generic\"},\n";
35
36my $on_subtypes=0;
37my $last_type=0;
38my $type;
39my $lc;
40my @types;
41my $max_types;
42
43while(<IN>) {
44    if (/^#define\s+MSG_TYPE_(\S*)/) {
45	if ($1 eq "LAST" || $1 eq "SUBTYPE_NONE") {
46	    $on_subtypes=1;
47	    next;
48	}
49	$type = $1;
50	$lc = $1;
51	$lc =~ tr /A-Z/a-z/;
52	if (! $on_subtypes) {
53	    $types[$max_types++] = $1;
54	    print OUT "{MSG_TYPE_$1, 0, \"$lc\"},\n";
55	} else {
56	    if ($type !~ /^$types[$last_type]/ ) {
57		my $i;
58		for ($i=0; $i< $max_types; $i++) {
59		    if ($type =~ /^$types[$i]/) {
60			$last_type=$i;
61			last;
62		    }
63		}
64		if ($i == $max_types) {
65		    print STDERR "Unable to find right type for $type\n";
66		}
67	    }
68	    print OUT "{MSG_TYPE_$types[$last_type], MSG_TYPE_$type, \"$lc\"},\n";
69	}
70    }
71}
72close(IN);
73
74print OUT "};\n";
75close(OUT);
76