Skip to content

Commit

Permalink
Merge branch 'space-wizards:master' into fix-warnings-2
Browse files Browse the repository at this point in the history
  • Loading branch information
TemporalOroboros authored Feb 12, 2025
2 parents 69ac472 + fea592e commit 0d6b379
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 27 deletions.
2 changes: 1 addition & 1 deletion MSBuild/Robust.Engine.Version.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<!-- This file automatically reset by Tools/version.py -->
<PropertyGroup><Version>243.0.0</Version></PropertyGroup>
<PropertyGroup><Version>244.0.0</Version></PropertyGroup>
</Project>
Expand Down
24 changes: 24 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ END TEMPLATE-->
*None yet*


## 244.0.0

### Breaking changes

* Increase physics speedcap default from 35m/s to 400m/s in-line with box2d v3.

### New features

* Add EntityManager overloads for ComponentRegistration that's faster than the generic methods.
* Add CreateWindowCenteredRight for BUIs.

### Bugfixes

* Avoid calling UpdateState before opening a BUI.


## 243.0.1

### Bugfixes

* Fixed `BaseWindow` sometimes not properly updating the mouse cursor shape.
* Revert `BaseWindow` OnClose ordering due to prior reliance upon the ordering.


## 243.0.0

### Breaking changes
Expand Down
96 changes: 96 additions & 0 deletions Robust.Benchmarks/EntityManager/HasComponentBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using JetBrains.Annotations;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.UnitTesting.Server;

namespace Robust.Benchmarks.EntityManager;

[Virtual]
public partial class HasComponentBenchmark
{
private static readonly Consumer Consumer = new();

private ISimulation _simulation = default!;
private IEntityManager _entityManager = default!;

private ComponentRegistration _compReg = default!;

private A _dummyA = new();

[UsedImplicitly]
[Params(1, 10, 100, 1000)]
public int N;

[GlobalSetup]
public void GlobalSetup()
{
_simulation = RobustServerSimulation
.NewSimulation()
.RegisterComponents(f => f.RegisterClass<A>())
.InitializeInstance();

_entityManager = _simulation.Resolve<IEntityManager>();
var map = _simulation.CreateMap().Uid;
var coords = new EntityCoordinates(map, default);
_compReg = _entityManager.ComponentFactory.GetRegistration(typeof(A));

for (var i = 0; i < N; i++)
{
var uid = _entityManager.SpawnEntity(null, coords);
_entityManager.AddComponent<A>(uid);
}
}

[Benchmark]
public void HasComponentGeneric()
{
for (var i = 2; i <= N+1; i++)
{
var uid = new EntityUid(i);
var result = _entityManager.HasComponent<A>(uid);
Consumer.Consume(result);
}
}

[Benchmark]
public void HasComponentCompReg()
{
for (var i = 2; i <= N+1; i++)
{
var uid = new EntityUid(i);
var result = _entityManager.HasComponent(uid, _compReg);
Consumer.Consume(result);
}
}

[Benchmark]
public void HasComponentType()
{
for (var i = 2; i <= N+1; i++)
{
var uid = new EntityUid(i);
var result = _entityManager.HasComponent(uid, typeof(A));
Consumer.Consume(result);
}
}

[Benchmark]
public void HasComponentGetType()
{
for (var i = 2; i <= N+1; i++)
{
var uid = new EntityUid(i);
var type = _dummyA.GetType();
var result = _entityManager.HasComponent(uid, type);
Consumer.Consume(result);
}
}

[ComponentProtoName("A")]
public sealed partial class A : Component
{
}
}
16 changes: 16 additions & 0 deletions Robust.Client/UserInterface/BoundUserInterfaceExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public static class BoundUserInterfaceExt
return window;
}

public static T CreateWindowCenteredRight<T>(this BoundUserInterface bui) where T : BaseWindow, new()
{
var window = GetWindow<T>(bui);

if (bui.EntMan.System<UserInterfaceSystem>().TryGetPosition(bui.Owner, bui.UiKey, out var position))
{
window.Open(position);
}
else
{
window.OpenCenteredRight();
}

return window;
}

/// <summary>
/// Creates a control for this BUI that will be disposed when it is disposed.
/// </summary>
Expand Down
16 changes: 3 additions & 13 deletions Robust.Client/UserInterface/CustomControls/BaseWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public virtual void Close()
return;
}

OnClose?.Invoke();
Parent.RemoveChild(this);
OnClose?.Invoke();
}

protected internal override void KeyBindDown(GUIBoundKeyEventArgs args)
Expand Down Expand Up @@ -103,6 +103,8 @@ protected internal override void MouseMove(GUIMouseMoveEventArgs args)
{
var cursor = CursorShape.Arrow;
var previewDragMode = GetDragModeFor(args.RelativePosition);
previewDragMode &= ~DragMode.Move;

switch (previewDragMode)
{
case DragMode.Top:
Expand All @@ -117,9 +119,6 @@ protected internal override void MouseMove(GUIMouseMoveEventArgs args)

case DragMode.Bottom | DragMode.Left:
case DragMode.Top | DragMode.Right:
cursor = CursorShape.Crosshair;
break;

case DragMode.Bottom | DragMode.Right:
case DragMode.Top | DragMode.Left:
cursor = CursorShape.Crosshair;
Expand Down Expand Up @@ -161,15 +160,6 @@ protected internal override void MouseMove(GUIMouseMoveEventArgs args)
var rect = new UIBox2(left, top, right, bottom);
LayoutContainer.SetPosition(this, rect.TopLeft);
SetSize = rect.Size;

/*
var timing = IoCManager.Resolve<IGameTiming>();
var l = GetValue<float>(LayoutContainer.MarginLeftProperty);
var t = GetValue<float>(LayoutContainer.MarginTopProperty);
Logger.Debug($"{timing.CurFrame}: {rect.TopLeft}/({l}, {t}), {rect.Size}/{SetSize}");
*/
}
}

Expand Down
5 changes: 2 additions & 3 deletions Robust.Shared/CVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,10 +1349,10 @@ protected CVars()
/// MaxLinVelocity is compared to the dot product of linearVelocity * frameTime.
/// </summary>
/// <remarks>
/// Default is 35 m/s. Around half a tile per tick at 60 ticks per second.
/// Default is 400 m/s in-line with Box2c. Box2d used 120m/s.
/// </remarks>
public static readonly CVarDef<float> MaxLinVelocity =
CVarDef.Create("physics.maxlinvelocity", 35f, CVar.SERVER | CVar.REPLICATED);
CVarDef.Create("physics.maxlinvelocity", 400f, CVar.SERVER | CVar.REPLICATED);

/// <summary>
/// Maximum angular velocity in full rotations per second.
Expand All @@ -1364,7 +1364,6 @@ protected CVars()
public static readonly CVarDef<float> MaxAngVelocity =
CVarDef.Create("physics.maxangvelocity", 15f);


/*
* User interface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public abstract class BoundUserInterface : IDisposable
[Dependency] protected readonly ISharedPlayerManager PlayerManager = default!;
protected readonly SharedUserInterfaceSystem UiSystem;

public bool IsOpened { get; protected set; }

public readonly Enum UiKey;
public EntityUid Owner { get; }

Expand Down Expand Up @@ -43,6 +45,10 @@ protected BoundUserInterface(EntityUid owner, Enum uiKey)
/// </summary>
protected internal virtual void Open()
{
if (IsOpened)
return;

IsOpened = true;
}

/// <summary>
Expand Down Expand Up @@ -93,6 +99,10 @@ protected internal virtual void ReceiveMessage(BoundUserInterfaceMessage message
/// </summary>
public void Close()
{
if (!IsOpened)
return;

IsOpened = false;
UiSystem.CloseUi(Owner, UiKey, PlayerManager.LocalEntity, predicted: true);
}

Expand Down
61 changes: 54 additions & 7 deletions Robust.Shared/GameObjects/EntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,23 @@ public void AddComponents(EntityUid target, ComponentRegistry registry, bool rem
{
var reg = _componentFactory.GetRegistration(name);

if (HasComponent(target, reg.Type))
if (removeExisting)
{
if (!removeExisting)
var comp = _componentFactory.GetComponent(reg);
_serManager.CopyTo(entry.Component, ref comp, notNullableOverride: true);
AddComponentInternal(target, comp, reg, overwrite: true, metadata: metadata);
}
else
{
if (HasComponent(target, reg))
{
continue;
}

RemoveComponent(target, reg.Type, metadata);
var comp = _componentFactory.GetComponent(reg);
_serManager.CopyTo(entry.Component, ref comp, notNullableOverride: true);
AddComponentInternal(target, comp, reg, overwrite: false, metadata: metadata);
}

var comp = _componentFactory.GetComponent(reg);
_serManager.CopyTo(entry.Component, ref comp, notNullableOverride: true);
AddComponent(target, comp, metadata: metadata);
}
}

Expand Down Expand Up @@ -315,6 +321,22 @@ public void AddComponent<T>(EntityUid uid, T component, bool overwrite = false,
AddComponentInternal(uid, component, overwrite, false, metadata);
}

private void AddComponentInternal<T>(
EntityUid uid,
T component,
ComponentRegistration compReg,
bool overwrite = false,
MetaDataComponent? metadata = null) where T : IComponent
{
if (!MetaQuery.Resolve(uid, ref metadata, false))
throw new ArgumentException($"Entity {uid} is not valid.", nameof(uid));

DebugTools.Assert(component.Owner == default);
component.Owner = uid;

AddComponentInternal(uid, component, compReg, overwrite, skipInit: false, metadata);
}

private void AddComponentInternal<T>(EntityUid uid, T component, bool overwrite, bool skipInit, MetaDataComponent? metadata) where T : IComponent
{
if (!MetaQuery.ResolveInternal(uid, ref metadata, false))
Expand Down Expand Up @@ -731,6 +753,14 @@ public bool HasComponent<T>([NotNullWhen(true)] EntityUid? uid) where T : ICompo
return uid.HasValue && HasComponent<T>(uid.Value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public bool HasComponent(EntityUid uid, ComponentRegistration reg)
{
var dict = _entTraitArray[reg.Idx.Value];
return dict.TryGetValue(uid, out var comp) && !comp.Deleted;
}

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
Expand Down Expand Up @@ -943,6 +973,23 @@ public bool TryGetComponent<T>([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(
return false;
}

/// <inheritdoc />
public bool TryGetComponent(EntityUid uid, ComponentRegistration reg, [NotNullWhen(true)] out IComponent? component)
{
var dict = _entTraitArray[reg.Idx.Value];
if (dict.TryGetValue(uid, out var comp))
{
if (!comp.Deleted)
{
component = comp;
return true;
}
}

component = null;
return false;
}

/// <inheritdoc />
public bool TryGetComponent(EntityUid uid, Type type, [NotNullWhen(true)] out IComponent? component)
{
Expand Down
17 changes: 17 additions & 0 deletions Robust.Shared/GameObjects/IEntityManager.Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public partial interface IEntityManager
/// <returns>True if the entity has the component type, otherwise false.</returns>
bool HasComponent<T>([NotNullWhen(true)] EntityUid? uid) where T : IComponent;

/// <summary>
/// Checks if the entity has a component type.
/// </summary>
/// <param name="uid">Entity UID to check.</param>
/// <param name="reg">The component registration to check for.</param>
/// <returns>True if the entity has the component type, otherwise false.</returns>
bool HasComponent(EntityUid uid, ComponentRegistration reg);

/// <summary>
/// Checks if the entity has a component type.
/// </summary>
Expand Down Expand Up @@ -294,6 +302,15 @@ public partial interface IEntityManager
/// <returns>If the component existed in the entity.</returns>
bool TryGetComponent<T>([NotNullWhen(true)] EntityUid? uid, [NotNullWhen(true)] out T? component) where T : IComponent?;

/// <summary>
/// Returns the component of a specific type.
/// </summary>
/// <param name="uid">Entity UID to check.</param>
/// <param name="reg">The component registration to check for.</param>
/// <param name="component">Component of the specified type (if exists).</param>
/// <returns>If the component existed in the entity.</returns>
bool TryGetComponent(EntityUid uid, ComponentRegistration reg, [NotNullWhen(true)] out IComponent? component);

/// <summary>
/// Returns the component of a specific type.
/// </summary>
Expand Down
Loading

0 comments on commit 0d6b379

Please sign in to comment.