Skip to content

Commit cd52308

Browse files
committed
Const qualify type on write
1 parent 591c9a7 commit cd52308

5 files changed

Lines changed: 29 additions & 109 deletions

File tree

schemas/world/types.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"ChatType": {
125125
"kind": "enum_class",
126126
"underlying": "uint8",
127-
"cpp_namespace": "ember::protocol::server",
127+
"cpp_namespace": "ember::protocol",
128128
"values": {
129129
"say": 0,
130130
"party": 1,
@@ -166,7 +166,7 @@
166166
"PlayerChatTag": {
167167
"kind": "enum_class",
168168
"underlying": "uint8",
169-
"cpp_namespace": "ember::protocol::server",
169+
"cpp_namespace": "ember::protocol",
170170
"values": {
171171
"tag_none": 0,
172172
"tag_afk": 1,
@@ -177,7 +177,7 @@
177177
"StatusID": {
178178
"kind": "enum_class",
179179
"underlying": "uint8",
180-
"cpp_namespace": "ember::protocol::server",
180+
"cpp_namespace": "ember::protocol",
181181
"values": {
182182
"none": 0,
183183
"wait_queue": 1,
@@ -2363,18 +2363,6 @@
23632363
"sixty": 5
23642364
}
23652365
},
2366-
"StatusId": {
2367-
"kind": "enum_class",
2368-
"underlying": "uint8",
2369-
"cpp_namespace": "ember::protocol",
2370-
"values": {
2371-
"none": 0,
2372-
"wait_queue": 1,
2373-
"wait_join": 2,
2374-
"in_progress": 3,
2375-
"wait_leave": 4
2376-
}
2377-
},
23782366
"Emote": {
23792367
"kind": "external",
23802368
"include": "dbcs/MemoryDefs.h",

src/libs/protocol/include/protocol/Packets.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <protocol/server/Weather.h>
3636
#include <protocol/server/AccountDataTimes.h>
3737
#include <protocol/server/GenericMove.h>
38-
#include <protocol/server/MessageChat.h>
3938
#include <protocol/server/TriggerCinematic.h>
4039
#include <protocol/server/StandStateUpdate.h>
4140
#include <protocol/server/Notification.h>
@@ -86,7 +85,6 @@ using smsg_weather = ServerPacket<ServerOpcode::smsg_weather,
8685
using smsg_account_data_times = ServerPacket<ServerOpcode::smsg_account_data_times, server::AccountDataTimes>;
8786
using smsg_channel_notify = ServerPacket<ServerOpcode::smsg_channel_notify, server::ChannelNotify>;
8887
using msg_move_set_facing_s = ServerPacket<ServerOpcode::msg_move_set_facing, server::GenericMove>;
89-
using smsg_messagechat = ServerPacket<ServerOpcode::smsg_messagechat, server::MessageChat>;
9088
using smsg_trigger_cinematic = ServerPacket<ServerOpcode::smsg_trigger_cinematic, server::TriggerCinematic>;
9189
using smsg_standstate_update = ServerPacket<ServerOpcode::smsg_standstate_update, server::StandStateUpdate>;
9290
using smsg_notification = ServerPacket<ServerOpcode::smsg_notification, server::Notification>;
Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1 @@
1-
/*
2-
* Copyright (c) 2026 Ember
3-
*
4-
* This Source Code Form is subject to the terms of the Mozilla Public
5-
* License, v. 2.0. If a copy of the MPL was not distributed with this
6-
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7-
*/
8-
9-
#pragma once
10-
11-
#include <protocol/Concepts.h>
12-
#include <protocol/StreamResult.h>
13-
#include <protocol/ResultCodes.h>
14-
#include <protocol/server/ChatTypes.h>
15-
#include <stdexcept>
16-
#include <cstdint>
17-
18-
namespace ember::protocol::server {
19-
20-
struct MessageChat final {
21-
ChatType type;
22-
std::uint32_t language;
23-
std::string message;
24-
std::uint64_t speech_bubble_attr;
25-
std::uint64_t chat_name_attr;
26-
std::string channel_name;
27-
std::uint32_t player_rank;
28-
std::uint64_t player_guid;
29-
std::string monster_name;
30-
PlayerChatTag player_tag;
31-
32-
StreamResult read_from_stream(le_stream auto& stream) {
33-
stream >> type;
34-
stream >> language;
35-
36-
if(type == SAY || type == YELL || type == PARTY) {
37-
stream >> speech_bubble_attr;
38-
stream >> chat_name_attr;
39-
} else if(type == CHANNEL) {
40-
stream >> spark::io::prefixed_null_terminated<std::string, std::uint32_t>(channel_name);
41-
stream >> player_rank;
42-
stream >> player_guid;
43-
}
44-
45-
stream >> spark::io::prefixed_null_terminated<std::string, std::uint32_t>(message);
46-
stream >> player_tag;
47-
return stream? StreamResult::success : StreamResult::failed;
48-
}
49-
50-
StreamResult write_to_stream(le_stream auto& stream) const {
51-
stream << type;
52-
stream << language;
53-
54-
if(type == MONSTER_WHISPER) {
55-
stream << monster_name;
56-
stream << std::uint64_t(0);
57-
} else if(type == SAY || type == YELL || type == PARTY) {
58-
stream << speech_bubble_attr;
59-
stream << chat_name_attr;
60-
} else if(type == WHISPER) {
61-
//stream << destination;
62-
} else if(type == CHANNEL) {
63-
stream << spark::io::null_terminated(channel_name);
64-
stream << player_rank;
65-
stream << player_guid;
66-
} else {
67-
stream << player_guid;
68-
}
69-
70-
stream << spark::io::prefixed_null_terminated<const std::string>(message);
71-
stream << player_tag;
72-
return stream? StreamResult::success : StreamResult::failed;
73-
}
74-
};
75-
76-
} // server, protocol, ember
1+
#include <generated/server/MessageChat.h>

src/realm/states/WorldEnter.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <protocol/client/ZoneUpdate.h>
2323
#include <protocol/client/ItemQuerySingle.h>
2424
#include <protocol/server/BattlefieldStatus.h>
25+
#include <protocol/server/MessageChat.h>
2526
#include <chrono>
2627
#include <format>
2728
#include <span>
@@ -59,7 +60,7 @@ void initiate_player_login(ClientContext& ctx, const PlayerLogin& event) {
5960
state_ctx.character_id = event.character_id_;
6061

6162
protocol::smsg_trigger_cinematic cinematic;
62-
cinematic->sequence_id = protocol::CinematicSequence::human;
63+
cinematic->sequence_id = 84; // temp
6364
ctx.send(cinematic);
6465

6566
protocol::smsg_login_verify_world verify_world;
@@ -93,10 +94,10 @@ void initiate_player_login(ClientContext& ctx, const PlayerLogin& event) {
9394

9495
protocol::smsg_messagechat motd;
9596
motd->language = 0;
96-
motd->type = protocol::server::SYSTEM;
97+
motd->type = protocol::ChatType::system;
9798
motd->message = "Welcome to a hacked together Ember test.";
9899
motd->player_guid = 0;
99-
motd->player_tag = protocol::server::TAG_NONE;
100+
motd->player_tag = protocol::PlayerChatTag::tag_none;
100101
ctx.send(motd);
101102
}
102103

@@ -407,7 +408,7 @@ void handle_messagechat(ClientContext& ctx) {
407408
response->message = packet->message;
408409
response->language = 0; // universal, we don't have any learned skills in this test
409410
response->player_guid = packed_guid;
410-
response->player_tag = protocol::server::TAG_GM;
411+
response->player_tag = protocol::PlayerChatTag::tag_gm;
411412

412413
if(packet->type == protocol::client::CHANNEL) {
413414
response->channel_name = packet->destination;
@@ -561,10 +562,10 @@ void log_msg(ClientContext& ctx, const LogRedirect& event) {
561562

562563
protocol::smsg_messagechat msg;
563564
msg->language = 0;
564-
msg->type = protocol::server::SYSTEM;
565+
msg->type = protocol::ChatType::system;
565566
msg->message = event.message;
566567
msg->player_guid = 0;
567-
msg->player_tag = protocol::server::TAG_NONE;
568+
msg->player_tag = protocol::PlayerChatTag::tag_none;
568569
ctx.send(msg);
569570
}
570571

@@ -578,16 +579,16 @@ void system_msg(ClientContext& ctx, const SystemMessage& event) {
578579
msg->language = 0;
579580

580581
if(event.type == SystemMessage::Type::whisper) {
581-
msg->type = protocol::server::MONSTER_WHISPER;
582-
msg->monster_name = "System";
582+
msg->type = protocol::ChatType::monster_whisper;
583+
msg->channel_name = "System";
583584
msg->message = event.message;
584585
} else {
585-
msg->type = protocol::server::SYSTEM;
586+
msg->type = protocol::ChatType::system;
586587
msg->message = std::format("[SERVER] {}", event.message);
587588
}
588589

589590
msg->player_guid = 0;
590-
msg->player_tag = protocol::server::TAG_NONE;
591+
msg->player_tag = protocol::PlayerChatTag::tag_none;
591592
ctx.send(msg);
592593
}
593594

src/tools/protogen/Generator.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
namespace ember::protogen {
2525

26+
enum class Dir {
27+
Read,
28+
Write,
29+
Both
30+
};
31+
2632
struct WalkState {
2733
std::vector<std::string> members;
2834
std::set<std::string> includes;
@@ -141,7 +147,7 @@ std::string cpp_type_name(const std::string& type, const TypeRegistry& reg) {
141147
return leaf;
142148
}
143149

144-
std::string string_adaptor_expr(const jsoncons::json& type_def, std::string_view expr) {
150+
std::string string_adaptor_expr(const jsoncons::json& type_def, std::string_view expr, Dir dir) {
145151
const auto encoding = type_def["encoding"].as<std::string>();
146152

147153
if(encoding == "null_terminated") {
@@ -150,13 +156,14 @@ std::string string_adaptor_expr(const jsoncons::json& type_def, std::string_view
150156

151157
const auto length_type = type_def["length_type"].as<std::string>();
152158
const auto length_cpp = cpp_type_for_primitive(length_type);
159+
std::string_view qualifier = (dir == Dir::Write)? "const " : "";
153160

154161
if(encoding == "prefixed") {
155-
return std::format("spark::io::prefixed<std::string, {}>({})", length_cpp, expr);
162+
return std::format("spark::io::prefixed<{}std::string, {}>({})", qualifier, length_cpp, expr);
156163
}
157164

158165
if(encoding == "prefixed_null_terminated") {
159-
return std::format("spark::io::prefixed_null_terminated<std::string, {}>({})", length_cpp, expr);
166+
return std::format("spark::io::prefixed_null_terminated<{}std::string, {}>({})", qualifier, length_cpp, expr);
160167
}
161168

162169
throw std::runtime_error(
@@ -262,8 +269,6 @@ std::string cpp_element_type(const std::string& type, const TypeRegistry& reg) {
262269
return cpp_type_name(type, reg);
263270
}
264271

265-
enum class Dir { Read, Write, Both };
266-
267272
// Emits stream ops for a single scalar value. `dir` controls which of
268273
// `read_ops` / `write_ops` are appended to — helpers that build their own
269274
// loops (e.g. until_end, which has asymmetric read vs write) use Read /
@@ -370,13 +375,16 @@ void emit_scalar_stream_op(const std::string& name, const std::string& type,
370375
}
371376

372377
if(kind == "string") {
373-
const auto adaptor = string_adaptor_expr(*def, qualified);
374378
if(do_read) {
379+
const auto adaptor = string_adaptor_expr(*def, qualified, Dir::Read);
375380
state.read_ops.emplace_back(std::format("{}stream >> {};", indent(state.read_tab), adaptor));
376381
}
382+
377383
if(do_write) {
384+
const auto adaptor = string_adaptor_expr(*def, qualified, Dir::Write);
378385
state.write_ops.emplace_back(std::format("{}stream << {};", indent(state.write_tab), adaptor));
379386
}
387+
380388
state.includes.insert("spark/buffers/StringAdaptors.h");
381389
return;
382390
}

0 commit comments

Comments
 (0)