summaryrefslogtreecommitdiffstats
path: root/src/Protocol/ProtocolPalettes.cpp
blob: 2dc0857a91ee44ae124fec46a4886c7e22372553 (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
#include "Globals.h"
#include "ProtocolPalettes.h"
#include "../BlockTypePalette.h"





void ProtocolPalettes::load(const AString & aProtocolFolder)
{
	auto contents = cFile::GetFolderContents(aProtocolFolder);
	for (const auto & c: contents)
	{
		auto fullName = aProtocolFolder + cFile::PathSeparator() + c;
		if (cFile::IsFolder(fullName))
		{
			loadSingleVersion(c, fullName);
		}
	}
}





std::shared_ptr<const BlockTypePalette> ProtocolPalettes::blockTypePalette(const AString & aProtocolVersion) const
{
	cCSLock lock(mCS);
	auto itr = mPalettes.find(aProtocolVersion);
	if (itr == mPalettes.end())
	{
		return nullptr;
	}
	return itr->second.mBlockTypePalette;
}





std::vector<AString> ProtocolPalettes::protocolVersions() const
{
	cCSLock lock(mCS);

	std::vector<AString> res;
	for (const auto & p: mPalettes)
	{
		res.push_back(p.first);
	}
	return res;
}





void ProtocolPalettes::loadSingleVersion(const AString & aProtocolVersion, const AString & aFolder)
{
	// Get the file list, sort by name
	auto contents = cFile::GetFolderContents(aFolder);
	std::sort(contents.begin(), contents.end());

	// Load files into the palettes:
	cCSLock lock(mCS);
	auto & pal = mPalettes[aProtocolVersion];
	for (const auto & c: contents)
	{
		if (c.length() < 8)
		{
			// Name too short, can't have the ".btp.txt" etc. suffix
			continue;
		}
		auto fnam = aFolder + cFile::PathSeparator() + c;
		if (!cFile::IsFile(fnam))
		{
			continue;
		}
		auto fileType = c.substr(c.length() - 8);
		if ((fileType == ".btp.txt") || (c == "blocks.json"))
		{
			try
			{
				pal.mBlockTypePalette->loadFromString(cFile::ReadWholeFile(fnam));
			}
			catch (...)
			{
				// Ignore silently
			}
		}
		else if ((fileType == ".itp.txt") || (c == "items.json"))
		{
			// TODO: Load item type palette
		}
	}
}





////////////////////////////////////////////////////////////////////////////////
// ProtocolPalettes::Palettes:

ProtocolPalettes::Palettes::Palettes():
	mBlockTypePalette(new BlockTypePalette)
{
}