summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorhovertank3d <[email protected]>2025-11-15 18:31:32 +0100
committerhovertank3d <[email protected]>2025-11-15 18:31:32 +0100
commitc2b53f27bad24003867afa5f5f7aedcf6fa7b26d (patch)
treeb1cbc5ae52957df6c6aeba60c7be5ae35872e709 /include
parentfa318825ef0404816728f733484caa1bc23afa53 (diff)
downloadogurec-c2b53f27bad24003867afa5f5f7aedcf6fa7b26d.tar.gz
ogurec-c2b53f27bad24003867afa5f5f7aedcf6fa7b26d.zip
minor fixes
Diffstat (limited to 'include')
-rw-r--r--include/net/client.hpp4
-rw-r--r--include/net/conn.hpp51
-rw-r--r--include/net/types.hpp102
-rw-r--r--include/util/io.hpp22
4 files changed, 121 insertions, 58 deletions
diff --git a/include/net/client.hpp b/include/net/client.hpp
index 87b0ea1..fdcb27d 100644
--- a/include/net/client.hpp
+++ b/include/net/client.hpp
@@ -26,10 +26,10 @@ class client {
struct sockaddr_in server_address {};
public:
- client(std::string hostname, int port)
+ client(std::string ip, int port)
{
server_address.sin_family = AF_INET;
- inet_pton(AF_INET, hostname.c_str(), &server_address.sin_addr);
+ inet_pton(AF_INET, ip.c_str(), &server_address.sin_addr);
server_address.sin_port = htons(port);
}
diff --git a/include/net/conn.hpp b/include/net/conn.hpp
index 6f57293..9755f88 100644
--- a/include/net/conn.hpp
+++ b/include/net/conn.hpp
@@ -86,12 +86,6 @@ public:
});
}
- template <packet::packet T>
- void send_packet(T p)
- {
- send_packet(T::packet_id, encode_packet(p));
- }
-
void send_packet(uint8_t id, std::span<uint8_t> payload)
{
std::vector<uint8_t> packet_data;
@@ -106,6 +100,13 @@ public:
rw.write(packet_data);
}
+ template <packet::packet T>
+ void send_packet(T p)
+ {
+ auto payload = encode_packet(p);
+ send_packet(T::packet_id, payload);
+ }
+
bool handle_netmodule(std::span<uint8_t> payload)
{
uint16_t id;
@@ -128,29 +129,33 @@ public:
bool handle()
{
- auto [id, len] = read_header();
- if (id == 0) {
- return false;
- }
+ try {
+ auto [id, len] = read_header();
+ if (id == 0) {
+ return false;
+ }
- std::vector<uint8_t> payload(len);
- rw.read(payload);
+ std::vector<uint8_t> payload(len);
+ rw.read(payload);
- // got netmodule
- if (id == 82) {
- return handle_netmodule(payload);
- }
+ // got netmodule
+ if (id == 82) {
+ return handle_netmodule(payload);
+ }
- if (handlers[id].size() <= 0) {
- return false;
- }
+ if (handlers[id].size() <= 0) {
+ return false;
+ }
- for (auto& h : handlers[id]) {
- if (h(payload) == true) {
- break;
+ for (auto& h : handlers[id]) {
+ if (h(payload) == true) {
+ break;
+ }
}
+ return true;
+ } catch (io::file_io::eof e) {
+ return false;
}
- return true;
}
};
} \ No newline at end of file
diff --git a/include/net/types.hpp b/include/net/types.hpp
index 13c96e0..5fe4bac 100644
--- a/include/net/types.hpp
+++ b/include/net/types.hpp
@@ -1,11 +1,14 @@
#pragma once
#include <array>
+#include <cstdint>
#include <cstring>
#include <string>
#include <unistd.h>
+#include <vector>
#include "util/bitset.hpp"
+#include "util/io.hpp"
#include "version.hpp"
namespace net {
@@ -22,25 +25,41 @@ concept compressed_packet = packet<T> && requires(T x) { x.compressed; };
template <class T>
concept netmodule = requires(T x) { x.module_id; };
-struct nstring : std::string {
- enum : uint8_t {
+struct nstring {
+ enum ns_type : uint8_t {
Literal,
Formattable,
LocalizationKey,
- } mode;
-
- std::string s;
-
- nstring(std::string&& s)
- : mode(Literal)
- , s(s)
- {
+ } type;
+
+ std::string text;
+ std::vector<std::string> substitutions;
+
+ template <class T>
+ ssize_t read(io::serialized_io<T>& io) {
+ uint8_t t;
+ auto offset = io.read(t);
+ type = (ns_type(t));
+
+ offset += io.read(text);
+
+ uint8_t subs_len;
+ offset += io.read(subs_len);
+ if (subs_len > 0) {
+ substitutions.resize(subs_len);
+ io.read(substitutions);
+ }
+
+ return offset;
}
-
- nstring(std::string& s)
- : mode(Literal)
- , s(s)
- {
+
+ template <class T>
+ ssize_t write(io::serialized_io<T>& io) {
+ auto offset = io.write(uint8_t(type));
+ offset += io.write(text);
+ offset += io.write(uint8_t(substitutions.size()));
+ offset += io.write(substitutions);
+ return offset;
}
};
@@ -50,6 +69,12 @@ struct rgb {
uint8_t b;
};
+template<class T>
+struct vec2 {
+ T x;
+ T y;
+};
+
struct death_reason {
enum class reason : int {
Player = 0,
@@ -209,6 +234,28 @@ struct player_health {
uint16_t max;
};
+struct drop_item {
+ static constexpr uint8_t packet_id = 21;
+
+ int16_t old_id;
+
+ vec2<float> position;
+ vec2<float> velocity;
+ int16_t stack;
+ uint8_t prefix;
+ bool own_ignore;
+
+ int16_t new_id;
+};
+
+struct player_zones {
+ static constexpr uint8_t packet_id = 36;
+
+ uint8_t client_id;
+ uint32_t zone_flags;
+ uint8_t zone_flags2;
+};
+
struct request_password {
static constexpr uint8_t packet_id = 37;
};
@@ -264,22 +311,14 @@ struct tower_powers {
uint16_t solar, nebula, vertex, stardust;
};
-struct monster_types {
- static constexpr uint8_t packet_id = 136;
-
- std::array<std::array<uint16_t, 3>, 2> types;
-};
-
struct connection_completed {
static constexpr uint8_t packet_id = 129;
};
-struct player_zones {
- static constexpr uint8_t packet_id = 36;
+struct monster_types {
+ static constexpr uint8_t packet_id = 136;
- uint8_t client_id;
- uint32_t zone_flags;
- uint8_t zone_flags2;
+ std::array<std::array<uint16_t, 3>, 2> types;
};
struct player_loadout {
@@ -301,9 +340,18 @@ struct damage_player {
int8_t cooldown_counter;
};
+// NetModules
+
+struct client_text {
+ static constexpr uint8_t module_id = 1;
+
+ nstring command;
+ nstring text;
+};
+
inline nstring operator""_ns(const char* str, std::size_t)
{
- return nstring(str);
+ return nstring{nstring::Literal, str};
}
}
diff --git a/include/util/io.hpp b/include/util/io.hpp
index 8cfaf4c..e99c86a 100644
--- a/include/util/io.hpp
+++ b/include/util/io.hpp
@@ -41,6 +41,8 @@ public:
{
}
+ struct eof {};
+
file_io(std::string filename)
: fd(open(filename.c_str(), O_RDWR))
{
@@ -56,13 +58,21 @@ public:
ssize_t read_data(char* buf, size_t nbytes)
{
auto bytes = ::read(fd, buf, nbytes);
+ if (bytes == EOF) {
+ throw eof{};
+ }
+
assert(bytes >= 0);
return bytes;
}
ssize_t write_data(const char* buf, size_t nbytes)
{
- auto bytes = ::write(fd, buf, nbytes);
+ auto bytes = ::write(fd, buf, nbytes);
+ if (bytes == EOF) {
+ throw eof{};
+ }
+
assert(bytes >= 0);
return bytes;
}
@@ -117,7 +127,7 @@ public:
ssize_t read(T& f)
{
auto bytes = io.read_data(reinterpret_cast<char*>(&f), sizeof(T));
- f = to_little(f);
+ //f = to_little(f);
return bytes;
}
@@ -158,21 +168,21 @@ public:
ssize_t write(const T f)
{
auto x = to_little(f);
- return io.write_data(reinterpret_cast<char*>(&x), sizeof(T));
+ return io.write_data(reinterpret_cast<const char*>(&x), sizeof(T));
}
template <std::floating_point T>
ssize_t write(const T f)
{
// FIXME: endianness
- return io.write_data(reinterpret_cast<char*>(&f), sizeof(T));
+ return io.write_data(reinterpret_cast<const char*>(&f), sizeof(T));
}
ssize_t write(const std::string f)
{
// FIXME: varint
- auto bytes = write(f.size());
- bytes += io.write_data(f.data(), f.size());
+ auto bytes = write(uint8_t(f.size()));
+ bytes += io.write_data(f.c_str(), f.size());
return bytes;
}