Sat, 14 Jul 2012 20:35:50 +0300
Added string table and support for string parameters in commands.
Makefile | file | annotate | diff | comparison | revisions | |
commands.def | file | annotate | diff | comparison | revisions | |
main.cxx | file | annotate | diff | comparison | revisions | |
objwriter.cxx | file | annotate | diff | comparison | revisions | |
parser.cxx | file | annotate | diff | comparison | revisions | |
scriptreader.cxx | file | annotate | diff | comparison | revisions | |
scriptreader.h | file | annotate | diff | comparison | revisions | |
stringtable.cxx | file | annotate | diff | comparison | revisions | |
stringtable.h | file | annotate | diff | comparison | revisions |
--- a/Makefile Sat Jul 14 18:03:37 2012 +0300 +++ b/Makefile Sat Jul 14 20:35:50 2012 +0300 @@ -6,7 +6,8 @@ g++ -Wall -c -o parser.o parser.cxx g++ -Wall -c -o events.o events.cxx g++ -Wall -c -o commands.o commands.cxx - g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o commands.o + g++ -Wall -c -o stringtable.o stringtable.cxx + g++ -Wall -o botc scriptreader.o objwriter.o str.o main.o parser.o events.o commands.o stringtable.o clean: rm -f *.o *~ botc
--- a/commands.def Sat Jul 14 18:03:37 2012 +0300 +++ b/commands.def Sat Jul 14 20:35:50 2012 +0300 @@ -49,7 +49,7 @@ 48:IsFavoriteWeapon:bool:1:1:str(weapon) 49:Say:void:1:1:str(message) 50:SayFromFile:void:2:2:str(filename):str(section) -51:SayFromChatFile:void:2:2:str(filename):str(section) +51:SayFromChatFile:void:1:1:str(section) 52:BeginChatting:void:0:0 53:StopChatting:void:0:0 54:ChatSectionExists:bool:1:1:str(section)
--- a/main.cxx Sat Jul 14 18:03:37 2012 +0300 +++ b/main.cxx Sat Jul 14 20:35:50 2012 +0300 @@ -48,6 +48,7 @@ #include "objwriter.h" #include "events.h" #include "commands.h" +#include "stringtable.h" #include "bots.h" #include "botcommands.h" @@ -71,6 +72,9 @@ ReadEvents (); ReadCommands (); + // Init string table + g_StringTable = new StringTable(); + // Prepare reader and writer ScriptReader *r = new ScriptReader (argv[1]); ObjWriter *w = new ObjWriter (argv[2]);
--- a/objwriter.cxx Sat Jul 14 18:03:37 2012 +0300 +++ b/objwriter.cxx Sat Jul 14 20:35:50 2012 +0300 @@ -59,6 +59,7 @@ } void ObjWriter::WriteString (char* s) { + Write<long> (strlen (s)); for (unsigned int u = 0; u < strlen (s); u++) Write<char> (s[u]); }
--- a/parser.cxx Sat Jul 14 18:03:37 2012 +0300 +++ b/parser.cxx Sat Jul 14 20:35:50 2012 +0300 @@ -48,6 +48,7 @@ #include "scriptreader.h" #include "events.h" #include "commands.h" +#include "stringtable.h" #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ ParserError ("%ss may only be defined at top level!", token.chars()); @@ -98,7 +99,6 @@ } w->Write (DH_STATENAME); - w->Write (statename.len()); w->WriteString (statename); w->Write (DH_STATEIDX); w->Write (g_NumStates); @@ -174,23 +174,18 @@ w->Write (DH_ENDMAINLOOP); } - /* - // State - w->WriteState ("stateSpawn"); - - w->Write (DH_ONENTER); - w->Write (DH_ENDONENTER); - - w->Write (DH_MAINLOOP); - w->Write (DH_ENDMAINLOOP); - - w->Write (DH_EVENT); - w->Write (BOTEVENT_PLAYER_FIREDSSG); - w->Write (DH_COMMAND); - w->Write (BOTCMD_BEGINJUMPING); - w->Write (0); - w->Write (DH_ENDEVENT); - */ + // 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); + 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]); + } + } } void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { @@ -210,21 +205,32 @@ break; } + /* if (!Next ()) ParserError ("unexpected end-of-file, unterminated command"); // If we get a ")" now, the user probably gave too few parameters if (!token.compare (")")) ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); + */ - // For now, it takes in just numbers. - // Needs to cater for string arguments too... - if (!token.isnumber()) - ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); - - int i = atoi (token.chars ()); - w->Write<long> (DH_PUSHNUMBER); - w->Write<long> (i); + switch (comm->argtypes[curarg]) { + case RETURNVAL_INT: + MustNumber(); + w->Write<long> (DH_PUSHNUMBER); + w->Write<long> (atoi (token.chars ())); + break; + case RETURNVAL_BOOLEAN: + MustBool(); + w->Write<long> (DH_PUSHNUMBER); + w->Write<long> (BoolValue ()); + break; + case RETURNVAL_STRING: + MustString(); + w->Write<long> (DH_PUSHSTRINGINDEX); + w->Write<long> (g_StringTable->Push (token.chars())); + break; + } if (curarg < comm->numargs - 1) { MustNext (",");
--- a/scriptreader.cxx Sat Jul 14 18:03:37 2012 +0300 +++ b/scriptreader.cxx Sat Jul 14 20:35:50 2012 +0300 @@ -231,4 +231,8 @@ } ParserError ("expected a boolean value, got `%s`", token.chars()); +} + +bool ScriptReader::BoolValue () { + return (!token.compare ("1") || !token.compare ("true") || !token.compare ("yes")); } \ No newline at end of file
--- a/scriptreader.h Sat Jul 14 18:03:37 2012 +0300 +++ b/scriptreader.h Sat Jul 14 20:35:50 2012 +0300 @@ -79,6 +79,7 @@ void MustString (); void MustNumber (); void MustBool (); + bool BoolValue (); void ParserError (const char* message, ...); void ParserWarning (const char* message, ...);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stringtable.cxx Sat Jul 14 20:35:50 2012 +0300 @@ -0,0 +1,105 @@ +/* + * botc source code + * Copyright (C) 2012 Santeri `Dusk` Piippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of the developer nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * 4. Redistributions in any form must be accompanied by information on how to + * obtain complete source code for the software and any accompanying + * software that uses the software. The source code must either be included + * in the distribution or be available for no more than the cost of + * distribution plus a nominal fee, and must be freely redistributable + * under reasonable conditions. For an executable file, complete source + * code means the source code for all modules it contains. It does not + * include source code for modules or files that typically accompany the + * major components of the operating system on which the executable file + * runs. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define __STRINGTABLE_CXX__ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "common.h" +#include "bots.h" +#include "stringtable.h" + +StringTable::StringTable() { + // 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; +} + +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; + + // Find a free slot in the table. + unsigned int a; + for (a = 0; a < MAX_LIST_STRINGS; a++) { + if (!strcmp (tmp, table[a])) + return a; + + // String is empty, thus it's free. + if (!strlen (table[a])) + break; + } + + // no free slots! + if (a == MAX_LIST_STRINGS) + error ("too many strings defined!"); + + // Now, dump the string into the slot + strncpy (table[a], tmp, len); + table[a][len] = 0; + + return a; +} + +unsigned int StringTable::Count () { + unsigned int count = 0; + for (unsigned int a = 0; a < MAX_LIST_STRINGS; a++) { + if (!strlen (table[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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stringtable.h Sat Jul 14 20:35:50 2012 +0300 @@ -0,0 +1,58 @@ +/* + * botc source code + * Copyright (C) 2012 Santeri `Dusk` Piippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of the developer nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * 4. Redistributions in any form must be accompanied by information on how to + * obtain complete source code for the software and any accompanying + * software that uses the software. The source code must either be included + * in the distribution or be available for no more than the cost of + * distribution plus a nominal fee, and must be freely redistributable + * under reasonable conditions. For an executable file, complete source + * code means the source code for all modules it contains. It does not + * include source code for modules or files that typically accompany the + * major components of the operating system on which the executable file + * runs. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#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); +}; + +#ifndef __STRINGTABLE_CXX__ +extern +#endif +StringTable* g_StringTable; \ No newline at end of file