Add basic main() and server.properties reader

This commit is contained in:
DankParrot 2020-07-23 01:49:35 -07:00
parent 842e4a422a
commit 4488d8bcf2
6 changed files with 251 additions and 0 deletions

51
build/server.properties Normal file
View File

@ -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

12
src/Main.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "config/ServerProperties.h"
using namespace Feather;
int main()
{
ServerProperties properties("server.properties");
printf("Starting server on port %d\n", properties.Get<int>("server-port"));
return 1;
}

View File

@ -0,0 +1,91 @@
#include "ServerProperties.h"
#include <iostream>
#include <fstream>
#include <string_view>
#include <algorithm>
#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>(string_view key)
{
return properties[string(key)];
}
template <>
bool ServerProperties::Get<bool>(string_view key)
{
string value = Get<string>(key);
if (StringUtil::iequal(value, STRING_TRUE)) {
return true;
} else {
// TODO: complain about non-bool
return false;
}
}
template <>
int ServerProperties::Get<int>(string_view key)
{
return std::stoi(Get<string>(key));
}
template <>
long ServerProperties::Get<long>(string_view key)
{
return std::stol(Get<string>(key));
}
template <>
long long ServerProperties::Get<long long>(string_view key)
{
return std::stoll(Get<string>(key));
}
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <string>
#include <unordered_map>
using std::string;
using std::string_view;
namespace Feather
{
class ServerProperties
{
public:
ServerProperties(const char* path);
template <typename T>
T Get(string_view key);
private:
std::unordered_map<string, string> properties;
};
}

13
src/util/StringUtil.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <cctype>
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;
}
}

63
src/util/StringUtil.h Normal file
View File

@ -0,0 +1,63 @@
#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>
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;
}
}