From: <ave...@us...> - 2003-11-01 18:31:30
|
Update of /cvsroot/gemrb/gemrb/gemrb/plugins/Core In directory sc8-pr-cvs1:/tmp/cvs-serv29278/Core Modified Files: Interface.cpp Variables.cpp Interface.h StringMgr.h Variables.h Log Message: Implementation of tokens (string to string dictionary) Used in strref resolution and ai scripting New scripting commands: SetToken, GetToken Index: Interface.cpp =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/Interface.cpp,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** Interface.cpp 31 Oct 2003 23:58:11 -0000 1.31 --- Interface.cpp 1 Nov 2003 18:31:27 -0000 1.32 *************** *** 3,7 **** #include "FileStream.h" #include "AnimationMgr.h" - #include "Slider.h" #ifdef WIN32 --- 3,6 ---- *************** *** 33,36 **** --- 32,36 ---- windowmgr = NULL; vars = NULL; + tokens = NULL; music = NULL; ConsolePopped = false; *************** *** 88,91 **** --- 88,93 ---- if(vars) delete(vars); + if(tokens) + delete(tokens); std::vector<Table>::iterator t; for(t = tables.begin(); t != tables.end(); ++t) { *************** *** 265,268 **** --- 267,271 ---- } printStatus("OK", LIGHT_GREEN); + printMessage("Core", "Initializing Variables Dictionary...", WHITE); vars = new Variables(); *************** *** 272,275 **** --- 275,288 ---- } printStatus("OK", LIGHT_GREEN); + + printMessage("Core", "Initializing Token Dictionary...", WHITE); + tokens = new Variables(); + if(!tokens) { + printStatus("ERROR", LIGHT_RED); + return GEM_ERROR; + } + tokens->SetType(GEM_VARIABLES_STRING); + printStatus("OK", LIGHT_GREEN); + printMessage("Core", "Initializing Music Manager...", WHITE); music = (MusicMgr*)GetInterface(IE_MUS_CLASS_ID); *************** *** 780,787 **** return video->Quit(); } ! Variables * Interface::GetDictionary() { return vars; } /** Get the Music Manager */ --- 793,805 ---- return video->Quit(); } ! /** Returns the variables dictionary */ Variables * Interface::GetDictionary() { return vars; + } + /** Returns the token dictionary */ + Variables * Interface::GetTokenDictionary() + { + return tokens; } /** Get the Music Manager */ Index: Variables.cpp =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/Variables.cpp,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** Variables.cpp 27 Oct 2003 11:28:12 -0000 1.13 --- Variables.cpp 1 Nov 2003 18:31:27 -0000 1.14 *************** *** 1,21 **** #include "Variables.h" #ifdef WIN32 ! int strnlen(const char * string, int maxlen) { if(!string) return -1; - int acc = 0; int i = 0; ! while(maxlen > 0) { ! if(string[i++]) ! acc++; ! else break; } ! return acc; } #endif ///////////////////////////////////////////////////////////////////////////// ! // inlines inline bool Variables::MyCopyKey(char *&dest, const char * key) const { --- 1,20 ---- #include "Variables.h" #ifdef WIN32 ! ! static int strnlen(const char * string, int maxlen) { if(!string) return -1; int i = 0; ! while(maxlen-- > 0) { ! if(!string[i]) break; + i++; } ! return i; } #endif ///////////////////////////////////////////////////////////////////////////// ! // private inlines inline bool Variables::MyCopyKey(char *&dest, const char * key) const { *************** *** 43,62 **** return nHash; } - //sets the way we handle keys, no parsing for .ini file entries, parsing for game variables - //you should set this only on an empty mapping - inline int Variables::ParseKey(int arg) - { - MYASSERT(m_nCount==0); - m_lParseKey=arg; - } - inline int Variables::GetCount() const - { return m_nCount; } - inline bool Variables::IsEmpty() const - { return m_nCount == 0; } - inline POSITION Variables::GetStartPosition() const - { return (m_nCount == 0) ? NULL : BEFORE_START_POSITION; } - ///////////////////////////////////////////////////////////////////////////// ! // out of lines void Variables::GetNextAssoc(POSITION& rNextPosition, const char*& rKey, unsigned long& rValue) const { --- 42,47 ---- return nHash; } ///////////////////////////////////////////////////////////////////////////// ! // functions void Variables::GetNextAssoc(POSITION& rNextPosition, const char*& rKey, unsigned long& rValue) const { *************** *** 138,143 **** pAssoc = pAssoc->pNext) { if(pAssoc->key) ! delete [] pAssoc->key; } } --- 123,131 ---- pAssoc = pAssoc->pNext) { + if(m_type==GEM_VARIABLES_STRING) + if(pAssoc->nValue) + free((void *)pAssoc->nValue); if(pAssoc->key) ! delete [] pAssoc->key; } } *************** *** 227,233 **** --- 215,249 ---- } + int Variables::GetValueLength(const char *key) const + { + unsigned int nHash; + Variables::MyAssoc* pAssoc = GetAssocAt(key, nHash); + if (pAssoc == NULL) + { + return 0; // not in map + } + + return strlen((char *) pAssoc->nValue); + } + + bool Variables::Lookup(const char *key, char *dest, int MaxLength) const + { + unsigned int nHash; + MYASSERT(m_type==GEM_VARIABLES_STRING); + Variables::MyAssoc* pAssoc = GetAssocAt(key, nHash); + if (pAssoc == NULL) + { + dest[0]=0; + return false; // not in map + } + + strncpy(dest,(char *) pAssoc->nValue, MaxLength); + return true; + } + bool Variables::Lookup(const char *key, unsigned long& rValue) const { unsigned int nHash; + MYASSERT(m_type==GEM_VARIABLES_INT); Variables::MyAssoc* pAssoc = GetAssocAt(key, nHash); if (pAssoc == NULL) *************** *** 238,241 **** --- 254,288 ---- } + void Variables::SetAt(const char *key, const char *value) + { + unsigned int nHash; + Variables::MyAssoc* pAssoc; + + MYASSERT(m_type==GEM_VARIABLES_STRING); + if ((pAssoc = GetAssocAt(key, nHash)) == NULL) + { + if (m_pHashTable == NULL) + InitHashTable(m_nHashTableSize); + + // it doesn't exist, add a new Association + pAssoc = NewAssoc(key); + // put into hash table + pAssoc->pNext = m_pHashTable[nHash]; + m_pHashTable[nHash] = pAssoc; + } + else { + if(pAssoc->nValue) + free((char *) pAssoc->nValue); + pAssoc->nValue=0; + } + + //set value only if we have a key + if(pAssoc->key) + { + pAssoc->nValue=(unsigned long) value; + pAssoc->nHashValue=nHash; + } + } + void Variables::SetAt(const char *key, unsigned long value) { *************** *** 243,246 **** --- 290,294 ---- Variables::MyAssoc* pAssoc; + MYASSERT(m_type==GEM_VARIABLES_INT); if ((pAssoc = GetAssocAt(key, nHash)) == NULL) { Index: Interface.h =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/Interface.h,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** Interface.h 31 Oct 2003 23:58:11 -0000 1.19 --- Interface.h 1 Nov 2003 18:31:27 -0000 1.20 *************** *** 71,74 **** --- 71,75 ---- std::vector<Window*> windows; Variables * vars; + Variables * tokens; MusicMgr * music; std::vector<Table> tables; *************** *** 126,129 **** --- 127,132 ---- /** Get the Variables Dictionary */ Variables * GetDictionary(); + /** Get the Token Dictionary */ + Variables * GetTokenDictionary(); /** Get the Music Manager */ MusicMgr * GetMusicMgr(); Index: StringMgr.h =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/StringMgr.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** StringMgr.h 14 Oct 2003 22:05:52 -0000 1.1.1.1 --- StringMgr.h 1 Nov 2003 18:31:27 -0000 1.2 *************** *** 23,27 **** virtual ~StringMgr(void); virtual bool Open(DataStream * stream, bool autoFree = true) = 0; ! virtual char * GetString(unsigned long strref) = 0; }; --- 23,27 ---- virtual ~StringMgr(void); virtual bool Open(DataStream * stream, bool autoFree = true) = 0; ! virtual char * GetString(unsigned long strref, int flag=0) = 0; }; Index: Variables.h =================================================================== RCS file: /cvsroot/gemrb/gemrb/gemrb/plugins/Core/Variables.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** Variables.h 27 Oct 2003 11:28:12 -0000 1.10 --- Variables.h 1 Nov 2003 18:31:27 -0000 1.11 *************** *** 27,30 **** --- 27,33 ---- // Variables<unsigned long, VALUE> + #define GEM_VARIABLES_INT 0 + #define GEM_VARIABLES_STRING 1 + #ifdef WIN32 *************** *** 39,43 **** #endif ! class GEM_EXPORT Variables //: public CObject { protected: --- 42,46 ---- #endif ! class GEM_EXPORT Variables { protected: *************** *** 55,70 **** // Attributes ! // number of elements ! int GetCount() const; ! bool IsEmpty() const; // Lookup bool Lookup(const char *key, unsigned long& rValue) const; // Operations void SetAt(const char *key, unsigned long newValue); void RemoveAll(); void InitHashTable(unsigned int hashSize, bool bAllocNow = true); - int ParseKey(int arg); // Implementation --- 58,87 ---- // Attributes ! //sets the way we handle keys, no parsing for .ini file entries, parsing for game variables ! //you should set this only on an empty mapping ! inline int ParseKey(int arg) { ! MYASSERT(m_nCount==0); ! m_lParseKey=arg; ! } ! //sets the way we handle values ! inline void SetType(int type) ! { m_type=type; } ! inline int GetCount() const ! { return m_nCount; } ! inline bool IsEmpty() const ! { return m_nCount == 0; } ! inline POSITION GetStartPosition() const ! { return (m_nCount == 0) ? NULL : BEFORE_START_POSITION; } // Lookup + int GetValueLength(const char *key) const; + bool Lookup(const char *key, char *dest, int MaxLength) const; bool Lookup(const char *key, unsigned long& rValue) const; // Operations + void SetAt(const char *key, const char *newValue); void SetAt(const char *key, unsigned long newValue); void RemoveAll(); void InitHashTable(unsigned int hashSize, bool bAllocNow = true); // Implementation *************** *** 77,80 **** --- 94,98 ---- struct Plex* m_pBlocks; int m_nBlockSize; + int m_type; //could be string or unsigned long MyAssoc* NewAssoc(const char *key); *************** *** 83,87 **** inline bool MyCopyKey(char *&dest, const char * key) const; inline unsigned int MyHashKey(const char *) const; ! POSITION GetStartPosition() const; void GetNextAssoc(POSITION& rNextPosition, const char*& rKey, unsigned long& rValue) const; --- 101,105 ---- inline bool MyCopyKey(char *&dest, const char * key) const; inline unsigned int MyHashKey(const char *) const; ! // POSITION GetStartPosition() const; void GetNextAssoc(POSITION& rNextPosition, const char*& rKey, unsigned long& rValue) const; |