Sat, 14 Jul 2012 22:03:34 +0300
Added -l command line parameter, if given, botc will list all available commands and exit.
commands.cxx | file | annotate | diff | comparison | revisions | |
commands.def | file | annotate | diff | comparison | revisions | |
commands.h | file | annotate | diff | comparison | revisions | |
main.cxx | file | annotate | diff | comparison | revisions | |
parser.cxx | file | annotate | diff | comparison | revisions | |
stringtable.cxx | file | annotate | diff | comparison | revisions | |
stringtable.h | file | annotate | diff | comparison | revisions |
--- a/commands.cxx Sat Jul 14 20:35:50 2012 +0300 +++ b/commands.cxx Sat Jul 14 22:03:34 2012 +0300 @@ -47,8 +47,6 @@ #include "str.h" #include "commands.h" -CommandDef* g_CommDef; - void ReadCommands () { ScriptReader* r = new ScriptReader ("commands.def"); g_CommDef = NULL; @@ -191,4 +189,45 @@ } return NULL; +} + +str GetCommandPrototype (CommandDef* comm) { + str text; + text += GetReturnTypeName (comm->returnvalue); + text += ' '; + text += comm->name; + text += '('; + + bool hasOptionalArguments = false; + for (int i = 0; i < comm->maxargs; i++) { + if (i == comm->numargs) { + hasOptionalArguments = true; + text += '['; + } + + if (i) + text += ", "; + + text += GetReturnTypeName (comm->argtypes[i]); + text += ' '; + text += comm->argnames[i]; + + if (i >= comm->numargs) { + text += '='; + + bool isString = comm->argtypes[i] == RETURNVAL_STRING; + if (isString) text += '"'; + + char defvalstring[8]; + sprintf (defvalstring, "%d", comm->defvals[i]); + text += defvalstring; + + if (isString) text += '"'; + } + } + + if (hasOptionalArguments) + text += ']'; + text += ')'; + return text; } \ No newline at end of file
--- a/commands.def Sat Jul 14 20:35:50 2012 +0300 +++ b/commands.def Sat Jul 14 22:03:34 2012 +0300 @@ -1,7 +1,7 @@ 0:changestate:void:1:1:str(statename) 1:delay:void:1:1:int(tics) 2:rand:int:2:2:int(a):int(b) -3:StringsAreEqual:bool:0:0 +3:StringsAreEqual:bool:2:2:str(string1):str(string2) 4:LookForPowerups:int:2:2:int(start):bool(visibilitycheck) 5:LookForWeapons:int:2:2:int(start):bool(visibilitycheck) 6:LookForAmmo:int:2:2:int(start):bool(visibilitycheck)
--- a/commands.h Sat Jul 14 20:35:50 2012 +0300 +++ b/commands.h Sat Jul 14 22:03:34 2012 +0300 @@ -66,5 +66,11 @@ CommandDef* GetCommandByName (str a); int GetReturnTypeByString (str token); str GetReturnTypeName (int r); +str GetCommandPrototype (CommandDef* comm); + +#ifndef __COMMANDS_CXX__ +extern +#endif +CommandDef* g_CommDef; #endif // __COMMANDS_H__ \ No newline at end of file
--- a/main.cxx Sat Jul 14 20:35:50 2012 +0300 +++ b/main.cxx Sat Jul 14 22:03:34 2012 +0300 @@ -54,6 +54,22 @@ #include "botcommands.h" int main (int argc, char** argv) { + // Intepret command-line parameters: + // -l: list commands + if (argc == 2 && !strcmp (argv[1], "-l")) { + ReadCommands (); + printf ("Begin list of commands:\n"); + printf ("------------------------------------------------------\n"); + + CommandDef* comm; + ITERATE_COMMANDS (comm) + printf ("%s\n", GetCommandPrototype (comm).chars()); + + printf ("------------------------------------------------------\n"); + printf ("End of command list\n"); + exit (0); + } + // Print header str header; str headerline = "-="; @@ -73,7 +89,7 @@ ReadCommands (); // Init string table - g_StringTable = new StringTable(); + InitStringTable(); // Prepare reader and writer ScriptReader *r = new ScriptReader (argv[1]);
--- a/parser.cxx Sat Jul 14 20:35:50 2012 +0300 +++ b/parser.cxx Sat Jul 14 22:03:34 2012 +0300 @@ -175,17 +175,14 @@ } // If we added strings here, we need to write a list of them. - unsigned int stringcount = g_StringTable->Count(); - printf ("Write %u strings\n", stringcount); + unsigned int stringcount = CountStringTable (); if (stringcount) { w->Write<long> (DH_STRINGLIST); w->Write<long> (stringcount); - - for (unsigned int a = 0; a < stringcount; a++) { - printf ("Write string: `%s`\n", g_StringTable->table[a]); - w->WriteString (g_StringTable->table[a]); - } + for (unsigned int a = 0; a < stringcount; a++) + w->WriteString (g_StringTable[a]); } + printf ("%u string%s written\n", stringcount, (stringcount != 1) ? "s" : ""); } void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { @@ -228,7 +225,7 @@ case RETURNVAL_STRING: MustString(); w->Write<long> (DH_PUSHSTRINGINDEX); - w->Write<long> (g_StringTable->Push (token.chars())); + w->Write<long> (PushToStringTable (token.chars())); break; }
--- a/stringtable.cxx Sat Jul 14 20:35:50 2012 +0300 +++ b/stringtable.cxx Sat Jul 14 22:03:34 2012 +0300 @@ -46,33 +46,22 @@ #include "bots.h" #include "stringtable.h" -StringTable::StringTable() { +void InitStringTable() { // Zero out everything first. for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++) for (unsigned int b = 0; b < MAX_STRING_LENGTH; b++) - table[a][b] = 0; + g_StringTable[a][b] = 0; } -unsigned int StringTable::Push (char* s) { - // Determine the length - size_t l1 = strlen (s); - size_t l2 = MAX_LIST_STRINGS - 1; - size_t len = (l1 < l2) ? l1 : l2; - - // First, copy the string to a temporary buffer, since otherwise - // it gets lost and becomes empty. - char tmp[len+1]; - strncpy (tmp, s, len); - tmp[len] = 0; - +unsigned int PushToStringTable (char* s) { // Find a free slot in the table. unsigned int a; for (a = 0; a < MAX_LIST_STRINGS; a++) { - if (!strcmp (tmp, table[a])) + if (!strcmp (s, g_StringTable[a])) return a; // String is empty, thus it's free. - if (!strlen (table[a])) + if (!strlen (g_StringTable[a])) break; } @@ -80,26 +69,25 @@ if (a == MAX_LIST_STRINGS) error ("too many strings defined!"); + // Determine the length + size_t l1 = strlen (s); + size_t l2 = MAX_LIST_STRINGS - 1; + size_t len = (l1 < l2) ? l1 : l2; + // Now, dump the string into the slot - strncpy (table[a], tmp, len); - table[a][len] = 0; + strncpy (g_StringTable[a], s, len); + g_StringTable[a][len] = 0; return a; } -unsigned int StringTable::Count () { +unsigned int CountStringTable () { unsigned int count = 0; for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++) { - if (!strlen (table[a])) + if (!strlen (g_StringTable[a])) break; else count++; } return count; -} - -char* StringTable::operator [] (unsigned int a) { - if (a > MAX_LIST_STRINGS) - return const_cast<char*> (""); - return table[a]; } \ No newline at end of file
--- a/stringtable.h Sat Jul 14 20:35:50 2012 +0300 +++ b/stringtable.h Sat Jul 14 22:03:34 2012 +0300 @@ -41,18 +41,11 @@ #include "bots.h" #include "str.h" -class StringTable { -public: - char table[MAX_LIST_STRINGS][MAX_STRING_LENGTH]; - StringTable(); - - unsigned int Push (char* s); - unsigned int Count (); - - char* operator [] (unsigned int a); -}; +void InitStringTable(); +unsigned int PushToStringTable (char* s); +unsigned int CountStringTable (); #ifndef __STRINGTABLE_CXX__ extern #endif -StringTable* g_StringTable; \ No newline at end of file +char g_StringTable[MAX_LIST_STRINGS][MAX_STRING_LENGTH]; \ No newline at end of file