1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "shell_command.h" |
---|
19 | |
---|
20 | #include "list.h" |
---|
21 | #include "debug.h" |
---|
22 | #include "class_list.h" |
---|
23 | |
---|
24 | #include "key_names.h" |
---|
25 | #include <stdarg.h> |
---|
26 | #include <stdio.h> |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | ShellCommandBase::ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...) |
---|
31 | { |
---|
32 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
33 | this->setName(commandName); |
---|
34 | |
---|
35 | va_list parameters; |
---|
36 | va_start(parameters, paramCount); |
---|
37 | |
---|
38 | this->classID = classID; |
---|
39 | this->className = ClassList::IDToString(classID); |
---|
40 | |
---|
41 | if (classID & CL_MASK_SINGLETON == CL_MASK_SINGLETON) |
---|
42 | this->isSingleton = true; |
---|
43 | else |
---|
44 | this->isSingleton = false; |
---|
45 | |
---|
46 | // handling parameters, and storing them: |
---|
47 | if (paramCount > FUNCTOR_MAX_ARGUMENTS) |
---|
48 | paramCount = FUNCTOR_MAX_ARGUMENTS; |
---|
49 | this->paramCount = paramCount; |
---|
50 | this->parameters = new long[paramCount]; |
---|
51 | |
---|
52 | for (unsigned int i = 0; i < paramCount; i++) |
---|
53 | { |
---|
54 | this->parameters[i] = va_arg(parameters, long); |
---|
55 | |
---|
56 | switch (this->parameters[i]) |
---|
57 | { |
---|
58 | case ParameterBool: |
---|
59 | this->defaultBools[i] = va_arg(parameters, int); |
---|
60 | break; |
---|
61 | case ParameterChar: |
---|
62 | this->defaultStrings[i] = new char[2]; |
---|
63 | sprintf(this->defaultStrings[0], "%c", va_arg(parameters, int)); |
---|
64 | break; |
---|
65 | case ParameterString: |
---|
66 | this->defaultStrings[i] = va_arg(parameters, char*); |
---|
67 | break; |
---|
68 | case ParameterInt: |
---|
69 | this->defaultInts[i] = va_arg(parameters, int); |
---|
70 | break; |
---|
71 | case ParameterUInt: |
---|
72 | this->defaultInts[i] = va_arg(parameters, unsigned int); |
---|
73 | break; |
---|
74 | case ParameterFloat: |
---|
75 | this->defaultFloats[i] = va_arg(parameters, double); |
---|
76 | break; |
---|
77 | case ParameterLong: |
---|
78 | this->defaultInts[i] = va_arg(parameters, long); |
---|
79 | break; |
---|
80 | default: |
---|
81 | break; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // adding this ShellCommand to the list of known Commands |
---|
86 | ShellCommandBase::commandList->add(this); |
---|
87 | } |
---|
88 | |
---|
89 | ShellCommandBase::~ShellCommandBase() |
---|
90 | { |
---|
91 | delete[] this->parameters; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | tList<ShellCommandBase>* ShellCommandBase::commandList = NULL; |
---|
96 | |
---|
97 | bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...) |
---|
98 | { |
---|
99 | va_list parameters; |
---|
100 | va_start(parameters, paramCount); |
---|
101 | |
---|
102 | if (ShellCommandBase::commandList == NULL) |
---|
103 | { |
---|
104 | ShellCommandBase::commandList = new tList<ShellCommandBase>; |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
109 | ShellCommandBase* elem = iterator->firstElement(); |
---|
110 | while(elem != NULL) |
---|
111 | { |
---|
112 | if (classID == elem->classID && !strcmp(commandName, elem->getName())) |
---|
113 | { |
---|
114 | PRINTF(2)("Command already registered\n"); |
---|
115 | delete iterator; |
---|
116 | return true; |
---|
117 | } |
---|
118 | elem = iterator->nextElement(); |
---|
119 | } |
---|
120 | delete iterator; |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * executes commands |
---|
127 | * @param executionString the string containing the following input |
---|
128 | * <ClassName> [<ObjectName>] <functionName> [parameter1[,parameter2[,...]]] |
---|
129 | * @return true on success, false otherwise. |
---|
130 | */ |
---|
131 | bool ShellCommandBase::execute(const char* executionString) |
---|
132 | { |
---|
133 | if (ShellCommandBase::commandList == NULL) |
---|
134 | return false; |
---|
135 | |
---|
136 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
137 | ShellCommandBase* elem = iterator->firstElement(); |
---|
138 | while(elem != NULL) |
---|
139 | { |
---|
140 | printf("%s\n", elem->getName()); |
---|
141 | if (!strncasecmp (executionString, elem->className, strlen(elem->className)) && |
---|
142 | (*(executionString+strlen(elem->className)) == ' ' || |
---|
143 | *(executionString+strlen(elem->className)) == ':' )) |
---|
144 | { |
---|
145 | const char* commandBegin = executionString + strlen(elem->className); |
---|
146 | |
---|
147 | PRINTF(4)("Class %s matches\n", elem->className); |
---|
148 | BaseObject* objectPointer = NULL; |
---|
149 | if (elem->isSingleton) |
---|
150 | { |
---|
151 | while(*commandBegin == ' ') |
---|
152 | commandBegin++; |
---|
153 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
154 | { |
---|
155 | elem = iterator->nextElement(); |
---|
156 | continue; |
---|
157 | } |
---|
158 | // getting singleton-reference |
---|
159 | tList<BaseObject>* list = ClassList::getList(elem->classID); |
---|
160 | if (list) |
---|
161 | objectPointer = list->firstElement(); |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | // checking for the Object |
---|
166 | while(*commandBegin == ' ') |
---|
167 | commandBegin++; |
---|
168 | tList<BaseObject>* list = ClassList::getList(elem->classID); |
---|
169 | if (list == NULL) |
---|
170 | break; |
---|
171 | tIterator<BaseObject>* iterBO = list->getIterator(); |
---|
172 | BaseObject* enumBO = iterBO->firstElement(); |
---|
173 | while(enumBO != NULL) |
---|
174 | { |
---|
175 | if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName()))) |
---|
176 | { |
---|
177 | PRINTF(4)("Object %s matches\n", enumBO->getName()); |
---|
178 | objectPointer = enumBO; |
---|
179 | break; |
---|
180 | } |
---|
181 | enumBO = iterBO->nextElement(); |
---|
182 | } |
---|
183 | delete iterBO; |
---|
184 | |
---|
185 | // break on no object Found. We cannot operate on Classes, but on Objects |
---|
186 | if (objectPointer == NULL) |
---|
187 | break; |
---|
188 | commandBegin = commandBegin + strlen(objectPointer->getName()); |
---|
189 | while(*commandBegin == ' ') |
---|
190 | commandBegin++; |
---|
191 | // checking for the requested function. |
---|
192 | if (strncmp (commandBegin, elem->getName(), strlen(elem->getName()))) |
---|
193 | { |
---|
194 | elem = iterator->nextElement(); |
---|
195 | continue; |
---|
196 | } |
---|
197 | PRINTF(5)("Function '%s' found\n", commandBegin); |
---|
198 | } |
---|
199 | const char* paramBegin = strchr(commandBegin, ' '); |
---|
200 | if (paramBegin == NULL) |
---|
201 | paramBegin = commandBegin + strlen(elem->getName()); |
---|
202 | |
---|
203 | if (objectPointer != NULL && paramBegin != NULL) |
---|
204 | { |
---|
205 | elem->executeCommand(objectPointer, paramBegin); |
---|
206 | delete iterator; |
---|
207 | return true; |
---|
208 | } |
---|
209 | } |
---|
210 | elem = iterator->nextElement(); |
---|
211 | } |
---|
212 | delete iterator; |
---|
213 | return true; |
---|
214 | } |
---|