Skip to content

Commit 3d1bf2c

Browse files
committed
WIP controller refactor.
1 parent 65ba52d commit 3d1bf2c

11 files changed

Lines changed: 381 additions & 181 deletions

godot4/gd_networked_controller.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "core/io/marshalls.h"
4141
#include "gd_network_interface.h"
4242
#include "modules/network_synchronizer/core/processor.h"
43+
#include "modules/network_synchronizer/net_utilities.h"
4344
#include "modules/network_synchronizer/scene_synchronizer_debugger.h"
4445
#include "scene/main/multiplayer_api.h"
4546

@@ -265,7 +266,11 @@ bool GdNetworkedController::is_nonet_controller() const {
265266
}
266267

267268
void GdNetworkedController::collect_inputs(double p_delta, DataBuffer &r_buffer) {
268-
NS_PROFILE_NODE
269+
CharString c = String(get_path()).utf8();
270+
if (c.size() >= std::numeric_limits<std::uint16_t>::max()) {
271+
c.resize(std::numeric_limits<std::uint16_t>::max() - 1);
272+
}
273+
NS_PROFILE_NAMED(c)
269274

270275
const bool executed = GDVIRTUAL_CALL(_collect_inputs, p_delta, &r_buffer);
271276
if (executed == false) {
@@ -274,7 +279,11 @@ void GdNetworkedController::collect_inputs(double p_delta, DataBuffer &r_buffer)
274279
}
275280

276281
void GdNetworkedController::controller_process(double p_delta, DataBuffer &p_buffer) {
277-
NS_PROFILE_NODE
282+
CharString c = String(get_path()).utf8();
283+
if (c.size() >= std::numeric_limits<std::uint16_t>::max()) {
284+
c.resize(std::numeric_limits<std::uint16_t>::max() - 1);
285+
}
286+
NS_PROFILE_NAMED(c)
278287

279288
const bool executed = GDVIRTUAL_CALL(
280289
_controller_process,
@@ -287,7 +296,11 @@ void GdNetworkedController::controller_process(double p_delta, DataBuffer &p_buf
287296
}
288297

289298
bool GdNetworkedController::are_inputs_different(DataBuffer &p_buffer_A, DataBuffer &p_buffer_B) {
290-
NS_PROFILE_NODE
299+
CharString c = String(get_path()).utf8();
300+
if (c.size() >= std::numeric_limits<std::uint16_t>::max()) {
301+
c.resize(std::numeric_limits<std::uint16_t>::max() - 1);
302+
}
303+
NS_PROFILE_NAMED(c)
291304

292305
bool are_different = true;
293306
const bool executed = GDVIRTUAL_CALL(
@@ -305,7 +318,11 @@ bool GdNetworkedController::are_inputs_different(DataBuffer &p_buffer_A, DataBuf
305318
}
306319

307320
uint32_t GdNetworkedController::count_input_size(DataBuffer &p_buffer) {
308-
NS_PROFILE_NODE
321+
CharString c = String(get_path()).utf8();
322+
if (c.size() >= std::numeric_limits<std::uint16_t>::max()) {
323+
c.resize(std::numeric_limits<std::uint16_t>::max() - 1);
324+
}
325+
NS_PROFILE_NAMED(c)
309326

310327
int input_size = 0;
311328
const bool executed = GDVIRTUAL_CALL(_count_input_size, &p_buffer, input_size);

godot4/gd_scene_synchronizer.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ void GdSceneSynchronizer::_bind_methods() {
6666
ClassDB::bind_method(D_METHOD("get_frames_per_seconds"), &GdSceneSynchronizer::get_frames_per_seconds);
6767

6868
ClassDB::bind_method(D_METHOD("register_node", "node"), &GdSceneSynchronizer::register_node_gdscript);
69-
ClassDB::bind_method(D_METHOD("register_node_as_controller_by_peer", "node", "peer"), &GdSceneSynchronizer::register_node_as_controller_by_peer);
7069
ClassDB::bind_method(D_METHOD("unregister_node", "node"), &GdSceneSynchronizer::unregister_node);
70+
ClassDB::bind_method(D_METHOD("setup_controller", "node", "peer", "collect_input_func", "count_input_size_func", "are_inputs_different_func", "proces_func"), &GdSceneSynchronizer::setup_controller);
7171
ClassDB::bind_method(D_METHOD("get_node_id", "node"), &GdSceneSynchronizer::get_node_id);
7272
ClassDB::bind_method(D_METHOD("get_node_from_id", "id", "expected"), &GdSceneSynchronizer::get_node_from_id, DEFVAL(true));
7373

@@ -468,8 +468,39 @@ uint32_t GdSceneSynchronizer::register_node_gdscript(Node *p_node) {
468468
return id.id;
469469
}
470470

471-
void GdSceneSynchronizer::register_node_as_controller_by_peer(Node *p_node, int p_peer) {
472-
scene_synchronizer.register_app_object_as_controlled_by_peer(scene_synchronizer.find_object_local_id(scene_synchronizer.to_handle(p_node)), p_peer);
471+
void GdSceneSynchronizer::setup_controller(
472+
Node *p_node,
473+
int p_peer,
474+
const Callable &p_collect_input_func,
475+
const Callable &p_count_input_size_func,
476+
const Callable &p_are_inputs_different_func,
477+
const Callable &p_process_func) {
478+
scene_synchronizer.setup_controller(
479+
scene_synchronizer.find_object_local_id(scene_synchronizer.to_handle(p_node)),
480+
p_peer,
481+
[p_collect_input_func](double p_delta, DataBuffer &r_collect_input) -> void {
482+
Array arguments;
483+
arguments.push_back(p_delta);
484+
arguments.push_back(&r_collect_input);
485+
p_collect_input_func.callv(arguments);
486+
},
487+
[p_count_input_size_func](DataBuffer &p_collect_input) -> int {
488+
Array arguments;
489+
arguments.push_back(&p_collect_input);
490+
return p_count_input_size_func.callv(arguments);
491+
},
492+
[p_are_inputs_different_func](DataBuffer &p_collect_input_A, DataBuffer &p_collect_input_B) -> bool {
493+
Array arguments;
494+
arguments.push_back(&p_collect_input_A);
495+
arguments.push_back(&p_collect_input_B);
496+
return p_are_inputs_different_func.callv(arguments);
497+
},
498+
[p_process_func](double p_delta, DataBuffer &p_collect_input) -> void {
499+
Array arguments;
500+
arguments.push_back(p_delta);
501+
arguments.push_back(&p_collect_input);
502+
p_process_func.callv(arguments);
503+
});
473504
}
474505

475506
void GdSceneSynchronizer::unregister_node(Node *p_node) {

godot4/gd_scene_synchronizer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ class GdSceneSynchronizer : public Node, public NS::SynchronizerManager {
9898
/// Register a new node and returns its `NodeData`.
9999
NS::ObjectLocalId register_node(Node *p_node);
100100
uint32_t register_node_gdscript(Node *p_node);
101-
void register_node_as_controller_by_peer(Node *p_node, int p_peer);
101+
void setup_controller(
102+
Node *p_node,
103+
int p_peer,
104+
const Callable &p_collect_input_func,
105+
const Callable &p_count_input_size_func,
106+
const Callable &p_are_inputs_different_func,
107+
const Callable &p_process_func);
102108
void unregister_node(Node *p_node);
103109

104110
/// Returns the node ID.

net_utilities.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include "core/object_data.h"
44
#include "networked_controller.h"
5+
#include "scene_synchronizer_debugger.h"
56
#include <limits>
7+
#include <memory>
68

79
void NS::PeerData::set_latency(int p_latency) {
810
compressed_latency = std::round(float(std::min(p_latency, 1000)) / 4.0);
@@ -12,6 +14,11 @@ int NS::PeerData::get_latency() const {
1214
return compressed_latency * 4.0;
1315
}
1416

17+
NS::NetworkedControllerBase &NS::PeerData::get_controller() {
18+
controller = std::make_unique<NS::NetworkedControllerBase>();
19+
return *controller.get();
20+
}
21+
1522
bool NS::SyncGroup::is_realtime_node_list_changed() const {
1623
return simulated_sync_objects_list_changed;
1724
}
@@ -242,7 +249,7 @@ void NS::SyncGroup::notify_variable_changed(ObjectData *p_object_data, const std
242249

243250
void NS::SyncGroup::set_trickled_update_rate(NS::ObjectData *p_object_data, float p_update_rate) {
244251
const int index = trickled_sync_objects.find(p_object_data);
245-
ERR_FAIL_COND(index < 0);
252+
ENSURE(index >= 0);
246253
trickled_sync_objects[index].update_rate = p_update_rate;
247254
}
248255

@@ -252,7 +259,7 @@ float NS::SyncGroup::get_trickled_update_rate(const NS::ObjectData *p_object_dat
252259
return trickled_sync_objects[i].update_rate;
253260
}
254261
}
255-
ERR_PRINT(String() + "NodeData " + p_object_data->object_name.c_str() + " not found into `trickled_sync_objects`.");
262+
SceneSynchronizerDebugger::singleton()->print(ERROR, "NodeData " + p_object_data->object_name + " not found into `trickled_sync_objects`.");
256263
return 0.0;
257264
}
258265

net_utilities.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "core/var_data.h"
99
#include <chrono>
1010
#include <map>
11+
#include <memory>
1112
#include <string>
1213
#include <vector>
1314

@@ -31,14 +32,6 @@
3132
ZoneScopedN(name); \
3233
ZoneText(str.c_str(), str.size());
3334

34-
#define NS_PROFILE_NODE \
35-
ZoneScoped; \
36-
CharString c = String(get_path()).utf8(); \
37-
if (c.size() >= std::numeric_limits<std::uint16_t>::max()) { \
38-
c.resize(std::numeric_limits<std::uint16_t>::max() - 1); \
39-
} \
40-
ZoneText(c.ptr(), c.size());
41-
4235
#define NS_PROFILE_SET_INFO(str) \
4336
ZoneText(str.c_str(), str.size());
4437

@@ -48,7 +41,6 @@
4841
#define NS_PROFILE_WITH_INFO(str)
4942
#define NS_PROFILE_NAMED(name)
5043
#define NS_PROFILE_NAMED_WITH_INFO(name, str)
51-
#define NS_PROFILE_NODE
5244
#define NS_PROFILE_SET_INFO(str)
5345

5446
#endif
@@ -150,6 +142,16 @@ bool insert_unique(std::vector<V> &r_vec, const T &p_val) {
150142
return false;
151143
}
152144

145+
template <class V, typename T>
146+
V &insert_unique_and_get(std::vector<V> &r_vec, const T &p_val) {
147+
auto it = find(r_vec, p_val);
148+
if (it == r_vec.end()) {
149+
r_vec.push_back(p_val);
150+
return r_vec[r_vec.size() - 1];
151+
}
152+
return *it;
153+
}
154+
153155
template <class V, typename T>
154156
void insert_at_position_expand(std::vector<V> &r_vec, std::size_t p_index, const T &p_val, const T &p_default) {
155157
if (r_vec.size() <= p_index) {
@@ -436,11 +438,11 @@ struct PeerAuthorityData {
436438
};
437439

438440
struct PeerData {
441+
std::unique_ptr<class NetworkedControllerBase> controller;
442+
439443
public:
440444
PeerAuthorityData authority_data;
441445

442-
ObjectNetId controller_id = ObjectNetId::NONE;
443-
444446
private:
445447
std::uint8_t compressed_latency = -1;
446448

@@ -453,6 +455,11 @@ struct PeerData {
453455

454456
void set_compressed_latency(std::uint8_t p_compressed_latency) { compressed_latency = p_compressed_latency; }
455457
std::uint8_t get_compressed_latency() const { return compressed_latency; }
458+
459+
NetworkedControllerBase &get_controller();
460+
const NetworkedControllerBase *get_controller_optional() const {
461+
return controller.get();
462+
}
456463
};
457464

458465
struct PeerServerData {

0 commit comments

Comments
 (0)