MCPcopy Create free account
hub / github.com/TurningWheel/Barony / writeToDocument

Method writeToDocument

src/mod_tools.cpp:283–349  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

281}
282
283void GameModeManager_t::Tutorial_t::writeToDocument()
284{
285 if ( levels.empty() )
286 {
287 printlog("[JSON]: Could not write tutorial scores to file due to empty levels vector.");
288 return;
289 }
290
291 if ( !PHYSFS_getRealDir(tutorialScoresFilename.c_str()) )
292 {
293 printlog("[JSON]: Error file %s does not exist", tutorialScoresFilename.c_str());
294 return;
295 }
296
297 std::string inputPath = PHYSFS_getRealDir(tutorialScoresFilename.c_str());
298 inputPath.append(tutorialScoresFilename.c_str());
299
300 File* fp = FileIO::open(inputPath.c_str(), "rb");
301 if ( !fp )
302 {
303 printlog("[JSON]: Error: Could not locate json file %s", inputPath.c_str());
304 return;
305 }
306 char buf[65536];
307 int count = fp->read(buf, sizeof(buf[0]), sizeof(buf) - 1);
308 buf[count] = '\0';
309 rapidjson::StringStream is(buf);
310 FileIO::close(fp);
311
312 rapidjson::Document d;
313 d.ParseStream(is);
314
315 if ( !d.HasMember("version") || !d.HasMember("levels") )
316 {
317 printlog("[JSON]: File %s corrupt, recreating...", inputPath.c_str());
318 d.Clear();
319 d.SetObject();
320 CustomHelpers::addMemberToRoot(d, "version", rapidjson::Value(1));
321 CustomHelpers::addMemberToRoot(d, "first_time_prompt", rapidjson::Value(FirstTimePrompt.showFirstTimePrompt));
322 CustomHelpers::addMemberToRoot(d, "first_tutorial_complete", rapidjson::Value(firstTutorialCompleted));
323
324 rapidjson::Value levelsObj(rapidjson::kObjectType);
325 CustomHelpers::addMemberToRoot(d, "levels", levelsObj);
326 for ( auto it = levels.begin(); it != levels.end(); ++it )
327 {
328 rapidjson::Value level(rapidjson::kObjectType);
329 level.AddMember("completion_time", rapidjson::Value(it->completionTime), d.GetAllocator());
330 CustomHelpers::addMemberToSubkey(d, "levels", it->filename, level);
331 }
332 }
333 else
334 {
335 d["first_time_prompt"].SetBool(this->FirstTimePrompt.showFirstTimePrompt);
336 if ( !d.HasMember("first_tutorial_complete") )
337 {
338 CustomHelpers::addMemberToRoot(d, "first_tutorial_complete", rapidjson::Value(false));
339 }
340 d["first_tutorial_complete"].SetBool(this->firstTutorialCompleted);

Callers 4

onClickEntryMethod · 0.80
buttonSkipPromptMethod · 0.80
actCustomPortalFunction · 0.80

Calls 9

printlogFunction · 0.85
c_strMethod · 0.80
appendMethod · 0.80
SetBoolMethod · 0.80
emptyMethod · 0.45
readMethod · 0.45
ClearMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected