Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make shared compstates copy cloneable types #5459

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
23 changes: 20 additions & 3 deletions Robust.Shared.CompNetworkGenerator/ComponentNetworkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,29 @@ public class ComponentNetworkGenerator : ISourceGenerator
stateFields.Append($@"
public {typeDisplayStr} {name} = default!;");

// get first ctor arg of the field attribute, which determines whether the field should be cloned
// (like if its a dict or list)
if (IsCloneType(type))
{
// get first ctor arg of the field attribute, which determines whether the field should be cloned
// (like if its a dict or list)
getStateInit.Append($@"
// Avoid the allocations on release
#if !FULL_RELEASE
if (type.NullableAnnotation == NullableAnnotation.NotAnnotated)
{
getStateInit.Append($@"
{name} = new(component.{name}),");
}
else
{
getStateInit.Append($@"
{name} = component.{name} == null ? null : new(component.{name}),");
}
#else
{
getStateInit.Append($@"
{name} = component.{name},");
}
#endif


handleStateSetters.Append($@"
if (state.{name} == null)
Expand Down
28 changes: 21 additions & 7 deletions Robust.Shared/Physics/Systems/FixtureSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Robust.Shared.Physics.Systems
public sealed partial class FixtureSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
private EntityQuery<PhysicsMapComponent> _mapQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;
Expand Down Expand Up @@ -224,9 +225,18 @@ internal void OnPhysicsInit(EntityUid uid, FixturesComponent component, PhysicsC

private void OnGetState(EntityUid uid, FixturesComponent component, ref ComponentGetState args)
{
var copied = new Dictionary<string, Fixture>(component.Fixtures.Count);

foreach (var (id, fixture) in component.Fixtures)
{
var copy = new Fixture();
fixture.CopyTo(copy);
copied[id] = copy;
}

args.State = new FixtureManagerComponentState
{
Fixtures = component.Fixtures,
Fixtures = copied,
};
}

Expand All @@ -239,10 +249,6 @@ private void OnHandleState(EntityUid uid, FixturesComponent component, ref Compo
Log.Error($"Tried to apply fixture state for an entity without physics: {ToPrettyString(uid)}");
return;
}
foreach (var fixture in component.Fixtures.Values)
{
fixture.Owner = uid;
}

var toAddFixtures = new ValueList<(string Id, Fixture Fixture)>();
var toRemoveFixtures = new ValueList<(string Id, Fixture Fixture)>();
Expand All @@ -260,6 +266,7 @@ private void OnHandleState(EntityUid uid, FixturesComponent component, ref Compo
}

TransformComponent? xform = null;
var regenerate = false;

// Add / update new fixtures
// FUTURE SLOTH
Expand All @@ -272,10 +279,12 @@ private void OnHandleState(EntityUid uid, FixturesComponent component, ref Compo
{
toAddFixtures.Add((id, fixture));
}
// Retained fixture but new data
else if (!existing.Equivalent(fixture))
{
toRemoveFixtures.Add((id, existing));
toAddFixtures.Add((id, fixture));
fixture.CopyTo(existing);
computeProperties = true;
regenerate = true;
}
}

Expand Down Expand Up @@ -309,6 +318,11 @@ private void OnHandleState(EntityUid uid, FixturesComponent component, ref Compo
{
FixtureUpdate(uid, manager: component, body: physics);
}

if (regenerate)
{
_broadphase.RegenerateContacts(uid, body: physics, fixtures: component, xform: xform);
}
}

#region Restitution
Expand Down
Loading