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:
@@ -92,31 +92,7 @@ namespace Barotrauma
|
||||
|
||||
dictionary.Add(id, this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the state of the entity into the message
|
||||
/// </summary>
|
||||
/// <param name="data">some data that was saved when the networkevent was created</param>
|
||||
/// <returns>false if something went wrong when filling the message, true if the msg is ready to be sent</returns>
|
||||
public virtual bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the state of the entity based on the data in the message
|
||||
/// </summary>
|
||||
|
||||
/// <param name="sendingTime"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns>false if the message is not valid (client trying to change something they're not authorized to, corrupt data, etc) and should be ignored</returns>
|
||||
public virtual bool ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime, out object data)
|
||||
{
|
||||
data = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Find an entity based on the ID
|
||||
/// </summary>
|
||||
|
||||
@@ -801,120 +801,6 @@ namespace Barotrauma
|
||||
|
||||
h.ID = (ushort)int.Parse(element.Attribute("ID").Value);
|
||||
}
|
||||
|
||||
public override bool FillNetworkData(Networking.NetworkEventType type, NetBuffer message, object data)
|
||||
{
|
||||
message.WriteRangedSingle(MathHelper.Clamp(volume/FullVolume, 0.0f, 1.5f), 0.0f, 1.5f, 6);
|
||||
message.WriteRangedSingle(MathHelper.Clamp(OxygenPercentage, 0.0f, 100.0f), 0.0f, 100.0f, 8);
|
||||
|
||||
message.Write(fireSources.Count > 0);
|
||||
if (fireSources.Count > 0)
|
||||
{
|
||||
message.Write((byte)fireSources.Count);
|
||||
for (int i = 0; i < Math.Min(fireSources.Count, 16); i++)
|
||||
{
|
||||
var fireSource = fireSources[i];
|
||||
|
||||
Vector2 normalizedPos = new Vector2(
|
||||
(fireSource.Position.X - rect.X) / rect.Width,
|
||||
(fireSource.Position.Y - (rect.Y - rect.Height)) / rect.Height);
|
||||
message.WriteRangedSingle(MathHelper.Clamp(normalizedPos.X, 0.0f, 1.0f), 0.0f, 1.0f, 4);
|
||||
message.WriteRangedSingle(MathHelper.Clamp(normalizedPos.Y, 0.0f, 1.0f), 0.0f, 1.0f, 4);
|
||||
|
||||
message.WriteRangedSingle(MathHelper.Clamp(fireSource.Size.X / rect.Width, 0.0f, 1.0f), 0, 1.0f, 6);
|
||||
}
|
||||
}
|
||||
|
||||
lastSentVolume = volume;
|
||||
lastSentOxygen = OxygenPercentage;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool ReadNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetIncomingMessage message, float sendingTime, out object data)
|
||||
{
|
||||
data = null;
|
||||
|
||||
if (GameMain.Server != null) return false;
|
||||
|
||||
if (sendingTime < lastNetworkUpdate) return false;
|
||||
|
||||
float newVolume = this.volume;
|
||||
float newOxygen = this.OxygenPercentage;
|
||||
|
||||
try
|
||||
{
|
||||
float newPercentage = message.ReadRangedSingle(0.0f, 1.5f, 6);
|
||||
newVolume = newPercentage * FullVolume;
|
||||
|
||||
newOxygen = message.ReadRangedSingle(0.0f, 100.0f, 8);
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
DebugConsole.Log("Failed to read network message for Hull {" + ID + "}! " + e.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
Volume = newVolume;
|
||||
OxygenPercentage = newOxygen;
|
||||
|
||||
bool hasFireSources = message.ReadBoolean();
|
||||
|
||||
|
||||
List<FireSource> newFireSources = new List<FireSource>();
|
||||
if (hasFireSources)
|
||||
{
|
||||
int fireSourceCount = message.ReadByte();
|
||||
for (int i = 0; i < fireSourceCount; i++)
|
||||
{
|
||||
Vector2 pos = Vector2.Zero;
|
||||
float size = 0.0f;
|
||||
pos.X = message.ReadRangedSingle(0.0f, 1.0f, 4);
|
||||
pos.Y = message.ReadRangedSingle(0.0f, 1.0f, 4);
|
||||
size = message.ReadRangedSingle(0.0f, 1.0f, 6);
|
||||
if (!MathUtils.IsValid(pos)) continue;
|
||||
|
||||
pos.X = MathHelper.Clamp(pos.X, 0.05f, 0.95f);
|
||||
pos.Y = MathHelper.Clamp(pos.Y, 0.05f, 0.95f);
|
||||
|
||||
pos = new Vector2(rect.X + rect.Width * pos.X, rect.Y - rect.Height + (rect.Height * pos.Y));
|
||||
|
||||
var existingFire = fireSources.Find(fs => fs.Contains(pos));
|
||||
if (existingFire!=null)
|
||||
{
|
||||
newFireSources.Add(existingFire);
|
||||
existingFire.Position = pos;
|
||||
existingFire.Size = new Vector2(
|
||||
existingFire.Hull == null ? size : size*existingFire.Hull.rect.Width,
|
||||
existingFire.Size.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
var newFire = new FireSource(pos + Submarine.Position, this, true);
|
||||
newFire.Size = new Vector2(
|
||||
newFire.Hull == null ? size : size * newFire.Hull.rect.Width,
|
||||
newFire.Size.Y);
|
||||
|
||||
//ignore if the fire wasn't added to this room (invalid position)?
|
||||
if (!fireSources.Contains(newFire)) continue;
|
||||
newFireSources.Add(newFire);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var toBeRemoved = fireSources.FindAll(fs => !newFireSources.Contains(fs));
|
||||
for (int i = toBeRemoved.Count - 1; i >= 0; i--)
|
||||
{
|
||||
toBeRemoved[i].Remove(true);
|
||||
}
|
||||
fireSources = newFireSources;
|
||||
|
||||
lastNetworkUpdate = sendingTime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -769,54 +769,6 @@ namespace Barotrauma
|
||||
if (s.gap != null) s.gap.ConnectedWall = this;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool FillNetworkData(NetworkEventType type, NetBuffer message, object data)
|
||||
{
|
||||
//var updateSections = Array.FindAll(sections, s => s != null && Math.Abs(s.damage - s.lastSentDamage) > 0.01f);
|
||||
|
||||
//if (updateSections.Length == 0) return false;
|
||||
|
||||
//Debug.Assert(updateSections.Length<255);
|
||||
|
||||
// message.Write((byte)updateSections.Length);
|
||||
|
||||
for (int i = 0; i < sections.Length; i++)
|
||||
{
|
||||
//if (Math.Abs(sections[i].damage - sections[i].lastSentDamage) < 0.01f) continue;
|
||||
|
||||
//message.Write((byte)i);
|
||||
message.WriteRangedSingle(sections[i].damage / Health, 0.0f, 1.0f, 8);
|
||||
|
||||
sections[i].lastSentDamage = sections[i].damage;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool ReadNetworkData(NetworkEventType type, NetIncomingMessage message, float sendingTime, out object data)
|
||||
{
|
||||
data = null;
|
||||
|
||||
if (GameMain.Server != null) return false;
|
||||
|
||||
if (sendingTime < lastUpdate) return false;
|
||||
|
||||
// int sectionCount = message.ReadByte();
|
||||
|
||||
for (int i = 0; i<sections.Count(); i++)
|
||||
{
|
||||
//byte sectionIndex = message.ReadByte();
|
||||
float damage = message.ReadRangedSingle(0.0f, 1.0f, 8) * Health;
|
||||
|
||||
//if (sectionIndex < 0 || sectionIndex >= sections.Length) continue;
|
||||
|
||||
SetDamage(i, damage);
|
||||
}
|
||||
|
||||
lastUpdate = sendingTime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -537,55 +537,6 @@ namespace Barotrauma
|
||||
return closest;
|
||||
}
|
||||
|
||||
public override bool FillNetworkData(Networking.NetworkEventType type, NetBuffer message, object data)
|
||||
{
|
||||
if (subBody == null) return false;
|
||||
|
||||
message.Write(subBody.Position.X);
|
||||
message.Write(subBody.Position.Y);
|
||||
|
||||
message.Write(Velocity.X);
|
||||
message.Write(Velocity.Y);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool ReadNetworkData(Networking.NetworkEventType type, NetIncomingMessage message, float sendingTime, out object data)
|
||||
{
|
||||
data = null;
|
||||
|
||||
if (GameMain.Server != null) return false;
|
||||
|
||||
Vector2 newTargetPosition, newSpeed;
|
||||
try
|
||||
{
|
||||
if (sendingTime <= lastNetworkUpdate) return false;
|
||||
|
||||
newTargetPosition = new Vector2(message.ReadFloat(), message.ReadFloat());
|
||||
newSpeed = new Vector2(message.ReadFloat(), message.ReadFloat());
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
#if DEBUG
|
||||
DebugConsole.ThrowError("invalid network message", e);
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!newSpeed.IsValid() || !newTargetPosition.IsValid()) return false;
|
||||
|
||||
//newTargetPosition = newTargetPosition + newSpeed * (float)(NetTime.Now - sendingTime);
|
||||
|
||||
subBody.TargetPosition = newTargetPosition;
|
||||
subBody.Velocity = newSpeed;
|
||||
|
||||
lastNetworkUpdate = sendingTime;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//saving/loading ----------------------------------------------------
|
||||
|
||||
public bool Save()
|
||||
|
||||
Reference in New Issue
Block a user