Cuberite
A lightweight, fast and extensible game server for Minecraft
ProtocolPalettes.cpp
Go to the documentation of this file.
1 #include "Globals.h"
2 #include "ProtocolPalettes.h"
3 #include "../BlockTypePalette.h"
4 
5 
6 
7 
8 
9 void ProtocolPalettes::load(const AString & aProtocolFolder)
10 {
11  auto contents = cFile::GetFolderContents(aProtocolFolder);
12  for (const auto & c: contents)
13  {
14  auto fullName = aProtocolFolder + cFile::PathSeparator() + c;
15  if (cFile::IsFolder(fullName))
16  {
17  loadSingleVersion(c, fullName);
18  }
19  }
20 }
21 
22 
23 
24 
25 
26 std::shared_ptr<const BlockTypePalette> ProtocolPalettes::blockTypePalette(const AString & aProtocolVersion) const
27 {
28  cCSLock lock(mCS);
29  auto itr = mPalettes.find(aProtocolVersion);
30  if (itr == mPalettes.end())
31  {
32  return nullptr;
33  }
34  return itr->second.mBlockTypePalette;
35 }
36 
37 
38 
39 
40 
41 std::vector<AString> ProtocolPalettes::protocolVersions() const
42 {
43  cCSLock lock(mCS);
44 
45  std::vector<AString> res;
46  for (const auto & p: mPalettes)
47  {
48  res.push_back(p.first);
49  }
50  return res;
51 }
52 
53 
54 
55 
56 
57 void ProtocolPalettes::loadSingleVersion(const AString & aProtocolVersion, const AString & aFolder)
58 {
59  // Get the file list, sort by name
60  auto contents = cFile::GetFolderContents(aFolder);
61  std::sort(contents.begin(), contents.end());
62 
63  // Load files into the palettes:
64  cCSLock lock(mCS);
65  auto & pal = mPalettes[aProtocolVersion];
66  for (const auto & c: contents)
67  {
68  if (c.length() < 8)
69  {
70  // Name too short, can't have the ".btp.txt" etc. suffix
71  continue;
72  }
73  auto fnam = aFolder + cFile::PathSeparator() + c;
74  if (!cFile::IsFile(fnam))
75  {
76  continue;
77  }
78  auto fileType = c.substr(c.length() - 8);
79  if ((fileType == ".btp.txt") || (c == "blocks.json"))
80  {
81  try
82  {
83  pal.mBlockTypePalette->loadFromString(cFile::ReadWholeFile(fnam));
84  }
85  catch (...)
86  {
87  // Ignore silently
88  }
89  }
90  else if ((fileType == ".itp.txt") || (c == "items.json"))
91  {
92  // TODO: Load item type palette
93  }
94  }
95 }
96 
97 
98 
99 
100 
102 // ProtocolPalettes::Palettes:
103 
105  mBlockTypePalette(new BlockTypePalette)
106 {
107 }
std::vector< AString > protocolVersions() const
Returns the version names of all protocols that have been loaded.
cCriticalSection mCS
The CS protecting all members against multithreaded access.
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
Definition: File.cpp:573
static char PathSeparator()
Definition: File.h:42
void loadSingleVersion(const AString &aProtocolVersion, const AString &aFolder)
Loads all the palettes from the specified folder into mPalettes under the aProtocolVersion key...
std::map< AString, Palettes > mPalettes
The map of protocol version -> all its palettes.
std::shared_ptr< const BlockTypePalette > blockTypePalette(const AString &aProtocolVersion) const
Returns the BlockTypePalette for the specified protocol.
Holds a palette that maps between block type + state and numbers.
static bool IsFile(const AString &a_Path)
Returns true if the specified path is a regular file.
Definition: File.cpp:425
void load(const AString &aProtocolFolder)
Loads all the per-protocol palettes.
static bool IsFolder(const AString &a_Path)
Returns true if the specified path is a folder.
Definition: File.cpp:410
std::string AString
Definition: StringUtils.h:13
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
static AStringVector GetFolderContents(const AString &a_Folder)
Returns the list of all items in the specified folder (files, folders, nix pipes, whatever&#39;s there)...
Definition: File.cpp:498