Skip to content

Commit 0d25530

Browse files
committed
Added syncing for the bio reactor inventory.
1 parent 57af77d commit 0d25530

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Linq;
2+
using NitroxClient.MonoBehaviours;
3+
using NitroxClient.Unity.Helper;
4+
using NitroxModel.DataStructures;
5+
using NitroxModel.Helper;
6+
using UnityEngine;
7+
8+
namespace NitroxClient.GameLogic.Bases.Spawning
9+
{
10+
/*
11+
* When a bio reactor is created, two objects are spawned: the main world object (BaseBioReactorGeometry) and
12+
* the core power logic as a separate game object (BaseBioReactor, also known as a 'module'). The BaseBioReactor
13+
* resides as a direct child of the base object (probably so UWE could iterate them easy). When the object spawns,
14+
* we use this class to set a deterministic id seeded by the parent id. This keeps inventory actions in sync.
15+
*/
16+
public class BaseBioReactorSpawnProcessor : BasePieceSpawnProcessor
17+
{
18+
public override TechType[] ApplicableTechTypes { get; } =
19+
{
20+
TechType.BaseBioReactor
21+
};
22+
23+
public override void SpawnPostProcess(Base latestBase, Int3 latestCell, GameObject finishedPiece)
24+
{
25+
NitroxId reactorId = NitroxEntity.GetId(finishedPiece);
26+
BaseBioReactorGeometry bioReactor = finishedPiece.RequireComponent<BaseBioReactorGeometry>();
27+
GameObject bioReactorModule = ((BaseBioReactor)bioReactor.ReflectionCall("GetModule")).gameObject;
28+
29+
NitroxId moduleId = generateDeterministicReactorModuleId(reactorId);
30+
NitroxEntity.SetNewId(bioReactorModule, moduleId);
31+
}
32+
33+
private NitroxId generateDeterministicReactorModuleId(NitroxId parentId)
34+
{
35+
string guid = parentId.ToString();
36+
char newLastChar = (guid.Last() == '0') ? '1' : '0';
37+
38+
string moduleGuid = guid.Substring(0, guid.Length - 1) + newLastChar;
39+
40+
return new NitroxId(moduleGuid);
41+
}
42+
43+
}
44+
}

NitroxClient/GameLogic/Helper/InventoryContainerHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using NitroxModel.DataStructures.Util;
2+
using NitroxModel.Helper;
3+
using NitroxModel.Logger;
24
using UnityEngine;
35

46
namespace NitroxClient.GameLogic.Helper
@@ -17,11 +19,19 @@ public static Optional<ItemsContainer> GetBasedOnOwnersType(GameObject owner)
1719
{
1820
return Optional.Of(storageContainer.container);
1921
}
22+
BaseBioReactor baseBioReactor = owner.GetComponentInChildren<BaseBioReactor>();
23+
if (baseBioReactor != null)
24+
{
25+
ItemsContainer container = (ItemsContainer)baseBioReactor.ReflectionGetProperty("container");
26+
return Optional.Of(container);
27+
}
2028
if (owner.name == "Player")
2129
{
2230
return Optional.Of(Inventory.Get().container);
2331
}
2432

33+
Log.Debug("Couldn't resolve container from gameObject: " + owner.name);
34+
2535
return Optional.Empty;
2636
}
2737
}

NitroxClient/NitroxClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Communication\Packets\Processors\RocketStageUpdateProcessor.cs" />
6262
<Compile Include="Communication\Packets\Processors\VehicleSpawnedProcessor.cs" />
6363
<Compile Include="Debuggers\EntityDebugger.cs" />
64+
<Compile Include="GameLogic\Bases\Spawning\BaseBioReactorSpawnProcessor.cs" />
6465
<Compile Include="GameLogic\Bases\Spawning\BasePieceSpawnProcessor.cs" />
6566
<Compile Include="GameLogic\Bases\Spawning\BaseLadderSpawnProcessor.cs" />
6667
<Compile Include="GameLogic\Bases\Spawning\BasePieceSpawnPrioritizer.cs" />

NitroxModel/Helper/ReflectionHelper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public static object ReflectionGet<T, T2>(this T2 o, string fieldName, bool isPu
4646
return fieldInfo.GetValue(o);
4747
}
4848

49+
public static object ReflectionGetProperty<T>(this T o, string fieldName)
50+
{
51+
Type t = typeof(T);
52+
PropertyInfo propertyInfo = GetProperty(t, fieldName);
53+
return propertyInfo.GetValue(o);
54+
}
55+
4956
public static void ReflectionSet<T>(this T o, string fieldName, object value, bool isPublic = false, bool isStatic = false)
5057
{
5158
ValidateStatic(o, isStatic);
@@ -105,6 +112,15 @@ private static FieldInfo GetField(this Type t, string fieldName, bool isPublic =
105112
return fieldInfo;
106113
}
107114

115+
private static PropertyInfo GetProperty(this Type t, string propertyName)
116+
{
117+
BindingFlags bindingFlags = GetBindingFlagsFromMethodQualifiers(false, false);
118+
PropertyInfo propertyInfo = t.GetProperty(propertyName, bindingFlags);
119+
Validate.NotNull(propertyInfo, $"Type \"{t.Name}\" does not have a property called \"{propertyName}\" with bindingFlags {bindingFlags}.");
120+
121+
return propertyInfo;
122+
}
123+
108124
private static BindingFlags GetBindingFlagsFromMethodQualifiers(bool isPublic, bool isStatic)
109125
{
110126
BindingFlags bindingFlags = isPublic ? BindingFlags.Public : BindingFlags.NonPublic;

0 commit comments

Comments
 (0)