File: TestData.cpp

package info (click to toggle)
endless-sky 0.10.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 414,608 kB
  • sloc: cpp: 73,435; python: 893; xml: 666; sh: 271; makefile: 28
file content (142 lines) | stat: -rw-r--r-- 4,105 bytes parent folder | download
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
/* TestData.cpp
Copyright (c) 2019-2020 by Peter van der Meer

Endless Sky 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.

Endless Sky 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 <https://www.gnu.org/licenses/>.
*/

#include "TestData.h"

#include "../DataFile.h"
#include "../DataNode.h"
#include "../DataWriter.h"
#include "../Files.h"
#include "../GameData.h"
#include "../Mission.h"
#include "../UniverseObjects.h"

using namespace std;



const string &TestData::Name() const
{
	return dataSetName;
}



// Loader to load the generic test-data entry
void TestData::Load(const DataNode &node, const filesystem::path &sourceDataFilePath)
{
	sourceDataFile = sourceDataFilePath;
	if(node.Size() < 2)
	{
		node.PrintTrace("Error: Unnamed test data:");
		return;
	}
	if(node.Token(0) != "test-data")
	{
		node.PrintTrace("Error: Unsupported root node:");
		return;
	}
	dataSetName = node.Token(1);

	for(const DataNode &child : node)
		// Only need to parse the category for now. The contents will be
		// scanned for at write-out of the test-data.
		if(child.Token(0) == "category" && child.Size() >= 2)
		{
			if(child.Token(1) == "savegame")
				dataSetType = Type::SAVEGAME;
			else if(child.Token(1) == "mission")
				dataSetType = Type::MISSION;
			else
				child.PrintTrace("Skipping unsupported category:");
		}
}



// Inject the test-data to the proper location.
bool TestData::Inject(const ConditionsStore *playerConditions,
	const set<const System *> *visitedSystems, const set<const Planet *> *visitedPlanets) const
{
	// Check if we have the required data to inject.
	if(dataSetName.empty() || sourceDataFile.empty())
		return false;

	// Determine data-type and call the relevant function.
	switch(dataSetType)
	{
		case Type::SAVEGAME:
			return InjectSavegame();
		case Type::MISSION:
			return InjectMission(playerConditions, visitedSystems, visitedPlanets);
		default:
			return false;
	}
}



const DataNode *TestData::GetContentsNode(const DataFile &sourceData) const
{
	for(const DataNode &rootNode : sourceData)
		// Check if we have found our dataset.
		if(rootNode.Size() > 1 && rootNode.Token(0) == "test-data" && rootNode.Token(1) == dataSetName)
			// Scan for the contents tag
			for(const DataNode &dataNode : rootNode)
				if(dataNode.Token(0) == "contents")
					return &dataNode;
	return nullptr;
}



// Write out testdata as savegame into the saves directory.
bool TestData::InjectSavegame() const
{
	const DataFile sourceData(sourceDataFile);
	// Get the contents node in the test data.
	const auto &nodePtr = GetContentsNode(sourceData);
	if(!nodePtr)
		return false;
	const DataNode &dataNode = *nodePtr;
	// Then write out the complete contents to the target file
	// Savegame data is written to the saves directory. Other test data
	// types might be injected differently, e.g. direct object loading.
	DataWriter dataWriter(Files::Saves() / (dataSetName + ".txt"));
	for(const DataNode &child : dataNode)
		dataWriter.Write(child);

	// Data was found and written. We are done successfully.
	return true;
}



bool TestData::InjectMission(const ConditionsStore *playerConditions,
	const set<const System *> *visitedSystems, const set<const Planet *> *visitedPlanets) const
{
	const DataFile sourceData(sourceDataFile);
	// Get the contents node in the test data.
	const auto &nodePtr = GetContentsNode(sourceData);
	if(!nodePtr)
		return false;

	const DataNode &dataNode = *nodePtr;
	for(const DataNode &node : dataNode)
		if(node.Token(0) == "mission" && node.Size() > 1)
			GameData::Objects().missions.Get(node.Token(1))->Load(node, playerConditions, visitedSystems, visitedPlanets);

	return true;
}