| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // FILE: INI.cpp //////////////////////////////////////////////////////////////////////////////////
- // Author: Colin Day, November 2001
- // Desc: INI Reader
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
- #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
- #define DEFINE_DEATH_NAMES
- #include "Common/INI.h"
- #include "Common/INIException.h"
- #include "Common/DamageFX.h"
- #include "Common/File.h"
- #include "Common/FileSystem.h"
- #include "Common/GameAudio.h"
- #include "Common/Science.h"
- #include "Common/SpecialPower.h"
- #include "Common/ThingFactory.h"
- #include "Common/ThingTemplate.h"
- #include "Common/Upgrade.h"
- #include "Common/Xfer.h"
- #include "Common/XferCRC.h"
- #include "GameClient/Anim2D.h"
- #include "GameClient/Color.h"
- #include "GameClient/FXList.h"
- #include "GameClient/GameText.h"
- #include "GameClient/Image.h"
- #include "GameClient/ParticleSys.h"
- #include "GameLogic/Armor.h"
- #include "GameLogic/ExperienceTracker.h"
- #include "GameLogic/FPUControl.h"
- #include "GameLogic/ObjectCreationList.h"
- #include "GameLogic/ScriptEngine.h"
- #include "GameLogic/Weapon.h"
- #ifdef _INTERNAL
- // for occasional debugging...
- //#pragma optimize("", off)
- //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
- #endif
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- static Xfer *s_xfer = NULL;
- //-------------------------------------------------------------------------------------------------
- /** This is the table of data types we can have in INI files. To add a new data type
- * block make a new entry in this table and add an appropriate parsing function */
- //-------------------------------------------------------------------------------------------------
- extern void parseReallyLowMHz( INI* ini); // yeah, so sue me (srj)
- struct BlockParse
- {
- const char *token;
- INIBlockParse parse;
- };
- static const BlockParse theTypeTable[] =
- {
- { "AIData", INI::parseAIDataDefinition },
- { "Animation", INI::parseAnim2DDefinition },
- { "Armor", INI::parseArmorDefinition },
- { "AudioEvent", INI::parseAudioEventDefinition },
- { "AudioSettings", INI::parseAudioSettingsDefinition },
- { "Bridge", INI::parseTerrainBridgeDefinition },
- { "Campaign", INI::parseCampaignDefinition },
- { "ChallengeGenerals", INI::parseChallengeModeDefinition },
- { "CommandButton", INI::parseCommandButtonDefinition },
- { "CommandMap", INI::parseMetaMapDefinition },
- { "CommandSet", INI::parseCommandSetDefinition },
- { "ControlBarScheme", INI::parseControlBarSchemeDefinition },
- { "ControlBarResizer", INI::parseControlBarResizerDefinition },
- { "CrateData", INI::parseCrateTemplateDefinition },
- { "Credits", INI::parseCredits},
- { "WindowTransition", INI::parseWindowTransitions},
- { "DamageFX", INI::parseDamageFXDefinition },
- { "DialogEvent", INI::parseDialogDefinition },
- { "DrawGroupInfo", INI::parseDrawGroupNumberDefinition },
- { "EvaEvent", INI::parseEvaEvent },
- { "FXList", INI::parseFXListDefinition },
- { "GameData", INI::parseGameDataDefinition },
- { "InGameUI", INI::parseInGameUIDefinition },
- { "Locomotor", INI::parseLocomotorTemplateDefinition },
- { "Language", INI::parseLanguageDefinition },
- { "MapCache", INI::parseMapCacheDefinition },
- { "MapData", INI::parseMapDataDefinition },
- { "MappedImage", INI::parseMappedImageDefinition },
- { "MiscAudio", INI::parseMiscAudio},
- { "Mouse", INI::parseMouseDefinition },
- { "MouseCursor", INI::parseMouseCursorDefinition },
- { "MultiplayerColor", INI::parseMultiplayerColorDefinition },
- { "MultiplayerStartingMoneyChoice", INI::parseMultiplayerStartingMoneyChoiceDefinition },
- { "OnlineChatColors", INI::parseOnlineChatColorDefinition },
- { "MultiplayerSettings",INI::parseMultiplayerSettingsDefinition },
- { "MusicTrack", INI::parseMusicTrackDefinition },
- { "Object", INI::parseObjectDefinition },
- { "ObjectCreationList", INI::parseObjectCreationListDefinition },
- { "ObjectReskin", INI::parseObjectReskinDefinition },
- { "ParticleSystem", INI::parseParticleSystemDefinition },
- { "PlayerTemplate", INI::parsePlayerTemplateDefinition },
- { "Road", INI::parseTerrainRoadDefinition },
- { "Science", INI::parseScienceDefinition },
- { "Rank", INI::parseRankDefinition },
- { "SpecialPower", INI::parseSpecialPowerDefinition },
- { "ShellMenuScheme", INI::parseShellMenuSchemeDefinition },
- { "Terrain", INI::parseTerrainDefinition },
- { "Upgrade", INI::parseUpgradeDefinition },
- { "Video", INI::parseVideoDefinition },
- { "WaterSet", INI::parseWaterSettingDefinition },
- { "WaterTransparency", INI::parseWaterTransparencyDefinition},
- { "Weather", INI::parseWeatherDefinition},
- { "Weapon", INI::parseWeaponTemplateDefinition },
- { "WebpageURL", INI::parseWebpageURLDefinition },
- { "HeaderTemplate", INI::parseHeaderTemplateDefinition },
- { "StaticGameLOD", INI::parseStaticGameLODDefinition },
- { "DynamicGameLOD", INI::parseDynamicGameLODDefinition },
- { "LODPreset", INI::parseLODPreset },
- { "BenchProfile", INI::parseBenchProfile },
- { "ReallyLowMHz", parseReallyLowMHz },
- { "ScriptAction", ScriptEngine::parseScriptAction },
- { "ScriptCondition", ScriptEngine::parseScriptCondition },
-
- { NULL, NULL }, // keep this last!
- };
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- Bool INI::isValidINIFilename( const char *filename )
- {
- if( filename == NULL )
- return FALSE;
- Int len = strlen( filename );
- if( len < 3 )
- return FALSE;
- if( filename[ len - 1 ] != 'I' && filename[ len - 1 ] != 'i' )
- return FALSE;
- if( filename[ len - 2 ] != 'N' && filename[ len - 2 ] != 'n' )
- return FALSE;
- if( filename[ len - 3 ] != 'I' && filename[ len - 3 ] != 'i' )
- return FALSE;
- return TRUE;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- INI::INI( void )
- {
- m_file = NULL;
- m_readBufferNext=m_readBufferUsed=0;
- m_filename = "None";
- m_loadType = INI_LOAD_INVALID;
- m_lineNum = 0;
- m_seps = " \n\r\t="; ///< make sure you update m_sepsPercent/m_sepsColon as well
- m_sepsPercent = " \n\r\t=%%";
- m_sepsColon = " \n\r\t=:";
- m_sepsQuote = "\"\n="; ///< stop at " = EOL
- m_blockEndToken = "END";
- m_endOfFile = FALSE;
- m_buffer[0] = 0;
- #if defined(_DEBUG) || defined(_INTERNAL)
- m_curBlockStart[0] = 0;
- #endif
- } // end INI
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- INI::~INI( void )
- {
- } // end ~INI
- //-------------------------------------------------------------------------------------------------
- /** Load all INI files in the specified directory (and subdirectories if indicated).
- * If we are to load subdirectories, we will load them *after* we load all the
- * files in the current directory */
- //-------------------------------------------------------------------------------------------------
- void INI::loadDirectory( AsciiString dirName, Bool subdirs, INILoadType loadType, Xfer *pXfer )
- {
- // sanity
- if( dirName.isEmpty() )
- throw INI_INVALID_DIRECTORY;
- try
- {
- FilenameList filenameList;
- dirName.concat('\\');
- TheFileSystem->getFileListInDirectory(dirName, "*.ini", filenameList, TRUE);
- // Load the INI files in the dir now, in a sorted order. This keeps things the same between machines
- // in a network game.
- FilenameList::const_iterator it = filenameList.begin();
- while (it != filenameList.end())
- {
- AsciiString tempname;
- tempname = (*it).str() + dirName.getLength();
- if ((tempname.find('\\') == NULL) && (tempname.find('/') == NULL)) {
- // this file doesn't reside in a subdirectory, load it first.
- load( *it, loadType, pXfer );
- }
- ++it;
- }
- it = filenameList.begin();
- while (it != filenameList.end())
- {
- AsciiString tempname;
- tempname = (*it).str() + dirName.getLength();
- if ((tempname.find('\\') != NULL) || (tempname.find('/') != NULL)) {
- load( *it, loadType, pXfer );
- }
- ++it;
- }
- }
- catch (...)
- {
- // propagate the exception
- throw;
- }
- } // end loadDirectory
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::prepFile( AsciiString filename, INILoadType loadType )
- {
- // if we have a file open already -- we can't do another one
- if( m_file != NULL )
- {
- DEBUG_CRASH(( "INI::load, cannot open file '%s', file already open\n", filename.str() ));
- throw INI_FILE_ALREADY_OPEN;
- } // end if
- // open the file
- m_file = TheFileSystem->openFile(filename.str(), File::READ);
- if( m_file == NULL )
- {
- DEBUG_CRASH(( "INI::load, cannot open file '%s'\n", filename.str() ));
- throw INI_CANT_OPEN_FILE;
- } // end if
- m_file = m_file->convertToRAMFile();
- // save our filename
- m_filename = filename;
- // save our load time
- m_loadType = loadType;
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::unPrepFile()
- {
- // close the file
- m_file->close();
- m_file = NULL;
- m_readBufferUsed=m_readBufferNext=0;
- m_filename = "None";
- m_loadType = INI_LOAD_INVALID;
- m_lineNum = 0;
- m_endOfFile = FALSE;
- s_xfer = NULL;
- }
- //-------------------------------------------------------------------------------------------------
- static INIBlockParse findBlockParse(const char* token)
- {
- for (const BlockParse* parse = theTypeTable; parse->token; ++parse)
- {
- if (strcmp( parse->token, token ) == 0)
- {
- return parse->parse;
- }
- }
- return NULL;
- }
- //-------------------------------------------------------------------------------------------------
- static INIFieldParseProc findFieldParse(const FieldParse* parseTable, const char* token, int& offset, const void*& userData)
- {
- for (const FieldParse* parse = parseTable; parse->token; ++parse)
- {
- if (strcmp( parse->token, token ) == 0)
- {
- offset = parse->offset;
- userData = parse->userData;
- return parse->parse;
- }
- }
- if (!parse->token && parse->parse)
- {
- offset = parse->offset;
- userData = token;
- return parse->parse;
- }
- else
- {
- return NULL;
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Load and parse an INI file */
- //-------------------------------------------------------------------------------------------------
- void INI::load( AsciiString filename, INILoadType loadType, Xfer *pXfer )
- {
- setFPMode(); // so we have consistent Real values for GameLogic -MDC
- s_xfer = pXfer;
- prepFile(filename, loadType);
- try
- {
- // read all lines in the file
- DEBUG_ASSERTCRASH( m_endOfFile == FALSE, ("INI::load, EOF at the beginning!\n") );
- while( m_endOfFile == FALSE )
- {
- // read this line
- readLine();
- AsciiString currentLine = m_buffer;
- // the first word is the type of data we're processing
- const char *token = strtok( m_buffer, m_seps );
- if( token )
- {
- INIBlockParse parse = findBlockParse(token);
- if (parse)
- {
- #if defined(_DEBUG) || defined(_INTERNAL)
- strcpy(m_curBlockStart, m_buffer);
- #endif
- try {
- (*parse)( this );
- } catch (...) {
- DEBUG_CRASH(("Error parsing block '%s' in INI file '%s'\n", token, m_filename.str()) );
- char buff[1024];
- sprintf(buff, "Error parsing INI file '%s' (Line: '%s')\n", m_filename.str(), currentLine.str());
- throw INIException(buff);
- }
- #if defined(_DEBUG) || defined(_INTERNAL)
- strcpy(m_curBlockStart, "NO_BLOCK");
- #endif
- }
- else
- {
- DEBUG_ASSERTCRASH( 0, ("[LINE: %d - FILE: '%s'] Unknown block '%s'\n",
- getLineNum(), getFilename().str(), token ) );
- throw INI_UNKNOWN_TOKEN;
- }
-
- } // end if
-
- } // end while
- }
- catch (...)
- {
- unPrepFile();
- // propagate the exception.
- throw;
- }
- unPrepFile();
- } // end load
- //-------------------------------------------------------------------------------------------------
- /** Read a line from the already open file. Any comments will be remved and
- * therefore ignored from any given line */
- //-------------------------------------------------------------------------------------------------
- void INI::readLine( void )
- {
- // sanity
- DEBUG_ASSERTCRASH( m_file, ("readLine(), file pointer is NULL\n") );
- if (m_endOfFile)
- *m_buffer=0;
- else
- {
- char *p=m_buffer;
- while (p!=m_buffer+INI_MAX_CHARS_PER_LINE)
- {
- // get next character
- if (m_readBufferNext==m_readBufferUsed)
- {
- // refill buffer
- m_readBufferNext=0;
- m_readBufferUsed=m_file->read(m_readBuffer,INI_READ_BUFFER);
- // EOF?
- if (!m_readBufferUsed)
- {
- m_endOfFile=true;
- *p=0;
- break;
- }
- }
- *p=m_readBuffer[m_readBufferNext++];
- // CR?
- if (*p=='\n')
- {
- *p=0;
- break;
- }
- DEBUG_ASSERTCRASH(*p != '\t', ("tab characters are not allowed in INI files (%s). please check your editor settings. Line Number %d\n",m_filename.str(), getLineNum()));
- // comment?
- if (*p==';')
- *p=0;
- // whitespace?
- else if (*p>0&&*p<32)
- *p=' ';
- p++;
- }
- *p=0;
- // increase our line count
- m_lineNum++;
- // check for at the max
- if ( p == m_buffer+INI_MAX_CHARS_PER_LINE )
- {
- DEBUG_ASSERTCRASH( 0, ("Buffer too small (%d) and was truncated, increase INI_MAX_CHARS_PER_LINE\n",
- INI_MAX_CHARS_PER_LINE) );
- } // end if
- }
- if (s_xfer)
- {
- s_xfer->xferUser( m_buffer, sizeof( char ) * strlen( m_buffer ) );
- //DEBUG_LOG(("Xfer val is now 0x%8.8X in %s, line %s\n", ((XferCRC *)s_xfer)->getCRC(),
- //m_filename.str(), m_buffer));
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse UnsignedByte from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseUnsignedByte( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- Int value = scanInt(token);
- if (value < 0 || value > 255)
- {
- DEBUG_CRASH(("Bad value INI::parseUnsignedByte"));
- throw ERROR_BUG;
- }
- *(Byte *)store = (Byte)value;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse signed short from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseShort( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- Int value = scanInt(token);
- if (value < -32768 || value > 32767)
- {
- DEBUG_CRASH(("Bad value INI::parseShort"));
- throw ERROR_BUG;
- }
- *(Short *)store = (Short)value;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse unsigned short from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseUnsignedShort( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- Int value = scanInt(token);
- if (value < 0 || value > 65535)
- {
- DEBUG_CRASH(("Bad value INI::parseUnsignedShort"));
- throw ERROR_BUG;
- }
- *(UnsignedShort *)store = (UnsignedShort)value;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse integer from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseInt( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- *(Int *)store = scanInt(token);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse unsigned integer from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseUnsignedInt( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- *(UnsignedInt *)store = scanUnsignedInt(token);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse real from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseReal( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- *(Real *)store = scanReal(token);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse real from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parsePositiveNonZeroReal( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- *(Real *)store = scanReal(token);
- if (*(Real *)store <= 0.0f)
- {
- DEBUG_CRASH(("invalid Real value %f -- expected > 0\n",*(Real*)store));
- throw INI_INVALID_DATA;
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a degree value (0 to 360) and store the radian value of that degree
- * in a Real */
- //-------------------------------------------------------------------------------------------------
- void INI::parseAngleReal( INI *ini, void * /*instance*/,
- void *store, const void *userData )
- {
- const char *token = ini->getNextToken();
- const Real RADS_PER_DEGREE = PI / 180.0f;
- *(Real *)store = scanReal( token ) * RADS_PER_DEGREE;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an angular velocity in degrees-per-sec and store the rads-per-frame value of that degree
- * in a Real */
- //-------------------------------------------------------------------------------------------------
- void INI::parseAngularVelocityReal( INI *ini, void * /*instance*/,
- void *store, const void *userData )
- {
- const char *token = ini->getNextToken();
- // scan the int and convert to radian and store as a real
- *(Real *)store = ConvertAngularVelocityInDegreesPerSecToRadsPerFrame(scanReal( token ));
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse Bool from buffer and assign at location 'store'. The buffer token must
- * be in the form of a string "Yes" or "No" (case is ignored) */
- //-------------------------------------------------------------------------------------------------
- void INI::parseBool( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- *(Bool*)store = INI::scanBool(ini->getNextToken());
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse Bool from buffer; if true, or in MASK, otherwise and out MASK. The buffer token must
- * be in the form of a string "Yes" or "No" (case is ignored) */
- //-------------------------------------------------------------------------------------------------
- void INI::parseBitInInt32( INI *ini, void *instance, void *store, const void* userData )
- {
- UnsignedInt* s = (UnsignedInt*)store;
- UnsignedInt mask = (UnsignedInt)userData;
- if (INI::scanBool(ini->getNextToken()))
- *s |= mask;
- else
- *s &= ~mask;
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- /*static*/ Bool INI::scanBool(const char* token)
- {
- // translate string yes/no into TRUE/FALSE
- if( stricmp( token, "yes" ) == 0 )
- return TRUE;
- else if( stricmp( token, "no" ) == 0 )
- return FALSE;
- else
- {
- DEBUG_CRASH(("invalid boolean token %s -- expected Yes or No\n",token));
- throw INI_INVALID_DATA;
- return false; // keep compiler happy
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an *ASCII* string from buffer and assign at location 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseAsciiString( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- AsciiString* asciiString = (AsciiString *)store;
- *asciiString = ini->getNextAsciiString();
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an *ASCII* string from buffer and assign at location 'store'. Has better support for quoted strings.
- We don't really need this function, but parseString() is broken and we want to leave it broken to
- maintain existing code.
- */
- //-------------------------------------------------------------------------------------------------
- void INI::parseQuotedAsciiString( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- AsciiString* asciiString = (AsciiString *)store;
- *asciiString = ini->getNextQuotedAsciiString();
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::parseAsciiStringVector( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- std::vector<AsciiString>* asv = (std::vector<AsciiString>*)store;
- asv->clear();
- for (const char *token = ini->getNextTokenOrNull(); token != NULL; token = ini->getNextTokenOrNull())
- {
- asv->push_back(token);
- }
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::parseAsciiStringVectorAppend( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- std::vector<AsciiString>* asv = (std::vector<AsciiString>*)store;
- // nope, don't clear. duh.
- // asv->clear();
- for (const char *token = ini->getNextTokenOrNull(); token != NULL; token = ini->getNextTokenOrNull())
- {
- asv->push_back(token);
- }
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- /* static */void INI::parseScienceVector( INI *ini, void * /*instance*/, void *store, const void *userData )
- {
- ScienceVec* asv = (ScienceVec*)store;
- asv->clear();
- for (const char *token = ini->getNextTokenOrNull(); token != NULL; token = ini->getNextTokenOrNull())
- {
- if (stricmp(token, "None") == 0)
- {
- asv->clear();
- return;
- }
- asv->push_back(INI::scanScience( token ));
- }
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- AsciiString INI::getNextQuotedAsciiString()
- {
- AsciiString result;
- char buff[INI_MAX_CHARS_PER_LINE];
- const char *token = getNextTokenOrNull(); // if null, just leave an empty string
- if (token != NULL)
- {
- if (token[0] != '\"')
- {
- // if token is simply "
- result.set( token ); // Start following the "
- }
- else
- { int strLen=0;
- Bool done=FALSE;
- if ((strLen=strlen(token)) > 1)
- {
- strcpy(buff, &token[1]); //skip the starting quote
- //Check for end of quoted string. Checking here fixes cases where quoted string on same line with other data.
- if (buff[strLen-2]=='"') //skip ending quote if present
- { buff[strLen-2]='\0';
- done=TRUE;
- }
- }
- if (!done)
- {
- token = getNextToken(getSepsQuote());
-
- if (strlen(token) > 1 && token[1] != '\t')
- {
- strcat(buff, " ");
- strcat(buff, token);
- }
- else
- { Int buflen=strlen(buff);
- if (buff[buflen-1]=='\"')
- buff[buflen-1]='\0';
- }
- }
- result.set(buff);
- }
- }
- return result;
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- AsciiString INI::getNextAsciiString()
- {
- AsciiString result;
- const char *token = getNextTokenOrNull(); // if null, just leave an empty string
- if (token != NULL)
- {
- if (token[0] != '\"')
- {
- // if token is simply "
- result.set( token ); // Start following the "
- }
- else
- {
- static char buff[INI_MAX_CHARS_PER_LINE];
- buff[0] = 0;
- if (strlen(token) > 1)
- {
- strcpy(buff, &token[1]);
- }
- token = getNextTokenOrNull(getSepsQuote());
- if (token) {
- if (strlen(token) > 1 && token[1] != '\t')
- {
- strcat(buff, " ");
- }
- strcat(buff, token);
- result.set(buff);
- } else {
- Int len = strlen(buff);
- if (len && buff[len-1] == '"') { // strip off trailing quote jba. [2/12/2003]
- buff[len-1] = 0;
- }
- result.set(buff);
- }
- }
- }
- return result;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a string label, get the *translated* actual text from the label and store
- * into a *UNICODE* string. */
- //-------------------------------------------------------------------------------------------------
- void INI::parseAndTranslateLabel( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- // translate
- UnicodeString translated = TheGameText->fetch( token );
- if( translated.isEmpty() )
- throw INI_INVALID_DATA;
- // save the translated text
- UnicodeString *theString = (UnicodeString *)store;
- theString->set( translated.str() );
- } // end parseAndTranslateLabel
- //-------------------------------------------------------------------------------------------------
- /** Parse a string label assumed as an image as part of the image collection. Translate
- * to an image pointer for storage */
- //-------------------------------------------------------------------------------------------------
- void INI::parseMappedImage( INI *ini, void * /*instance*/, void *store, const void *userData )
- {
- const char *token = ini->getNextToken();
- if( TheMappedImageCollection )
- {
- typedef const Image* ConstImagePtr;
- *(ConstImagePtr*)store = TheMappedImageCollection->findImageByName( AsciiString( token ) );
- }
-
- //KM: If we are in the worldbuilder, we want to parse commandbuttons for informational purposes,
- //but we don't care about the images -- because we never access them. In RTS/GUIEdit, they always
- //exist -- and in those cases, it will never call this code anyways because it'll throw long before.
- //else
- // throw INI_UNKNOWN_ERROR;
- } // end parseMappedImage
- // ------------------------------------------------------------------------------------------------
- /** Parse a string label assumed as a Anim2D template name. Translate that name to an
- * actual template pointer for storage */
- // ------------------------------------------------------------------------------------------------
- /*static*/ void INI::parseAnim2DTemplate( INI *ini, void *instance, void *store, const void *userData )
- {
- const char *token = ini->getNextToken();
- if( TheAnim2DCollection )
- {
- Anim2DTemplate **anim2DTemplate = (Anim2DTemplate **)store;
- *anim2DTemplate = TheAnim2DCollection->findTemplate( AsciiString( token ) );
- } // end if
- else
- {
- DEBUG_CRASH(( "INI::parseAnim2DTemplate - TheAnim2DCollection is NULL\n" ));
- throw INI_UNKNOWN_ERROR;
- } // end else
- } // end parseAnim2DTemplate
- //-------------------------------------------------------------------------------------------------
- /** Parse a percent in int or real form such as "23%" or "95.4%" and assign
- * to location 'store' as a number from 0.0 to 1.0 */
- //-------------------------------------------------------------------------------------------------
- void INI::parsePercentToReal( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken(ini->getSepsPercent());
- Real *theReal = (Real *)store;
- *theReal = scanPercentToReal(token);
- } // end parsePercentToReal
- //-------------------------------------------------------------------------------------------------
- /** 'store' points to an 32 bit unsigned integer. We will zero that integer, parse each token
- * in the buffer, if the token is in the userData table of strings, we will set the
- * according bit flag for it */
- //-------------------------------------------------------------------------------------------------
- void INI::parseBitString8( INI* ini, void * /*instance*/, void *store, const void* userData )
- {
- UnsignedInt tmp;
- INI::parseBitString32(ini, NULL, &tmp, userData);
- if (tmp & 0xffffff00)
- {
- DEBUG_CRASH(("Bad bitstring list INI::parseBitString8"));
- throw ERROR_BUG;
- }
- *(Byte*)store = (Byte)tmp;
- }
- //-------------------------------------------------------------------------------------------------
- /** 'store' points to an 32 bit unsigned integer. We will zero that integer, parse each token
- * in the buffer, if the token is in the userData table of strings, we will set the
- * according bit flag for it */
- //-------------------------------------------------------------------------------------------------
- void INI::parseBitString32( INI* ini, void * /*instance*/, void *store, const void* userData )
- {
- ConstCharPtrArray flagList = (ConstCharPtrArray)userData;
- UnsignedInt *bits = (UnsignedInt *)store;
- if( flagList == NULL || flagList[ 0 ] == NULL)
- {
- DEBUG_ASSERTCRASH( flagList, ("INTERNAL ERROR! parseBitString32: No flag list provided!\n") );
- throw INI_INVALID_NAME_LIST;
- }
- Bool foundNormal = false;
- Bool foundAddOrSub = false;
- // loop through all tokens
- for (const char *token = ini->getNextTokenOrNull(); token != NULL; token = ini->getNextTokenOrNull())
- {
- if (stricmp(token, "NONE") == 0)
- {
- if (foundNormal || foundAddOrSub)
- {
- DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
- throw INI_INVALID_NAME_LIST;
- }
- *bits = 0;
- break;
- }
- if (token[0] == '+')
- {
- if (foundNormal)
- {
- DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
- throw INI_INVALID_NAME_LIST;
- }
- Int bitIndex = INI::scanIndexList(token+1, flagList); // this throws if the token is not found
- *bits |= (1 << bitIndex);
- foundAddOrSub = true;
- }
- else if (token[0] == '-')
- {
- if (foundNormal)
- {
- DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
- throw INI_INVALID_NAME_LIST;
- }
- Int bitIndex = INI::scanIndexList(token+1, flagList); // this throws if the token is not found
- *bits &= ~(1 << bitIndex);
- foundAddOrSub = true;
- }
- else
- {
- if (foundAddOrSub)
- {
- DEBUG_CRASH(("you may not mix normal and +- ops in bitstring lists"));
- throw INI_INVALID_NAME_LIST;
- }
- if (!foundNormal)
- *bits = 0;
- Int bitIndex = INI::scanIndexList(token, flagList); // this throws if the token is not found
- *bits |= (1 << bitIndex);
- foundNormal = true;
- }
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a color in the form of
- *
- * RGB_COLOR = R:100 G:114 B:245
- * and store in "RGBColor" structure pointed to by 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseRGBColor( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char* names[3] = { "R", "G", "B" };
- Int colors[3];
- for( Int i = 0; i < 3; i++ )
- {
- colors[i] = scanInt(ini->getNextSubToken(names[i]));
- if( colors[ i ] < 0 )
- throw INI_INVALID_DATA;
- if( colors[ i ] > 255 )
- throw INI_INVALID_DATA;
- }
- // assign the color components to the "RGBColor" pointer at 'store'
- RGBColor *theColor = (RGBColor *)store;
- theColor->red = (Real)colors[ 0 ] / 255.0f;
- theColor->green = (Real)colors[ 1 ] / 255.0f;
- theColor->blue = (Real)colors[ 2 ] / 255.0f;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a color in the form of
- *
- * RGB_COLOR = R:100 G:114 B:245 [A:233]
- * and store in "RGBAColorInt" structure pointed to by 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseRGBAColorInt( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char* names[4] = { "R", "G", "B", "A" };
- Int colors[4];
- for( Int i = 0; i < 4; i++ )
- {
- const char* token = ini->getNextTokenOrNull(ini->getSepsColon());
- if (token == NULL)
- {
- if (i < 3)
- {
- throw INI_INVALID_DATA;
- }
- else
- {
- // it's ok for A to be omitted.
- colors[i] = 255;
- }
- }
- else
- {
- // if present, the token must match.
- if (stricmp(token, names[i]) != 0)
- {
- throw INI_INVALID_DATA;
- }
- colors[i] = scanInt(ini->getNextToken(ini->getSepsColon()));
- }
- if( colors[ i ] < 0 )
- throw INI_INVALID_DATA;
- if( colors[ i ] > 255 )
- throw INI_INVALID_DATA;
- }
- //
- // assign the color components to the "RGBColorInt" pointer at 'store', keep
- // the numbers as between 0 and 255
- //
- RGBAColorInt *theColor = (RGBAColorInt *)store;
- theColor->red = colors[ 0 ];
- theColor->green = colors[ 1 ];
- theColor->blue = colors[ 2 ];
- theColor->alpha = colors[ 3 ];
- } // end parseRGBAColorInt
- //-------------------------------------------------------------------------------------------------
- /** Parse a color in the form of
- *
- * RGB_COLOR = R:100 G:114 B:245 [A:233]
- * and store in "Color" structure pointed to by 'store' */
- //-------------------------------------------------------------------------------------------------
- void INI::parseColorInt( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char* names[4] = { "R", "G", "B", "A" };
- Int colors[4];
- for( Int i = 0; i < 4; i++ )
- {
- const char* token = ini->getNextTokenOrNull(ini->getSepsColon());
- if (token == NULL)
- {
- if (i < 3)
- {
- throw INI_INVALID_DATA;
- }
- else
- {
- // it's ok for A to be omitted.
- colors[i] = 255;
- }
- }
- else
- {
- // if present, the token must match.
- if (stricmp(token, names[i]) != 0)
- {
- throw INI_INVALID_DATA;
- }
- colors[i] = scanInt(ini->getNextToken(ini->getSepsColon()));
- }
- if( colors[ i ] < 0 )
- throw INI_INVALID_DATA;
- if( colors[ i ] > 255 )
- throw INI_INVALID_DATA;
- }
- //
- // assign the color components to the "Color" pointer at 'store', keep
- // the numbers as between 0 and 255
- //
- Color *theColor = (Color *)store;
- *theColor = GameMakeColor(colors[0], colors[1], colors[2], colors[3]);
- } // end parseColorInt
- //-------------------------------------------------------------------------------------------------
- /** Parse a 3D coordinate of reals in the form of:
- * FIELD_NAME = X:400 Y:-214.3 Z:8.6 */
- //-------------------------------------------------------------------------------------------------
- void INI::parseCoord3D( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- Coord3D *theCoord = (Coord3D *)store;
- theCoord->x = scanReal(ini->getNextSubToken("X"));
- theCoord->y = scanReal(ini->getNextSubToken("Y"));
- theCoord->z = scanReal(ini->getNextSubToken("Z"));
- } // end parseCoord3D
- //-------------------------------------------------------------------------------------------------
- /** Parse a 2D coordinate of reals in the form of:
- * FIELD_NAME = X:400 Y:-214.3 */
- //-------------------------------------------------------------------------------------------------
- void INI::parseCoord2D( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- Coord2D *theCoord = (Coord2D *)store;
- theCoord->x = scanReal(ini->getNextSubToken("X"));
- theCoord->y = scanReal(ini->getNextSubToken("Y"));
- } // end parseCoord2D
- //-------------------------------------------------------------------------------------------------
- /** Parse a 2D coordinate of Ints in the form of:
- * FIELD_NAME = X:400 Y:-214 */
- //-------------------------------------------------------------------------------------------------
- void INI::parseICoord2D( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- ICoord2D *theCoord = (ICoord2D *)store;
- theCoord->x = scanInt(ini->getNextSubToken("X"));
- theCoord->y = scanInt(ini->getNextSubToken("Y"));
- } // end parseICoord2D
- //-------------------------------------------------------------------------------------------------
- /** Parse an audio event and assign to the 'AudioEventRTS*' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseDynamicAudioEventRTS( INI *ini, void * /*instance*/, void *store, const void* userData )
- {
- const char *token = ini->getNextToken();
- DynamicAudioEventRTS** theSound = (DynamicAudioEventRTS**)store;
-
- // translate the string into a sound
- if (stricmp(token, "NoSound") == 0)
- {
- if (*theSound)
- {
- (*theSound)->deleteInstance();
- *theSound = NULL;
- }
- }
- else
- {
- if (*theSound == NULL)
- *theSound = newInstance(DynamicAudioEventRTS);
- (*theSound)->m_event.setEventName(AsciiString(token));
- }
-
- if (*theSound)
- TheAudio->getInfoForAudioEvent(&(*theSound)->m_event);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an audio event and assign to the 'AudioEventRTS*' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseAudioEventRTS( INI *ini, void * /*instance*/, void *store, const void* userData )
- {
- const char *token = ini->getNextToken();
- AudioEventRTS *theSound = (AudioEventRTS*)store;
-
- // translate the string into a sound
- if (stricmp(token, "NoSound") != 0) {
- theSound->setEventName(AsciiString(token));
- }
- TheAudio->getInfoForAudioEvent(theSound);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an ThingTemplate and assign to the 'ThingTemplate *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseThingTemplate( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- if (!TheThingFactory)
- {
- DEBUG_CRASH(("TheThingFactory not inited yet"));
- throw ERROR_BUG;
- }
- typedef const ThingTemplate *ConstThingTemplatePtr;
- ConstThingTemplatePtr* theThingTemplate = (ConstThingTemplatePtr*)store;
- if (stricmp(token, "None") == 0)
- {
- *theThingTemplate = NULL;
- }
- else
- {
- const ThingTemplate *tt = TheThingFactory->findTemplate(token); // could be null!
- DEBUG_ASSERTCRASH(tt, ("ThingTemplate %s not found!\n",token));
- // assign it, even if null!
- *theThingTemplate = tt;
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an ArmorTemplate and assign to the 'ArmorTemplate *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseArmorTemplate( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- typedef const ArmorTemplate *ConstArmorTemplatePtr;
- ConstArmorTemplatePtr* theArmorTemplate = (ConstArmorTemplatePtr*)store;
- if (stricmp(token, "None") == 0)
- {
- *theArmorTemplate = NULL;
- }
- else
- {
- const ArmorTemplate *tt = TheArmorStore->findArmorTemplate(token); // could be null!
- DEBUG_ASSERTCRASH(tt, ("ArmorTemplate %s not found!\n",token));
- // assign it, even if null!
- *theArmorTemplate = tt;
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an WeaponTemplate and assign to the 'WeaponTemplate *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseWeaponTemplate( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- typedef const WeaponTemplate *ConstWeaponTemplatePtr;
- ConstWeaponTemplatePtr* theWeaponTemplate = (ConstWeaponTemplatePtr*)store;
- const WeaponTemplate *tt = TheWeaponStore->findWeaponTemplate(token); // could be null!
- DEBUG_ASSERTCRASH(tt || stricmp(token, "None") == 0, ("WeaponTemplate %s not found!\n",token));
- // assign it, even if null!
- *theWeaponTemplate = tt;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an FXList and assign to the 'FXList *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseFXList( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- typedef const FXList *ConstFXListPtr;
- ConstFXListPtr* theFXList = (ConstFXListPtr*)store;
- const FXList *fxl = TheFXListStore->findFXList(token); // could be null!
- DEBUG_ASSERTCRASH(fxl != NULL || stricmp(token, "None") == 0, ("FXList %s not found!\n",token));
- // assign it, even if null!
- *theFXList = fxl;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a particle system and assign to 'ParticleSystemTemplate *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseParticleSystemTemplate( INI *ini, void * /*instance*/, void *store, const void *userData )
- {
- const char *token = ini->getNextToken();
- const ParticleSystemTemplate *pSystemT = TheParticleSystemManager->findTemplate( AsciiString( token ) );
- DEBUG_ASSERTCRASH( pSystemT || stricmp( token, "None" ) == 0, ("ParticleSystem %s not found!\n",token) );
- typedef const ParticleSystemTemplate* ConstParticleSystemTemplatePtr;
- ConstParticleSystemTemplatePtr* theParticleSystemTemplate = (ConstParticleSystemTemplatePtr*)store;
- *theParticleSystemTemplate = pSystemT;
- } // end parseParticleSystemTemplate
- //-------------------------------------------------------------------------------------------------
- /** Parse an DamageFX and assign to the 'DamageFX *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseDamageFX( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- typedef const DamageFX *ConstDamageFXPtr;
- ConstDamageFXPtr* theDamageFX = (ConstDamageFXPtr*)store;
- if (stricmp(token, "None") == 0)
- {
- *theDamageFX = NULL;
- }
- else
- {
- const DamageFX *fxl = TheDamageFXStore->findDamageFX(token); // could be null!
- DEBUG_ASSERTCRASH(fxl, ("DamageFX %s not found!\n",token));
- // assign it, even if null!
- *theDamageFX = fxl;
- }
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse an ObjectCreationList and assign to the 'ObjectCreationList *' at store */
- //-------------------------------------------------------------------------------------------------
- void INI::parseObjectCreationList( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- typedef const ObjectCreationList *ConstObjectCreationListPtr;
- ConstObjectCreationListPtr* theObjectCreationList = (ConstObjectCreationListPtr*)store;
- const ObjectCreationList *ocl = TheObjectCreationListStore->findObjectCreationList(token); // could be null!
- DEBUG_ASSERTCRASH(ocl || stricmp(token, "None") == 0, ("ObjectCreationList %s not found!\n",token));
- // assign it, even if null!
- *theObjectCreationList = ocl;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a upgrade template string and store as template pointer */
- //-------------------------------------------------------------------------------------------------
- void INI::parseUpgradeTemplate( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- if (!TheUpgradeCenter)
- {
- DEBUG_CRASH(("TheUpgradeCenter not inited yet"));
- throw ERROR_BUG;
- }
- const UpgradeTemplate *uu = TheUpgradeCenter->findUpgrade( AsciiString( token ) );
- DEBUG_ASSERTCRASH( uu || stricmp( token, "None" ) == 0, ("Upgrade %s not found!\n",token) );
- typedef const UpgradeTemplate* ConstUpgradeTemplatePtr;
- ConstUpgradeTemplatePtr* theUpgradeTemplate = (ConstUpgradeTemplatePtr *)store;
- *theUpgradeTemplate = uu;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a special power template string and store as template pointer */
- //-------------------------------------------------------------------------------------------------
- void INI::parseSpecialPowerTemplate( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- if (!TheSpecialPowerStore)
- {
- DEBUG_CRASH(("TheSpecialPowerStore not inited yet"));
- throw ERROR_BUG;
- }
- const SpecialPowerTemplate *sPowerT = TheSpecialPowerStore->findSpecialPowerTemplate( AsciiString( token ) );
- if( !sPowerT && stricmp( token, "None" ) != 0 )
- {
- DEBUG_CRASH( ("[LINE: %d in '%s'] Specialpower %s not found!\n", ini->getLineNum(), ini->getFilename().str(), token) );
- }
- typedef const SpecialPowerTemplate* ConstSpecialPowerTemplatePtr;
- ConstSpecialPowerTemplatePtr* theSpecialPowerTemplate = (ConstSpecialPowerTemplatePtr *)store;
- *theSpecialPowerTemplate = sPowerT;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a science string and store as science type */
- //-------------------------------------------------------------------------------------------------
- /* static */void INI::parseScience( INI *ini, void * /*instance*/, void *store, const void *userData )
- {
- const char *token = ini->getNextToken();
- if (!TheScienceStore)
- {
- DEBUG_CRASH(("TheScienceStore not inited yet"));
- throw ERROR_BUG;
- }
- *((ScienceType *)store) = INI::scanScience(token);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a single string token, check for that token in the index list
- * of names provided and store the index into that list.
- *
- * NOTE: Is is assumed that we are going to store the index into
- * a 4 byte integer. This works well for INT and ENUM definitions */
- //-------------------------------------------------------------------------------------------------
- void INI::parseIndexList( INI* ini, void * /*instance*/, void *store, const void* userData )
- {
- ConstCharPtrArray nameList = (ConstCharPtrArray)userData;
- *(Int *)store = scanIndexList(ini->getNextToken(), nameList);
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a single string token, check for that token in the index list
- * of names provided and store the index into that list.
- *
- * NOTE: Is is assumed that we are going to store the index into
- * a 4 byte integer. This works well for INT and ENUM definitions */
- //-------------------------------------------------------------------------------------------------
- void INI::parseByteSizedIndexList( INI* ini, void * /*instance*/, void *store, const void* userData )
- {
- ConstCharPtrArray nameList = (ConstCharPtrArray)userData;
- Int value = scanIndexList(ini->getNextToken(), nameList);
- if (value < 0 || value > 255)
- {
- DEBUG_CRASH(("Bad index list INI::parseByteSizedIndexList"));
- throw ERROR_BUG;
- }
- *(Byte *)store = (Byte)value;
- }
- //-------------------------------------------------------------------------------------------------
- /** Parse a single string token, check for that token in the index list
- * of names provided and store the associated value into that list.
- *
- * NOTE: Is is assumed that we are going to store the index into
- * a 4 byte integer. This works well for INT and ENUM definitions */
- //-------------------------------------------------------------------------------------------------
- void INI::parseLookupList( INI* ini, void * /*instance*/, void *store, const void* userData )
- {
- ConstLookupListRecArray lookupList = (ConstLookupListRecArray)userData;
- *(Int *)store = scanLookupList(ini->getNextToken(), lookupList);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////
-
- //-------------------------------------------------------------------------------------------------
- void MultiIniFieldParse::add(const FieldParse* f, UnsignedInt e)
- {
- if (m_count < MAX_MULTI_FIELDS)
- {
- m_fieldParse[m_count] = f;
- m_extraOffset[m_count] = e;
- ++m_count;
- }
- else
- {
- DEBUG_CRASH(("too many multi-fields in INI::initFromINIMultiProc"));
- throw ERROR_BUG;
- }
- }
- //-------------------------------------------------------------------------------------------------
- void INI::initFromINI( void *what, const FieldParse* parseTable )
- {
- MultiIniFieldParse p;
- p.add(parseTable);
- initFromINIMulti(what, p);
- }
- //-------------------------------------------------------------------------------------------------
- void INI::initFromINIMultiProc( void *what, BuildMultiIniFieldProc proc )
- {
- MultiIniFieldParse p;
- (*proc)(p);
- initFromINIMulti(what, p);
- }
- //-------------------------------------------------------------------------------------------------
- void INI::initFromINIMulti( void *what, const MultiIniFieldParse& parseTableList )
- {
- Bool done = FALSE;
- if( what == NULL )
- {
- DEBUG_ASSERTCRASH( 0, ("INI::initFromINI - Invalid parameters supplied!\n") );
- throw INI_INVALID_PARAMS;
- }
- // read each of the data fields
- while( !done )
- {
- // read next line
- readLine();
- // check for end token
- const char* field = strtok( m_buffer, INI::getSeps() );
- if( field )
- {
- if( stricmp( field, m_blockEndToken ) == 0 )
- {
- done = TRUE;
- }
- else
- {
- Bool found = false;
- for (int ptIdx = 0; ptIdx < parseTableList.getCount(); ++ptIdx)
- {
- int offset = 0;
- void* userData = 0;
- INIFieldParseProc parse = findFieldParse(parseTableList.getNthFieldParse(ptIdx), field, offset, userData);
- if (parse)
- {
- // parse this block and check for parse errors
- try {
- (*parse)( this, what, (char *)what + offset + parseTableList.getNthExtraOffset(ptIdx), userData );
- } catch (...) {
- DEBUG_CRASH( ("[LINE: %d - FILE: '%s'] Error reading field '%s' of block '%s'\n",
- INI::getLineNum(), INI::getFilename().str(), field, m_curBlockStart) );
- char buff[1024];
- sprintf(buff, "[LINE: %d - FILE: '%s'] Error reading field '%s'\n", INI::getLineNum(), INI::getFilename().str(), field);
- throw INIException(buff);
- }
-
- found = true;
- break;
-
- }
- }
- if (!found)
- {
- DEBUG_ASSERTCRASH( 0, ("[LINE: %d - FILE: '%s'] Unknown field '%s' in block '%s'\n",
- INI::getLineNum(), INI::getFilename().str(), field, m_curBlockStart) );
- throw INI_UNKNOWN_TOKEN;
- }
- } // end else
- } // end if
- // sanity check for reaching end of file with no closing end token
- if( done == FALSE && INI::isEOF() == TRUE )
- {
- done = TRUE;
- DEBUG_ASSERTCRASH( 0, ("Error parsing block '%s', in INI file '%s'. Missing '%s' token\n",
- m_curBlockStart, getFilename().str(), m_blockEndToken) );
- throw INI_MISSING_END_TOKEN;
- } // end if
- } // end while
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ const char* INI::getNextToken(const char* seps)
- {
- if (!seps) seps = getSeps();
- const char *token = ::strtok(NULL, seps);
- if (!token)
- throw INI_INVALID_DATA;
- return token;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ const char* INI::getNextTokenOrNull(const char* seps)
- {
- if (!seps) seps = getSeps();
- const char *token = ::strtok(NULL, seps);
- return token;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ ScienceType INI::scanScience(const char* token)
- {
- return TheScienceStore->friend_lookupScience( token );
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ Int INI::scanInt(const char* token)
- {
- Int value;
- if (sscanf( token, "%d", &value ) != 1)
- throw INI_INVALID_DATA;
- return value;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ UnsignedInt INI::scanUnsignedInt(const char* token)
- {
- UnsignedInt value;
- if (sscanf( token, "%u", &value ) != 1) // unsigned int is %u, not %d
- throw INI_INVALID_DATA;
- return value;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ Real INI::scanReal(const char* token)
- {
- Real value;
- if (sscanf( token, "%f", &value ) != 1)
- throw INI_INVALID_DATA;
- return value;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ Real INI::scanPercentToReal(const char* token)
- {
- Real value;
- if (sscanf( token, "%f", &value ) != 1)
- throw INI_INVALID_DATA;
- return value / 100.0f;
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ Int INI::scanIndexList(const char* token, ConstCharPtrArray nameList)
- {
- if( nameList == NULL || nameList[ 0 ] == NULL )
- {
- DEBUG_ASSERTCRASH( 0, ("INTERNAL ERROR! scanIndexList, invalid name list\n") );
- throw INI_INVALID_NAME_LIST;
- }
- // search for matching name
- Int count = 0;
- for(ConstCharPtrArray name = nameList; *name; name++, count++ )
- {
- if( stricmp( *name, token ) == 0 )
- {
- return count;
- }
- }
- DEBUG_CRASH(("token %s is not a valid member of the index list\n",token));
- throw INI_INVALID_DATA;
- return 0; // never executed, but keeps compiler happy
- }
- //-------------------------------------------------------------------------------------------------
- /*static*/ Int INI::scanLookupList(const char* token, ConstLookupListRecArray lookupList)
- {
- if( lookupList == NULL || lookupList[ 0 ].name == NULL )
- {
- DEBUG_ASSERTCRASH( 0, ("INTERNAL ERROR! scanLookupList, invalid name list\n") );
- throw INI_INVALID_NAME_LIST;
- }
- // search for matching name
- Bool found = false;
- for( const LookupListRec* lookup = &lookupList[0]; lookup->name; lookup++ )
- {
- if( stricmp( lookup->name, token ) == 0 )
- {
- return lookup->value;
- found = true;
- break;
- }
- }
- DEBUG_CRASH(("token %s is not a valid member of the lookup list\n",token));
- throw INI_INVALID_DATA;
- return 0; // never executed, but keeps compiler happy
- }
- //-------------------------------------------------------------------------------------------------
- const char* INI::getNextSubToken(const char* expected)
- {
- const char* token = getNextToken(getSepsColon());
- if (stricmp(token, expected) != 0)
- throw INI_INVALID_DATA;
- return getNextToken(getSepsColon());
- }
- //-------------------------------------------------------------------------------------------------
- /**
- * Parse a "random variable".
- * The format is "FIELD = low high [distribution]".
- */
- void INI::parseGameClientRandomVariable( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- GameClientRandomVariable *var = static_cast<GameClientRandomVariable *>(store);
- const char* token;
- token = ini->getNextToken();
- Real low = INI::scanReal(token);
- token = ini->getNextToken();
- Real high = INI::scanReal(token);
- // if omitted, assume uniform
- GameClientRandomVariable::DistributionType type = GameClientRandomVariable::UNIFORM;
- token = ini->getNextTokenOrNull();
- if (token)
- type = (GameClientRandomVariable::DistributionType)INI::scanIndexList(token, GameClientRandomVariable::DistributionTypeNames);
- // set the range of the random variable
- var->setRange( low, high, type );
- }
- //-------------------------------------------------------------------------------------------------
- // parse a duration in msec and convert to duration in frames
- void INI::parseDurationReal( INI *ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- Real val = scanReal(ini->getNextToken());
- *(Real *)store = ConvertDurationFromMsecsToFrames(val);
- }
- //-------------------------------------------------------------------------------------------------
- // parse a duration in msec and convert to duration in integral number of frames, (unsignedint) rounding UP
- void INI::parseDurationUnsignedInt( INI *ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- UnsignedInt val = scanUnsignedInt(ini->getNextToken());
- *(UnsignedInt *)store = (UnsignedInt)ceilf(ConvertDurationFromMsecsToFrames((Real)val));
- }
- // ------------------------------------------------------------------------------------------------
- // parse a duration in msec and convert to duration in integral number of frames, (unsignedshort) rounding UP
- void INI::parseDurationUnsignedShort( INI *ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- UnsignedInt val = scanUnsignedInt(ini->getNextToken());
- *(UnsignedShort *)store = (UnsignedShort)ceilf(ConvertDurationFromMsecsToFrames((Real)val));
- }
- //-------------------------------------------------------------------------------------------------
- // parse acceleration in (dist/sec) and convert to (dist/frame)
- void INI::parseVelocityReal( INI *ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- Real val = scanReal(token);
- *(Real *)store = ConvertVelocityInSecsToFrames(val);
- }
- //-------------------------------------------------------------------------------------------------
- // parse acceleration in (dist/sec^2) and convert to (dist/frame^2)
- void INI::parseAccelerationReal( INI *ini, void * /*instance*/, void *store, const void* /*userData*/ )
- {
- const char *token = ini->getNextToken();
- Real val = scanReal(token);
- *(Real *)store = ConvertAccelerationInSecsToFrames(val);
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::parseVeterancyLevelFlags(INI* ini, void* /*instance*/, void* store, const void* /*userData*/)
- {
- VeterancyLevelFlags flags = VETERANCY_LEVEL_FLAGS_ALL;
- for (const char* token = ini->getNextToken(); token; token = ini->getNextTokenOrNull())
- {
- if (stricmp(token, "ALL") == 0)
- {
- flags = VETERANCY_LEVEL_FLAGS_ALL;
- continue;
- }
- else if (stricmp(token, "NONE") == 0)
- {
- flags = VETERANCY_LEVEL_FLAGS_NONE;
- continue;
- }
- else if (token[0] == '+')
- {
- VeterancyLevel dt = (VeterancyLevel)INI::scanIndexList(token+1, TheVeterancyNames);
- flags = setVeterancyLevelFlag(flags, dt);
- continue;
- }
- else if (token[0] == '-')
- {
- VeterancyLevel dt = (VeterancyLevel)INI::scanIndexList(token+1, TheVeterancyNames);
- flags = clearVeterancyLevelFlag(flags, dt);
- continue;
- }
- else
- {
- throw INI_UNKNOWN_TOKEN;
- }
- }
- *(VeterancyLevelFlags*)store = flags;
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::parseSoundsList( INI* ini, void *instance, void *store, const void* /*userData*/ )
- {
- std::vector<AsciiString> *vec = (std::vector<AsciiString>*) store;
- vec->clear();
- const char* SEPS = " \t,=";
- const char *c = ini->getNextTokenOrNull(SEPS);
- while ( c )
- {
- vec->push_back( c );
- c = ini->getNextTokenOrNull(SEPS);
- }
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::parseDamageTypeFlags(INI* ini, void* /*instance*/, void* store, const void* /*userData*/)
- {
- DamageTypeFlags flags = DAMAGE_TYPE_FLAGS_NONE;
- flags.flip();
- for (const char* token = ini->getNextToken(); token; token = ini->getNextTokenOrNull())
- {
- if (stricmp(token, "ALL") == 0)
- {
- flags = DAMAGE_TYPE_FLAGS_NONE;
- flags.flip();
- continue;
- }
- if (stricmp(token, "NONE") == 0)
- {
- flags = DAMAGE_TYPE_FLAGS_NONE;
- continue;
- }
- if (token[0] == '+')
- {
- DamageType dt = (DamageType)DamageTypeFlags::getSingleBitFromName(token+1);
- flags = setDamageTypeFlag(flags, dt);
- continue;
- }
- if (token[0] == '-')
- {
- DamageType dt = (DamageType)DamageTypeFlags::getSingleBitFromName(token+1);
- flags = clearDamageTypeFlag(flags, dt);
- continue;
- }
- throw INI_UNKNOWN_TOKEN;
- }
- *(DamageTypeFlags*)store = flags;
- }
- //-------------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------------------------------
- void INI::parseDeathTypeFlags(INI* ini, void* /*instance*/, void* store, const void* /*userData*/)
- {
- DeathTypeFlags flags = DEATH_TYPE_FLAGS_ALL;
- for (const char* token = ini->getNextToken(); token; token = ini->getNextTokenOrNull())
- {
- if (stricmp(token, "ALL") == 0)
- {
- flags = DEATH_TYPE_FLAGS_ALL;
- continue;
- }
- if (stricmp(token, "NONE") == 0)
- {
- flags = DEATH_TYPE_FLAGS_NONE;
- continue;
- }
- if (token[0] == '+')
- {
- DeathType dt = (DeathType)INI::scanIndexList(token+1, TheDeathNames);
- flags = setDeathTypeFlag(flags, dt);
- continue;
- }
- if (token[0] == '-')
- {
- DeathType dt = (DeathType)INI::scanIndexList(token+1, TheDeathNames);
- flags = clearDeathTypeFlag(flags, dt);
- continue;
- }
- throw INI_UNKNOWN_TOKEN;
- }
- *(DeathTypeFlags*)store = flags;
- }
- //-------------------------------------------------------------------------------------------------
- // parse the line and return whether the given line is a Block declaration of the form
- // [whitespace] blockType [whitespace] blockName [EOL]
- // both blockType and blockName are case insensitive
- Bool INI::isDeclarationOfType( AsciiString blockType, AsciiString blockName, char *bufferToCheck )
- {
- Bool retVal = true;
- if (!bufferToCheck || blockType.isEmpty() || blockName.isEmpty()) {
- return false;
- }
- // DO NOT RETURN EARLY FROM THIS FUNCTION. (beyond this point)
- // we have to restore the bufferToCheck to its previous state before returning, so
- // it is important to get through all the checks.
-
- char restoreChar;
- char *tempBuff = bufferToCheck;
- int blockTypeLength = blockType.getLength();
- int blockNameLength = blockName.getLength();
- while (isspace(*tempBuff)) {
- ++tempBuff;
- }
-
- if (strlen(tempBuff) > blockTypeLength) {
- restoreChar = tempBuff[blockTypeLength];
- tempBuff[blockTypeLength] = 0;
-
- if (stricmp(blockType.str(), tempBuff) != 0) {
- retVal = false;
- }
- tempBuff[blockTypeLength] = restoreChar;
- tempBuff = tempBuff + blockTypeLength;
- } else {
- retVal = false;
- }
- while (isspace(*tempBuff)) {
- ++tempBuff;
- }
- if (strlen(tempBuff) > blockNameLength) {
- restoreChar = tempBuff[blockNameLength];
- tempBuff[blockNameLength] = 0;
-
- if (stricmp(blockName.str(), tempBuff) != 0) {
- retVal = false;
- }
- tempBuff[blockNameLength] = restoreChar;
- tempBuff = tempBuff + blockNameLength;
- } else {
- retVal = false;
- }
- while (strlen(tempBuff)) {
- retVal = retVal && isspace(tempBuff[0]);
- ++tempBuff;
- }
- return retVal;
- }
- //-------------------------------------------------------------------------------------------------
- // parse the line and return whether the given line is a Block declaration of the form
- // [whitespace] end [EOL]
- Bool INI::isEndOfBlock( char *bufferToCheck )
- {
- Bool retVal = true;
- if (!bufferToCheck) {
- return false;
- }
- // DO NOT RETURN EARLY FROM THIS FUNCTION (beyond this point)
- // we have to restore the bufferToCheck to its previous state before returning, so
- // it is important to get through all the checks.
-
- static const char* endString = "End";
- int endStringLength = strlen(endString);
- char restoreChar;
- char *tempBuff = bufferToCheck;
-
- while (isspace(*tempBuff)) {
- ++tempBuff;
- }
-
- if (strlen(tempBuff) > endStringLength) {
- restoreChar = tempBuff[endStringLength];
- tempBuff[endStringLength] = 0;
-
- if (stricmp(endString, tempBuff) != 0) {
- retVal = false;
- }
- tempBuff[endStringLength] = restoreChar;
- tempBuff = tempBuff + endStringLength;
- } else {
- retVal = false;
- }
- while (strlen(tempBuff)) {
- retVal = retVal && isspace(tempBuff[0]);
- ++tempBuff;
- }
- return retVal;
- }
|