Skip to content

Commit

Permalink
load config file given on command line, allow the user to define admi…
Browse files Browse the repository at this point in the history
…n/motd/rules/items files
  • Loading branch information
deoxxa committed Nov 28, 2010
1 parent c420d7d commit 79fdf3c
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 69 deletions.
23 changes: 22 additions & 1 deletion bin/config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ servername = "Mineserver alpha testserver"
# Userlimit
userlimit = 50

# IP
ip = "0.0.0.0"

# Port
port = 25565

# Admin file
admin_file = "admin.txt"

# Item alias file
items_file = "items.txt"

# MOTD file
motd_file = "motd.txt"

# Rules file
rules_file = "rules.txt"

# Map Release time - time in seconds to keep unused map chunks in memory
# Memory vs. CPU tradeoff. Reloading map data takes a bit of CPU each time
# but the map in memory consumes it around 100kb/chunk
Expand Down Expand Up @@ -39,4 +54,10 @@ kit_starter = "273, 274, 275, 50, 50, 50, 50, 50"
liquid_physics = 1

# Generate flatland map
map_flatland = true;
map_flatland = false;

# Ore Density
oreDensity = 24

# Sea level
seaLevel = 63
4 changes: 2 additions & 2 deletions src/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool Chat::checkMotd(std::string motdFile)
std::cout << "> Warning: " << motdFile << " not found. Creating..." << std::endl;

std::ofstream motdofs(motdFile.c_str());
motdofs << DEFAULTMOTDFILE << std::endl;
motdofs << MOTD_CONTENT << std::endl;
motdofs.close();
}

Expand All @@ -106,7 +106,7 @@ bool Chat::loadAdmins(std::string adminFile)
std::cout << "> Warning: " << adminFile << " not found. Creating..." << std::endl;

std::ofstream adminofs(adminFile.c_str());
adminofs << DEFAULTADMINFILE << std::endl;
adminofs << ADMIN_CONTENT << std::endl;
adminofs.close();

return true;
Expand Down
13 changes: 6 additions & 7 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ namespace
if(tUser != NULL)
{
// Send rules
std::ifstream ifs( RULESFILE.c_str());
std::ifstream ifs(Conf::get().sValue("rules_file").c_str());
std::string temp;

if(ifs.fail())
{
std::cout << "> Warning: " << RULESFILE << " not found." << std::endl;
std::cout << "> Warning: " << Conf::get().sValue("rules_file") << " not found." << std::endl;
return;
}
else
Expand Down Expand Up @@ -286,14 +286,13 @@ void regenerateLighting(User *user, std::string command, std::deque<std::string>

void reloadConfiguration(User *user, std::string command, std::deque<std::string> args)
{
Chat::get().loadAdmins(ADMINFILE);
Conf::get().load(CONFIGFILE);
Chat::get().loadAdmins(Conf::get().sValue("admin_file"));
Conf::get().load(CONFIG_FILE);

// Set physics enable state based on config
Physics::get().enabled = ((Conf::get().iValue("liquid_physics") == 0) ? false : true);
Physics::get().enabled = (Conf::get().bValue("liquid_physics"));

Chat::get().sendMsg(user, COLOR_DARK_MAGENTA + "SERVER:" + COLOR_RED+
" Reloaded admins and config", Chat::USER);
Chat::get().sendMsg(user, COLOR_DARK_MAGENTA + "SERVER: "+COLOR_RED+"Reloaded admins and config", Chat::USER);

// Note that the MOTD is loaded on-demand each time it is requested
}
Expand Down
14 changes: 13 additions & 1 deletion src/config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ ip = "0.0.0.0"
# Port
port = 25565

# Admin file
admin_file = "admin.txt"

# Item alias file
items_file = "items.txt"

# MOTD file
motd_file = "motd.txt"

# Rules file
rules_file = "rules.txt"

# Map Release time - time in seconds to keep unused map chunks in memory
# Memory vs. CPU tradeoff. Reloading map data takes a bit of CPU each time
# but the map in memory consumes it around 100kb/chunk
Expand Down Expand Up @@ -48,4 +60,4 @@ map_flatland = false;
oreDensity = 24

# Sea level
seaLevel = 63
seaLevel = 63
4 changes: 2 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool Conf::load(std::string configFile)
std::ifstream ifs(configFile.c_str());

// If configfile does not exist
if(ifs.fail() && configFile == CONFIGFILE)
if(ifs.fail() && configFile == CONFIG_FILE)
{
// TODO: Load default configuration from the internets!
std::cout << ">>> " << configFile << " not found." << std::endl;
Expand All @@ -63,7 +63,7 @@ bool Conf::load(std::string configFile)
"#" << std::endl;
confofs.close();

this->load(CONFIGFILE);
this->load(CONFIG_FILE);
}

std::string temp;
Expand Down
9 changes: 4 additions & 5 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,22 @@
#ifndef _CONFIG_H
#define _CONFIG_H

#include <string>
#include <map>
#include <vector>

class Conf
{
private:
Conf()
{
}
Conf() {}
std::map<std::string, std::string> confSet;
public:
static Conf &get();
bool load(std::string configFile);
int iValue(std::string name);
std::string sValue(std::string name);
bool bValue(std::string name);
std::vector<int> vValue(std::string name);

static Conf &get();
};

#endif
17 changes: 8 additions & 9 deletions src/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ void initConstants()
defaultConf.insert(std::pair<std::string, std::string>("mapdir", "testmap"));
defaultConf.insert(std::pair<std::string, std::string>("userlimit", "20"));
defaultConf.insert(std::pair<std::string, std::string>("map_release_time", "10"));
defaultConf.insert(std::pair<std::string, std::string>("liquid_physics", "1"));
defaultConf.insert(std::pair<std::string, std::string>("liquid_physics", "true"));
defaultConf.insert(std::pair<std::string, std::string>("map_flatland", "false"));
defaultConf.insert(std::pair<std::string, std::string>("oreDensity", "24"));
defaultConf.insert(std::pair<std::string, std::string>("seaLevel", "63"));
defaultConf.insert(std::pair<std::string, std::string>("server_full_message",
"Server is currently full"));
defaultConf.insert(std::pair<std::string, std::string>("default_kick_message",
"You have been kicked"));
defaultConf.insert(std::pair<std::string, std::string>("wrong_protocol_message",
"Wrong client protocol"));
defaultConf.insert(std::pair<std::string, std::string>("server_full_message", "Server is currently full"));
defaultConf.insert(std::pair<std::string, std::string>("default_kick_message", "You have been kicked"));
defaultConf.insert(std::pair<std::string, std::string>("wrong_protocol_message", "Wrong client protocol"));
defaultConf.insert(std::pair<std::string, std::string>("admin_file", "admin.txt"));
defaultConf.insert(std::pair<std::string, std::string>("motd_file", "motd.txt"));
defaultConf.insert(std::pair<std::string, std::string>("rules_file", "rules.txt"));

// Block drops (10000 = 100%)
BLOCKDROPS.insert(std::pair<uint8, Drop>(BLOCK_STONE, Drop(BLOCK_COBBLESTONE, 10000, 1, true)));
Expand All @@ -67,5 +67,4 @@ void initConstants()
BLOCKDROPS.insert(std::pair<uint8, Drop>(BLOCK_TNT, Drop(BLOCK_TNT, 10000, 0, true)));
BLOCKDROPS.insert(std::pair<uint8, Drop>(BLOCK_GLASS, Drop(BLOCK_GLASS, 10000, 0, true)));
BLOCKDROPS.insert(std::pair<uint8, Drop>(BLOCK_MOB_SPAWNER, Drop(BLOCK_MOB_SPAWNER, 10000, 0, true)));

}
}
30 changes: 14 additions & 16 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ enum
BLOCK_STATUS_BLOCK_BROKEN
};


// Chat colors
#define COLOR_BLACK std::string("§0")
#define COLOR_DARK_BLUE std::string("§1")
Expand Down Expand Up @@ -98,8 +97,7 @@ enum Block
enum
{
ITEM_IRON_SPADE = 256, ITEM_IRON_PICKAXE, ITEM_IRON_AXE, ITEM_FLINT_AND_STEEL, ITEM_APPLE,
ITEM_BOW,
ITEM_ARROW, ITEM_COAL, ITEM_DIAMOND, ITEM_IRON_INGOT, ITEM_GOLD_INGOT, ITEM_IRON_SWORD,
ITEM_BOW, ITEM_ARROW, ITEM_COAL, ITEM_DIAMOND, ITEM_IRON_INGOT, ITEM_GOLD_INGOT, ITEM_IRON_SWORD,
ITEM_WOODEN_SWORD, ITEM_WOODEN_SPADE, ITEM_WOODEN_PICKAXE, ITEM_WOODEN_AXE, ITEM_STONE_SWORD,
ITEM_STONE_SPADE, ITEM_STONE_PICKAXE, ITEM_STONE_AXE, ITEM_DIAMOND_SWORD,
ITEM_DIAMOND_SPADE, ITEM_DIAMOND_PICKAXE, ITEM_DIAMOND_AXE, ITEM_STICK, ITEM_BOWL,
Expand All @@ -121,29 +119,32 @@ enum
};

// Records
enum { ITEM_GOLD_RECORD = 2256, ITEM_GREEN_RECORD };
enum
{
ITEM_GOLD_RECORD = 2256, ITEM_GREEN_RECORD
};

const std::string VERSION = "0.1.12 (Alpha)";

const char COMMENTPREFIX = '#';

// Adminfile
const std::string ADMINFILE = "admin.txt";
const std::string DEFAULTADMINFILE =
"# This is default admin file. Write admin nicks here one for each line";
const std::string ADMIN_FILE = "admin.txt";
const std::string ADMIN_CONTENT = "# This is default admin file. Add nicknames here for admins, one per line.";

// Motdfile
const std::string MOTDFILE = "motd.txt";
const std::string DEFAULTMOTDFILE = "This is default motd! Please edit motd.txt";
const std::string MOTD_FILE = "motd.txt";
const std::string MOTD_CONTENT = "This is the default motd! Please edit motd.txt to change this message.";

// Configuration
const std::string CONFIGFILE = "config.cfg";
const std::string CONFIG_FILE = "config.cfg";

// Item aliases
const std::string ITEMALIASFILE = "item_alias.cfg";
const std::string ITEMS_FILE = "item_alias.cfg";

// Server rules
const std::string RULESFILE = "rules.txt";
const std::string RULES_FILE = "rules.txt";
const std::string RULES_CONTENT = "This is the default rules file. Please edit rules.txt to change this message.";

// Default configuration
extern std::map<std::string, std::string> defaultConf;
Expand All @@ -158,10 +159,7 @@ struct Drop
uint8 count;
bool exclusive;

Drop()
{

}
Drop() {}

Drop(uint16 item_id, uint32 probability, uint8 count, bool exclusive)
{
Expand Down
43 changes: 29 additions & 14 deletions src/mineserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
#include "packets.h"
#include "physics.h"


#ifdef WIN32
static bool quit = false;
#endif
Expand All @@ -95,7 +94,7 @@ void sighandler(int sig_num)
Mineserver::Get().Stop();
}

int main(void)
int main(int argc, char *argv[])
{
#ifdef WIN32
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
Expand All @@ -104,7 +103,7 @@ int main(void)
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);

return Mineserver::Get().Run();
return Mineserver::Get().Run(argc, argv);
}

Mineserver::Mineserver()
Expand All @@ -116,29 +115,45 @@ event_base *Mineserver::GetEventBase()
return m_eventBase;
}

int Mineserver::Run()
int Mineserver::Run(int argc, char *argv[])
{

uint32 starttime = (uint32)time(0);
uint32 tick = (uint32)time(0);

initConstants();

Chat::get().loadAdmins(ADMINFILE);
Chat::get().checkMotd(MOTDFILE);
std::string file_config;
file_config.assign(CONFIG_FILE);
std::string file_admin;
file_admin.assign(ADMIN_FILE);
std::string file_items;
file_items.assign(ITEMS_FILE);
std::string file_motd;
file_motd.assign(MOTD_FILE);
std::string file_rules;
file_rules.assign(RULES_FILE);

if (argc > 1)
file_config.assign(argv[1]);

// Initialize conf
Conf::get().load(CONFIGFILE);
Conf::get().load(file_config);

// Load item aliases
Conf::get().load(ITEMALIASFILE);
Conf::get().load(file_items);

// Load admins
Chat::get().loadAdmins(file_admin);
// Load MOTD
Chat::get().checkMotd(file_motd);

//Set physics enable state according to config
Physics::get().enabled = (Conf::get().iValue("liquid_physics") ? true : false);
// Set physics enable state according to config
Physics::get().enabled = (Conf::get().bValue("liquid_physics"));

//Initialize map
// Initialize map
Map::get().initMap();

//Initialize packethandler
// Initialize packethandler
PacketHandler::get().initPackets();

// Load ip from config
Expand Down Expand Up @@ -328,4 +343,4 @@ bool Mineserver::Stop()
m_running=false;

return true;
};
};
22 changes: 11 additions & 11 deletions src/mineserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@
class Mineserver
{
private:
struct event m_listenEvent;
event_base *m_eventBase;
struct event m_listenEvent;
event_base *m_eventBase;
int m_socketlisten;
bool m_running;

public:
static Mineserver &Get()
{
static Mineserver server;
return server;
}

Mineserver();
int Run();
static Mineserver &Get()
{
static Mineserver server;
return server;
}

Mineserver();
int Run(int argc, char *argv[]);
bool Stop();
event_base *GetEventBase();
event_base *GetEventBase();
};

#endif
2 changes: 1 addition & 1 deletion src/packets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int PacketHandler::login_request(User *user)
}

// Send motd
std::ifstream motdfs( MOTDFILE.c_str());
std::ifstream motdfs(Conf::get().sValue("motd_file").c_str());

std::string temp;

Expand Down

0 comments on commit 79fdf3c

Please sign in to comment.