Compare commits
11 Commits
50923e618b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f45ab91998 | |||
| f3554454a9 | |||
| 83451137d0 | |||
| 352fa5a034 | |||
| 1662477d20 | |||
| 7e8b4d107d | |||
| 3bcff4467c | |||
| b288f7f973 | |||
| ad2deb5819 | |||
| 93a8e1acfb | |||
| ef4264a29b |
37
.clang-format
Normal file
37
.clang-format
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
|
||||||
|
AlignAfterOpenBracket: DontAlign
|
||||||
|
BreakConstructorInitializers: AfterColon
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
|
PointerAlignment: Left
|
||||||
|
SortIncludes: true
|
||||||
|
SpacesBeforeTrailingComments: 2
|
||||||
|
UseTab: Always
|
||||||
|
MaxEmptyLinesToKeep: 5
|
||||||
|
|
||||||
|
TabWidth: 4
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentCaseLabels: true
|
||||||
|
|
||||||
|
ColumnLimit: 135
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
|
||||||
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
|
BreakBeforeBraces: Custom
|
||||||
|
BraceWrapping:
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: false
|
||||||
|
AfterEnum: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
32
.gitea/workflows/ubuntu.yaml
Normal file
32
.gitea/workflows/ubuntu.yaml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
name: Linux arm64
|
||||||
|
run-name: Build And Test
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out repository code
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: "Clone submodule"
|
||||||
|
run: "git submodule update --init"
|
||||||
|
|
||||||
|
- name: Prepare XMake
|
||||||
|
uses: xmake-io/github-action-setup-xmake@v1
|
||||||
|
with:
|
||||||
|
xmake-version: latest
|
||||||
|
actions-cache-folder: '.xmake-cache'
|
||||||
|
actions-cache-key: 'ubuntu'
|
||||||
|
|
||||||
|
- name: XMake config
|
||||||
|
run: xmake f -p linux -y --root
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: xmake --root
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
xmake f -m debug --root
|
||||||
|
xmake test --root
|
||||||
2
Blitz
2
Blitz
Submodule Blitz updated: 2da89c8dab...026a841a04
19
README.md
Normal file
19
README.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Blitz-Console
|
||||||
|
|
||||||
|
## Update submodule
|
||||||
|
|
||||||
|
```
|
||||||
|
git submodule update --init
|
||||||
|
```
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```
|
||||||
|
xmake
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
```
|
||||||
|
xmake run
|
||||||
|
```
|
||||||
75
src/Client.cpp
Normal file
75
src/Client.cpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#include "Client.h"
|
||||||
|
|
||||||
|
#include "blitz/misc/Format.h"
|
||||||
|
#include "blitz/misc/Log.h"
|
||||||
|
#include "blitz/protocol/packets/ChatPacket.h"
|
||||||
|
#include "blitz/protocol/packets/ConnexionInfoPacket.h"
|
||||||
|
#include "blitz/protocol/packets/KeepAlivePacket.h"
|
||||||
|
#include "blitz/protocol/packets/PlayerLoginPacket.h"
|
||||||
|
#include "blitz/protocol/packets/PlayerShootPacket.h"
|
||||||
|
|
||||||
|
static void PrintColoredText(const protocol::ColoredText& text) {
|
||||||
|
std::string msg;
|
||||||
|
for (auto& part : text) {
|
||||||
|
msg += utils::Format("\033[38;2;%i;%i;%im%s", static_cast<int>(part.color.r * 255), static_cast<int>(part.color.g * 255),
|
||||||
|
static_cast<int>(part.color.b * 255), part.text.c_str());
|
||||||
|
}
|
||||||
|
msg += "\033[0m";
|
||||||
|
utils::LOG(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
Client::Client() : network::Connexion(&m_Dispatcher) {
|
||||||
|
RegisterHandlers();
|
||||||
|
}
|
||||||
|
|
||||||
|
Client::~Client() {
|
||||||
|
GetDispatcher()->UnregisterHandler(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::RegisterHandlers() {
|
||||||
|
GetDispatcher()->RegisterHandler(protocol::PacketType::KeepAlive, this);
|
||||||
|
GetDispatcher()->RegisterHandler(protocol::PacketType::Disconnect, this);
|
||||||
|
GetDispatcher()->RegisterHandler(protocol::PacketType::Chat, this);
|
||||||
|
GetDispatcher()->RegisterHandler(protocol::PacketType::ConnexionInfo, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Client::Connect(const std::string& pseudo, const std::string& address, std::uint16_t port) {
|
||||||
|
if (!Connexion::Connect(address, port))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_PlayerName = pseudo;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::HandlePacket(const protocol::ChatPacket* packet) {
|
||||||
|
PrintColoredText(packet->GetMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::HandlePacket(const protocol::DisconnectPacket* packet) {
|
||||||
|
utils::LOG("Disconnected !");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::HandlePacket(const protocol::KeepAlivePacket* packet) {
|
||||||
|
protocol::KeepAlivePacket response(packet->GetAliveID());
|
||||||
|
SendPacket(&response);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::HandlePacket(const protocol::ConnexionInfoPacket* packet) {
|
||||||
|
m_PlayerID = packet->GetConnectionID();
|
||||||
|
Login(m_PlayerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::Login(const std::string& pseudo) {
|
||||||
|
protocol::PlayerLoginPacket packet(pseudo);
|
||||||
|
SendPacket(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::SendTextChat(const std::string& msg) {
|
||||||
|
protocol::ChatPacket packet(protocol::ChatPacket::ColorizeText(msg));
|
||||||
|
SendPacket(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::Shoot() {
|
||||||
|
protocol::PlayerShootPacket packet({}, 0, 0);
|
||||||
|
SendPacket(&packet);
|
||||||
|
}
|
||||||
31
src/Client.h
Normal file
31
src/Client.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "blitz/common/Defines.h"
|
||||||
|
#include "blitz/network/Connexion.h"
|
||||||
|
|
||||||
|
using namespace blitz;
|
||||||
|
|
||||||
|
class Client : public network::Connexion {
|
||||||
|
private:
|
||||||
|
std::string m_PlayerName;
|
||||||
|
game::PlayerID m_PlayerID;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Client();
|
||||||
|
virtual ~Client();
|
||||||
|
|
||||||
|
virtual void HandlePacket(const protocol::DisconnectPacket* packet) override;
|
||||||
|
virtual void HandlePacket(const protocol::KeepAlivePacket* packet) override;
|
||||||
|
virtual void HandlePacket(const protocol::ChatPacket* packet) override;
|
||||||
|
virtual void HandlePacket(const protocol::ConnexionInfoPacket* packet) override;
|
||||||
|
|
||||||
|
virtual bool Connect(const std::string& pseudo, const std::string& address, std::uint16_t port);
|
||||||
|
|
||||||
|
void SendTextChat(const std::string& msg);
|
||||||
|
void Shoot();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Login(const std::string& pseudo);
|
||||||
|
void RegisterHandlers();
|
||||||
|
};
|
||||||
|
|
||||||
153
src/main.cpp
153
src/main.cpp
@@ -1,6 +1,155 @@
|
|||||||
|
#include "Client.h"
|
||||||
|
#include "blitz/misc/Format.h"
|
||||||
|
#include "blitz/misc/Log.h"
|
||||||
|
#include "blitz/network/Network.h"
|
||||||
|
#include "server/Server.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
namespace bu = blitz::utils;
|
||||||
|
|
||||||
|
struct Argument {
|
||||||
|
std::string Name;
|
||||||
|
std::string Descritpion;
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::vector<Argument> CMD_ARGUMENTS = {
|
||||||
|
{"-P\t", "The port to listen/connect to. Default : 25565"},
|
||||||
|
{"--help\t", "Displays help"},
|
||||||
|
{"-C\t", "Instead of creating an internal server, connects to the one provided"},
|
||||||
|
{"-N\t", "Set player name"},
|
||||||
|
};
|
||||||
|
|
||||||
|
static void DisplayHelp() {
|
||||||
|
bu::LOG("Usage :");
|
||||||
|
for (std::size_t i = 0; i < CMD_ARGUMENTS.size(); i++) {
|
||||||
|
Argument& arg = CMD_ARGUMENTS[i];
|
||||||
|
bu::LOG(bu::Format("\t%s\t%s", arg.Name.c_str(), arg.Descritpion.c_str()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConsoleThread {
|
||||||
|
private:
|
||||||
|
std::thread m_Thread;
|
||||||
|
bool m_Running = true;
|
||||||
|
Client& m_Client;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ConsoleThread(Client& client) : m_Client(client) {}
|
||||||
|
|
||||||
|
void Start() {
|
||||||
|
m_Thread = std::thread([this]() {
|
||||||
|
std::string line;
|
||||||
|
while (m_Running) {
|
||||||
|
getline(std::cin, line);
|
||||||
|
|
||||||
|
if (line == "/stop") {
|
||||||
|
bu::LOG("Disconnecting ...");
|
||||||
|
m_Running = false;
|
||||||
|
} else if (line == "/shoot") {
|
||||||
|
bu::LOG("Shooting ...");
|
||||||
|
m_Client.Shoot();
|
||||||
|
} else if (line == "/help") {
|
||||||
|
bu::LOG("use \"/stop\" to disconnect");
|
||||||
|
bu::LOG("use \"/shoot\" to shoot");
|
||||||
|
} else {
|
||||||
|
m_Client.SendTextChat(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsRunning() const {
|
||||||
|
return m_Running;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Stop() {
|
||||||
|
m_Thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
~ConsoleThread() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
static void ProcessArgs(std::string& host, std::uint16_t port, const std::string& pseudo) {
|
||||||
|
std::unique_ptr<blitz::server::Server> internalServer;
|
||||||
|
|
||||||
|
if (host.empty()) {
|
||||||
|
internalServer = std::make_unique<blitz::server::Server>();
|
||||||
|
internalServer->Start(port, false);
|
||||||
|
host = "localhost";
|
||||||
|
}
|
||||||
|
|
||||||
|
Client client;
|
||||||
|
client.Connect(pseudo, host, port);
|
||||||
|
|
||||||
|
ConsoleThread consoleThread{client};
|
||||||
|
consoleThread.Start();
|
||||||
|
|
||||||
|
while (consoleThread.IsRunning()) {
|
||||||
|
client.UpdateSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
client.CloseConnection();
|
||||||
|
consoleThread.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** args) {
|
||||||
|
blitz::network::NetworkInitializer network;
|
||||||
|
|
||||||
|
std::uint16_t port = 25565;
|
||||||
|
std::string host;
|
||||||
|
std::string pseudo = "pseudo";
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
std::string arg = args[i];
|
||||||
|
|
||||||
|
if (arg == "--help") {
|
||||||
|
DisplayHelp();
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg == "-P") {
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i >= argc) {
|
||||||
|
bu::LOG("You must specify a valid port !");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
port = std::stoi(args[i]);
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
bu::LOG("You must specify a valid port !");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg == "-C") {
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i >= argc) {
|
||||||
|
bu::LOG("You must specify a valid host !");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
host = args[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg == "-N") {
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i >= argc) {
|
||||||
|
bu::LOG("You must specify a valid name !");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
pseudo = args[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ProcessArgs(host, port, pseudo);
|
||||||
|
|
||||||
|
bu::LOG("Goodbye !");
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
|
||||||
std::cout << "hello world!" << std::endl;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
77
xmake.lua
77
xmake.lua
@@ -1,5 +1,6 @@
|
|||||||
add_rules("mode.debug", "mode.release")
|
add_rules("mode.debug", "mode.release", "mode.valgrind")
|
||||||
|
|
||||||
|
add_includedirs("Blitz/include")
|
||||||
set_languages("c++17")
|
set_languages("c++17")
|
||||||
|
|
||||||
includes("Blitz/xmake/Blitz.lua")
|
includes("Blitz/xmake/Blitz.lua")
|
||||||
@@ -9,72 +10,10 @@ target("BlitzConsole")
|
|||||||
add_deps("Blitz")
|
add_deps("Blitz")
|
||||||
add_files("src/*.cpp")
|
add_files("src/*.cpp")
|
||||||
|
|
||||||
--
|
-- Valgrind test
|
||||||
-- If you want to known more usage about xmake, please see https://xmake.io
|
if is_mode("valgrind") then
|
||||||
--
|
on_run(function (target)
|
||||||
-- ## FAQ
|
os.execv("valgrind", {"-s", "--leak-check=full", target:targetfile()})
|
||||||
--
|
end)
|
||||||
-- You can enter the project directory firstly before building project.
|
end
|
||||||
--
|
|
||||||
-- $ cd projectdir
|
|
||||||
--
|
|
||||||
-- 1. How to build project?
|
|
||||||
--
|
|
||||||
-- $ xmake
|
|
||||||
--
|
|
||||||
-- 2. How to configure project?
|
|
||||||
--
|
|
||||||
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
|
|
||||||
--
|
|
||||||
-- 3. Where is the build output directory?
|
|
||||||
--
|
|
||||||
-- The default output directory is `./build` and you can configure the output directory.
|
|
||||||
--
|
|
||||||
-- $ xmake f -o outputdir
|
|
||||||
-- $ xmake
|
|
||||||
--
|
|
||||||
-- 4. How to run and debug target after building project?
|
|
||||||
--
|
|
||||||
-- $ xmake run [targetname]
|
|
||||||
-- $ xmake run -d [targetname]
|
|
||||||
--
|
|
||||||
-- 5. How to install target to the system directory or other output directory?
|
|
||||||
--
|
|
||||||
-- $ xmake install
|
|
||||||
-- $ xmake install -o installdir
|
|
||||||
--
|
|
||||||
-- 6. Add some frequently-used compilation flags in xmake.lua
|
|
||||||
--
|
|
||||||
-- @code
|
|
||||||
-- -- add debug and release modes
|
|
||||||
-- add_rules("mode.debug", "mode.release")
|
|
||||||
--
|
|
||||||
-- -- add macro definition
|
|
||||||
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
|
|
||||||
--
|
|
||||||
-- -- set warning all as error
|
|
||||||
-- set_warnings("all", "error")
|
|
||||||
--
|
|
||||||
-- -- set language: c99, c++11
|
|
||||||
-- set_languages("c99", "c++11")
|
|
||||||
--
|
|
||||||
-- -- set optimization: none, faster, fastest, smallest
|
|
||||||
-- set_optimize("fastest")
|
|
||||||
--
|
|
||||||
-- -- add include search directories
|
|
||||||
-- add_includedirs("/usr/include", "/usr/local/include")
|
|
||||||
--
|
|
||||||
-- -- add link libraries and search directories
|
|
||||||
-- add_links("tbox")
|
|
||||||
-- add_linkdirs("/usr/local/lib", "/usr/lib")
|
|
||||||
--
|
|
||||||
-- -- add system link libraries
|
|
||||||
-- add_syslinks("z", "pthread")
|
|
||||||
--
|
|
||||||
-- -- add compilation and link flags
|
|
||||||
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
|
|
||||||
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
|
|
||||||
--
|
|
||||||
-- @endcode
|
|
||||||
--
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user