Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 35 additions & 58 deletions src/diff_history_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,34 @@
#include "diff_history_encoder.h"
#include "utils.h"

#include <godot_cpp/core/object.hpp>
#include <godot_cpp/classes/stream_peer_buffer.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/object.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/classes/stream_peer_buffer.hpp>

Ref<_DiffHistoryEncoder> _DiffHistoryEncoder::new_(Ref<_PropertyHistoryBuffer> p_history, Ref<PropertyCache> p_property_cache)
{
Ref<_DiffHistoryEncoder> _DiffHistoryEncoder::new_(Ref<_PropertyHistoryBuffer> p_history, Ref<PropertyCache> p_property_cache, Ref<_NetworkSchema> p_schema) {
Ref<_DiffHistoryEncoder> ref;
ref.instantiate();
ref->_history = p_history;
ref->_property_cache = p_property_cache;
ref->_schema = p_schema;
return ref;
}

void _DiffHistoryEncoder::add_properties(TypedArray<PropertyEntry> properties)
{
void _DiffHistoryEncoder::add_properties(TypedArray<PropertyEntry> properties) {
bool has_new_properties = false;

for(Ref<PropertyEntry> property_entry : properties)
{
for (Ref<PropertyEntry> property_entry : properties) {
bool is_new = _ensure_property_idx(property_entry->_to_string());
has_new_properties = has_new_properties || is_new;
}

// If we added any new properties, increment version
if(has_new_properties)
if (has_new_properties)
_version = (_version + 1) % 256;
}

PackedByteArray _DiffHistoryEncoder::encode(int tick, int reference_tick, TypedArray<PropertyEntry> properties)
{
PackedByteArray _DiffHistoryEncoder::encode(int tick, int reference_tick, TypedArray<PropertyEntry> properties) {
ERR_FAIL_COND_V_MSG(properties.size() > 255, PackedByteArray(), "Property indices may not fit into bytes!");

Ref<_PropertySnapshot> snapshot = _history->get_snapshot(tick);
Expand All @@ -42,95 +39,83 @@ PackedByteArray _DiffHistoryEncoder::encode(int tick, int reference_tick, TypedA
_full_snapshot = snapshot->as_dictionary();
_encoded_snapshot = diff_snapshot->as_dictionary();

if(diff_snapshot->is_empty())
if (diff_snapshot->is_empty())
return PackedByteArray();

Ref<StreamPeerBuffer> buffer;
buffer.instantiate();
buffer->put_u8(_version);

for(String property : diff_snapshot->properties())
{
for (String property : diff_snapshot->properties()) {
int property_idx = _property_indexes.get_by_value(property);
Variant property_value = diff_snapshot->get_value(property);

buffer->put_u8(property_idx);
buffer->put_var(property_value);
_schema->encode(property, property_value, buffer);
/*
buffer->put_var(property_value);*/
}

return buffer->get_data_array();
}

Ref<_PropertySnapshot> _DiffHistoryEncoder::decode(PackedByteArray data, TypedArray<PropertyEntry> properties)
{
Ref<_PropertySnapshot> _DiffHistoryEncoder::decode(PackedByteArray data, TypedArray<PropertyEntry> properties) {
Ref<_PropertySnapshot> result = _PropertySnapshot::new_();

if(data.is_empty())
if (data.is_empty())
return result;

Ref<StreamPeerBuffer> buffer;
buffer.instantiate();
buffer->set_data_array(data);

uint8_t packet_version = buffer->get_u8();
if(packet_version != _version)
{
if(!_has_received)
{

if (packet_version != _version) {
if (!_has_received) {
// This is the first time we receive data
// Assume the version is OK
_version = packet_version;
}
else
{
} else {
// Since we don't remove entries, only add, we can still parse what we can
_logger->warning(vformat("Property config version mismatch - own %d != received %d", _version, packet_version));
}
}

_has_received = true;

while(buffer->get_available_bytes() > 0)
{
while (buffer->get_available_bytes() > 0) {
int property_idx = buffer->get_u8();
Variant property_value = buffer->get_var();
if(!_property_indexes.has_key(property_idx))
{
if (!_property_indexes.has_key(property_idx)) {
_logger->warning(vformat("Received unknown property index %d, ignoring!", property_idx));
continue;
}

String property_entry = _property_indexes.get_by_key(property_idx);
Variant property_value = _schema->decode(property_entry, buffer);
result->set_value(property_entry, property_value);
}

return result;
}

bool _DiffHistoryEncoder::apply(int tick, Ref<_PropertySnapshot> snapshot, int reference_tick, int sender)
{
if(tick < (int) Utils::get_autoload("NetworkRollback")->get("history_start"))
{
bool _DiffHistoryEncoder::apply(int tick, Ref<_PropertySnapshot> snapshot, int reference_tick, int sender) {
if (tick < (int)Utils::get_autoload("NetworkRollback")->get("history_start")) {
// State too old!
_logger->error(vformat("Received diff snapshot for @%d, rejecting because older than %s frames", tick, Utils::get_autoload("NetworkRollback")->get("history_limit")));
return false;
}

if(snapshot->is_empty())
if (snapshot->is_empty())
return true;

if(sender > 0)
{
if (sender > 0) {
snapshot->sanitize(sender, _property_cache);
if(snapshot->is_empty())
{
if (snapshot->is_empty()) {
_logger->warning(vformat("Received invalid diff from #%s for @%s", sender, tick));
return false;
}
}

if(!_history->has(reference_tick))
{
if (!_history->has(reference_tick)) {
// Reference tick missing, hope for the best
_logger->warning(vformat("Reference tick %d missing for #%s applying %d", reference_tick, sender, tick));
}
Expand All @@ -140,27 +125,22 @@ bool _DiffHistoryEncoder::apply(int tick, Ref<_PropertySnapshot> snapshot, int r
return true;
}

Dictionary _DiffHistoryEncoder::get_encoded_snapshot()
{
Dictionary _DiffHistoryEncoder::get_encoded_snapshot() {
return _encoded_snapshot;
}

Dictionary _DiffHistoryEncoder::get_full_snapshot()
{
Dictionary _DiffHistoryEncoder::get_full_snapshot() {
return _full_snapshot;
}

bool _DiffHistoryEncoder::_ensure_property_idx(String property)
{
if(_property_indexes.has_value(property))
{
bool _DiffHistoryEncoder::_ensure_property_idx(String property) {
if (_property_indexes.has_value(property)) {
return false;
}

ERR_FAIL_COND_V_MSG(_property_indexes.size() >= 256, false, "Property index map is full, can't add new property!");
int idx = UtilityFunctions::hash(property) % 256;
while(_property_indexes.has_key(idx))
{
while (_property_indexes.has_key(idx)) {
idx = UtilityFunctions::hash(idx + 1) % 256;
}
_property_indexes.insert(idx, property);
Expand All @@ -170,17 +150,14 @@ bool _DiffHistoryEncoder::_ensure_property_idx(String property)

Ref<_NetfoxLogger> _DiffHistoryEncoder::_logger;

void _DiffHistoryEncoder::_bind_methods()
{
void _DiffHistoryEncoder::_bind_methods() {
_logger = _NetfoxLogger::for_netfox("DiffHistoryEncoder");

ClassDB::bind_static_method("_DiffHistoryEncoder", D_METHOD("new_", "p_history", "p_property_cache"), &_DiffHistoryEncoder::new_);
ClassDB::bind_method(D_METHOD("add_properties", "properties"), &_DiffHistoryEncoder::add_properties);
ClassDB::bind_method(D_METHOD("encode", "tick", "reference_tick", "properties"), &_DiffHistoryEncoder::encode);
ClassDB::bind_method(D_METHOD("decode", "data", "properties"), &_DiffHistoryEncoder::decode);
ClassDB::bind_method(D_METHOD("apply", "tick", "snapshot", "reference_tick", "sender"), &_DiffHistoryEncoder::apply, DEFVAL(-1));
ClassDB::bind_method(D_METHOD("get_encoded_snapshot"), &_DiffHistoryEncoder::get_encoded_snapshot);
ClassDB::bind_method(D_METHOD("get_full_snapshot"), &_DiffHistoryEncoder::get_full_snapshot);

}

16 changes: 9 additions & 7 deletions src/diff_history_encoder.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
#pragma once

#include "internal_bimap.h"
#include "logger.h"
#include "network_schema.h"
#include "property_cache.h"
#include "property_entry.h"
#include "logger.h"
#include "property_history_buffer.h"
#include "property_snapshot.h"
#include "internal_bimap.h"

#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/godot.hpp>
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/classes/ref_counted.hpp>

using namespace godot;

class _DiffHistoryEncoder : public RefCounted {
GDCLASS(_DiffHistoryEncoder, RefCounted);
public:

public:
protected:
Ref<_PropertyHistoryBuffer> _history;
Ref<PropertyCache> _property_cache;
Ref<_NetworkSchema> _schema;

Dictionary _full_snapshot;
Dictionary _encoded_snapshot;
Expand All @@ -37,15 +39,15 @@ class _DiffHistoryEncoder : public RefCounted {
_DiffHistoryEncoder() = default;
~_DiffHistoryEncoder() override = default;

static Ref<_DiffHistoryEncoder> new_(Ref<_PropertyHistoryBuffer> p_history, Ref<PropertyCache> p_property_cache);
static Ref<_DiffHistoryEncoder> new_(Ref<_PropertyHistoryBuffer> p_history, Ref<PropertyCache> p_property_cache, Ref<_NetworkSchema> p_schema);

void add_properties(TypedArray<PropertyEntry> properties);
PackedByteArray encode(int tick, int reference_tick, TypedArray<PropertyEntry> properties);
Ref<_PropertySnapshot> decode(PackedByteArray data, TypedArray<PropertyEntry> properties);

// TODO: Rework metrics so these are not needed
// TODO: Rework metrics so these are not needed
bool apply(int tick, Ref<_PropertySnapshot> snapshot, int reference_tick, int sender = -1);
Dictionary get_encoded_snapshot();
Dictionary get_full_snapshot();
bool _ensure_property_idx(String property);
};
};
38 changes: 38 additions & 0 deletions src/network_schema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include "serializers.h"
#include <godot_cpp/classes/stream_peer_buffer.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/variant/dictionary.hpp>

using namespace godot;

class _NetworkSchema : public RefCounted {
GDCLASS(_NetworkSchema, RefCounted);

protected:
Dictionary _serializers;
Ref<_VariantSerializer> _fallback;
static void _bind_methods() {
ClassDB::bind_static_method("_NetworkSchema", D_METHOD("new_", "p_serializers"), &_NetworkSchema::new_);
};

public:
_NetworkSchema() {
_fallback.instantiate();
};
~_NetworkSchema() {};
void encode(String path, Variant value, Ref<StreamPeerBuffer> &buffer) {
Ref<NetworkSchemaSerializer> serializer = Object::cast_to<NetworkSchemaSerializer>(_serializers.get(path, _fallback));
serializer->encode(value, buffer);
}
Variant decode(String path, Ref<StreamPeerBuffer> &buffer) {
Ref<NetworkSchemaSerializer> serializer = Object::cast_to<NetworkSchemaSerializer>(_serializers.get(path, _fallback));
return serializer->decode(buffer);
}
static Ref<_NetworkSchema> new_(Dictionary p_serializers) {
Ref<_NetworkSchema> ref;
ref.instantiate();
ref->_serializers = p_serializers;
return ref;
}
};
Loading
Loading