Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Networking/EntitySpawner.cs
3 using System.Collections.Generic;
4 
5 namespace Barotrauma
6 {
7  partial class EntitySpawner : Entity, IServerSerializable
8  {
9  public readonly List<(Entity entity, bool isRemoval)> receivedEvents = new List<(Entity entity, bool isRemoval)>();
10 
11  public void ClientEventRead(IReadMessage message, float sendingTime)
12  {
13  bool remove = message.ReadBoolean();
14 
15  if (remove)
16  {
17  ushort entityId = message.ReadUInt16();
18  var entity = FindEntityByID(entityId);
19  if (entity != null)
20  {
21  DebugConsole.Log($"Received entity removal message for \"{entity}\".");
22  if (entity is Item item && item.Container?.GetComponent<Deconstructor>() != null)
23  {
24  if (item.Prefab.ContentPackage == ContentPackageManager.VanillaCorePackage &&
25  /* we don't need info of every deconstructed item, we can get a good sample size just by logging 5% */
26  Rand.Range(0.0f, 1.0f) < 0.05f)
27  {
28  GameAnalyticsManager.AddDesignEvent("ItemDeconstructed:" + (GameMain.GameSession?.GameMode?.Preset.Identifier ?? "none".ToIdentifier()) + ":" + item.Prefab.Identifier);
29  }
30  }
31  entity.Remove();
32  }
33  else
34  {
35  DebugConsole.Log("Received entity removal message for ID " + entityId + ". Entity with a matching ID not found.");
36  }
37  receivedEvents.Add((entity, true));
38  }
39  else
40  {
41  switch (message.ReadByte())
42  {
43  case (byte)SpawnableType.Item:
44  var newItem = Item.ReadSpawnData(message, true);
45  if (newItem == null)
46  {
47  DebugConsole.ThrowError("Received an item spawn message, but spawning the item failed.");
48  }
49  else
50  {
51  if (newItem.Container?.GetComponent<Fabricator>() != null)
52  {
53  if (newItem.Prefab.ContentPackage == ContentPackageManager.VanillaCorePackage &&
54  /* we don't need info of every fabricated item, we can get a good sample size just by logging 5% */
55  Rand.Range(0.0f, 1.0f) < 0.05f)
56  {
57  GameAnalyticsManager.AddDesignEvent("ItemFabricated:" + (GameMain.GameSession?.GameMode?.Preset.Identifier ?? "none".ToIdentifier()) + ":" + newItem.Prefab.Identifier);
58  }
59  }
60  receivedEvents.Add((newItem, false));
61  }
62  break;
63  case (byte)SpawnableType.Character:
64  var character = Character.ReadSpawnData(message);
65  if (character == null)
66  {
67  DebugConsole.ThrowError("Received character spawn message, but spawning the character failed.");
68  }
69  else
70  {
71  receivedEvents.Add((character, false));
72  }
73  break;
74  default:
75  DebugConsole.ThrowError("Received invalid entity spawn message (unknown spawnable type)");
76  break;
77  }
78  }
79  }
80  }
81 }
static Entity FindEntityByID(ushort ID)
Find an entity based on the ID
Definition: Entity.cs:204
void ClientEventRead(IReadMessage message, float sendingTime)
readonly List<(Entity entity, bool isRemoval)> receivedEvents
static GameSession?? GameSession
Definition: GameMain.cs:88
readonly Identifier Identifier
static Item ReadSpawnData(IReadMessage msg, bool spawn=true)
Interface for entities that the server can send events to the clients