Browse Source

command refactoring, commands are now just function pointers, game
server interface for commands, general commands moved to one namespace /
file

Kajetan Johannes Hammerle 4 years ago
parent
commit
b5b6b65e29

+ 0 - 8
.gitignore

@@ -1,8 +0,0 @@
-/nbproject
-/build_server
-/build_client
-/build_tests
-LineCounter.jar
-README
-specs
-start.sh

+ 2 - 2
meson.build

@@ -2,11 +2,11 @@ project('cubes plus plus', 'cpp')
 
 sourcesCommon = ['common/stream/Stream.cpp', 'common/utils/Face.cpp', 'common/block/Block.cpp', 'common/block/Blocks.cpp', 'common/block/BlockAir.cpp', 'common/world/Chunk.cpp', 'common/world/World.cpp']
 
-sourcesServer = ['server/Main.cpp', 'server/GameServer.cpp', 'server/network/Server.cpp', 'server/CommandManager.cpp', 'server/commands/BaseCommand.cpp', 'server/commands/TestCommand.cpp', 'server/CommandUtils.cpp']
+sourcesServer = ['server/Main.cpp', 'server/GameServer.cpp', 'server/network/Server.cpp', 'server/commands/CommandManager.cpp', 'server/commands/CommandUtils.cpp', 'server/commands/GeneralCommands.cpp']
 
 sourcesClient = ['client/Main.cpp', 'client/engine/Clock.cpp', 'client/engine/DirectRenderer.cpp', 'client/engine/KeyManager.cpp', 'client/engine/Mesh.cpp', 'client/engine/MouseManager.cpp', 'client/engine/Shader.cpp', 'client/engine/Texture.cpp', 'client/engine/Utils.cpp', 'client/engine/Wrapper.cpp', 'client/engine/shader/ShaderProgram.cpp', 'client/engine/shader/WorldShader.cpp', 'client/engine/shader/FramebufferRectangle.cpp', 'client/engine/shader/SSAOShader.cpp', 'client/engine/shader/SSAOBlurShader.cpp', 'client/engine/shader/WorldPostShader.cpp', 'client/engine/shader/OverlayShader.cpp', 'client/GameClient.cpp', 'client/rendering/ChunkRenderer.cpp', 'client/rendering/ClientChunkProvider.cpp', 'client/rendering/block/BlockRenderer.cpp', 'client/rendering/block/BlockRenderers.cpp', 'client/rendering/entity/EntityRenderer.cpp', 'client/rendering/gui/GUI.cpp', 'client/rendering/gui/StartMenu.cpp', 'client/math/Matrix3D.cpp', 'client/math/Matrix3DStack.cpp', 'client/math/StackOverflow.cpp', 'client/math/StackUnderflow.cpp', 'client/math/Vector3D.cpp', 'client/math/Plane3D.cpp', 'client/math/Camera3D.cpp', 'client/network/Client.cpp', 'client/network/ClientListener.cpp']
 
-sourcesTest = ['tests/Main.cpp', 'server/CommandUtils.cpp']
+sourcesTest = ['tests/Main.cpp', 'server/commands/CommandUtils.cpp']
 
 threadDep = dependency('threads')
 glewDep = dependency('glew')

+ 0 - 36
server/CommandManager.cpp

@@ -1,36 +0,0 @@
-#include "server/CommandManager.h"
-#include "server/commands/TestCommand.h"
-#include "server/CommandUtils.h"
-
-CommandManager::CommandManager()
-{
-    registerCommand(new TestCommand());
-}
-
-CommandManager::~CommandManager()
-{
-}
-
-void CommandManager::registerCommand(BaseCommand* command)
-{
-    commands.insert(pair<string, unique_ptr<BaseCommand>> (command->getName(), command));
-}
-
-void CommandManager::execute(ICommandSource& cs, const string& rawCommand) const
-{
-    vector<string> args;
-    
-    string command;
-    if(CommandUtils::splitString(rawCommand, command, args))
-    {
-        cout << "Invalid command syntax: '" << rawCommand << "'" << endl;
-        return;
-    }
-
-    if(commands.find(command) == commands.end())
-    {
-        cout << "Unknown command: '" << command << "'" << endl;
-        return;
-    }
-    commands.begin()->second->execute(cs, args);
-}

+ 0 - 26
server/CommandManager.h

@@ -1,26 +0,0 @@
-#ifndef COMMANDMANAGER_H
-#define COMMANDMANAGER_H
-
-#include <iostream>
-#include <unordered_map>
-#include "server/commands/BaseCommand.h"
-#include <memory>
-
-using namespace std;
-
-class CommandManager
-{
-public:
-    CommandManager();
-    virtual ~CommandManager();
-    
-    void execute(ICommandSource& cs, const string& rawCommand) const;
-
-private:
-    unordered_map<string, unique_ptr<BaseCommand>> commands;
-    
-    void registerCommand(BaseCommand* command);
-};
-
-#endif
-

+ 6 - 1
server/GameServer.cpp

@@ -30,7 +30,7 @@ void GameServer::start(unsigned short port, unsigned short maxClients)
         {
             break;
         }
-        commandManager.execute(*this, command);
+        commandManager.execute(*this, *this, command);
     }
     
     server.stop();
@@ -76,3 +76,8 @@ void GameServer::onClientDisconnect(int socket)
     answer.write(s.data(), s.length());
     answer.sendToSocket(socket);
 }
+
+bool GameServer::isServer() const
+{
+    return true;
+}

+ 7 - 4
server/GameServer.h

@@ -2,13 +2,15 @@
 #define GAMESERVER_H
 
 #include <iostream>
+
 #include "server/network/IServerListener.h"
-#include "server/ICommandSource.h"
-#include "server/CommandManager.h"
+#include "server/commands/ICommandSource.h"
+#include "server/commands/CommandManager.h"
+#include "server/IGameServer.h"
 
 using namespace std;
 
-class GameServer : public IServerListener, public ICommandSource
+class GameServer : public IServerListener, public ICommandSource, public IGameServer
 {
 public:
     GameServer();
@@ -21,7 +23,8 @@ public:
     void onClientPackage(int socket, Stream& in) override;
     void onClientDisconnect(int socket) override;
     
-    void stop();
+    void stop() override;
+    bool isServer() const override;
     
 private:
     bool isRunning = false;

+ 0 - 10
server/ICommandSource.h

@@ -1,10 +0,0 @@
-#ifndef ICOMMANDSOURCE_H
-#define ICOMMANDSOURCE_H
-
-class ICommandSource
-{
-public:
-};
-
-#endif
-

+ 13 - 0
server/IGameServer.h

@@ -0,0 +1,13 @@
+#ifndef IGAMESERVER_H
+#define IGAMESERVER_H
+
+class IGameServer
+{
+public:
+    virtual ~IGameServer() = default;
+    
+    virtual void stop() = 0;
+};
+
+#endif
+

+ 0 - 14
server/commands/BaseCommand.cpp

@@ -1,14 +0,0 @@
-#include "server/commands/BaseCommand.h"
-
-BaseCommand::BaseCommand(string name) : name(name)
-{
-}
-
-BaseCommand::~BaseCommand()
-{
-}
-
-const string& BaseCommand::getName() const
-{
-    return name;
-}

+ 0 - 24
server/commands/BaseCommand.h

@@ -1,24 +0,0 @@
-#ifndef BASECOMMAND_H
-#define BASECOMMAND_H
-
-#include <iostream>
-#include <vector>
-#include "server/ICommandSource.h"
-
-using namespace std;
-
-class BaseCommand
-{
-public:
-    BaseCommand(string name);
-    virtual ~BaseCommand();
-    
-    const string& getName() const;
-    virtual void execute(ICommandSource& cs, vector<string>& args) const = 0;
-    
-private:
-    const string name;
-};
-
-#endif
-

+ 37 - 0
server/commands/CommandManager.cpp

@@ -0,0 +1,37 @@
+#include "server/commands/CommandManager.h"
+#include "server/commands/CommandUtils.h"
+#include "server/commands/GeneralCommands.h"
+
+CommandManager::CommandManager()
+{
+    registerCommand("test", GeneralCommands::test);
+    registerCommand("stop", GeneralCommands::stop);
+}
+
+CommandManager::~CommandManager()
+{
+}
+
+void CommandManager::registerCommand(const string& name, void (*command) (IGameServer& gs, ICommandSource&, vector<string>&))
+{
+    commands[name] = command;
+}
+
+void CommandManager::execute(IGameServer& gs, ICommandSource& cs, const string& rawCommand) const
+{
+    vector<string> args;
+    
+    string command;
+    if(CommandUtils::splitString(rawCommand, command, args))
+    {
+        cout << "Invalid command syntax: '" << rawCommand << "'" << endl;
+        return;
+    }
+
+    if(commands.find(command) == commands.end())
+    {
+        cout << "Unknown command: '" << command << "'" << endl;
+        return;
+    }
+    commands.begin()->second(gs, cs, args);
+}

+ 27 - 0
server/commands/CommandManager.h

@@ -0,0 +1,27 @@
+#ifndef COMMANDMANAGER_H
+#define COMMANDMANAGER_H
+
+#include <iostream>
+#include <unordered_map>
+#include <vector>
+
+#include "server/commands/ICommandSource.h"
+
+using namespace std;
+
+class CommandManager
+{
+public:
+    CommandManager();
+    virtual ~CommandManager();
+    
+    void execute(IGameServer& gs, ICommandSource& cs, const string& rawCommand) const;
+
+private:
+    unordered_map<string, void (*) (IGameServer& gs, ICommandSource&, vector<string>&)> commands;
+    
+    void registerCommand(const string& name, void (*command) (IGameServer& gs, ICommandSource&, vector<string>&));
+};
+
+#endif
+

+ 1 - 1
server/CommandUtils.cpp → server/commands/CommandUtils.cpp

@@ -1,4 +1,4 @@
-#include "server/CommandUtils.h"
+#include "server/commands/CommandUtils.h"
 #include <iostream>
 
 static bool splitStringIntern(const string& rawCommand, string& command, vector<string>& args)

+ 0 - 0
server/CommandUtils.h → server/commands/CommandUtils.h


+ 16 - 0
server/commands/GeneralCommands.cpp

@@ -0,0 +1,16 @@
+#include "server/commands/GeneralCommands.h"
+
+void GeneralCommands::test(IGameServer& gs, ICommandSource& cs, vector<string>& args)
+{
+    cout << "test command" << endl;
+    for(unsigned long i = 0; i < args.size(); i++)
+    {
+        cout << " - " << args[i] << endl;
+    }
+}
+
+void GeneralCommands::stop(IGameServer& gs, ICommandSource& cs, vector<string>& args)
+{
+    cout << cs.isServer() << endl;
+    gs.stop();
+}

+ 18 - 0
server/commands/GeneralCommands.h

@@ -0,0 +1,18 @@
+#ifndef GENERALCOMMANDS_H
+#define GENERALCOMMANDS_H
+
+#include <vector>
+#include <iostream>
+
+#include "server/commands/ICommandSource.h"
+
+using namespace std;
+
+namespace GeneralCommands
+{
+    void test(IGameServer& gs, ICommandSource& cs, vector<string>& args);
+    void stop(IGameServer& gs, ICommandSource& cs, vector<string>& args);
+};
+
+#endif
+

+ 18 - 0
server/commands/ICommandSource.h

@@ -0,0 +1,18 @@
+#ifndef ICOMMANDSOURCE_H
+#define ICOMMANDSOURCE_H
+
+#include "server/IGameServer.h"
+
+class ICommandSource
+{
+public:
+    virtual ~ICommandSource() = default;
+    
+    virtual bool isServer() const
+    {
+        return false;
+    }
+};
+
+#endif
+

+ 0 - 20
server/commands/TestCommand.cpp

@@ -1,20 +0,0 @@
-#include "server/commands/TestCommand.h"
-
-TestCommand::TestCommand() : BaseCommand("test")
-{
-}
-
-TestCommand::~TestCommand()
-{
-    cout << "destroy test command" << endl;
-}
-
-void TestCommand::execute(ICommandSource& cs, vector<string>& args) const
-{
-    cout << "test command" << endl;
-    for(unsigned long i = 0; i < args.size(); i++)
-    {
-        cout << " - " << args[i] << endl;
-    }
-}
-

+ 0 - 19
server/commands/TestCommand.h

@@ -1,19 +0,0 @@
-#ifndef TESTCOMMAND_H
-#define TESTCOMMAND_H
-
-#include "server/commands/BaseCommand.h"
-
-class TestCommand : public BaseCommand
-{
-public:
-    TestCommand();
-    virtual ~TestCommand();
-    
-    void execute(ICommandSource& cs, vector<string>& args) const;
-    
-private:
-
-};
-
-#endif 
-

+ 1 - 1
tests/Main.cpp

@@ -1,5 +1,5 @@
 #include <iostream>
-#include "server/CommandUtils.h"
+#include "server/commands/CommandUtils.h"
 
 using namespace std;