Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ced262d

Browse files
authored
fixes reagent containers being absolutely broken (unitystation#11141)
* refactor: add support for multiple of same component to components cache Also propagate the enabled filter * fix: makes the internal base reagent container no transfer so that it doesn't consume any other transfer interaction. * fix: ensure that every time we try to use a reagent container, it is an enabled one * fix: lazyly get reference to the reagent container, since its state might change during the round. * chore: remove dead code * fix: remove nothandedible shit
1 parent 89c8207 commit ced262d

42 files changed

Lines changed: 228 additions & 352 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

UnityProject/Assets/Prefabs/Items/Item.prefab

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/Prefabs/Items/Tools/ArtSupplies/Crayons/_CrayonBase.prefab

Lines changed: 26 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/Scripts/US13/ChemistryComponents/MorphableReagentContainer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using US13.Core.Sprite_Handler;
55
using US13.Items;
6+
using Util;
67

78
namespace US13.ChemistryComponents
89
{
@@ -36,7 +37,7 @@ private void Awake()
3637
mainSpriteHandler = mainSpriteRender.GetComponent<SpriteHandler>();
3738

3839
fillVisual = GetComponent<ReagentContainerFillVisualisation>();
39-
serverContainer = GetComponent<ReagentContainer>();
40+
serverContainer = this.GetCachedComponent<ReagentContainer>(includeDisabled: false);
4041
item = GetComponent<ItemAttributesV2>();
4142

4243
// save default data

UnityProject/Assets/Scripts/US13/ChemistryComponents/ReagentContainer.TransferInteraction.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ public float TransferAmount
8888

8989
public bool WillInteract(InventoryApply interaction, NetworkSide side)
9090
{
91-
if (enabled == false) return false;
9291
if (DefaultWillInteract.Default(interaction, side) == false) return false;
9392
return WillInteractHelp(interaction.UsedObject, interaction.TargetObject, side, null);
9493
}
9594

9695
public bool WillInteract(HandApply interaction, NetworkSide side)
9796
{
98-
if (enabled == false) return false;
9997
if (DefaultWillInteract.Default(interaction, side) == false) return false;
10098

10199
var playerScript = interaction.Performer.GetComponent<PlayerScript>();
@@ -120,7 +118,7 @@ private bool WillInteractHarm(GameObject srcObject, GameObject dstObject, Networ
120118
{
121119
if (srcObject == null || dstObject == null) return false;
122120

123-
var srcContainer = srcObject.GetComponent<ReagentContainer>();
121+
var srcContainer = srcObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
124122

125123
if (srcContainer == null) return false;
126124

@@ -135,8 +133,8 @@ private bool WillInteractHelp(GameObject srcObject, GameObject dstObject, Networ
135133
{
136134
if (srcObject == null || dstObject == null) return false;
137135

138-
var objectInHands = srcObject.GetComponent<ReagentContainer>();
139-
var target = dstObject.GetComponent<ReagentContainer>();
136+
var objectInHands = srcObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
137+
var target = dstObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
140138

141139
if (target == null || objectInHands == null) return false;
142140

@@ -237,8 +235,8 @@ private bool WillInteractHelp(GameObject srcObject, GameObject dstObject, Networ
237235

238236
public void ServerPerformInteraction(InventoryApply interaction)
239237
{
240-
var one = interaction.UsedObject.GetComponent<ReagentContainer>();
241-
var two = interaction.TargetObject.GetComponent<ReagentContainer>();
238+
var one = interaction.UsedObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
239+
var two = interaction.TargetObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
242240

243241
ServerTransferInteraction(one, two, interaction.Performer);
244242
}
@@ -249,8 +247,8 @@ public void ServerPerformInteraction(HandApply interaction)
249247

250248
if (interaction.Intent == Intent.Help)
251249
{
252-
var one = interaction.HandObject.GetComponent<ReagentContainer>();
253-
var two = interaction.TargetObject.GetComponent<ReagentContainer>();
250+
var one = interaction.HandObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
251+
var two = interaction.TargetObject.GetCachedComponent<ReagentContainer>(includeDisabled: false);
254252

255253
var reagentContainerObjectInteractionScript =
256254
interaction.TargetObject.GetComponent<ReagentContainerObjectInteractionScript>();

UnityProject/Assets/Scripts/US13/ChemistryComponents/ReagentContainerFillVisualisation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using US13.Core.Lifecycle;
55
using US13.Core.Sprite_Handler;
6+
using Util;
67

78
namespace US13.ChemistryComponents
89
{
@@ -42,7 +43,7 @@ private void Awake()
4243
return;
4344
fillSpriteHandler = fillSpriteRender.GetComponent<SpriteHandler>();
4445

45-
serverContainer = GetComponent<ReagentContainer>();
46+
serverContainer = this.GetCachedComponent<ReagentContainer>(includeDisabled: false);
4647
if (serverContainer)
4748
{
4849
serverContainer.OnReagentMixChanged.AddListener(ServerUpdateFillState);

UnityProject/Assets/Scripts/US13/Core/Editor/GeneratePlantSOs.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using UnityEditor;
55
using Newtonsoft.Json;
6+
using Util;
67

78
public class GeneratePlantSOs : EditorWindow
89
{
@@ -286,7 +287,7 @@ public static void Generate()
286287
//Set plant data for food
287288
newFood.plantData = plantdata;
288289
289-
var newReagents = prefabVariant.GetComponent<ReagentContainer>();
290+
var newReagents = prefabVariant.GetCachedComponent<ReagentContainer>(includeDisabled: false);
290291
291292
//add reagents to food
292293
if (plat.ContainsKey("reagents_add"))

UnityProject/Assets/Scripts/US13/Core/Factories/EffectsFactory.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using US13.Managers.MatrixManager;
88
using US13.Managers.NetworkManagement;
99
using US13.Objects.Construction.FloorDecals;
10+
using Util;
1011

1112
namespace US13.Core.Factories
1213
{
@@ -90,7 +91,7 @@ public static void BloodSplat(Vector3 worldPos, ReagentMix bloodReagents = defau
9091
bloodTileInst.GameObject.name = $"{colorDesc} blood {sizeDesc}";
9192

9293
var bloodTileGO = bloodTileInst.GameObject;
93-
var tileReagents = bloodTileGO.GetComponent<ReagentContainer>();
94+
var tileReagents = bloodTileGO.GetCachedComponent<ReagentContainer>(includeDisabled: false);
9495
if (bloodTileGO)
9596
{
9697
var decal = bloodTileGO.GetComponent<FloorDecal>();
@@ -133,7 +134,7 @@ public static void ChemSplat(Vector3 worldPos, Color color, ReagentMix reagents)
133134
if (chemTileInst.Successful)
134135
{
135136
var chemTileGO = chemTileInst.GameObject;
136-
var tileReagents = chemTileGO.GetComponent<ReagentContainer>();
137+
var tileReagents = chemTileGO.GetCachedComponent<ReagentContainer>(includeDisabled: false);
137138

138139
var colorDesc = TextUtils.ColorToString(reagents.MixColor);
139140
var stateDesc = ChemistryUtils.GetMixStateDescription(reagents);
@@ -162,7 +163,7 @@ public static void PowderSplat(Vector3 worldPos, Color color, ReagentMix reagent
162163
if (powderTileInst.Successful)
163164
{
164165
var powderTileGO = powderTileInst.GameObject;
165-
var tileReagents = powderTileGO.GetComponent<ReagentContainer>();
166+
var tileReagents = powderTileGO.GetCachedComponent<ReagentContainer>(includeDisabled: false);
166167

167168
var colorDesc = TextUtils.ColorToString(reagents.MixColor);
168169
var stateDesc = ChemistryUtils.GetMixStateDescription(reagents);

UnityProject/Assets/Scripts/US13/Core/Utils/CommonComponents.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CommonComponents : MonoBehaviour
2727

2828
public IPlayerPossessable IPlayerPossessable;
2929

30-
public Dictionary<Type, Component> dictionary = new Dictionary<Type, Component>();
30+
public Dictionary<Type, Component[]> dictionary = new Dictionary<Type, Component[]>();
3131

3232
[SerializeField] private List<Component> commonComponentsToRegister = new List<Component>();
3333

@@ -40,7 +40,8 @@ private void Awake()
4040
{
4141
if (c == null) continue;
4242
var type = c.GetType();
43-
dictionary.TryAdd(type, c);
43+
if (dictionary.ContainsKey(type)) continue;
44+
dictionary[type] = this.GetComponents(type);
4445
}
4546
}
4647

@@ -49,25 +50,51 @@ private void OnDestroy()
4950
ComponentsTracker<CommonComponents>.UnregisterInstance(this);
5051
}
5152

52-
public bool TrySafeGetComponent<T>(out T component) where T : Component
53+
public bool TrySafeGetComponent<T>(out T component, bool includeDisabled = true) where T : Component
5354
{
5455
if (dictionary.ContainsKey(typeof(T)) == false)
5556
{
56-
dictionary[typeof(T)] = this.GetComponent<T>();
57+
dictionary[typeof(T)] = GetComponents(typeof(T));
5758
}
5859

59-
component = dictionary[typeof(T)] as T;
60-
return component != null;
60+
var arr = dictionary[typeof(T)];
61+
if (includeDisabled)
62+
{
63+
component = arr.Length > 0 ? arr[0] as T : null;
64+
return component != null;
65+
}
66+
67+
foreach (var c in arr)
68+
{
69+
if (c is Behaviour b && b.enabled == false) continue;
70+
component = c as T;
71+
return component != null;
72+
}
73+
74+
component = null;
75+
return false;
6176
}
6277

63-
public T SafeGetComponent<T>() where T : Component
78+
public T SafeGetComponent<T>(bool includeDisabled = true) where T : Component
6479
{
6580
if (dictionary.ContainsKey(typeof(T)) == false)
6681
{
67-
dictionary[typeof(T)] = this.GetComponent<T>();
82+
dictionary[typeof(T)] = GetComponents(typeof(T));
83+
}
84+
85+
var arr = dictionary[typeof(T)];
86+
if (includeDisabled)
87+
{
88+
return arr.Length > 0 ? arr[0] as T : null;
89+
}
90+
91+
foreach (var c in arr)
92+
{
93+
if (c is Behaviour b && b.enabled == false) continue;
94+
return c as T;
6895
}
6996

70-
return dictionary[typeof(T)] as T;
97+
return null;
7198
}
7299

73100
}

UnityProject/Assets/Scripts/US13/Effects/FloorEffect/FloorPrintEffect.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using US13.ChemistryComponents;
44
using US13.Core.Sprite_Handler;
55
using US13.Core.Transform;
6+
using Util;
67

78
namespace US13.Effects.FloorEffect
89
{
@@ -17,7 +18,7 @@ public class FloorPrintEffect : MonoBehaviour
1718

1819
public void Awake()
1920
{
20-
ReagentMix = this.GetComponent<ReagentContainer>();
21+
ReagentMix = this.GetCachedComponent<ReagentContainer>(includeDisabled: false);
2122
}
2223

2324

0 commit comments

Comments
 (0)