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

Readd Building Patches #2069

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
bf7514d
Disable patches for legacy building
tornac1234 Dec 26, 2022
d1f57ae
Created virtual objects for every possible building (and ghosts)
tornac1234 Dec 26, 2022
138441d
Add a singleplayer tester for the alternative building system
tornac1234 Dec 26, 2022
d57e3e8
Fix transpiler pattern matching not adding instructions at the end of…
tornac1234 Dec 29, 2022
23d7f4c
Adapted all of the code to work in simple peer-2-peer
tornac1234 Dec 30, 2022
1247519
Add a first implementation (unfinished and unclean) of the server-sid…
tornac1234 Dec 30, 2022
efcde9d
Added a BuildPieceIdentifier to be able to identify objects under Bas…
tornac1234 Dec 30, 2022
229d9b4
Fix a null exception during RestoreGhost()
tornac1234 Dec 30, 2022
2e41c70
Completed the server-side managing of the build packets (still a bug …
tornac1234 Dec 30, 2022
f0d4a3c
Refactored save data in PersistedWorldData
tornac1234 Dec 30, 2022
4d1301d
Added the required ProtoBufNet attributes to the new data classes
tornac1234 Dec 30, 2022
ce1e55f
Added building restore process to initial sync
tornac1234 Dec 30, 2022
b2d1791
Fix modules not being correctly handled on server-side
tornac1234 Dec 31, 2022
d1e9246
Try improving build piece identifying.
tornac1234 Jan 2, 2023
0dda03f
On the way to deprecate client force-updating bases when it's not val…
tornac1234 Jan 2, 2023
96d7dfb
Replace every ProtoContract/ProtoMember to DataContract/DataMember
tornac1234 Jan 2, 2023
4e0489d
Fix BuildPieceIdentifier being wrongly created for destructed pieces
tornac1234 Jan 5, 2023
f10b980
Fixed cooldowns, and added cooldown cleanup for when they're finished
tornac1234 Jan 8, 2023
d653a57
Made the building queue use safely yielded coroutines
tornac1234 Feb 20, 2023
8c00dbd
Added NitroxId to interior pieces and NitroxEntity to IBaseModules ac…
tornac1234 Feb 27, 2023
1345829
Fix WaterParks NitroxEntity setting
tornac1234 Feb 28, 2023
0c5f780
Fix being able to deconstruct buildings with remote players inside
tornac1234 Mar 1, 2023
191c36e
Add NitroxEntity to VehicleDockingBay of moonpools
tornac1234 Mar 15, 2023
7162dba
First shot at moving buildings to the entity system (a lot to correct…
tornac1234 Mar 29, 2023
2319339
Removed ExistsInGlobalRoot and WaterParkId fields from WorldEntity (r…
tornac1234 Apr 2, 2023
6f324da
Moved the whole building system to the entity system (incomplete but …
tornac1234 Apr 8, 2023
f069ca0
Fix MapRooms not getting a NitroxEntity attached to their functionali…
tornac1234 Apr 10, 2023
2f1f143
Reparent player children of a removed entity to GlobalRoot
tornac1234 Apr 10, 2023
8013cb5
Add sync/persistence for base pieces (not for alien containments tho)…
tornac1234 Apr 16, 2023
6cd3cab
Fix moonpool not spawning vehicle, recursive search for players causi…
tornac1234 Apr 19, 2023
1a41cf1
Add MapRooms as children for bases, that will get their nitrox ids sa…
tornac1234 Apr 20, 2023
c804f14
Fix ghost visual glitch
tornac1234 Apr 20, 2023
532160f
Fixed several edge-cases issues for WaterParks and finished its basic…
tornac1234 May 10, 2023
0fdee30
Fixed vehicles not being correctly docked to moonpools and constructa…
tornac1234 May 10, 2023
374431d
Refactoring (for build-related entities) positions to localpositions …
tornac1234 May 10, 2023
6f99bf2
Added a resync buildings procedure (with a button in the settings) an…
tornac1234 Jun 10, 2023
8ad30d8
Add a safe building mode for both client and server. Also fix the ope…
tornac1234 Jun 15, 2023
f942989
First steps of entity batch spawning applied to item spawning, fixed …
tornac1234 Jun 26, 2023
f301477
Introduced data compression for SavedBase data (drastically reduced t…
tornac1234 Jul 4, 2023
40bf8b9
Deprecated legacy building system.
tornac1234 Jul 8, 2023
cb74608
Mass refactoring (cleaning up the project), moving classes in their o…
tornac1234 Jul 12, 2023
f4a650a
Fix an issue in batch spawning (about ignored exceptions), and vehicl…
tornac1234 Jul 13, 2023
faa9ace
Some cleaning
tornac1234 Jul 13, 2023
78e6916
Replace NitroxEntity.TryGetIdFrom by the implementation from #1908
tornac1234 Jul 13, 2023
490dbbb
Some refactoring + fix operation tracker breaking bug
tornac1234 Jul 16, 2023
852f3bd
Resolved merge conflicts, fixed an issue with metadata update spammin…
tornac1234 Jul 17, 2023
5371422
Some more mass refactoring to avoid having too many util static classes
tornac1234 Jul 19, 2023
8946a43
QOL
Jannify May 22, 2023
5763418
Sync/Mirror cinematic animation for remote player
Jannify May 21, 2023
83f688a
Added waiting for players skip and auto skip intro on DEBUG
Jannify Jun 23, 2023
06a1daa
Added source generator for NitroxPatch boilerplate
Jul 9, 2023
410c98c
Applied HarmonyRegisterPatchGenerator to all applicable patch classes
Jul 15, 2023
5557bea
Added warn log for errors returned by LogHowToConnectAsync
Jul 16, 2023
92daab8
Merge branch 'pr/2063'
Checkerb0ard Jul 30, 2023
b019865
Merge branch 'pr/2043'
Checkerb0ard Jul 30, 2023
c918669
Fix Building errors
Checkerb0ard Jul 30, 2023
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
Prev Previous commit
Next Next commit
Fixed several edge-cases issues for WaterParks and finished its basic…
… inventory/fishes sync/restore for larger water parks. Split PostSpawnProcessors into for Constructables and Modules.
  • Loading branch information
tornac1234 committed Jul 16, 2023
commit 532160fe29479192f03cf3ec074ef66448251519
10 changes: 8 additions & 2 deletions NitroxClient/ClientAutoFacRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ private static void RegisterCoreDependencies(ContainerBuilder containerBuilder)
.SingleInstance();

containerBuilder.RegisterAssemblyTypes(currentAssembly)
.AssignableTo<IEntityPostSpawner>()
.As<IEntityPostSpawner>()
.AssignableTo<IConstructablePostSpawner>()
.As<IConstructablePostSpawner>()
.AsSelf()
.SingleInstance();

containerBuilder.RegisterAssemblyTypes(currentAssembly)
.AssignableTo<IBaseModulePostSpawner>()
.As<IBaseModulePostSpawner>()
.AsSelf()
.SingleInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ public override void Process(EntityReparented packet)

GameObject newParent = NitroxEntity.RequireObjectFrom(packet.NewParentId);

if (entity.Value.TryGetComponent(out Pickupable pickupable))
{
// If the entity is being parented to a WaterPark
if (newParent.TryGetComponent(out WaterPark waterPark))
{
pickupable.SetVisible(false);
pickupable.Activate(false);
waterPark.AddItem(pickupable);
// The reparenting is automatic here so we don't need to continue
return;
}
// If the entity was parented to a WaterPark but is picked up by someone
else if (pickupable.TryGetComponent(out WaterParkItem waterParkItem))
{
pickupable.Deactivate();
waterParkItem.SetWaterPark(null);
}
}

using (PacketSuppressor<EntityReparented>.Suppress())
{
Type entityType = entities.RequireEntityType(packet.Id);
Expand Down
116 changes: 62 additions & 54 deletions NitroxClient/GameLogic/Bases/New/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using NitroxModel.DataStructures;
using NitroxModel.DataStructures.GameLogic;
using NitroxModel.DataStructures.GameLogic.Buildings.New;
using NitroxModel.DataStructures.GameLogic.Entities;
using NitroxModel.DataStructures.GameLogic.Entities.Bases;
using NitroxModel.DataStructures.Util;
using NitroxModel_Subnautica.DataStructures;
Expand Down Expand Up @@ -85,13 +86,21 @@ public static bool TryGetGhostFace(BaseGhost baseGhost, out Base.Face face)
return false;
}

/// <remarks>
/// Even if the corresponding module was found, in some cases (with WaterParks notably) we don't want to transfer the id
/// we then return false because the gameobject may have already been marked.
/// </remarks>
/// <returns>
/// Whether or not the id was successfully transferred
/// </returns>
public static bool TryTransferIdFromGhostToModule(BaseGhost baseGhost, NitroxId id, ConstructableBase constructableBase, out GameObject moduleObject)
{
Log.Debug($"TryTransferIdFromGhostToModule({baseGhost},{id})");
// 1. Find the face of the target piece
Base.Face? face = null;
bool isWaterPark = baseGhost is BaseAddWaterPark;
bool isMapRoomGhost = baseGhost is BaseAddMapRoomGhost;
// Only three types of ghost which spawn a module
// Only four types of ghost which spawn a module
if ((baseGhost is BaseAddFaceGhost faceGhost && faceGhost.modulePrefab) ||
(baseGhost is BaseAddModuleGhost moduleGhost && moduleGhost.modulePrefab) ||
isMapRoomGhost ||
Expand Down Expand Up @@ -131,14 +140,10 @@ public static bool TryTransferIdFromGhostToModule(BaseGhost baseGhost, NitroxId
break;

case TechType.BaseMoonpool:
// Moonpools are a very specific case, we tweak them to work as modules (while they're not)
// Moonpools are a very specific case, we tweak them to work as interior pieces (while they're not)
Optional<GameObject> objectOptional = baseGhost.targetBase.gameObject.EnsureComponent<MoonpoolManager>().RegisterMoonpool(constructableBase.transform, id);
moduleObject = objectOptional.Value;
if (objectOptional.HasValue)
{
return true;
}
return false;
return moduleObject;

case TechType.BaseMapRoom:
// In the case the ghost is under a BaseDeconstructable, this is a good way to identify a MapRoom
Expand All @@ -152,59 +157,60 @@ public static bool TryTransferIdFromGhostToModule(BaseGhost baseGhost, NitroxId
}
}

if (face.HasValue)
if (!face.HasValue)
{
if (isMapRoomGhost)
if (constructableBase.techType != TechType.BaseWaterPark)
{
MapRoomFunctionality mapRoomFunctionality = baseGhost.targetBase.GetMapRoomFunctionalityForCell(face.Value.cell);
if (mapRoomFunctionality)
{
// As MapRooms can be built as the first piece of a base, we need to make sure that they receive a new id if they're not in a base
if (constructableBase.GetComponentInParent<Base>(true))
{
NitroxEntity.SetNewId(mapRoomFunctionality.gameObject, id);
}
else
{
NitroxEntity.SetNewId(mapRoomFunctionality.gameObject, id.Increment());
}
moduleObject = mapRoomFunctionality.gameObject;
return true;
}
Log.Error($"Couldn't find MapRoomFunctionality of built MapRoom (cell: {face.Value.cell})");
moduleObject = null;
return false;
Log.Error($"No face could be found for ghost {baseGhost}");
}
moduleObject = null;
return false;
}

IBaseModule module = baseGhost.targetBase.GetModule(face.Value);
if (module != null)
// 2. Use that face to find the newly created piece and set its id to the desired one
if (isMapRoomGhost)
{
MapRoomFunctionality mapRoomFunctionality = baseGhost.targetBase.GetMapRoomFunctionalityForCell(face.Value.cell);
if (mapRoomFunctionality)
{
moduleObject = (module as Component).gameObject;
// If the WaterPark is higher than one, it means that the newly built WaterPark will be merged with one that already has a NitroxEntity
if (module is WaterPark waterPark && waterPark.height > 1)
// As MapRooms can be built as the first piece of a base, we need to make sure that they receive a new id if they're not in a base
if (constructableBase.GetComponentInParent<Base>(true))
{
Log.Debug($"Found WaterPark higher than 1 {waterPark.height}, not transferring NitroxEntity to it");
return true;
NitroxEntity.SetNewId(mapRoomFunctionality.gameObject, id);
}

Log.Debug($"Successfully transferred NitroxEntity to {module} [{id}]");
NitroxEntity.SetNewId(moduleObject, id);
else
{
NitroxEntity.SetNewId(mapRoomFunctionality.gameObject, id.Increment());
}
moduleObject = mapRoomFunctionality.gameObject;
return true;
}
else
Log.Error($"Couldn't find MapRoomFunctionality of built MapRoom (cell: {face.Value.cell})");
moduleObject = null;
return false;
}

IBaseModule module = baseGhost.targetBase.GetModule(face.Value);
if (module != null)
{
// If the WaterPark is higher than one, it means that the newly built WaterPark will be merged with one that already has a NitroxEntity
if (module is WaterPark waterPark && waterPark.height > 1)
{
// When a WaterPark is merged with another one, we won't find its module but we don't care about that
if (isWaterPark)
{
moduleObject = null;
return false;
}
Log.Error("Couldn't find the module's GameObject of built interior piece when transfering its NitroxEntity to the module.");
Log.Debug($"Found WaterPark higher than 1 [{waterPark.height}], not transferring NitroxEntity to it");
// as the WaterPark is necessarily merged, we won't need to do anything about it
moduleObject = null;
return false;
}

Log.Debug($"Successfully transferred NitroxEntity to {module} [{id}]");
moduleObject = (module as Component).gameObject;
NitroxEntity.SetNewId(moduleObject, id);
return true;
}
else
// When a WaterPark is merged with another one, we won't find its module but we don't care about that
else if (!isWaterPark)
{
Log.Error($"No face could be found for ghost {baseGhost}");
Log.Error("Couldn't find the module's GameObject of built interior piece when transfering its NitroxEntity to the module.");
}

moduleObject = null;
Expand Down Expand Up @@ -237,9 +243,6 @@ public static Int3 GetMapRoomFunctionalityCell(BaseGhost baseGhost)
}
}


// TODO: Add further metadata in modules and interior pieces

public static class NitroxGlobalRoot
{
public static SavedGlobalRoot From(Transform globalRoot)
Expand Down Expand Up @@ -461,7 +464,6 @@ public static IEnumerator RestoreModule(Transform parent, ModuleEntity moduleEnt

Constructable constructable = moduleObject.GetComponent<Constructable>();
constructable.SetIsInside(moduleEntity.IsInside);
// TODO: If IsInside is false, maybe set a null environment
SkyEnvironmentChanged.Send(moduleObject, moduleObject.GetComponentInParent<SubRoot>(true));
constructable.constructedAmount = moduleEntity.ConstructedAmount;
constructable.SetState(moduleEntity.ConstructedAmount >= 1f, false);
Expand Down Expand Up @@ -517,8 +519,6 @@ public static IEnumerator RestoreGhost(Transform parent, GhostEntity ghostEntity

yield return NitroxGhostMetadata.ApplyMetadataToGhost(baseGhost, ghostEntity.Metadata, @base);

// TODO: Fix hatch ghosts showing the wrong model
// TODO: Fix ghost visual glitch (probably a duplicate model) (black ghost)
// Necessary to wait for BaseGhost.Start()
yield return null;
// Verify that the metadata didn't destroy the GameObject
Expand Down Expand Up @@ -766,9 +766,17 @@ public static InteriorPieceEntity From(IBaseModule module)
}

// TODO: Verify if this is necessary or not
// EDIT: This is most likely not
// EDIT: This is most likely not, apart from the PlanterEntity which is important
switch (module)
{
case LargeRoomWaterPark largeRoomWaterPark:
PlanterEntity leftPlanter = new(interiorPiece.Id.Increment(), interiorPiece.Id);
PlanterEntity rightPlanter = new(leftPlanter.Id.Increment(), interiorPiece.Id);
interiorPiece.ChildEntities.Add(leftPlanter);
interiorPiece.ChildEntities.Add(rightPlanter);
// TODO: Eventually add the children items as entities
interiorPiece.Constructed = largeRoomWaterPark.constructed;
break;
case WaterPark waterPark:
interiorPiece.Constructed = waterPark.constructed;
break;
Expand Down
53 changes: 39 additions & 14 deletions NitroxClient/GameLogic/Bases/New/BuildingTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public class BuildingTester : MonoBehaviour
public int Slot = 0;
private JsonSerializer serializer;
public string SavePath = Path.Combine(NitroxUser.LauncherPath, "SavedBases");

public NitroxId TempId;
public InteriorPieceEntity NewWaterPark;

public TemporaryBuildData Temp;
public Dictionary<NitroxId, DateTimeOffset> BasesCooldown;
/// <summary>
/// Time in milliseconds before local player can build on a base that was modified by another player
Expand All @@ -44,6 +43,7 @@ public void Start()
{
Main = this;
BuildQueue = new();
Temp = new();
BasesCooldown = new();
serializer = new();
serializer.NullValueHandling = NullValueHandling.Ignore;
Expand Down Expand Up @@ -173,14 +173,16 @@ public IEnumerator BuildBase(PlaceBase placeBase)
public IEnumerator UpdatePlacedBase(UpdateBase updateBase)
{
// Is probably useless check but can probably be useful for desync detection
if (!NitroxEntity.TryGetComponentFrom(updateBase.BaseId, out Base parentBase))
if (!NitroxEntity.TryGetComponentFrom<Base>(updateBase.BaseId, out _))
{
Log.Debug("Couldn't find the parentBase");
// TODO: Ask for resync
yield break;
}
if (NitroxEntity.TryGetComponentFrom(updateBase.FormerGhostId, out ConstructableBase constructableBase))
{
Temp.ChildrenTransfer = updateBase.ChildrenTransfer;

BaseGhost baseGhost = constructableBase.model.GetComponent<BaseGhost>();
constructableBase.SetState(true, true);
BasesCooldown[updateBase.BaseId] = DateTimeOffset.Now;
Expand Down Expand Up @@ -251,18 +253,13 @@ public IEnumerator DeconstructPiece(PieceDeconstructed pieceDeconstructed)
continue;
}
Log.Debug($"Found a BaseDeconstructable {baseDeconstructable.name}, will now deconstruct it manually");
using (new PacketSuppressor<BaseDeconstructed>())
using (new PacketSuppressor<PieceDeconstructed>())
using (new PacketSuppressor<WaterParkDeconstructed>())
using (PacketSuppressor<BaseDeconstructed>.Suppress())
using (PacketSuppressor<PieceDeconstructed>.Suppress())
using (PacketSuppressor<WaterParkDeconstructed>.Suppress())
{
TempId = pieceDeconstructed.PieceId;
if (pieceDeconstructed is WaterParkDeconstructed waterParkDeconstructed)
{
NewWaterPark = waterParkDeconstructed.NewWaterPark;
}
Temp.Fill(pieceDeconstructed);
baseDeconstructable.Deconstruct();
TempId = null;
NewWaterPark = null;
Temp.Reset();
}
BasesCooldown[pieceDeconstructed.BaseId] = DateTimeOffset.Now;
yield break;
Expand Down Expand Up @@ -442,4 +439,32 @@ public void DeleteTargetBase()
}
}

public class TemporaryBuildData
{
public NitroxId Id;
public InteriorPieceEntity NewWaterPark;
public List<NitroxId> MovedChildrenIds;
public (NitroxId, NitroxId) ChildrenTransfer;
public bool Transfer;

public void Fill(PieceDeconstructed pieceDeconstructed)
{
Id = pieceDeconstructed.PieceId;
if (pieceDeconstructed is WaterParkDeconstructed waterParkDeconstructed)
{
NewWaterPark = waterParkDeconstructed.NewWaterPark;
MovedChildrenIds = waterParkDeconstructed.MovedChildrenIds;
Transfer = waterParkDeconstructed.Transfer;
}
}

public void Reset()
{
Id = null;
NewWaterPark = null;
MovedChildrenIds = null;
ChildrenTransfer = (null, null);
Transfer = false;
}
}
}
12 changes: 8 additions & 4 deletions NitroxClient/GameLogic/Bases/New/NitroxGhostMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public static class NitroxGhostMetadata
{
public static T From<T>(BaseGhost baseGhost) where T : GhostMetadata, new()
{
T metadata = new();
metadata.TargetOffset = baseGhost.targetOffset.ToDto();
T metadata = new()
{
TargetOffset = baseGhost.targetOffset.ToDto()
};
return metadata;
}

Expand Down Expand Up @@ -91,8 +93,10 @@ public static BaseDeconstructableGhostMetadata From(BaseGhost baseGhost)
BaseDeconstructableGhostMetadata metadata = NitroxGhostMetadata.From<BaseDeconstructableGhostMetadata>(baseGhost);
if (baseGhost.TryGetComponentInParent(out ConstructableBase constructableBase) && constructableBase.moduleFace.HasValue)
{
metadata.ModuleFace = constructableBase.moduleFace.Value.ToDto();
IBaseModule baseModule = baseGhost.targetBase.GetModule(constructableBase.moduleFace.Value);
Base.Face moduleFace = constructableBase.moduleFace.Value;
metadata.ModuleFace = moduleFace.ToDto();
moduleFace.cell += baseGhost.targetBase.GetAnchor();
IBaseModule baseModule = baseGhost.targetBase.GetModule(moduleFace);
if (baseModule != null && (baseModule as MonoBehaviour).TryGetComponent(out PrefabIdentifier identifier))
{
metadata.ClassId = identifier.ClassId;
Expand Down
8 changes: 7 additions & 1 deletion NitroxClient/GameLogic/Entities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Entities(IPacketSender packetSender, ThrottledPacketSender throttledPacke
entitySpawnersByType[typeof(EscapePodWorldEntity)] = entitySpawnersByType[typeof(WorldEntity)];
entitySpawnersByType[typeof(PlayerWorldEntity)] = entitySpawnersByType[typeof(WorldEntity)];
entitySpawnersByType[typeof(VehicleWorldEntity)] = entitySpawnersByType[typeof(WorldEntity)];
entitySpawnersByType[typeof(GlobalRootEntity)] = entitySpawnersByType[typeof(WorldEntity)];
entitySpawnersByType[typeof(BuildEntity)] = new BuildEntitySpawner(this);
entitySpawnersByType[typeof(ModuleEntity)] = new ModuleEntitySpawner(this);
entitySpawnersByType[typeof(GhostEntity)] = new GhostEntitySpawner();
Expand Down Expand Up @@ -100,7 +101,7 @@ public IEnumerator SpawnAsync(IEnumerable<Entity> entities)
UpdatePosition(worldEntity);
}
}
else if (entity.ParentId != null && !WasParentSpawned(entity.ParentId))
else if (entity.ParentId != null && !IsParentReady(entity.ParentId))
{
AddPendingParentEntity(entity);
}
Expand Down Expand Up @@ -214,6 +215,11 @@ public Type RequireEntityType(NitroxId id)
throw new InvalidOperationException($"Did not have a type for {id}");
}

public bool IsParentReady(NitroxId id)
{
return WasParentSpawned(id) || (NitroxEntity.TryGetObjectFrom(id, out GameObject o) && o);
}

public bool WasParentSpawned(NitroxId id)
{
return spawnedAsType.ContainsKey(id);
Expand Down
Loading