summaryrefslogtreecommitdiffstats
path: root/source/cFileFormatUpdater.cpp
blob: d17a35bb012eedb70ff2029c33fcab553dc8d1b6 (plain) (blame)
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

#include "Globals.h"

#include "cFileFormatUpdater.h"
#include "Vector3d.h"
#include "Vector3f.h"

#include "cItem.h"
#include <json/json.h>





typedef std::list< std::string > StringList;
StringList GetDirectoryContents( const char* a_Directory );





void cFileFormatUpdater::UpdateFileFormat()
{
	UpdatePlayersOfWorld("world");
}





// Convert player .bin files to JSON
void cFileFormatUpdater::UpdatePlayersOfWorld( const char* a_WorldName )
{
	std::string PlayerDir = std::string( a_WorldName ) + "/player/";

	StringList AllFiles = GetDirectoryContents( PlayerDir.c_str() );
	for( StringList::iterator itr = AllFiles.begin(); itr != AllFiles.end(); ++itr )
	{
		std::string & FileName = *itr;
		if( FileName.rfind(".bin") != std::string::npos ) // Get only the files ending in .bin
		{
			PlayerBINtoJSON( (PlayerDir + FileName).c_str() );
		}
	}
}





#define READ(File, Var) \
	if (File.Read(&Var, sizeof(Var)) != sizeof(Var)) \
	{ \
		LOGERROR("ERROR READING \"%s\" FROM FILE \"%s\"", #Var, a_FileName); \
		return; \
	}

// Converts player binary files to human readable JSON
void cFileFormatUpdater::PlayerBINtoJSON( const char* a_FileName )
{
	Vector3d	PlayerPos;
	Vector3f	PlayerRot;
	short		PlayerHealth = 0;

	const unsigned int NumInventorySlots = 45; // At this time the player inventory has/had 45 slots
	cItem IventoryItems[ NumInventorySlots ];

	cFile f;
	if (!f.Open(a_FileName, cFile::fmRead))
	{
		return;
	}

	// First read player position, rotation and health
	READ(f, PlayerPos.x);
	READ(f, PlayerPos.y);
	READ(f, PlayerPos.z);
	READ(f, PlayerRot.x);
	READ(f, PlayerRot.y);
	READ(f, PlayerRot.z);
	READ(f, PlayerHealth);


	for(unsigned int i = 0; i < NumInventorySlots; i++)
	{
		cItem & Item = IventoryItems[i];
		READ(f, Item.m_ItemID);
		READ(f, Item.m_ItemCount);
		READ(f, Item.m_ItemHealth);
	}
	f.Close();

	// Loaded all the data, now create the JSON data
	Json::Value JSON_PlayerPosition;
	JSON_PlayerPosition.append( Json::Value( PlayerPos.x ) );
	JSON_PlayerPosition.append( Json::Value( PlayerPos.y ) );
	JSON_PlayerPosition.append( Json::Value( PlayerPos.z ) );

	Json::Value JSON_PlayerRotation;
	JSON_PlayerRotation.append( Json::Value( PlayerRot.x ) );
	JSON_PlayerRotation.append( Json::Value( PlayerRot.y ) );
	JSON_PlayerRotation.append( Json::Value( PlayerRot.z ) );

	Json::Value JSON_Inventory;
	for(unsigned int i = 0; i < NumInventorySlots; i++)
	{
		Json::Value JSON_Item;
		IventoryItems[i].GetJson( JSON_Item );
		JSON_Inventory.append( JSON_Item );
	}

	Json::Value root;
	root["position"] = JSON_PlayerPosition;
	root["rotation"] = JSON_PlayerRotation;
	root["inventory"] = JSON_Inventory;
	root["health"] = PlayerHealth;

	Json::StyledWriter writer;
	std::string JsonData = writer.write( root );

	// Get correct filename
	std::string FileName = a_FileName;
	std::string FileNameWithoutExt = FileName.substr(0, FileName.find_last_of(".") );
	std::string FileNameJson = FileNameWithoutExt + ".json";

	// Write to file
	if (!f.Open(FileNameJson.c_str(), cFile::fmWrite))
	{
		return;
	}
	if (f.Write(JsonData.c_str(), JsonData.size()) != JsonData.size())
	{
		LOGERROR("ERROR WRITING PLAYER JSON TO FILE \"%s\"", FileNameJson.c_str());
		return;
	}
	f.Close();

	// Delete old format file, only do this when conversion has succeeded
	if (std::remove(a_FileName) != 0)
	{
		LOGERROR("COULD NOT DELETE old format file \"%s\"", a_FileName);
		return;
	}

	LOGINFO("Successfully converted binary to Json \"%s\"", FileNameJson.c_str() );
}





// Helper function
StringList GetDirectoryContents( const char* a_Directory )
{
	StringList AllFiles;
#ifdef _WIN32
	std::string FileFilter = std::string( a_Directory ) + "*.*";
	HANDLE hFind;
	WIN32_FIND_DATA FindFileData;

	if( ( hFind = FindFirstFile(FileFilter.c_str(), &FindFileData) ) != INVALID_HANDLE_VALUE)
	{
		do
		{
			AllFiles.push_back( FindFileData.cFileName );
		} while( FindNextFile(hFind, &FindFileData) );
		FindClose(hFind);
	}
#else
	DIR *dp;
	struct dirent *dirp;
	if( (dp  = opendir( a_Directory ) ) == NULL) 
	{
		LOGERROR("Error (%i) opening %s\n", errno, a_Directory );
	}
	else
	{
		while ((dirp = readdir(dp)) != NULL) 
		{
			AllFiles.push_back( dirp->d_name );
		}
		closedir(dp);
	}
#endif

	return AllFiles;
}