Skip to content

Commit 2a83691

Browse files
committed
Controller refactoring WIP.
1 parent 38668c6 commit 2a83691

20 files changed

Lines changed: 412 additions & 1047 deletions

core/object_data.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "object_data.h"
22

3-
#include "modules/network_synchronizer/core/core.h"
3+
#include "core.h"
4+
#include "ensure.h"
45
#include "object_data_storage.h"
56

67
NS_NAMESPACE_BEGIN
@@ -51,7 +52,12 @@ bool ObjectData::can_trickled_sync() const {
5152
return func_trickled_collect && func_trickled_apply;
5253
}
5354

54-
void ObjectData::set_controlled_by_peer(int p_peer) {
55+
void ObjectData::set_controlled_by_peer(
56+
int p_peer,
57+
std::function<void(double /*delta*/, DataBuffer & /*r_data_buffer*/)> p_collect_input_func,
58+
std::function<int(DataBuffer & /*p_data_buffer*/)> p_count_input_size_func,
59+
std::function<bool(DataBuffer & /*p_data_buffer_A*/, DataBuffer & /*p_data_buffer_B*/)> p_are_inputs_different_func,
60+
std::function<void(double /*delta*/, DataBuffer & /*p_data_buffer*/)> p_process_func) {
5561
if (p_peer == controlled_by_peer) {
5662
return;
5763
}
@@ -60,23 +66,22 @@ void ObjectData::set_controlled_by_peer(int p_peer) {
6066
controlled_by_peer = p_peer;
6167

6268
storage.notify_set_controlled_by_peer(old_peer, *this);
63-
}
64-
65-
int ObjectData::get_controlled_by_peer() const {
66-
return controlled_by_peer;
67-
}
6869

69-
void ObjectData::set_controller(NetworkedControllerBase *p_controller) {
70-
if (controller == p_controller) {
71-
return;
70+
if (controlled_by_peer != -1) {
71+
ENSURE_MSG(p_collect_input_func, "The function collect_input_func is not valid.");
72+
ENSURE_MSG(p_count_input_size_func, "The function count_input_size is not valid.");
73+
ENSURE_MSG(p_are_inputs_different_func, "The function are_inputs_different is not valid.");
74+
ENSURE_MSG(p_process_func, "The function process is not valid.");
7275
}
7376

74-
controller = p_controller;
75-
storage.notify_set_controller(*this);
77+
controller_funcs.collect_input = p_collect_input_func;
78+
controller_funcs.count_input_size = p_count_input_size_func;
79+
controller_funcs.are_inputs_different = p_are_inputs_different_func;
80+
controller_funcs.process = p_process_func;
7681
}
7782

78-
NetworkedControllerBase *ObjectData::get_controller() const {
79-
return controller;
83+
int ObjectData::get_controlled_by_peer() const {
84+
return controlled_by_peer;
8085
}
8186

8287
VarId ObjectData::find_variable_id(const std::string &p_var_name) const {

core/object_data.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ struct ObjectData {
4141
ObjectData(class ObjectDataStorage &p_storage);
4242

4343
private:
44-
// ID used to reference this NodeData in the networked calls.
44+
/// ID used to reference this ObjectData in the networked calls.
45+
/// This id is set by the server and the client may not have it yet.
4546
ObjectNetId net_id = ObjectNetId::NONE;
47+
48+
/// ID used to reference this ObjectData locally. This id is always set.
4649
ObjectLocalId local_id = ObjectLocalId::NONE;
50+
4751
int controlled_by_peer = -1;
4852

49-
/// Associated controller.
50-
class NetworkedControllerBase *controller = nullptr;
53+
public:
54+
struct {
55+
std::function<void(double /*delta*/, DataBuffer & /*r_data_buffer*/)> collect_input;
56+
std::function<int(DataBuffer & /*p_data_buffer*/)> count_input_size;
57+
std::function<bool(DataBuffer & /*p_data_buffer_A*/, DataBuffer & /*p_data_buffer_B*/)> are_inputs_different;
58+
std::function<void(double /*delta*/, DataBuffer & /*p_data_buffer*/)> process;
59+
} controller_funcs;
5160

5261
public:
5362
uint64_t instance_id = 0; // TODO remove this?
@@ -74,12 +83,14 @@ struct ObjectData {
7483
bool has_registered_process_functions() const;
7584
bool can_trickled_sync() const;
7685

77-
void set_controlled_by_peer(int p_peer);
86+
void set_controlled_by_peer(
87+
int p_peer,
88+
std::function<void(double /*delta*/, DataBuffer & /*r_data_buffer*/)> p_collect_input_func = nullptr,
89+
std::function<int(DataBuffer & /*p_data_buffer*/)> p_count_input_size_func = nullptr,
90+
std::function<bool(DataBuffer & /*p_data_buffer_A*/, DataBuffer & /*p_data_buffer_B*/)> p_are_inputs_different_func = nullptr,
91+
std::function<void(double /*delta*/, DataBuffer & /*p_data_buffer*/)> p_process_func = nullptr);
7892
int get_controlled_by_peer() const;
7993

80-
void set_controller(class NetworkedControllerBase *p_controller);
81-
class NetworkedControllerBase *get_controller() const;
82-
8394
VarId find_variable_id(const std::string &p_var_name) const;
8495
};
8596

core/object_data_storage.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ ObjectDataStorage::~ObjectDataStorage() {
2525
free_local_indices.clear();
2626
objects_data.clear();
2727
objects_data_organized_by_netid.clear();
28-
objects_data_controllers.clear();
2928
objects_data_controlled_by_peers.clear();
3029
}
3130

@@ -61,12 +60,6 @@ void ObjectDataStorage::deallocate_object_data(ObjectData &p_object_data) {
6160
objects_data_organized_by_netid[net_id.id] = nullptr;
6261
}
6362

64-
// Clear the controller pointer.
65-
if (p_object_data.controller) {
66-
p_object_data.controller = nullptr;
67-
notify_set_controller(p_object_data);
68-
}
69-
7063
// Clear the peers array.
7164
const int cbp = p_object_data.controlled_by_peer;
7265
p_object_data.controlled_by_peer = -1;
@@ -120,15 +113,6 @@ ObjectLocalId ObjectDataStorage::find_object_local_id(ObjectHandle p_handle) con
120113
return ObjectLocalId::NONE;
121114
}
122115

123-
ObjectLocalId ObjectDataStorage::find_object_local_id(const NetworkedControllerBase &p_controller) const {
124-
for (auto od : objects_data_controllers) {
125-
if (od->controller == (&p_controller)) {
126-
return od->local_id;
127-
}
128-
}
129-
return ObjectLocalId::NONE;
130-
}
131-
132116
NS::ObjectData *ObjectDataStorage::get_object_data(ObjectNetId p_net_id, bool p_expected) {
133117
if (p_expected) {
134118
ERR_FAIL_UNSIGNED_INDEX_V(p_net_id.id, objects_data_organized_by_netid.size(), nullptr);
@@ -185,10 +169,6 @@ const std::vector<ObjectData *> &ObjectDataStorage::get_objects_data() const {
185169
return objects_data;
186170
}
187171

188-
const std::vector<ObjectData *> &ObjectDataStorage::get_controllers_objects_data() const {
189-
return objects_data_controllers;
190-
}
191-
192172
const std::vector<ObjectData *> &ObjectDataStorage::get_sorted_objects_data() const {
193173
return objects_data_organized_by_netid;
194174
}
@@ -225,19 +205,6 @@ bool ObjectDataStorage::is_empty() const {
225205
return true;
226206
}
227207

228-
void ObjectDataStorage::notify_set_controller(ObjectData &p_object) {
229-
auto it = std::find(objects_data_controllers.begin(), objects_data_controllers.end(), &p_object);
230-
if (p_object.controller) {
231-
if (it == objects_data_controllers.end()) {
232-
objects_data_controllers.push_back(&p_object);
233-
}
234-
} else {
235-
if (it != objects_data_controllers.end()) {
236-
objects_data_controllers.erase(it);
237-
}
238-
}
239-
}
240-
241208
void ObjectDataStorage::notify_set_controlled_by_peer(int p_old_peer, ObjectData &p_object) {
242209
if (p_old_peer != -1) {
243210
std::vector<ObjectData *> *objects = NS::MapFunc::get_or_null(objects_data_controlled_by_peers, p_old_peer);

core/object_data_storage.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class ObjectDataStorage {
1919
// All registered objects, that have the NetId assigned, organized per NetId.
2020
std::vector<ObjectData *> objects_data_organized_by_netid;
2121

22-
// All the controller nodes.
23-
std::vector<ObjectData *> objects_data_controllers;
24-
2522
std::map<int, std::vector<ObjectData *>> objects_data_controlled_by_peers;
2623

2724
public:
@@ -34,7 +31,6 @@ class ObjectDataStorage {
3431
void object_set_net_id(ObjectData &p_object_data, ObjectNetId p_new_id);
3532

3633
ObjectLocalId find_object_local_id(ObjectHandle p_handle) const;
37-
ObjectLocalId find_object_local_id(const class NetworkedControllerBase &p_controller) const;
3834

3935
ObjectData *get_object_data(ObjectNetId p_net_id, bool p_expected = true);
4036
const ObjectData *get_object_data(ObjectNetId p_net_id, bool p_expected = true) const;
@@ -45,15 +41,13 @@ class ObjectDataStorage {
4541
void reserve_net_ids(int p_count);
4642

4743
const std::vector<ObjectData *> &get_objects_data() const;
48-
const std::vector<ObjectData *> &get_controllers_objects_data() const;
4944
const std::vector<ObjectData *> &get_sorted_objects_data() const;
5045
const std::map<int, std::vector<ObjectData *>> &get_peers_controlled_objects_data() const;
5146
const std::vector<ObjectData *> *get_peer_controlled_objects_data(int p_peer) const;
5247

5348
ObjectNetId generate_net_id() const;
5449
bool is_empty() const;
5550

56-
void notify_set_controller(ObjectData &p_object);
5751
void notify_set_controlled_by_peer(int p_old_peer, ObjectData &p_object);
5852
};
5953

0 commit comments

Comments
 (0)