Skip to content
Open
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
5 changes: 2 additions & 3 deletions addons/netfox/servers/data/object-snapshot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ func record_property(property: NodePath) -> void:
set_value(property, _object.get_indexed(property))

func apply() -> void:
for property in properties():
var value := get_value(property)
_object.set_indexed(property, value)
for property in _data:
_object.set_indexed(property, _data[property])

func is_auth() -> bool:
return _is_auth
Expand Down
4 changes: 3 additions & 1 deletion addons/netfox/servers/data/per-object-history.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func subjects() -> Array[Object]:
result.assign(_data.keys())
return result

func subjects_raw() -> Array:
return _data.keys()

func is_auth(tick: int, subject: Object) -> bool:
if not _data.has(subject):
return false
Expand All @@ -30,7 +33,6 @@ func erase_subject(subject: Object) -> void:
_data.erase(subject)

func ensure_snapshot(tick: int, subject: Object, carry_forward: bool) -> _ObjectSnapshot:
var has_subject := _data.has(subject)
if not _data.has(subject):
_data[subject] = _HistoryBuffer.new(_history_size)
var history := _data[subject] as _HistoryBuffer
Expand Down
6 changes: 6 additions & 0 deletions addons/netfox/servers/data/property-pool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ func get_properties_of(subject: Object) -> Array[NodePath]:
properties.assign(_properties_by_subject.get(subject, []))
return properties

func get_properties_of_raw(subject: Object) -> Array:
return _properties_by_subject.get(subject, [])

func get_subjects() -> Array[Object]:
var subjects := [] as Array[Object]
subjects.assign(_properties_by_subject.keys())
return subjects

func get_subjects_raw() -> Array:
return _properties_by_subject.keys()

func is_empty() -> bool:
return _properties_by_subject.is_empty()

Expand Down
6 changes: 3 additions & 3 deletions addons/netfox/servers/data/snapshot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func merge(snapshot: _Snapshot) -> bool:

func apply() -> void:
for subject in _data:
for property in _data[subject]:
var value = _data[subject][property]
(subject as Object).set_indexed(property, value)
var props := _data[subject] as Dictionary
for property in props:
(subject as Object).set_indexed(property, props[property])

func sanitize(sender: int) -> void:
var invalid_subjects := []
Expand Down
14 changes: 8 additions & 6 deletions addons/netfox/servers/network-history-server.gd
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func _record(tick: int, history: _PerObjectHistory, snapshots: _HistoryBuffer, p
if not snapshots.has_at(tick):
snapshots.set_at(tick, snapshot)

for subject in property_pool.get_subjects():
for subject in property_pool.get_subjects_raw():
assert(subject is Node, "Only nodes supported for now!")

# Don't record history when subject is not alive to prevent state corruption.
Expand All @@ -222,10 +222,12 @@ func _record(tick: int, history: _PerObjectHistory, snapshots: _HistoryBuffer, p
_logger.warning("Dropping recorded tick @%d for subject %s as out-of-bounds", [tick, subject])
continue

assert(not property_pool.get_properties_of(subject).is_empty(), "Subject present in property pool without properties! Please report a bug!")
for property in property_pool.get_properties_of(subject):
subject_snapshot.record_property(property)
snapshot.record_property(subject, property)
var properties := property_pool.get_properties_of_raw(subject)
assert(not properties.is_empty(), "Subject present in property pool without properties! Please report a bug!")
for property in properties:
var value: Variant = subject.get_indexed(property)
subject_snapshot.set_value(property, value)
snapshot.set_property(subject, property, value)
snapshot.set_auth(subject, is_auth)
subject_snapshot.set_auth(is_auth)

Expand All @@ -238,7 +240,7 @@ func _record(tick: int, history: _PerObjectHistory, snapshots: _HistoryBuffer, p
func _restore_latest(tick: int, history: _PerObjectHistory) -> bool:
var any_applied := false

for subject in history.subjects():
for subject in history.subjects_raw():
# Grab latest snapshot up to tick
var snapshot := history.get_latest_snapshot(tick, subject)

Expand Down
53 changes: 53 additions & 0 deletions test/netfox/servers/network-history-server-rollback.perf.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
extends VestTest

# Perf coverage for rollback record/restore properties

func get_suite_name() -> String:
return "NetworkHistoryServer (rollback state)"

func suite():
for count in [16, 256, 4096]:
test("%d nodes x 4 properties" % count, func():
var nodes := await get_nodes(count)
for node in nodes:
NetworkHistoryServer.register_rollback_state(node, "position")
NetworkHistoryServer.register_rollback_state(node, "rotation")
NetworkHistoryServer.register_rollback_state(node, "velocity")
NetworkHistoryServer.register_rollback_state(node, "health")

NetworkHistoryServer._record_rollback_state(0)

benchmark("record()", func(__):
NetworkHistoryServer._record_rollback_state(0)
).with_duration(1.).with_batch_size(16).run()

benchmark("restore()", func(__):
NetworkHistoryServer._restore_rollback_state(0)
).with_duration(1.).with_batch_size(16).run()

for node in nodes:
NetworkHistoryServer.deregister(node)
free_nodes(nodes)
)

func get_nodes(count: int) -> Array[Node]:
var nodes := [] as Array[Node]

for i in count:
var node := StateNode.new()
node.name = "Node %d" % i
nodes.append(node)
Vest.get_tree().root.add_child.call_deferred(node)

for node in nodes:
await node.ready

return nodes

func free_nodes(nodes: Array[Node]) -> void:
for node in nodes:
node.queue_free()

class StateNode extends Node3D:
var velocity := Vector3.ZERO
var health := 100
Loading