Skip to content

Commit 81ec5e7

Browse files
authored
Merge pull request #367 from PhantomGamers/patch-fix-destruction
Refactor UpgradeEntityRequest and DestructEntityRequest
2 parents 294c9d1 + 48dafab commit 81ec5e7

File tree

9 files changed

+110
-129
lines changed

9 files changed

+110
-129
lines changed

NebulaClient/PacketProcessors/Factory/Entity/DestructEntityRequestProcessor.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,9 @@ public class DestructEntityRequestProcessor : IPacketProcessor<DestructEntityReq
1111
{
1212
public void ProcessPacket(DestructEntityRequest packet, NebulaConnection conn)
1313
{
14-
PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId);
15-
// We only execute the code if the client has loaded the factory at least once.
16-
// Else it will get it once it goes to the planet for the first time.
17-
if (planet.factory != null)
14+
using(FactoryManager.EventFromServer.On())
1815
{
19-
using (FactoryManager.EventFromServer.On())
20-
{
21-
FactoryManager.PacketAuthor = packet.AuthorId;
22-
FactoryManager.TargetPlanet = packet.PlanetId;
23-
24-
var pab = GameMain.mainPlayer.controller.actionBuild;
25-
if (pab != null)
26-
{
27-
// Backup current factory & set factory to request planet factory
28-
var tmpFactory = pab.factory;
29-
pab.factory = planet.factory;
30-
pab.noneTool.factory = planet.factory;
31-
32-
pab.DoDismantleObject(packet.ObjId);
33-
34-
// Restore factory
35-
pab.factory = tmpFactory;
36-
pab.noneTool.factory = tmpFactory;
37-
}
38-
FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE;
39-
FactoryManager.PacketAuthor = -1;
40-
}
16+
DestructEntityRequestManager.DestructEntityRequest(packet);
4117
}
4218
}
4319
}

NebulaClient/PacketProcessors/Factory/Entity/UpgradeEntityRequestProcessor.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,9 @@ public class UpgradeEntityRequestProcessor : IPacketProcessor<UpgradeEntityReque
1111
{
1212
public void ProcessPacket(UpgradeEntityRequest packet, NebulaConnection conn)
1313
{
14-
PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId);
15-
16-
// We only execute the code if the client has loaded the factory at least once.
17-
// Else he will get it once it goes to the planet for the first time.
18-
if (planet.factory != null)
14+
using(FactoryManager.EventFromServer.On())
1915
{
20-
using (FactoryManager.EventFromServer.On())
21-
{
22-
if (packet.PlanetId != GameMain.localPlanet?.id)
23-
{
24-
planet.physics = new PlanetPhysics(planet);
25-
planet.physics.Init();
26-
planet.audio = new PlanetAudio(planet);
27-
planet.audio.Init();
28-
}
29-
30-
ItemProto itemProto = LDB.items.Select(packet.UpgradeProtoId);
31-
FactoryManager.TargetPlanet = packet.PlanetId;
32-
planet.factory.UpgradeFinally(GameMain.mainPlayer, packet.ObjId, itemProto);
33-
FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE;
34-
35-
if (packet.PlanetId != GameMain.localPlanet?.id)
36-
{
37-
planet.physics.Free();
38-
planet.physics = null;
39-
planet.audio.Free();
40-
planet.audio = null;
41-
}
42-
}
16+
UpgradeEntityRequestManager.UpgradeEntityRequest(packet);
4317
}
4418
}
4519
}

NebulaHost/PacketProcessors/Factory/Entity/DestructEntityRequestProcessor.cs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,9 @@ public class DestructEntityRequestProcessor : IPacketProcessor<DestructEntityReq
1111
{
1212
public void ProcessPacket(DestructEntityRequest packet, NebulaConnection conn)
1313
{
14-
using (FactoryManager.EventFromClient.On())
14+
using(FactoryManager.EventFromClient.On())
1515
{
16-
PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId);
17-
FactoryManager.PacketAuthor = packet.AuthorId;
18-
FactoryManager.TargetPlanet = packet.PlanetId;
19-
if (packet.PlanetId != GameMain.mainPlayer.planetId)
20-
{
21-
//Creating rendering batches is required to properly handle DestructFinally for the belts, since model needs to be changed.
22-
//ToDo: Optimize it somehow, since creating and destroying rendering batches is not optimal.
23-
planet.factory.cargoTraffic.CreateRenderingBatches();
24-
}
25-
26-
var pab = GameMain.mainPlayer.controller.actionBuild;
27-
if (pab != null)
28-
{
29-
// Backup current factory & set factory to request planet factory
30-
var tmpFactory = pab.factory;
31-
pab.factory = planet.factory;
32-
pab.noneTool.factory = planet.factory;
33-
34-
pab.DoDismantleObject(packet.ObjId);
35-
36-
// Restore factory
37-
pab.factory = tmpFactory;
38-
pab.noneTool.factory = tmpFactory;
39-
}
40-
41-
if (packet.PlanetId != GameMain.mainPlayer.planetId)
42-
{
43-
planet.factory.cargoTraffic.DestroyRenderingBatches();
44-
}
45-
FactoryManager.PacketAuthor = -1;
46-
FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE;
16+
DestructEntityRequestManager.DestructEntityRequest(packet);
4717
}
4818
}
4919
}

NebulaHost/PacketProcessors/Factory/Entity/UpgradeEntityRequestProcessor.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,7 @@ public void ProcessPacket(UpgradeEntityRequest packet, NebulaConnection conn)
1313
{
1414
using (FactoryManager.EventFromClient.On())
1515
{
16-
PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId);
17-
18-
// Physics could be null, if the host is not on the requested planet
19-
if (packet.PlanetId != GameMain.localPlanet?.id)
20-
{
21-
planet.physics = new PlanetPhysics(planet);
22-
planet.physics.Init();
23-
planet.audio = new PlanetAudio(planet);
24-
planet.audio.Init();
25-
}
26-
27-
ItemProto itemProto = LDB.items.Select(packet.UpgradeProtoId);
28-
FactoryManager.TargetPlanet = packet.PlanetId;
29-
planet.factory.UpgradeFinally(GameMain.mainPlayer, packet.ObjId, itemProto);
30-
FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE;
31-
32-
if (packet.PlanetId != GameMain.localPlanet?.id)
33-
{
34-
planet.physics.Free();
35-
planet.physics = null;
36-
planet.audio.Free();
37-
planet.audio = null;
38-
}
16+
UpgradeEntityRequestManager.UpgradeEntityRequest(packet);
3917
}
4018
}
4119
}

NebulaModel/Packets/Factory/DestructEntityRequest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ public class DestructEntityRequest
44
{
55
public int PlanetId { get; set; }
66
public int ObjId { get; set; }
7+
public int ProtoId { get; set; }
78
public int AuthorId { get; set; }
89

910
public DestructEntityRequest() { }
10-
public DestructEntityRequest(int planetId, int objId, int authorId)
11+
public DestructEntityRequest(int planetId, int objId, int protoId, int authorId)
1112
{
1213
AuthorId = authorId;
1314
PlanetId = planetId;
1415
ObjId = objId;
16+
ProtoId = protoId;
1517
}
1618
}
1719
}

NebulaPatcher/Patches/Dynamic/PlanetFactory_Patch.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public static bool DismantleFinally_Prefix(PlanetFactory __instance, Player play
8383
FactoryManager.RemovePrebuildRequest(__instance.planetId, -objId);
8484
}
8585

86+
if (LocalPlayer.IsMasterClient || !FactoryManager.EventFromServer)
87+
{
88+
LocalPlayer.SendPacket(new DestructEntityRequest(__instance.planetId, objId, protoId, FactoryManager.PacketAuthor == -1 ? LocalPlayer.PlayerId : FactoryManager.PacketAuthor));
89+
}
90+
8691
return LocalPlayer.IsMasterClient || FactoryManager.EventFromServer;
8792
}
8893

NebulaPatcher/Patches/Dynamic/PlayerAction_Build_Patch.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,19 @@ namespace NebulaPatcher.Patches.Dynamic
99
class PlayerAction_Build_Patch
1010
{
1111
[HarmonyPrefix]
12-
[HarmonyPatch("DoDismantleObject")]
13-
public static bool DoDismantleObject_Prefix(PlayerAction_Build __instance, int objId)
12+
[HarmonyPatch(nameof(PlayerAction_Build.SetFactoryReferences))]
13+
public static bool SetFactoryReferences_Prefix()
1414
{
15-
if (!SimulatedWorld.Initialized)
16-
return true;
17-
18-
// Make sure these are being set
19-
__instance.SetFactoryReferences();
20-
__instance.SetToolsFactoryReferences();
21-
22-
if (LocalPlayer.IsMasterClient || !FactoryManager.EventFromServer)
15+
if(!SimulatedWorld.Initialized)
2316
{
24-
LocalPlayer.SendPacket(new DestructEntityRequest(__instance.factory.planetId, objId, FactoryManager.PacketAuthor == -1 ? LocalPlayer.PlayerId : FactoryManager.PacketAuthor));
17+
return true;
2518
}
2619

27-
return LocalPlayer.IsMasterClient || FactoryManager.EventFromServer;
28-
}
29-
30-
[HarmonyPrefix]
31-
[HarmonyPatch(nameof(PlayerAction_Build.SetFactoryReferences))]
32-
public static bool SetFactoryReferences_Prefix()
33-
{
34-
if((FactoryManager.EventFromServer || FactoryManager.EventFromClient) && FactoryManager.PacketAuthor != LocalPlayer.PlayerId)
20+
if((FactoryManager.EventFromServer || FactoryManager.EventFromClient) && FactoryManager.PacketAuthor != LocalPlayer.PlayerId && FactoryManager.TargetPlanet != GameMain.localPlanet?.id)
3521
{
3622
return false;
3723
}
24+
3825
return true;
3926
}
4027
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using NebulaModel.Packets.Factory;
2+
3+
namespace NebulaWorld.Factory
4+
{
5+
public class DestructEntityRequestManager
6+
{
7+
public static void DestructEntityRequest(DestructEntityRequest packet)
8+
{
9+
PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId);
10+
11+
// We only execute the code if the client has loaded the factory at least once.
12+
// Else it will get it once it goes to the planet for the first time.
13+
if (planet.factory == null)
14+
{
15+
return;
16+
}
17+
18+
// Physics could be null, if the host is not on the requested planet
19+
if (packet.PlanetId != GameMain.localPlanet?.id)
20+
{
21+
planet.physics = new PlanetPhysics(planet);
22+
planet.physics.Init();
23+
planet.audio = new PlanetAudio(planet);
24+
planet.audio.Init();
25+
26+
//Creating rendering batches is required to properly handle DestructFinally for the belts, since model needs to be changed.
27+
//ToDo: Optimize it somehow, since creating and destroying rendering batches is not optimal.
28+
planet.factory.cargoTraffic.CreateRenderingBatches();
29+
}
30+
31+
FactoryManager.TargetPlanet = packet.PlanetId;
32+
FactoryManager.PacketAuthor = packet.AuthorId;
33+
int protoId = packet.ProtoId;
34+
planet.factory.DismantleFinally(GameMain.mainPlayer, packet.ObjId, ref protoId);
35+
FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE;
36+
FactoryManager.PacketAuthor = -1;
37+
38+
if (packet.PlanetId != GameMain.localPlanet?.id)
39+
{
40+
planet.physics.Free();
41+
planet.physics = null;
42+
planet.audio.Free();
43+
planet.audio = null;
44+
planet.factory.cargoTraffic.DestroyRenderingBatches();
45+
}
46+
}
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using NebulaModel.Packets.Factory;
2+
3+
namespace NebulaWorld.Factory
4+
{
5+
public class UpgradeEntityRequestManager
6+
{
7+
public static void UpgradeEntityRequest(UpgradeEntityRequest packet)
8+
{
9+
PlanetData planet = GameMain.galaxy.PlanetById(packet.PlanetId);
10+
11+
// We only execute the code if the client has loaded the factory at least once.
12+
// Else he will get it once it goes to the planet for the first time.
13+
if (planet.factory == null)
14+
{
15+
return;
16+
}
17+
18+
// Physics could be null, if the host is not on the requested planet
19+
if (packet.PlanetId != GameMain.localPlanet?.id)
20+
{
21+
planet.physics = new PlanetPhysics(planet);
22+
planet.physics.Init();
23+
planet.audio = new PlanetAudio(planet);
24+
planet.audio.Init();
25+
}
26+
27+
ItemProto itemProto = LDB.items.Select(packet.UpgradeProtoId);
28+
FactoryManager.TargetPlanet = packet.PlanetId;
29+
planet.factory.UpgradeFinally(GameMain.mainPlayer, packet.ObjId, itemProto);
30+
FactoryManager.TargetPlanet = FactoryManager.PLANET_NONE;
31+
32+
if (packet.PlanetId != GameMain.localPlanet?.id)
33+
{
34+
planet.physics.Free();
35+
planet.physics = null;
36+
planet.audio.Free();
37+
planet.audio = null;
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)