1 #include "validation_file.h"
3 #include <nel/misc/config_file.h>
4 #include <nel/misc/common.h>
5 #include <nel/misc/path.h>
7 void CValidationFile::loadMissionValidationFile(std::string filename
)
9 // load the configuration file
10 NLMISC::CConfigFile cf
;
11 std::string pathName
= NLMISC::CPath::lookup(filename
, false);
15 nlwarning("Can't find index file '%s' in search path, no mission will be valid", filename
.c_str());
21 NLMISC::CConfigFile::CVar
* var
= cf
.getVarPtr("AuthorizedStates");
24 for (uint i
=0; i
<var
->size(); ++i
)
25 _AuthorizedStates
.push_back(var
->asString(i
));
27 int missionStatesFields
= 3;
28 var
= cf
.getVarPtr("MissionStatesFields");
30 missionStatesFields
= var
->asInt();
32 nlwarning("Mission validation file does not contain MissionStatesFields variable. Parsing may fail and corrupt data.");
34 var
= cf
.getVarPtr("MissionStates");
37 for (uint i
=0; i
<var
->size()/missionStatesFields
; ++i
)
39 std::string mission
= var
->asString(i
*missionStatesFields
);
40 std::string stateName
= var
->asString(i
*missionStatesFields
+1);
41 std::string hashKey
= var
->asString(i
*missionStatesFields
+2);
42 _MissionStates
.insert(std::make_pair(mission
, CMissionState(mission
, stateName
, hashKey
)));
47 void CValidationFile::saveMissionValidationFile(std::string filename
)
49 // load the configuration file
50 std::string pathName
= NLMISC::CPath::lookup(filename
, false);
54 nlwarning("Can't find index file '%s' in search path, no mission will be valid", filename
.c_str());
57 FILE* file
= nlfopen(pathName
, "w");
62 "// AuthorizedStates contains the list of authorized states. EGS mission\n"
63 "// manager can accept any number of states. Default state is the first one.\n"
64 "AuthorizedStates = {\n");
65 std::deque
<std::string
>::iterator itAuth
, itAuthEnd
= _AuthorizedStates
.end();
66 for (itAuth
=_AuthorizedStates
.begin(); itAuth
!=itAuthEnd
; ++itAuth
)
67 fprintf(file
, "\t\"%s\",\n", itAuth
->c_str());
68 fprintf(file
, "%s", "};\n\n");
70 // MissionStatesFields
72 "// MissionStatesFields contains the number of fields in MissionStates, for\n"
73 "// future compatibility purpose.\n"
74 "MissionStatesFields = ");
75 fprintf(file
, "%d", 3); // 3 fields: name, state, hash key
76 fprintf(file
, "%s", ";\n\n");
80 "// MissionStates contains a list of mission with for each the state of the\n"
81 "// mission and its hash key. The tool will add new missions with the default\n"
82 "// state. It will flag missions with a modified hash key with default state to\n"
83 "// prevent untested modified missions to be published.\n"
84 "// :NOTE: You can add a field to this structure without the need to modify EGS\n"
85 "// code. Simply update MissionStatesFields.\n"
86 "MissionStates = {\n");
87 TMissionStateContainer::iterator itMission
, itMissionEnd
= _MissionStates
.end();
88 for (itMission
=_MissionStates
.begin(); itMission
!=itMissionEnd
; ++itMission
)
89 fprintf(file
, "\t%-42s %-12s \"%s\",\n", ("\""+itMission
->second
.name
+"\",").c_str(), ("\""+itMission
->second
.state
+"\",").c_str(), itMission
->second
.hashKey
.c_str());
90 fprintf(file
, "};\n\n");
95 // :NOTE: This function exists in mission_template.cpp. If you change it here modify the other file.
96 std::string
buildHashKey(std::string
const& content
)
99 size_t size
= content
.length()/4;
100 for (size_t i
=0; i
<size
; ++i
)
103 for (int j
=0; j
<4; ++j
)
104 val
+= content
[4*i
+j
]<<8*j
;
107 sum
= sum
>>1 | 0x80000000;
111 return NLMISC::toString("0x%08X", sum
);
114 bool CMission::parsePrim(NLLIGO::IPrimitive
const* prim
)
116 // init default values
117 std::vector
<std::string
>* params
;
118 // get the mission script
119 if (!prim
->getPropertyByName("script", params
) || !params
)
121 nlwarning("ERROR : cant find mission script!!!!!!");
127 std::vector
<std::string
>::iterator itParam
, itParamEnd
= params
->end();
128 for (itParam
=params
->begin(); itParam
!=itParamEnd
; ++itParam
)
130 content
+= *itParam
+ "\n";
132 hashKey
= buildHashKey(content
);