Browse Source

block registry

Kajetan Johannes Hammerle 4 years ago
parent
commit
eb265049d0

+ 8 - 3
common/block/Block.cpp

@@ -1,10 +1,15 @@
 #include "common/block/Block.h"
 
-Block::Block(BlockId id) : id(id)
+Block::Block(u16 id, const std::string& registry) : id(id), registry(registry)
 {
 }
 
-BlockId Block::getId() const
+u16 Block::getId() const
 {
     return id;
-}
+}
+
+const std::string& Block::getRegistry() const
+{
+    return registry;
+}

+ 7 - 5
common/block/Block.h

@@ -1,18 +1,20 @@
 #ifndef BLOCK_H
 #define BLOCK_H
 
-#include <cstdint>
+#include <string>
 
-typedef uint16_t BlockId;
+#include "common/utils/Types.h"
 
 class Block
 {
 public:
-    Block(BlockId id);
-    BlockId getId() const;
+    Block(u16 id, const std::string& registry);
+    u16 getId() const;
+    const std::string& getRegistry() const;
 
 private:
-    const BlockId id;
+    const u16 id;
+    const std::string registry;
 };
 
 #endif

+ 48 - 15
common/block/BlockRegistry.cpp

@@ -1,24 +1,57 @@
-#include "BlockRegistry.h"
+#include <unordered_map>
+#include <vector>
+#include <fstream>
+#include <iostream>
 
-Block BlockRegistry::NULL_BLOCK(0);
+#include "BlockRegistry.h"
 
-BlockRegistry::BlockRegistry() : idCounter(1)
+namespace BlockRegistry
 {
+    static Block NULL_BLOCK(0, "null_block");
+    static std::unordered_map<std::string, u16> registry;
+    static std::vector<Block> idMap = {NULL_BLOCK};
     
-}
+    void loadFromFile(const std::string& path)
+    {
+        std::ifstream in;
+        in.open(path);
+        if(in.fail())
+        {
+            std::cout << "cannot read blocks from file '" << path << "'" << std::endl;
+            return;
+        }
+        std::string line;
+        while(true)
+        {
+            std::getline(in, line, '\n');
+            if(in.fail())
+            {
+                break;
+            }
+            u16 id = idMap.size();
+            std::cout << id << std::endl;
+            idMap.emplace_back(id, line);
+            registry[line] = id;
+        }
+    }
 
-const Block& BlockRegistry::getBlock(const std::string& name) const
-{
-    const std::unordered_map<std::string, Block>::const_iterator& iter = registry.find(name);
-    if(iter == registry.end())
+    const Block& getBlock(const std::string& name)
     {
-        return NULL_BLOCK;
-    }    
-    return iter->second;
-}
+        const std::unordered_map<std::string, u16>::const_iterator& iter = registry.find(name);
+        if(iter == registry.end())
+        {
+            return NULL_BLOCK;
+        }    
+        return idMap[iter->second];
+    }
 
-const Block& BlockRegistry::getBlock(BlockId id) const
-{
-    return NULL_BLOCK;
+    const Block& getBlock(u16 id)
+    {
+        if(id >= idMap.size())
+        {
+            return NULL_BLOCK;
+        }
+        return idMap[id];
+    }
 }
 

+ 6 - 15
common/block/BlockRegistry.h

@@ -1,24 +1,15 @@
 #ifndef BLOCKREGISTRY_H
 #define BLOCKREGISTRY_H
 
-#include <unordered_map>
-
+#include "common/utils/Types.h"
 #include "common/block/Block.h"
 
-class BlockRegistry
+namespace BlockRegistry
 {
-public:
-    static Block NULL_BLOCK;
-    
-    BlockRegistry();
-    
-    const Block& getBlock(const std::string&) const;
-    const Block& getBlock(BlockId id) const;
-    
-private:
-    std::unordered_map<std::string, Block> registry;
-    BlockId idCounter;
-};
+    void loadFromFile(const std::string& path);
+    const Block& getBlock(const std::string& registry);
+    const Block& getBlock(u16 id);
+}
 
 #endif
 

+ 17 - 0
common/utils/Types.h

@@ -0,0 +1,17 @@
+#ifndef TYPES_H
+#define TYPES_H
+
+#include <cstdint>
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+typedef int8_t s8;
+typedef int16_t s16;
+typedef int32_t s32;
+typedef int64_t s64;
+
+#endif
+

+ 1 - 1
meson.build

@@ -18,7 +18,7 @@ pngDep = dependency('libpng')
 executable('game_server', 
     sources: sourcesCommon + sourcesServer,
     dependencies : threadDep,
-    cpp_args: ['-Wall', '-Wextra', '-pedantic', '-Werror', '-Wno-unused-parameter'])
+    cpp_args: ['-Wall', '-Wextra', '-pedantic', '-Werror'])
     
 #executable('game_tests', 
 #    sources: sourcesTest)

+ 5 - 0
resources/blocks

@@ -0,0 +1,5 @@
+air
+dirt
+stone
+grass
+test

+ 1 - 10
server/GameServer.cpp

@@ -3,11 +3,6 @@
 #include "server/GameServer.h"
 #include "server/network/Server.h"
 
-/*void GameServer::stop()
-{
-    isRunning = false;
-}*/
-
 void GameServer::start(uint16_t port, uint16_t maxClients)
 {
     std::cout << port << std::endl;
@@ -21,11 +16,7 @@ void GameServer::start(uint16_t port, uint16_t maxClients)
     {
         std::cout << "> ";
         std::string command;
-        getline(std::cin, command, '\n');
-        if(command == "q")
-        {
-            break;
-        }
+        std::getline(std::cin, command, '\n');
         commandManager.execute(serverCommands, command);
     }
 }

+ 8 - 3
server/Main.cpp

@@ -1,9 +1,14 @@
+#include <iostream>
+
 #include "server/GameServer.h"
+#include "common/block/BlockRegistry.h"
 
-int main(int argc, char** argv)
+int main(int /*argc*/, char** /*argv*/)
 {
-    GameServer server;
-    server.start(25565, 2);
+    BlockRegistry::loadFromFile("resources/blocks");
+
+    //GameServer server;
+    //server.start(25565, 2);
     return 0;
 }
 

+ 4 - 5
server/commands/GeneralCommands.cpp

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

+ 2 - 2
server/network/Server.h

@@ -7,7 +7,7 @@
 
 #include "server/network/IServerListener.h"
 
-class Server
+class Server final
 {
 private:
     struct ConnectedClient
@@ -19,7 +19,7 @@ private:
     
 public:
     Server(uint16_t port, uint16_t maxClients, const IServerListener& listener);
-    virtual ~Server();
+    ~Server();
 
     bool isRunning() const;