Removed Fill/ReadNetworkData

These functions needed to be replaced because they rely heavily on reliability.

Instead, new functions called (Write/Read)Data(Server/Client) will be added. 

Separating client and server code into completely separate functions will help ensure that proper security checks are performed.
This commit is contained in:
juanjp600
2016-08-29 20:02:46 -03:00
parent a00ceb5b9f
commit af220dbc2a
28 changed files with 29 additions and 2297 deletions
+1 -84
View File
@@ -498,89 +498,6 @@ namespace Barotrauma
}
}
}
public override bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
{
for (int i = 0; i < capacity; i++)
{
message.Write(Items[i]==null ? (ushort)0 : (ushort)Items[i].ID);
}
return true;
}
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime)
{
if (sendingTime < lastUpdate) return;
character.ClearInput(InputType.Use);
List<Item> droppedItems = new List<Item>();
List<Item> prevItems = new List<Item>(Items);
for (int i = 0; i<capacity; i++)
{
ushort itemId = message.ReadUInt16();
if (itemId == 0)
{
if (Items[i] != null)
{
droppedItems.Add(Items[i]);
Items[i].Drop(character, false);
}
}
else
{
Item item = Entity.FindEntityByID(itemId) as Item;
if (item == null) continue;
//item already in the right slot, no need to do anything
if (Items[i] == item) continue;
//if the item is in the inventory of some other character and said character isn't dead/unconscious,
//don't let this character pick it up
if (GameMain.Server != null)
{
var owner = item.ParentInventory == null ? null : item.ParentInventory.Owner as Character;
if (owner != null && owner != character)
{
if (!character.CanAccessItem(item)) return;
}
}
//some other item already in the slot -> drop it
if (Items[i] != null) Items[i].Drop(character, false);
if (TryPutItem(item, i, false, false))
{
if (droppedItems.Contains(item)) droppedItems.Remove(item);
}
}
}
lastUpdate = sendingTime;
if (GameMain.Server == null) return;
var sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender != null && sender.Character != null)
{
foreach (Item item in droppedItems)
{
GameServer.Log(sender.Character == character ?
character.Name + " dropped " + item.Name :
sender.Character + " removed " + item.Name + " from " + character + "'s inventory", Color.Orange);
}
foreach (Item item in Items)
{
if (item == null || prevItems.Contains(item)) continue;
GameServer.Log(sender.Character == character ?
character.Name + " picked up " + item.Name :
sender.Character + " placed " + item.Name + " in " + character + "'s inventory", Color.Orange);
}
}
}
}
}
@@ -651,65 +651,6 @@ namespace Barotrauma.Items.Components
break;
}
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.Write(docked);
if (docked)
{
message.Write(dockingTarget.item.ID);
message.Write((ushort)hullIds[0]);
message.Write((ushort)hullIds[1]);
message.Write((ushort)gapId);
}
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
bool isDocked = message.ReadBoolean();
if (isDocked)
{
ushort dockingTargetID = message.ReadUInt16();
Entity targetEntity = Entity.FindEntityByID(dockingTargetID);
if (targetEntity == null || !(targetEntity is Item))
{
DebugConsole.ThrowError("Invalid docking port network event (can't dock to "+targetEntity.ToString()+")");
return;
}
dockingTarget = (targetEntity as Item).GetComponent<DockingPort>();
if (dockingTarget == null)
{
DebugConsole.ThrowError("Invalid docking port network event ("+targetEntity+" doesn't have a docking port component)");
return;
}
hullIds[0] = message.ReadUInt16();
hullIds[1] = message.ReadUInt16();
gapId = message.ReadUInt16();
if (hulls != null)
{
if (hulls[0] != null) hulls[0].ID = (ushort)hullIds[0];
if (hulls[1] != null) hulls[1].ID = (ushort)hullIds[1];
}
if (gap != null) gap.ID = (ushort)gapId;
Dock(dockingTarget);
}
else
{
Undock();
}
}
}
}
+1 -16
View File
@@ -476,21 +476,6 @@ namespace Barotrauma.Items.Components
item.NewComponentEvent(this, false, true);
}
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.Write(isOpen);
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
if (sendingTime < lastReceivedMessage) return;
lastReceivedMessage = sendingTime;
SetState(message.ReadBoolean(), true);
}
}
}
@@ -286,42 +286,6 @@ namespace Barotrauma.Items.Components
Msg = "";
}
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.Write(item.SimPosition.X);
message.Write(item.SimPosition.Y);
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
Vector2 newPos = Vector2.Zero;
try
{
newPos = new Vector2(message.ReadFloat(), message.ReadFloat());
}
catch
{
return;
}
item.SetTransform(newPos, 0.0f);
if (!attached)
{
Use(1.0f);
if (GameMain.Server == null) return;
var sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender != null && sender.Character != null)
{
Networking.GameServer.Log(sender.characterInfo.Name+" attached a "+item.Name+" on a wall", Color.Orange);
}
}
}
}
}
@@ -789,14 +789,6 @@ namespace Barotrauma.Items.Components
return ic;
}
public virtual bool FillNetworkData(NetworkEventType type, NetBuffer message)
{
return false;
}
public virtual void ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime)
{
}
}
}
@@ -125,30 +125,6 @@ namespace Barotrauma.Items.Components
container.Inventory.Locked = IsActive;
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
var containers = item.GetComponents<ItemContainer>();
containers[0].Inventory.FillNetworkData(type, message, null);
containers[1].Inventory.FillNetworkData(type, message, null);
message.Write(IsActive);
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
if (sendingTime < lastNetworkUpdate) return;
var containers = item.GetComponents<ItemContainer>();
containers[0].Inventory.ReadNetworkData(type, message, sendingTime);
containers[1].Inventory.ReadNetworkData(type, message, sendingTime);
SetActive(message.ReadBoolean());
lastNetworkUpdate = sendingTime;
}
}
}
@@ -367,46 +367,6 @@ namespace Barotrauma.Items.Components
return true;
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
int itemIndex = fabricatedItem == null ? -1 : fabricableItems.IndexOf(fabricatedItem);
message.WriteRangedInteger(-1, fabricableItems.Count-1, itemIndex);
var containers = item.GetComponents<ItemContainer>();
containers[0].Inventory.FillNetworkData(type, message, null);
containers[1].Inventory.FillNetworkData(type, message, null);
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
if (sendingTime < lastNetworkUpdate) return;
int itemIndex = message.ReadRangedInteger(-1, fabricableItems.Count-1);
var containers = item.GetComponents<ItemContainer>();
containers[0].Inventory.ReadNetworkData(type, message, sendingTime);
containers[1].Inventory.ReadNetworkData(type, message, sendingTime);
if (itemIndex == -1)
{
CancelFabricating();
}
else
{
//if already fabricating the selected item, return
if (fabricatedItem != null && fabricableItems.IndexOf(fabricatedItem) != itemIndex) return;
if (itemIndex < 0 || itemIndex >= fabricableItems.Count) return;
SelectItem(null, fabricableItems[itemIndex]);
StartFabricating(fabricableItems[itemIndex]);
}
lastNetworkUpdate = sendingTime;
}
}
}
@@ -194,50 +194,6 @@ namespace Barotrauma.Items.Components
if (!IsActive) currPowerConsumption = 0.0f;
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.WriteRangedInteger(-10, 10, (int)(flowPercentage / 10.0f));
message.Write(IsActive);
message.WritePadBits();
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
float newFlow = 0.0f;
bool newActive;
if (sendingTime < lastUpdate) return;
try
{
newFlow = message.ReadRangedInteger(-10,10)*10.0f;
newActive = message.ReadBoolean();
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("invalid network message", e);
#endif
return;
}
FlowPercentage = newFlow;
IsActive = newActive;
lastUpdate = sendingTime;
if (GameMain.Server == null) return;
var sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender != null)
{
Networking.GameServer.Log("Pump settings adjusted by " + sender.name, Color.Orange);
Networking.GameServer.Log("Active: " + (IsActive ? "yes" : "no ") + " Pumping speed: " + (int)flowPercentage + " %", Color.Orange);
}
}
}
}
@@ -405,26 +405,7 @@ namespace Barotrauma.Items.Components
if (screenOverlay != null) screenOverlay.Remove();
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.Write(IsActive);
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
try
{
IsActive = message.ReadBoolean();
isActiveTickBox.Selected = IsActive;
}
catch
{
return;
}
}
}
class RadarBlip
@@ -535,72 +535,6 @@ namespace Barotrauma.Items.Components
break;
}
}
public override bool FillNetworkData(NetworkEventType type, NetBuffer message)
{
message.Write(autoTemp);
if (GameMain.Server != null)
{
message.WriteRangedSingle(temperature, 0.0f, 10000.0f, 16);
}
message.WriteRangedSingle(shutDownTemp, 0.0f, 10000.0f, 8);
message.WriteRangedSingle(coolingRate, 0.0f, 100.0f, 8);
message.WriteRangedSingle(fissionRate, 0.0f, 100.0f, 8);
return true;
}
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime)
{
if (sendingTime < lastUpdate) return;
bool newAutoTemp;
float newTemperature = Temperature, newShutDownTemp;
float newCoolingRate, newFissionRate;
try
{
newAutoTemp = message.ReadBoolean();
if (GameMain.Server == null)
{
newTemperature = message.ReadRangedSingle(0.0f, 10000.0f, 16);
}
newShutDownTemp = message.ReadRangedSingle(0.0f, 10000.0f, 8);
newShutDownTemp = MathUtils.RoundTowardsClosest(newShutDownTemp, 100.0f);
newCoolingRate = message.ReadRangedSingle(0.0f, 100.0f, 8);
newFissionRate = message.ReadRangedSingle(0.0f, 100.0f, 8);
}
catch (Exception e)
{
#if DEBUG
DebugConsole.ThrowError("invalid network message", e);
#endif
return;
}
AutoTemp = newAutoTemp;
Temperature = newTemperature;
ShutDownTemp = newShutDownTemp;
CoolingRate = newCoolingRate;
FissionRate = newFissionRate;
lastUpdate = sendingTime;
if (GameMain.Server == null) return;
var sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender != null)
{
Networking.GameServer.Log(
"Reactor settings adjusted by " + sender.name+": \n"+
"Autotemp: " + (autoTemp ? "ON " : "OFF") + " Shutdown temp: " + shutDownTemp +
" Cooling rate: " + (int)coolingRate + " Fission rate: " + (int)fissionRate,
Color.Orange);
}
}
}
}
@@ -329,59 +329,6 @@ namespace Barotrauma.Items.Components
base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
}
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.Write(targetVelocity.X);
message.Write(targetVelocity.Y);
message.Write(autoPilot);
if (autoPilot)
{
message.Write(posToMaintain != null);
if (posToMaintain != null)
{
message.Write(((Vector2)posToMaintain).X);
message.Write(((Vector2)posToMaintain).Y);
}
}
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
Vector2 newTargetVelocity = Vector2.Zero;
bool newAutoPilot = false;
Vector2? newPosToMaintain = null;
try
{
newTargetVelocity = new Vector2(message.ReadFloat(), message.ReadFloat());
newAutoPilot = message.ReadBoolean();
if (newAutoPilot)
{
bool maintainPos = message.ReadBoolean();
if (maintainPos)
{
newPosToMaintain = new Vector2(
message.ReadFloat(),
message.ReadFloat());
}
}
}
catch
{
return;
}
TargetVelocity = newTargetVelocity;
AutoPilot = newAutoPilot;
maintainPosTickBox.Selected = newPosToMaintain != null;
posToMaintain = newPosToMaintain;
}
}
}
@@ -245,32 +245,6 @@ namespace Barotrauma.Items.Components
spriteBatch.DrawString(GUI.Font, "Recharge rate: " + (int)((rechargeSpeed / maxRechargeSpeed) * 100.0f) + " %", new Vector2(x + 30, y + 95), Color.White);
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.WriteRangedSingle(MathHelper.Clamp(rechargeSpeed / MaxRechargeSpeed, 0.0f, 1.0f), 0.0f, 1.0f, 8);
message.WriteRangedSingle(MathHelper.Clamp(charge / capacity, 0.0f, 1.0f), 0.0f, 1.0f, 8);
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
float newRechargeSpeed = 0f;
float newCharge = 0.0f;
try
{
newRechargeSpeed = message.ReadRangedSingle(0.0f, 1.0f, 8);
newRechargeSpeed *= MaxRechargeSpeed;
newCharge = message.ReadRangedSingle(0.0f, 1.0f, 8);
newCharge *= capacity;
}
catch { }
RechargeSpeed = newRechargeSpeed;
Charge = newCharge;
}
}
}
@@ -141,60 +141,5 @@ namespace Barotrauma.Items.Components
}
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
foreach (Connection c in Connections)
{
Wire[] wires = Array.FindAll(c.Wires, w => w != null);
message.WriteRangedInteger(0, Connection.MaxLinked, wires.Length);
for (int i = 0 ; i < wires.Length; i++)
{
message.Write(wires[i].Item.ID);
}
}
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
if (GameMain.Server != null)
{
var sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender != null)
{
Networking.GameServer.Log(item.Name + " rewired by " + sender.name, Color.Orange);
}
}
System.Diagnostics.Debug.WriteLine("connectionpanel update");
foreach (Connection c in Connections)
{
//int wireCount = c.Wires.Length;
c.ClearConnections();
int wireCount = message.ReadRangedInteger(0, Connection.MaxLinked);
for (int i = 0; i < wireCount; i++)
{
ushort wireId = message.ReadUInt16();
Item wireItem = MapEntity.FindEntityByID(wireId) as Item;
if (wireItem == null) continue;
Wire wireComponent = wireItem.GetComponent<Wire>();
if (wireComponent == null) continue;
c.Wires[i] = wireComponent;
wireComponent.Connect(c, false);
var otherConnection = c.Wires[i].OtherConnection(c);
Networking.GameServer.Log(
item.Name+" ("+ c.Name + ") -> " +
(otherConnection == null ? "none" : otherConnection.Item.Name+" ("+(otherConnection.Name)+")"), Color.Orange);
}
c.UpdateRecipients();
}
}
}
}
@@ -525,35 +525,6 @@ namespace Barotrauma.Items.Components
base.RemoveComponentSpecific();
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetBuffer message)
{
message.Write((byte)Math.Min(Nodes.Count, 10));
for (int i = 0; i < Math.Min(Nodes.Count,10); i++)
{
message.Write(Nodes[i].X);
message.Write(Nodes[i].Y);
}
return true;
}
public override void ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime)
{
Nodes.Clear();
List<Vector2> newNodes = new List<Vector2>();
int nodeCount = message.ReadByte();
for (int i = 0; i<nodeCount; i++)
{
Vector2 newNode = new Vector2(message.ReadFloat(), message.ReadFloat());
if (!MathUtils.IsValid(newNode)) return;
newNodes.Add(newNode);
}
Nodes = newNodes;
Drawable = Nodes.Any();
}
}
}
+1 -70
View File
@@ -385,75 +385,6 @@ namespace Barotrauma
item.Sprite.Draw(spriteBatch, new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), item.Color);
}
public virtual bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
{
for (int i = 0; i < capacity; i++)
{
message.Write((ushort)(Items[i]==null ? 0 : Items[i].ID));
}
return true;
}
public virtual void ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime)
{
if (sendingTime < lastUpdate) return;
//List<ushort> newItemIDs = new List<ushort>();
//List<Item> droppedItems = new List<Item>();
List<Item> prevItems = new List<Item>(Items);
Client sender = GameMain.Server == null ? null : GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
for (int i = 0; i < capacity; i++)
{
ushort itemId = message.ReadUInt16();
if (itemId == 0)
{
if (Items[i] != null) Items[i].Drop();
}
else
{
var item = Entity.FindEntityByID(itemId) as Item;
if (item == null) continue;
//if the item is in the inventory of some other character and said character isn't dead/unconscious,
//don't let this character pick it up
if (GameMain.Server != null)
{
if (sender == null || sender.Character == null) return;
if (!sender.Character.CanAccessItem(item)) continue;
}
TryPutItem(item, i, true, false);
}
}
lastUpdate = sendingTime;
if (GameMain.Server == null) return;
if (sender != null && sender.Character != null)
{
foreach (Item item in Items)
{
if (item == null) continue;
if (!prevItems.Contains(item))
{
GameServer.Log(sender.Character + " placed " + item.Name + " in " + Owner, Color.Orange);
}
}
foreach (Item item in prevItems)
{
if (item == null) continue;
if (!Items.Contains(item))
{
GameServer.Log(sender.Character + " removed " + item.Name + " from " + Owner.ToString(), Color.Orange);
}
}
}
}
}
}
+1 -210
View File
@@ -1624,216 +1624,7 @@ namespace Barotrauma
new NetworkEvent(isImportant ?
NetworkEventType.ImportantComponentUpdate : NetworkEventType.ComponentUpdate, ID, isClient, index);
}
public override bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
{
message.Write((byte)MathHelper.Clamp(condition*2.55f,0.0f,255.0f));
switch (type)
{
case NetworkEventType.DropItem:
if (body != null) body.FillNetworkData(message);
break;
case NetworkEventType.PhysicsBodyPosition:
#if DEBUG
System.Diagnostics.Debug.Assert(body != null, "Tried to send a PhysicsBodyPosition message for an item that has no physics body ("+Name+")");
#else
if (body == null) return false;
#endif
body.FillNetworkData(message);
break;
case NetworkEventType.ItemFixed:
byte requirementIndex = (byte)data;
message.Write(requirementIndex);
break;
case NetworkEventType.InventoryUpdate:
var itemContainers = GetComponents<ItemContainer>();
if (itemContainers == null || !itemContainers.Any()) return false;
message.WriteRangedInteger(1, ItemContainer.MaxInventoryCount, itemContainers.Count);
foreach (ItemContainer container in itemContainers)
{
container.Inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data);
}
return true;
case NetworkEventType.ComponentUpdate:
case NetworkEventType.ImportantComponentUpdate:
int componentIndex = (int)data;
if (componentIndex < 0 || componentIndex >= components.Count) return false;
message.Write((byte)componentIndex);
bool sent = components[componentIndex].FillNetworkData(type, message);
if (sent) components[componentIndex].NetworkUpdateSent = true;
return sent;
case NetworkEventType.ApplyStatusEffect:
ActionType actionType = (ActionType)data;
message.WriteRangedInteger(0, Enum.GetValues(typeof(ActionType)).Length, (int)actionType);
return true;
case NetworkEventType.UpdateProperty:
var allProperties = GetProperties<InGameEditable>();
ObjectProperty objectProperty = allProperties.Find(op => op.Name == (string)data);
if (objectProperty != null)
{
message.Write((string)data);
object value = objectProperty.GetValue();
if (value is string)
{
message.Write((byte)0);
message.Write((string)value);
}
else if (value is float)
{
message.Write((byte)1);
message.Write((float)value);
}
else if (value is int)
{
message.Write((byte)2);
message.Write((int)value);
}
else if (value is bool)
{
message.Write((byte)3);
message.Write((bool)value);
}
else
{
message.Write((byte)200);
}
}
break;
}
return true;
}
public override bool ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime, out object data)
{
data = null;
Condition = (float)message.ReadByte()/2.55f;
switch (type)
{
case NetworkEventType.DropItem:
if (GameMain.Server != null)
{
Client sender = GameMain.Server.ConnectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender == null || sender.Character == null || !sender.Character.CanAccessItem(this))
{
return false;
}
}
Drop(null, false);
if (body != null)
{
body.ReadNetworkData(message, sendingTime);
body.MoveToTargetPosition();
}
break;
case NetworkEventType.PhysicsBodyPosition:
//clients don't have authority over item positions
if (GameMain.Server != null) return false;
if (body != null) body.ReadNetworkData(message, sendingTime);
FindHull();
break;
case NetworkEventType.ItemFixed:
byte requirementIndex = message.ReadByte();
data = requirementIndex;
if (requirementIndex >= FixRequirements.Count) return false;
FixRequirements[requirementIndex].Fixed = true;
break;
case NetworkEventType.InventoryUpdate:
var itemContainers = GetComponents<ItemContainer>();
if (itemContainers == null || !itemContainers.Any()) return false;
int containerCount = message.ReadRangedInteger(1, ItemContainer.MaxInventoryCount);
for (int i = 0; i < containerCount;i++ )
{
itemContainers[i].Inventory.ReadNetworkData(type, message, sendingTime);
}
break;
case NetworkEventType.ComponentUpdate:
case NetworkEventType.ImportantComponentUpdate:
int componentIndex = message.ReadByte();
data = componentIndex;
if (componentIndex < 0 || componentIndex > components.Count - 1) return false;
components[componentIndex].NetworkUpdateSent = true;
components[componentIndex].ReadNetworkData(type, message, sendingTime);
break;
case NetworkEventType.ApplyStatusEffect:
ActionType actionType = (ActionType)message.ReadRangedInteger(0, Enum.GetValues(typeof(ActionType)).Length);
data = actionType;
ApplyStatusEffects(actionType, 1.0f);
break;
case NetworkEventType.UpdateProperty:
string propertyName = "";
try
{
propertyName = message.ReadString();
data = propertyName;
}
catch
{
return false;
}
var allProperties = GetProperties<InGameEditable>();
ObjectProperty property = allProperties.Find(op => op.Name == propertyName);
if (property == null) return false;
try
{
switch (message.ReadByte())
{
case 0:
property.TrySetValue(message.ReadString());
break;
case 1:
property.TrySetValue(message.ReadFloat());
break;
case 2:
property.TrySetValue(message.ReadInt32());
break;
case 3:
property.TrySetValue(message.ReadBoolean());
break;
}
}
catch
{
return false;
}
break;
}
return true;
}
public override void Remove()
{
base.Remove();
+1 -131
View File
@@ -89,113 +89,6 @@ namespace Barotrauma
spawnItems.Add(item);
}
public void FillNetworkData(Lidgren.Network.NetBuffer message, List<Item> items)
{
message.Write((byte)items.Count);
for (int i = 0; i < items.Count; i++)
{
message.Write(items[i].Prefab.Name);
message.Write(items[i].ID);
if (items[i].ParentInventory == null || items[i].ParentInventory.Owner == null)
{
message.Write((ushort)0);
message.Write(items[i].Position.X);
message.Write(items[i].Position.Y);
}
else
{
message.Write(items[i].ParentInventory.Owner.ID);
int index = items[i].ParentInventory.FindIndex(items[i]);
message.Write(index < 0 ? (byte)255 : (byte)index);
}
if (items[i].Name == "ID Card")
{
message.Write(items[i].Tags);
}
}
}
public void ReadNetworkData(Lidgren.Network.NetBuffer message)
{
var itemCount = message.ReadByte();
for (int i = 0; i < itemCount; i++)
{
string itemName = message.ReadString();
ushort itemId = message.ReadUInt16();
Vector2 pos = Vector2.Zero;
ushort inventoryId = message.ReadUInt16();
Submarine sub = null;
int inventorySlotIndex = -1;
if (inventoryId > 0)
{
inventorySlotIndex = message.ReadByte();
}
else
{
pos = new Vector2(message.ReadSingle(), message.ReadSingle());
}
string tags = "";
if (itemName == "ID Card")
{
tags = message.ReadString();
}
var prefab = MapEntityPrefab.list.Find(me => me.Name == itemName);
if (prefab == null) continue;
var itemPrefab = prefab as ItemPrefab;
if (itemPrefab == null) continue;
Inventory inventory = null;
var inventoryOwner = Entity.FindEntityByID(inventoryId);
if (inventoryOwner != null)
{
if (inventoryOwner is Character)
{
inventory = (inventoryOwner as Character).Inventory;
}
else if (inventoryOwner is Item)
{
var containers = (inventoryOwner as Item).GetComponents<Items.Components.ItemContainer>();
if (containers!=null && containers.Any())
{
inventory = containers.Last().Inventory;
}
}
}
var item = new Item(itemPrefab, pos, null);
item.ID = itemId;
item.CurrentHull = Hull.FindHull(pos, null, false);
item.Submarine = item.CurrentHull == null ? null : item.CurrentHull.Submarine;
if (!string.IsNullOrEmpty(tags)) item.Tags = tags;
if (inventory != null)
{
if (inventorySlotIndex >= 0 && inventorySlotIndex < 255 &&
inventory.TryPutItem(item, inventorySlotIndex, false, false))
{
continue;
}
inventory.TryPutItem(item, item.AllowedSlots, false);
}
}
}
public void Clear()
{
spawnQueue.Clear();
@@ -243,30 +136,7 @@ namespace Barotrauma
if (GameMain.Server != null) GameMain.Server.SendItemRemoveMessage(items);
}
public void FillNetworkData(Lidgren.Network.NetBuffer message, List<Item> items)
{
message.Write((byte)items.Count);
foreach (Item item in items)
{
message.Write(item.ID);
}
}
public void ReadNetworkData(Lidgren.Network.NetBuffer message)
{
var itemCount = message.ReadByte();
for (int i = 0; i<itemCount; i++)
{
ushort itemId = message.ReadUInt16();
var item = MapEntity.FindEntityByID(itemId) as Item;
if (item == null) continue;
item.Remove();
}
}
public void Clear()
{
removeQueue.Clear();