1#!/usr/bin/awk -f
2#
3# This awk script creates a Java class containing important enumerations.
4# These enumerations are taken from:  rts/ExternalAI/Interface/AISCommands.h
5# It currently only takes CommandTopic and UnitCommandOptions enumerations
6#
7# This script uses functions from the following files:
8# * common.awk
9# * commonDoc.awk
10# Variables that can be set on the command-line (with -v):
11# * GENERATED_SOURCE_DIR: the generated sources root dir
12#
13# usage:
14# 	awk -f thisScript.awk -f common.awk -f commonDoc.awk
15# 	awk -f thisScript.awk -f common.awk -f commonDoc.awk \
16#       -v 'GENERATED_SOURCE_DIR=/tmp/build/AI/Interfaces/Java/src-generated/main'
17#
18
19BEGIN {
20	# initialize things
21
22	# define the field splitter(-regex)
23	FS = "[ \t]+";
24
25	# Used by other scripts
26	JAVA_MODE = 1;
27
28	# These vars can be assigned externally, see file header.
29	# Set the default values if they were not supplied on the command line.
30	if (!GENERATED_SOURCE_DIR) {
31		GENERATED_SOURCE_DIR = "../src-generated/main";
32	}
33	if (!JAVA_GENERATED_SOURCE_DIR) {
34		JAVA_GENERATED_SOURCE_DIR = GENERATED_SOURCE_DIR "/java";
35	}
36	if (!NATIVE_GENERATED_SOURCE_DIR) {
37		NATIVE_GENERATED_SOURCE_DIR = GENERATED_SOURCE_DIR "/native";
38	}
39
40	myPkgA = "com.springrts.ai";
41	myPkgD = convertJavaNameFormAToD(myPkgA);
42    myClass = "Enumerations";
43
44    #create empty arrays, holding names and values of the two enumerations
45	cmdsTopicNamesLength = 0;
46    split("", cmdsTopicNames);
47	cmdsTopicValuesLength = 0;
48    split("", cmdsTopicValues);
49	unitCmdsTopicNamesLength = 0;
50    split("", unitCmdsTopicNames);
51	unitCmdsTopicValuesLength = 0;
52    split("", unitCmdsTopicValues);
53}
54
55
56function createJavaFileName(fileName_fn) {
57	return JAVA_GENERATED_SOURCE_DIR "/" myPkgD "/" fileName_fn ".java";
58}
59
60function printGeneralJavaHeader(outFile_h, javaPkg_h, javaClassName_h) {
61
62	printCommentsHeader(outFile_h);
63	print("") >> outFile_h;
64	print("package " javaPkg_h ";") >> outFile_h;
65	print("") >> outFile_h;
66	print("") >> outFile_h;
67	print("/**") >> outFile_h;
68	print(" * These are the Java exposed enumerations.") >> outFile_h;
69	print(" * We are not calling the engine, this is a pure Java class.") >> outFile_h;
70	print(" *") >> outFile_h;
71	print(" * @author	AWK wrapper script") >> outFile_h;
72	print(" * @version	GENERATED") >> outFile_h;
73	print(" */") >> outFile_h;
74    print("public abstract class " javaClassName_h " {") >> outFile_h;
75	print("") >> outFile_h;
76}
77
78function printJavaHeader() {
79
80	outFile_i = createJavaFileName(myClass);
81
82	printGeneralJavaHeader(outFile_i, myPkgA, myClass);
83}
84
85function printJavaEnums(enums) {
86    printJavaEnum("CommandTopic", cmdsTopicNames, cmdsTopicNamesLength, cmdsTopicValues);
87    print("") >> outFile_i;
88    printJavaEnum("UnitCommandOptions", unitCmdsTopicNames, unitCmdsTopicNamesLength, unitCmdsTopicValues);
89}
90
91function printJavaEnum(enumName, names, namesLength, values) {
92	outFile_i = createJavaFileName(myClass);
93    printEnumHeader(enumName);
94
95    # Prints the enum members and values
96    first = 0;
97	for (i=0; i<namesLength;i++) {
98        if (first == 0) {
99            first = 1;
100            printf("\t\t") >> outFile_i;
101        } else {
102            printf(",\n\t\t") >> outFile_i;
103        }
104        printJavaEnumItem(names[i], values[i]);
105	}
106
107    if (first != 0) {
108        print(";\n") >> outFile_i;
109    }
110    printEnumEnd(enumName);
111}
112
113function printEnumHeader(name) {
114	outFile_i = createJavaFileName(myClass);
115
116    # Prints the enum start
117    print("\tpublic enum " name " {") >> outFile_i;
118}
119
120function printEnumEnd(name) {
121	outFile_i = createJavaFileName(myClass);
122
123    # Prints the enum end
124    print("\t\tprivate final int id;") >> outFile_i;
125    print("\t\t" name "(int id) { this.id = id; }") >> outFile_i;
126    print("\t\tpublic int getValue() { return id; }") >> outFile_i;
127    print("\t}") >> outFile_i;
128}
129
130function printJavaEnumItem(key, value) {
131    printf(key "(" value ")") >> outFile_i;
132}
133
134function printJavaEnd() {
135
136	outFile_i = createJavaFileName(myClass);
137
138	print("}") >> outFile_i;
139	print("") >> outFile_i;
140
141	close(outFile_i);
142}
143
144/^[ \t]*COMMAND_.*$/ {
145
146	doWrapp = !match(($0), /.*COMMAND_NULL.*/);
147	if (doWrapp) {
148        #parse enumeration of format x = number1,
149		sub(",", "", $4);
150		cmdsTopicNames[cmdsTopicNamesLength] = $2;
151		cmdsTopicNamesLength++;
152		cmdsTopicValues[cmdsTopicValuesLength] = $4;
153		cmdsTopicValuesLength++;
154	} else {
155		print("Java-AIInterface: NOTE: JNI level: COMMANDS: intentionally not wrapped: " $2);
156	}
157}
158
159/^[ \t]*UNIT_COMMAND_.*$/ {
160	doWrapp = !match(($0), /.*COMMAND_NULL.*/);
161	if (doWrapp) {
162		unitCmdsTopicNames[unitCmdsTopicNamesLength] = $2;
163		unitCmdsTopicNamesLength++;
164        #parse enumeration of format x = (number1 << number2),
165        sub("[ \t]*//.*", "", $0);
166		sub(",", "", $0);
167		sub($3, "", $0);
168		sub($2, "", $0);
169		sub("[ \t]*", "", $0);
170		unitCmdsTopicValues[unitCmdsTopicValuesLength] = $0;
171		unitCmdsTopicValuesLength++;
172	} else {
173		print("Java-AIInterface: NOTE: JNI level: UNIT_COMMANDS: intentionally not wrapped: " $2);
174	}
175}
176
177# This function has to return true (1) if a doc comment (eg: /** foo bar */)
178# can be deleted.
179# If there is no special condition you want to apply,
180# it should always return true (1),
181# cause there are additional mechanism to prevent accidential deleting.
182# see: commonDoc.awk
183function canDeleteDocumentation() {
184	return isInsideEvtStruct != 1;
185}
186
187END {
188	# finalize things
189	printJavaHeader();
190	printJavaEnums();
191	printJavaEnd();
192}
193