Don't use ienumerable stuff in body system (#7316)

This commit is contained in:
mirrorcult
2022-03-28 13:52:53 -07:00
committed by GitHub
parent 6405ac6d9b
commit 6060dbca1b
2 changed files with 14 additions and 10 deletions

View File

@@ -47,18 +47,22 @@ namespace Content.Server.Body.Systems
/// <param name="uid">The entity to check for the component on.</param> /// <param name="uid">The entity to check for the component on.</param>
/// <param name="body">The body to check for mechanisms on.</param> /// <param name="body">The body to check for mechanisms on.</param>
/// <typeparam name="T">The component to check for.</typeparam> /// <typeparam name="T">The component to check for.</typeparam>
public IEnumerable<(T Comp, MechanismComponent Mech)> GetComponentsOnMechanisms<T>(EntityUid uid, public List<(T Comp, MechanismComponent Mech)> GetComponentsOnMechanisms<T>(EntityUid uid,
SharedBodyComponent? body=null) where T : Component SharedBodyComponent? body=null) where T : Component
{ {
if (!Resolve(uid, ref body)) if (!Resolve(uid, ref body))
yield break; return new();
var query = EntityManager.GetEntityQuery<T>();
var list = new List<(T Comp, MechanismComponent Mech)>(3);
foreach (var (part, _) in body.Parts) foreach (var (part, _) in body.Parts)
foreach (var mechanism in part.Mechanisms) foreach (var mechanism in part.Mechanisms)
{ {
if (EntityManager.TryGetComponent<T>((mechanism).Owner, out var comp)) if (query.TryGetComponent(mechanism.Owner, out var comp))
yield return (comp, mechanism); list.Add((comp, mechanism));
} }
return list;
} }
/// <summary> /// <summary>
@@ -71,7 +75,7 @@ namespace Content.Server.Body.Systems
/// <typeparam name="T">The component to check for.</typeparam> /// <typeparam name="T">The component to check for.</typeparam>
/// <returns>Whether any were found.</returns> /// <returns>Whether any were found.</returns>
public bool TryGetComponentsOnMechanisms<T>(EntityUid uid, public bool TryGetComponentsOnMechanisms<T>(EntityUid uid,
[NotNullWhen(true)] out IEnumerable<(T Comp, MechanismComponent Mech)>? comps, [NotNullWhen(true)] out List<(T Comp, MechanismComponent Mech)>? comps,
SharedBodyComponent? body=null) where T: Component SharedBodyComponent? body=null) where T: Component
{ {
if (!Resolve(uid, ref body)) if (!Resolve(uid, ref body))
@@ -80,9 +84,9 @@ namespace Content.Server.Body.Systems
return false; return false;
} }
comps = GetComponentsOnMechanisms<T>(uid, body).ToArray(); comps = GetComponentsOnMechanisms<T>(uid, body);
if (!comps.Any()) if (comps.Count == 0)
{ {
comps = null; comps = null;
return false; return false;

View File

@@ -99,7 +99,7 @@ namespace Content.Server.Body.Systems
if (!Resolve(uid, ref body, false)) if (!Resolve(uid, ref body, false))
return; return;
var organs = _bodySystem.GetComponentsOnMechanisms<LungComponent>(uid, body).ToArray(); var organs = _bodySystem.GetComponentsOnMechanisms<LungComponent>(uid, body);
// Inhale gas // Inhale gas
var ev = new InhaleLocationEvent(); var ev = new InhaleLocationEvent();
@@ -114,8 +114,8 @@ namespace Content.Server.Body.Systems
var ratio = (Atmospherics.BreathVolume / ev.Gas.Volume); var ratio = (Atmospherics.BreathVolume / ev.Gas.Volume);
var actualGas = ev.Gas.RemoveRatio(ratio); var actualGas = ev.Gas.RemoveRatio(ratio);
var lungRatio = 1.0f / organs.Length; var lungRatio = 1.0f / organs.Count;
var gas = organs.Length == 1 ? actualGas : actualGas.RemoveRatio(lungRatio); var gas = organs.Count == 1 ? actualGas : actualGas.RemoveRatio(lungRatio);
foreach (var (lung, _) in organs) foreach (var (lung, _) in organs)
{ {
// Merge doesn't remove gas from the giver. // Merge doesn't remove gas from the giver.