From 4488d8bcf282ea1b62d6088b12fc00779534c1cf Mon Sep 17 00:00:00 2001 From: DankParrot Date: Thu, 23 Jul 2020 01:49:35 -0700 Subject: [PATCH] Add basic main() and server.properties reader --- build/server.properties | 51 ++++++++++++++++++ src/Main.cpp | 12 +++++ src/config/ServerProperties.cpp | 91 +++++++++++++++++++++++++++++++++ src/config/ServerProperties.h | 21 ++++++++ src/util/StringUtil.cpp | 13 +++++ src/util/StringUtil.h | 63 +++++++++++++++++++++++ 6 files changed, 251 insertions(+) create mode 100644 build/server.properties create mode 100644 src/Main.cpp create mode 100644 src/config/ServerProperties.cpp create mode 100644 src/config/ServerProperties.h create mode 100644 src/util/StringUtil.cpp create mode 100644 src/util/StringUtil.h diff --git a/build/server.properties b/build/server.properties new file mode 100644 index 0000000..c11c6bb --- /dev/null +++ b/build/server.properties @@ -0,0 +1,51 @@ +#Minecraft server properties +#(last boot timestamp) +enable-jmx-monitoring=false +rcon.port=25575 +level-seed= +gamemode=survival +enable-command-block=false +enable-query=false +generator-settings= +level-name=world +motd=A Minecraft Server +query.port=25565 +pvp=true +generate-structures=true +difficulty=easy +network-compression-threshold=256 +max-tick-time=60000 +max-players=20 +use-native-transport=true +online-mode=true +enable-status=true +allow-flight=false +broadcast-rcon-to-ops=true +view-distance=10 +max-build-height=256 +server-ip= +allow-nether=true +server-port=25565 +enable-rcon=false +sync-chunk-writes=true +op-permission-level=4 +prevent-proxy-connections=false +resource-pack= +entity-broadcast-range-percentage=100 +rcon.password= +player-idle-timeout=0 +force-gamemode=false +rate-limit=0 +hardcore=false +white-list=false +broadcast-console-to-ops=true +spawn-npcs=true +spawn-animals=true +snooper-enabled=true +function-permission-level=2 +level-type=default +spawn-monsters=true +enforce-whitelist=false +resource-pack-sha1= +spawn-protection=16 +max-world-size=29999984 diff --git a/src/Main.cpp b/src/Main.cpp new file mode 100644 index 0000000..55a3442 --- /dev/null +++ b/src/Main.cpp @@ -0,0 +1,12 @@ +#include +#include "config/ServerProperties.h" + +using namespace Feather; + +int main() +{ + ServerProperties properties("server.properties"); + printf("Starting server on port %d\n", properties.Get("server-port")); + + return 1; +} diff --git a/src/config/ServerProperties.cpp b/src/config/ServerProperties.cpp new file mode 100644 index 0000000..754b58b --- /dev/null +++ b/src/config/ServerProperties.cpp @@ -0,0 +1,91 @@ +#include "ServerProperties.h" +#include +#include +#include +#include + +#include "../util/StringUtil.h" + +static const string STRING_TRUE("true"); +static const string STRING_FALSE("false"); + +namespace Feather +{ + ServerProperties::ServerProperties(const char* path) + { + std::ifstream file; + file.open(path); + + printf("Reading %s...\n", path); + if (file.is_open()) + { + string line; + while(std::getline(file, line)) + { + // trip whitespace + StringUtil::trim(line); + + // skip comments + if (line[0] == '#') continue; + + // key=value + size_t pos = line.find('='); + + // java also accepts key:value + if (pos == string::npos) + pos = line.find(':'); + + if (pos == string::npos) + continue; + + string key = line.substr(0, pos); + StringUtil::trim(key); + string value = line.substr(pos + 1); + StringUtil::trim(value); + + properties.insert( {key, value} ); + } + file.close(); + } + + //for ( auto const& pair : properties ) + // std::cout << "[" << pair.first << "] = [" << pair.second << "]\n"; + + } + + template <> + string ServerProperties::Get(string_view key) + { + return properties[string(key)]; + } + + template <> + bool ServerProperties::Get(string_view key) + { + string value = Get(key); + if (StringUtil::iequal(value, STRING_TRUE)) { + return true; + } else { + // TODO: complain about non-bool + return false; + } + } + + template <> + int ServerProperties::Get(string_view key) + { + return std::stoi(Get(key)); + } + + template <> + long ServerProperties::Get(string_view key) + { + return std::stol(Get(key)); + } + + template <> + long long ServerProperties::Get(string_view key) + { + return std::stoll(Get(key)); + } +} \ No newline at end of file diff --git a/src/config/ServerProperties.h b/src/config/ServerProperties.h new file mode 100644 index 0000000..67c8e47 --- /dev/null +++ b/src/config/ServerProperties.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +using std::string; +using std::string_view; + +namespace Feather +{ + class ServerProperties + { + public: + ServerProperties(const char* path); + + template + T Get(string_view key); + private: + std::unordered_map properties; + }; +} \ No newline at end of file diff --git a/src/util/StringUtil.cpp b/src/util/StringUtil.cpp new file mode 100644 index 0000000..b300a60 --- /dev/null +++ b/src/util/StringUtil.cpp @@ -0,0 +1,13 @@ +#include + +namespace Feather::StringUtil +{ + bool iequalc(char c1, char c2) + { + if (c1 == c2) + return true; + else if (std::toupper(c1) == std::toupper(c2)) + return true; + return false; + } +} \ No newline at end of file diff --git a/src/util/StringUtil.h b/src/util/StringUtil.h new file mode 100644 index 0000000..3b1cbb8 --- /dev/null +++ b/src/util/StringUtil.h @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +using std::string; + +namespace Feather::StringUtil +{ + + bool iequalc(char c1, char c2); + + // case insensitive string equal + static inline bool iequal(string const& s1, string const& s2) + { + return s1.size() == s2.size() && std::equal(s1.begin(), s1.end(), s2.begin(), iequalc); + } + + // trim from start (in place) + static inline void ltrim(string &s) + { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { + return !std::isspace(ch); + })); + } + + // trim from end (in place) + static inline void rtrim(string &s) + { + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { + return !std::isspace(ch); + }).base(), + s.end()); + } + + // trim from both ends (in place) + static inline void trim(string &s) + { + ltrim(s); + rtrim(s); + } + + // trim from start (copying) + static inline string ltrim_copy(string s) + { + ltrim(s); + return s; + } + + // trim from end (copying) + static inline string rtrim_copy(string s) + { + rtrim(s); + return s; + } + + // trim from both ends (copying) + static inline string trim_copy(string s) + { + trim(s); + return s; + } +} \ No newline at end of file