summaryrefslogtreecommitdiffstats
path: root/src/Protocol/ProtocolPalettes.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2020-01-03 17:31:13 +0100
committerMattes D <github@xoft.cz>2020-01-07 06:53:17 +0100
commit4aef80b47eb6941d7fc41e57efe147af0ece1f9b (patch)
tree4aeb7c9e8e4aa3ae2ceed1cc60155d868852c5cd /src/Protocol/ProtocolPalettes.cpp
parentStringUtils: Added note to StringsConcat about StringJoin. (diff)
downloadcuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.tar
cuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.tar.gz
cuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.tar.bz2
cuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.tar.lz
cuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.tar.xz
cuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.tar.zst
cuberite-4aef80b47eb6941d7fc41e57efe147af0ece1f9b.zip
Diffstat (limited to 'src/Protocol/ProtocolPalettes.cpp')
-rw-r--r--src/Protocol/ProtocolPalettes.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/Protocol/ProtocolPalettes.cpp b/src/Protocol/ProtocolPalettes.cpp
new file mode 100644
index 000000000..2dc0857a9
--- /dev/null
+++ b/src/Protocol/ProtocolPalettes.cpp
@@ -0,0 +1,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)
+{
+}