-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNgf.cpp
1115 lines (962 loc) · 41.5 KB
/
Ngf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* =====================================================================================
*
* Filename: Ngf.cpp
*
* Description: NGF implementation
*
* Version: 1.0
* Created: 10/22/2008 11:29:15 AM
*
* Author: Nikhilesh (nikki)
*
* =====================================================================================
*/
#include "OgreScriptLoader.h"
#include "OgreResourceGroupManager.h"
#include "OgreLogManager.h"
#include "Ngf.h"
using namespace std;
using namespace Ogre;
template<> NGF::GameObjectFactory* Ogre::Singleton<NGF::GameObjectFactory>::msSingleton = 0;
template<> NGF::GameObjectManager* Ogre::Singleton<NGF::GameObjectManager>::msSingleton = 0;
template<> NGF::WorldManager* Ogre::Singleton<NGF::WorldManager>::msSingleton = 0;
namespace NGF {
/*
* =====================================================================================
* NGF::PropertyList
* =====================================================================================
*/
Ogre::String PropertyList::getValue(Ogre::String key, unsigned int index, Ogre::String defaultVal)
{
PropertyList::iterator itr = find(key);
if (itr != end())
{
std::vector<Ogre::String> values = itr->second;
if (index < values.size())
{
return values[index];
}
}
return defaultVal;
}
//----------------------------------------------------------------------------------
PropertyList & PropertyList::addProperty(Ogre::String key, Ogre::String values,
Ogre::String delims)
{
std::vector<Ogre::String> vals;
vals.reserve(10);
unsigned int numSplits = 0;
size_t start = 0, pos;
do
{
pos = values.find_first_of(delims, start);
if (pos == start)
{
//Do nothing
start = pos + 1;
}
else if (pos == Ogre::String::npos)
{
//Copy the rest of the string
vals.push_back(values.substr(start));
break;
}
else
{
//Copy up to delimiter
vals.push_back(values.substr(start, pos - start));
start = pos + 1;
}
//Parse up to next real data
start = values.find_first_not_of(delims, start);
++numSplits;
} while (pos != Ogre::String::npos);
insert(PropertyPair(key, vals));
return *this;
}
//----------------------------------------------------------------------------------
PropertyList PropertyList::create(Ogre::String key, Ogre::String values, Ogre::String delims)
{
NGF::PropertyList props;
props.addProperty(key, values, delims);
return props;
}
/*
* =====================================================================================
* NGF::GameObject
* =====================================================================================
*/
GameObject* GameObject::addFlag(Ogre::String flag)
{
if (mFlags.empty())
{
mFlags = "|";
}
mFlags += (flag + "|");
return this;
}
//----------------------------------------------------------------------------------
bool GameObject::removeFlag(Ogre::String flag)
{
std::string::size_type pos1 = mFlags.find("|" + flag + "|");
if (pos1 == Ogre::String::npos)
{
return false;
}
++pos1;
std::string::size_type pos2 = flag.length() + 1;
mFlags.erase(pos1, pos2);
return true;
}
//----------------------------------------------------------------------------------
bool GameObject::hasFlag(Ogre::String flag) const
{
return !(mFlags.find("|" + flag + "|") == Ogre::String::npos);
}
/*
* =====================================================================================
* NGF::GameObjectFactory
* =====================================================================================
*/
GameObjectFactory& GameObjectFactory::getSingleton(void)
{
assert(msSingleton); return *msSingleton;
}
GameObjectFactory* GameObjectFactory::getSingletonPtr(void)
{
return msSingleton;
}
//----------------------------------------------------------------------------------
GameObject* GameObjectFactory::createObject(Ogre::String type, Ogre::Vector3 pos, Ogre::Quaternion rot, PropertyList props, Ogre::String name)
{
CreateFunctionMap::iterator iter = mCreateFunctions.find(type);
if (iter != mCreateFunctions.end())
return iter->second(pos, rot, props, name); //Found.
return 0; //Not found.
}
//----------------------------------------------------------------------------------
GameObject* GameObjectFactory::_createObject(Ogre::String type, ID id, Ogre::Vector3 pos, Ogre::Quaternion rot, PropertyList props, Ogre::String name)
{
IDCreateFunctionMap::iterator iter = mIDCreateFunctions.find(type);
if (iter != mIDCreateFunctions.end())
return iter->second(id, pos, rot, props, name); //Found.
return 0; //Not found.
}
/*
* =====================================================================================
* NGF::GameObjectManager
* =====================================================================================
*/
GameObjectManager* GameObjectManager::getSingletonPtr(void)
{
return msSingleton;
}
GameObjectManager& GameObjectManager::getSingleton(void)
{
assert(msSingleton); return *msSingleton;
}
//----------------------------------------------------------------------------------
GameObjectManager::GameObjectManager()
: mObjectFactory(new GameObjectFactory())
{
}
//----------------------------------------------------------------------------------
void GameObjectManager::tick(bool paused, const Ogre::FrameEvent & evt)
{
std::map<ID,GameObject*>::iterator objIter;
for (objIter = mGameObjectMap.begin();
objIter != mGameObjectMap.end(); ++objIter)
{
GameObject *obj = objIter->second;
if (paused)
{
obj->pausedTick(evt);
}
else
{
obj->unpausedTick(evt);
}
}
std::vector<ID>::iterator iter;
for (iter = mObjectsToDestroy.begin();
iter != mObjectsToDestroy.end(); ++iter)
{
destroyObject(*iter);
}
mObjectsToDestroy.clear();
}
//----------------------------------------------------------------------------------
bool GameObjectManager::destroyObject(ID objID)
{
std::map<ID,GameObject*>::iterator objIter = mGameObjectMap.find(objID);
if (objIter == mGameObjectMap.end())
{
return false;
}
else
{
GameObject *obj = objIter->second;
mGameObjectMap.erase(objIter);
obj->destroy(); //For scripting, as scripting languages are GCed.
delete obj;
return true;
}
}
//----------------------------------------------------------------------------------
void GameObjectManager::destroyAll(void)
{
std::map<ID,GameObject*>::iterator objIter;
std::map<ID,GameObject*> persistentObjects;
for (objIter = mGameObjectMap.begin();
objIter != mGameObjectMap.end();)
{
GameObject *obj = objIter->second;
if (obj->isPersistent()) //If it doesn't want to die...
persistentObjects.insert(*objIter);
else
delete obj;
mGameObjectMap.erase(objIter++);
}
mGameObjectMap = persistentObjects;
}
//----------------------------------------------------------------------------------
GameObject* GameObjectManager::getByID(ID objID) const
{
std::map<ID,GameObject*>::const_iterator objIter = mGameObjectMap.find(objID);
return (objIter == mGameObjectMap.end()) ? NULL : objIter->second;
}
//----------------------------------------------------------------------------------
void GameObjectManager::forEachGameObject(ForEachFunction func)
{
std::map<ID,GameObject*>::iterator objIter;
for (objIter = mGameObjectMap.begin();
objIter != mGameObjectMap.end(); ++objIter)
{
func(objIter->second);
}
}
//----------------------------------------------------------------------------------
void GameObjectManager::sendMessage(GameObject *obj, Message msg) const
{
if (obj)
{
obj->receiveMessage(msg);
}
}
//----------------------------------------------------------------------------------
GameObject* GameObjectManager::getByName(Ogre::String name)
{
GameObject* findObj = NULL;
std::map<ID,GameObject*>::iterator objIter;
for (objIter = mGameObjectMap.begin();
objIter != mGameObjectMap.end(); ++objIter)
{
GameObject *obj = objIter->second;
if (obj->getName() == name)
{
findObj = obj;
break;
}
}
return findObj;
}
/*
* =====================================================================================
* NGF::WorldManager
* =====================================================================================
*/
WorldManager& WorldManager::getSingleton(void)
{
assert(msSingleton); return *msSingleton;
}
WorldManager* WorldManager::getSingletonPtr(void)
{
return msSingleton;
}
//----------------------------------------------------------------------------------
WorldManager::WorldManager()
{
shuttingdown = false;
stoppedLast = false;
}
//----------------------------------------------------------------------------------
WorldManager::~WorldManager()
{
std::vector<World*>::iterator iter;
for (iter = worlds.begin(); iter!= worlds.end(); ++iter)
{
delete *iter;
}
worlds.clear();
}
//----------------------------------------------------------------------------------
void WorldManager::shutdown(void)
{
shuttingdown = true;
}
//----------------------------------------------------------------------------------
bool WorldManager::tick(const Ogre::FrameEvent &evt)
{
if (!shuttingdown)
{
worlds[currentWorld]->tick(evt);
return true;
}
else
{
if (!stoppedLast)
{
worlds[currentWorld]->stop();
stoppedLast = true;
}
return false;
}
}
//----------------------------------------------------------------------------------
void WorldManager::addWorld(World *newWorld)
{
worlds.push_back(newWorld);
}
//----------------------------------------------------------------------------------
void WorldManager::start(unsigned int firstWorld)
{
if (firstWorld <= worlds.size() && firstWorld != -1)
{
currentWorld = firstWorld;
worlds[currentWorld]->init();
}
else
{
OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Bad world index given", "NGF::WorldManager::start()");
}
}
//----------------------------------------------------------------------------------
void WorldManager::nextWorld(void)
{
if ((currentWorld + 1) < worlds.size())
{
worlds[currentWorld]->stop();
worlds[++currentWorld]->init();
}
else
{
shutdown();
}
}
//----------------------------------------------------------------------------------
bool WorldManager::previousWorld(void)
{
if(currentWorld != 0)
{
worlds[currentWorld]->stop();
worlds[--currentWorld]->init();
return true;
}
return false;
}
//----------------------------------------------------------------------------------
void WorldManager::gotoWorld(unsigned int worldNumber)
{
if (worldNumber < worlds.size() && worldNumber != -1)
{
worlds[currentWorld]->stop();
worlds[(currentWorld = worldNumber)]->init();
}
else
{
OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Bad world index given", "NGF::WorldManager::gotoWorld()");
}
}
//----------------------------------------------------------------------------------
void WorldManager::removeWorld(unsigned int worldNumber)
{
if (worldNumber < worlds.size() && worldNumber != -1)
{
if (worldNumber == currentWorld)
previousWorld();
delete worlds[worldNumber];
worlds.erase(worlds.begin() + worldNumber);
}
else
{
OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Bad world index given", "NGF::WorldManager::removeWorld()");
}
}
/*
* =====================================================================================
* NGF::Loading
* =====================================================================================
*/
namespace Loading {
//The parser class definitions.
class ConfigNode
{
public:
ConfigNode(ConfigNode *parent, const Ogre::String &name = "untitled");
~ConfigNode();
inline void setName(const Ogre::String &name)
{
this->name = name;
}
inline Ogre::String &getName()
{
return name;
}
inline void addValue(const Ogre::String &value)
{
values.push_back(value);
}
inline void clearValues()
{
values.clear();
}
inline std::vector<Ogre::String> &getValues()
{
return values;
}
inline const Ogre::String &getValue(unsigned int index = 0)
{
assert(index < values.size());
return values[index];
}
inline float getValueF(unsigned int index = 0)
{
assert(index < values.size());
return Ogre::StringConverter::parseReal(values[index]);
}
inline double getValueD(unsigned int index = 0)
{
assert(index < values.size());
std::istringstream str(values[index]);
double ret = 0;
str >> ret;
return ret;
}
inline int getValueI(unsigned int index = 0)
{
assert(index < values.size());
return Ogre::StringConverter::parseInt(values[index]);
}
ConfigNode *addChild(const Ogre::String &name = "untitled", bool replaceExisting = false);
ConfigNode *findChild(const Ogre::String &name, bool recursive = false);
inline std::vector<ConfigNode*> &getChildren()
{
return children;
}
inline ConfigNode *getChild(unsigned int index = 0)
{
assert(index < children.size());
return children[index];
}
void setParent(ConfigNode *newParent);
inline ConfigNode *getParent()
{
return parent;
}
private:
Ogre::String name;
std::vector<Ogre::String> values;
std::vector<ConfigNode*> children;
ConfigNode *parent;
int lastChildFound; //The last child node's index found with a call to findChild()
std::vector<ConfigNode*>::iterator _iter;
bool _removeSelf;
};
class ConfigScriptLoader: public Ogre::ScriptLoader
{
public:
ConfigScriptLoader(Ogre::String);
~ConfigScriptLoader();
inline static ConfigScriptLoader &getSingleton() { return *singletonPtr; }
inline static ConfigScriptLoader *getSingletonPtr() { return singletonPtr; }
Ogre::Real getLoadingOrder() const;
const Ogre::StringVector &getScriptPatterns() const;
ConfigNode *getConfigScript(const Ogre::String &type, const Ogre::String &name);
std::vector<std::string> getScriptsOfType(const Ogre::String &type);
void parseScript(Ogre::DataStreamPtr &stream, const Ogre::String &groupName);
private:
static ConfigScriptLoader *singletonPtr;
typedef std::map<std::string, std::vector<std::string> > ScriptMap;
ScriptMap scriptListMap;
Ogre::Real mLoadOrder;
Ogre::StringVector mScriptPatterns;
std::map<Ogre::String, ConfigNode*> scriptList;
//Parsing
char *parseBuff, *parseBuffEnd, *buffPtr;
size_t parseBuffLen;
enum Token
{
TOKEN_Text,
TOKEN_NewLine,
TOKEN_OpenBrace,
TOKEN_CloseBrace,
TOKEN_EOF,
};
Token tok, lastTok;
Ogre::String tokVal, lastTokVal;
char *lastTokPos;
void _parseNodes(ConfigNode *parent);
void _nextToken();
void _prevToken();
};
//----------------------------------------------------------------------------------
Loader::Loader(LoaderHelperFunction help)
{
mHelper = help;
mUseFactory = (help == NULL);
mGameMgr = GameObjectManager::getSingletonPtr();
new ConfigScriptLoader("*.ngf");
}
//----------------------------------------------------------------------------------
void Loader::loadLevel(Ogre::String levelname, Ogre::Vector3 displace, Ogre::Quaternion rotate)
{
//Get the script and its children (the objects).
ConfigNode *lvl = ConfigScriptLoader::getSingleton().getConfigScript("ngflevel", levelname);
if (!lvl)
{
OGRE_EXCEPT(Ogre::Exception::ERR_FILE_NOT_FOUND, "NGF level not found!", "NGF::Loading::Loader::loadNGF()");
return;
}
std::vector<ConfigNode*> objs = lvl->getChildren();
//Iterate through the children and do stuff.
for (std::vector<ConfigNode*>::iterator i = objs.begin(); i != objs.end(); ++i)
{
ConfigNode *obj = (*i);
//Get the type and name.
Ogre::String type = obj->findChild("type")->getValues()[0];
Ogre::String name = obj->findChild("name")->getValues()[0];
//Get the position.
std::vector<Ogre::String> posCoord = obj->findChild("position")->getValues();
Ogre::Vector3 pos(Ogre::StringConverter::parseReal(posCoord[0]),
Ogre::StringConverter::parseReal(posCoord[1]),
Ogre::StringConverter::parseReal(posCoord[2]));
//Displace it accordingly.
pos = rotate * pos;
pos += displace;
//Get the rotation.
std::vector<Ogre::String> rotCoord = obj->findChild("rotation")->getValues();
Ogre::Quaternion rot( Ogre::StringConverter::parseReal(rotCoord[0]),
Ogre::StringConverter::parseReal(rotCoord[1]),
Ogre::StringConverter::parseReal(rotCoord[2]),
Ogre::StringConverter::parseReal(rotCoord[3]));
//Displace it accordingly.
rot = rot * rotate;
//Since there are property keys and each key has more than one value, we have a lot to do.
PropertyList properties;
ConfigNode *propNode = obj->findChild("properties");
//Some objects might not store properties.
if (propNode)
{
//We get the keys and iterate through them.
std::vector<ConfigNode*> props = propNode->getChildren();
for (std::vector<ConfigNode*>::iterator j = props.begin(); j != props.end(); ++j)
{
//Put the key and its values in the map. Luckily, a ConfigNode can return an std::vector
//containing all its values, so we don't have to iterate through them.
ConfigNode *prop = (*j);
properties.insert(PropertyPair(prop->getName(), prop->getValues()));
}
}
else
{
//Use an empty property list in case the object doesn't have any properties.
properties = PropertyList();
}
//Call the callback function.
if (mUseFactory)
mGameMgr->createObject(type, pos, rot, properties, name);
else
mHelper(type, name, pos, rot, properties);
}
}
//----------------------------------------------------------------------------------
std::vector<Ogre::String> Loader::getLevels()
{
return ConfigScriptLoader::getSingleton().getScriptsOfType("ngflevel");
}
//----------------------------------------------------------------------------------
ConfigScriptLoader *ConfigScriptLoader::singletonPtr = NULL;
//----------------------------------------------------------------------------------
ConfigScriptLoader::ConfigScriptLoader(Ogre::String pattern = "*.object")
{
//Init singleton
if (singletonPtr)
OGRE_EXCEPT(1, "Multiple ConfigScriptManager objects are not allowed", "ConfigScriptManager::ConfigScriptManager()");
singletonPtr = this;
//Register as a ScriptLoader
mLoadOrder = 100.0f;
mScriptPatterns.push_back(pattern);
ResourceGroupManager::getSingleton()._registerScriptLoader(this);
}
//----------------------------------------------------------------------------------
ConfigScriptLoader::~ConfigScriptLoader()
{
singletonPtr = NULL;
//Delete all scripts
std::map<String, ConfigNode*>::iterator i;
for (i = scriptList.begin(); i != scriptList.end(); i++){
delete i->second;
}
scriptList.clear();
//Unregister with resource group manager
if (ResourceGroupManager::getSingletonPtr())
ResourceGroupManager::getSingleton()._unregisterScriptLoader(this);
}
//----------------------------------------------------------------------------------
Real ConfigScriptLoader::getLoadingOrder() const
{
return mLoadOrder;
}
//----------------------------------------------------------------------------------
const StringVector &ConfigScriptLoader::getScriptPatterns() const
{
return mScriptPatterns;
}
//----------------------------------------------------------------------------------
ConfigNode *ConfigScriptLoader::getConfigScript(const String &type, const String &name)
{
std::map<String, ConfigNode*>::iterator i;
String key = type + ' ' + name;
i = scriptList.find(key);
//If found..
if (i != scriptList.end())
return i->second;
else
return NULL;
}
//----------------------------------------------------------------------------------
std::vector<std::string> ConfigScriptLoader::getScriptsOfType(const Ogre::String &type)
{
ScriptMap::iterator scripts = scriptListMap.find(type);
if (scripts != scriptListMap.end())
{
return (*scripts).second;
}
return std::vector<std::string>();
}
//----------------------------------------------------------------------------------
void ConfigScriptLoader::parseScript(DataStreamPtr &stream, const String &groupName)
{
//Copy the entire file into a buffer for fast access
parseBuffLen = stream->size();
parseBuff = new char[parseBuffLen];
buffPtr = parseBuff;
stream->read(parseBuff, parseBuffLen);
parseBuffEnd = parseBuff + parseBuffLen;
//Close the stream (it's no longer needed since everything is in parseBuff)
//stream->close(); //Commented out until ZipDataStream 'double close' problem is fixed.
//Get first token
_nextToken();
if (tok == TOKEN_EOF)
return;
//Parse the script
_parseNodes(0);
if (tok == TOKEN_CloseBrace)
OGRE_EXCEPT(1, "Parse Error: Closing brace out of place", "ConfigScript::load()");
//Delete the buffer
delete[] parseBuff;
}
//----------------------------------------------------------------------------------
void ConfigScriptLoader::_nextToken()
{
lastTok = tok;
lastTokVal = tokVal;
lastTokPos = buffPtr;
//EOF token
if (buffPtr >= parseBuffEnd){
tok = TOKEN_EOF;
return;
}
//(Get next character)
int ch = *buffPtr++;
while (ch == ' ' || ch == 9){ //Skip leading spaces / tabs
ch = *buffPtr++;
}
//Newline token
if (ch == '\r' || ch == '\n'){
do {
ch = *buffPtr++;
} while ((ch == '\r' || ch == '\n') && buffPtr < parseBuffEnd);
buffPtr--;
tok = TOKEN_NewLine;
return;
}
//Open brace token
else if (ch == '{'){
tok = TOKEN_OpenBrace;
return;
}
//Close brace token
else if (ch == '}'){
tok = TOKEN_CloseBrace;
return;
}
//Text token, verify valid char
if (ch < 32 || ch > 122)
OGRE_EXCEPT(1, "Parse Error: Invalid character", "ConfigScript::load()");
tokVal = "";
tok = TOKEN_Text;
//Very hacky parsing here. Please don't use this as an example for parsing. :-)
if (ch == '"') {
ch = *buffPtr++; //Skip the " character.
do {
//Skip comments
if (ch == '/'){
int ch2 = *buffPtr;
//C++ style comment (//)
if (ch2 == '/'){
buffPtr++;
do {
ch = *buffPtr++;
} while (ch != '\r' && ch != '\n' && buffPtr < parseBuffEnd);
tok = TOKEN_NewLine;
return;
}
}
//Add valid char to tokVal
tokVal += ch;
//Next char
ch = *buffPtr++;
} while (ch >= 32 && ch <= 122 && ch != '"' && buffPtr < parseBuffEnd);
}
else
{
do {
//Skip comments
if (ch == '/'){
int ch2 = *buffPtr;
//C++ style comment (//)
if (ch2 == '/'){
buffPtr++;
do {
ch = *buffPtr++;
} while (ch != '\r' && ch != '\n' && buffPtr < parseBuffEnd);
tok = TOKEN_NewLine;
return;
}
}
//':' means 'string to end of line', and all next lines starting with ':'.
if (ch == ':')
{
again:
++buffPtr;
do {
ch = *buffPtr++;
if (ch != '\r' && ch != '\n')
tokVal += ch;
} while (ch != '\r' && ch != '\n' && buffPtr < parseBuffEnd);
//(Get next character)
char *old = (buffPtr - 1);
ch = *buffPtr++;
while (ch == '\r' || ch == '\n' || ch == ' ' || ch == 9) { //Skip any other stuff.
ch = *buffPtr++;
}
if (ch == ':')
{
tokVal += '\n';
goto again;
}
buffPtr = old;
return;
}
//Add valid char to tokVal
tokVal += ch;
//Next char
ch = *buffPtr++;
} while (ch > 32 && ch <= 122 && buffPtr < parseBuffEnd);
}
buffPtr--;
return;
}
//----------------------------------------------------------------------------------
void ConfigScriptLoader::_prevToken()
{
tok = lastTok;
tokVal = lastTokVal;
buffPtr = lastTokPos;
}
//----------------------------------------------------------------------------------
void ConfigScriptLoader::_parseNodes(ConfigNode *parent)
{
typedef std::pair<String, ConfigNode*> ScriptItem;
while (1) {
switch (tok){
//Node
case TOKEN_Text:
//Add the new node
ConfigNode *newNode;
if (parent)
newNode = parent->addChild(tokVal);
else
newNode = new ConfigNode(0, tokVal);
//Get values
_nextToken();
while (tok == TOKEN_Text){
newNode->addValue(tokVal);
_nextToken();
}
//Add root nodes to scriptList
if (!parent){
String key;
if (newNode->getValues().empty())
{
key = newNode->getName() + ' ';
}
else
{
String name = newNode->getValues().front();
String type = newNode->getName();
key = type + ' ' + name;
ScriptMap::iterator scripts = scriptListMap.find(type);
if (scripts == scriptListMap.end())
{
std::vector<std::string> newVector;
newVector.push_back(name);
scriptListMap.insert(std::pair<std::string, std::vector<std::string> >
(type, newVector));
}
else
{
((*scripts).second).push_back(name);
}
}
scriptList.insert(ScriptItem(key, newNode));
}
//Skip any blank spaces
while (tok == TOKEN_NewLine)
_nextToken();
//Add any sub-nodes
if (tok == TOKEN_OpenBrace){
//Parse nodes
_nextToken();
_parseNodes(newNode);
//Skip blank spaces
while (tok == TOKEN_NewLine)
_nextToken();
//Check for matching closing brace
if (tok != TOKEN_CloseBrace)
OGRE_EXCEPT(1, "Parse Error: Expecting closing brace", "ConfigScript::load()");
} else {
//If it's not a opening brace, back up so the system will parse it properly
_prevToken();
}
break;
//Out of place brace
case TOKEN_OpenBrace:
OGRE_EXCEPT(1, "Parse Error: Opening brace out of plane", "ConfigScript::load()");
break;
//Return if end of nodes have been reached
case TOKEN_CloseBrace:
return;
//Return if reached end of file
case TOKEN_EOF:
return;
}
//Next token