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:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user