Linux multi-monitor fullscreen support
[ryzomcore.git] / studio / src / plugins / mission_compiler / validation_file.cpp
blobc284f50cd76b906b0a4d90cfa180a0407325eb9e
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)
8 {
9 // load the configuration file
10 NLMISC::CConfigFile cf;
11 std::string pathName = NLMISC::CPath::lookup(filename, false);
13 if (pathName.empty())
15 nlwarning("Can't find index file '%s' in search path, no mission will be valid", filename.c_str());
16 return;
18 cf.load(pathName);
20 // get the variable
21 NLMISC::CConfigFile::CVar* var = cf.getVarPtr("AuthorizedStates");
22 if (var)
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");
29 if (var)
30 missionStatesFields = var->asInt();
31 else
32 nlwarning("Mission validation file does not contain MissionStatesFields variable. Parsing may fail and corrupt data.");
34 var = cf.getVarPtr("MissionStates");
35 if (var)
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);
52 if (pathName.empty())
54 nlwarning("Can't find index file '%s' in search path, no mission will be valid", filename.c_str());
55 return;
57 FILE* file = nlfopen(pathName, "w");
58 nlassert(file!=NULL);
60 // AuthorizedStates
61 fprintf(file, "%s",
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
71 fprintf(file, "%s",
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");
78 // MissionStates
79 fprintf(file, "%s",
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");
92 fclose(file);
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)
98 uint32 sum = 0;
99 size_t size = content.length()/4;
100 for (size_t i=0; i<size; ++i)
102 uint32 val = 0;
103 for (int j=0; j<4; ++j)
104 val += content[4*i+j]<<8*j;
105 sum += val;
106 if (sum&1)
107 sum = sum>>1 | 0x80000000;
108 else
109 sum = sum>>1;
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!!!!!!");
122 return false;
125 // parse them
126 std::string content;
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);
133 return true;