Skip to content

Commit

Permalink
Lowering network load
Browse files Browse the repository at this point in the history
400kb/s -> 200 kb/s
  • Loading branch information
ari-steas committed Jan 6, 2024
1 parent a63500d commit 1738680
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using Sandbox.Game;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Game.Components;
using VRage.Utils;

namespace Heart_Module.Data.Scripts.HeartModule.ExceptionHandler
{

public class CriticalHandle
{
const int WarnTimeSeconds = 20;
Expand Down Expand Up @@ -39,7 +34,7 @@ public void Update()
}

if (!MyAPIGateway.Utilities.IsDedicated)
MyAPIGateway.Utilities.ShowNotification($"HeartMod CRITICAL ERROR - Shutting down in {secondsRemaining}s", 1000/60);
MyAPIGateway.Utilities.ShowNotification($"HeartMod CRITICAL ERROR - Shutting down in {secondsRemaining}s", 1000 / 60);
}

public void UnloadData()
Expand All @@ -55,6 +50,7 @@ public static void ThrowCriticalException(Exception ex, Type callingType, ulong
private void m_ThrowCriticalException(Exception ex, Type callingType, ulong callerId = ulong.MaxValue)
{
HeartData.I.IsSuspended = true;
HeartData.I.Log.Log("Start Throw Critical Exception " + CriticalCloseTime);
if (CriticalCloseTime != -1)
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Heart_Module.Data.Scripts.HeartModule.ExceptionHandler
{
Expand All @@ -27,6 +23,8 @@ public void Log(string message)

public void LogException(Exception ex, Type callingType, string prefix = "")
{
if (ex == null)
return;
Log(prefix + $"Exception in {callingType.FullName}! {ex.Message}\n{ex.StackTrace}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Heart_Module.Data.Scripts.HeartModule.Network;
using ProtoBuf;
using System;
using VRage.Utils;

namespace Heart_Module.Data.Scripts.HeartModule.ExceptionHandler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using Heart_Module.Data.Scripts.HeartModule.ExceptionHandler;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Scripting;
using VRage.Utils;

namespace Heart_Module.Data.Scripts.HeartModule.ErrorHandler
{
public class SoftHandle
{
public static void RaiseException(string message, Type callingType = null, ulong callerId = ulong.MaxValue)
{
MyAPIGateway.Utilities.ShowNotification("Minor Exception: " + message);
//MyAPIGateway.Utilities.ShowNotification("Minor Exception: " + message);
Exception soft = new Exception(message);
HeartData.I.Log.LogException(soft, callingType ?? typeof(SoftHandle), callerId != ulong.MaxValue ? $"Shared exception from {callerId}: " : "");
if (MyAPIGateway.Session.IsServer)
Expand All @@ -23,7 +17,7 @@ public static void RaiseException(string message, Type callingType = null, ulong

public static void RaiseException(Exception exception, Type callingType = null, ulong callerId = ulong.MaxValue)
{
MyAPIGateway.Utilities.ShowNotification("Minor Exception: " + exception.Message);
//MyAPIGateway.Utilities.ShowNotification("Minor Exception: " + exception.Message);
HeartData.I.Log.LogException(exception, callingType ?? typeof(SoftHandle), callerId != ulong.MaxValue ? $"Shared exception from {callerId}: " : "");
}

Expand Down
5 changes: 1 addition & 4 deletions Heart Module/Data/Scripts/HeartModule/HeartLoad.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Heart_Module.Data.Scripts.HeartModule.ErrorHandler;
using Heart_Module.Data.Scripts.HeartModule.ExceptionHandler;
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using VRage.Game.Components;

namespace Heart_Module.Data.Scripts.HeartModule
Expand All @@ -12,7 +9,7 @@ namespace Heart_Module.Data.Scripts.HeartModule
internal class HeartLoad : MySessionComponentBase
{
CriticalHandle handle;


public override void LoadData()
{
Expand Down
5 changes: 0 additions & 5 deletions Heart Module/Data/Scripts/HeartModule/Network/HeartNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Game.Components;
using VRage.Game.ModAPI;
using VRage.Utils;

namespace Heart_Module.Data.Scripts.HeartModule.Network
{
Expand Down
5 changes: 0 additions & 5 deletions Heart Module/Data/Scripts/HeartModule/Network/PacketBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using Heart_Module.Data.Scripts.HeartModule.Projectiles.StandardClasses;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Heart_Module.Data.Scripts.HeartModule.Network
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.Utils;
using VRageMath;

namespace Heart_Module.Data.Scripts.HeartModule.Projectiles
Expand Down Expand Up @@ -45,13 +41,13 @@ private void m_Update()
switch (damageEvent.Type)
{
case DamageEvent.DamageEntType.Grid:
m_GridDamageHandler((IMyCubeGrid) damageEvent.Entity, damageEvent);
m_GridDamageHandler((IMyCubeGrid)damageEvent.Entity, damageEvent);
break;
case DamageEvent.DamageEntType.Character:
m_CharacterDamageHandler((IMyCharacter) damageEvent.Entity, damageEvent);
m_CharacterDamageHandler((IMyCharacter)damageEvent.Entity, damageEvent);
break;
case DamageEvent.DamageEntType.Projectile:
m_ProjectileDamageHandler((Projectile) damageEvent.Entity, damageEvent);
m_ProjectileDamageHandler((Projectile)damageEvent.Entity, damageEvent);
break;
}
}
Expand Down
88 changes: 54 additions & 34 deletions Heart Module/Data/Scripts/HeartModule/Projectiles/Projectile.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Heart_Module.Data.Scripts.HeartModule.Debug;
using Heart_Module.Data.Scripts.HeartModule.ErrorHandler;
using Heart_Module.Data.Scripts.HeartModule.Projectiles.StandardClasses;
using Sandbox.Game.Entities;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using VRage.Game;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.ModAPI;
using VRage.Utils;
using VRageMath;

namespace Heart_Module.Data.Scripts.HeartModule.Projectiles
Expand All @@ -23,12 +21,12 @@ public partial class Projectile
public Vector3D InheritedVelocity;
#endregion

public long Firer;
public Vector3D Position;
public Vector3D Direction;
public float Velocity;
public int RemainingImpacts;
public long Firer = -1;
public Vector3D Position = Vector3D.Zero;
public Vector3D Direction = Vector3D.Up;
public float Velocity = 0;
public int RemainingImpacts = 0;

public Action<Projectile> Close = (p) => { };
public long LastUpdate { get; private set; }

Expand All @@ -39,24 +37,25 @@ public partial class Projectile
public Projectile() { }

public Projectile(SerializableProjectile projectile)
{
{
if (!ProjectileManager.I.IsIdAvailable(projectile.Id))
{
SoftHandle.RaiseSyncException("Unable to spawn projectile - duplicate Id!");
ProjectileManager.I.GetProjectile(projectile.Id)?.SyncUpdate(projectile);
return;
}

if (!ProjectileDefinitionManager.HasDefinition(projectile.DefinitionId))
if (!projectile.DefinitionId.HasValue || !ProjectileDefinitionManager.HasDefinition(projectile.DefinitionId.Value))
{
SoftHandle.RaiseSyncException("Unable to spawn projectile - invalid DefinitionId!");
DefinitionId = -1;
return;
}

Id = projectile.Id;
DefinitionId = projectile.DefinitionId;
Definition = ProjectileDefinitionManager.GetDefinition(projectile.DefinitionId);
Firer = projectile.Firer;
DefinitionId = projectile.DefinitionId.Value;
Definition = ProjectileDefinitionManager.GetDefinition(projectile.DefinitionId.Value);
Firer = projectile.Firer.GetValueOrDefault(0);
// TODO fill in from Definition

SyncUpdate(projectile);
Expand Down Expand Up @@ -102,7 +101,7 @@ public void DrawUpdate(float delta)
{
DebugDraw.AddPoint(Position + (InheritedVelocity + Direction * (Velocity + Definition.PhysicalProjectile.Acceleration * delta)) * delta, Color.Green, 0.000001f);
}

public void CheckHits(float delta)
{
List<IHitInfo> intersects = new List<IHitInfo>();
Expand Down Expand Up @@ -137,38 +136,59 @@ public void ProjectileHit(IMyEntity impact)

public void SyncUpdate(SerializableProjectile projectile)
{
if (DefinitionId != projectile.DefinitionId)
{
SoftHandle.RaiseSyncException("DefinitionId Mismatch!");
return;
}

QueuedDispose = !projectile.IsActive;

LastUpdate = projectile.Timestamp;
float delta = (DateTime.Now.Ticks - LastUpdate) / (float) TimeSpan.TicksPerSecond;
float delta = (DateTime.Now.Ticks - LastUpdate) / (float)TimeSpan.TicksPerSecond;

// The following values may be null to save network load
if (projectile.Direction.HasValue)
Direction = projectile.Direction.Value;
if (projectile.Position.HasValue)
Position = projectile.Position.Value;
if (projectile.Velocity.HasValue)
Velocity = projectile.Velocity.Value;
if (projectile.InheritedVelocity.HasValue)
InheritedVelocity = projectile.InheritedVelocity.Value;
if (projectile.RemainingImpacts.HasValue)
RemainingImpacts = projectile.RemainingImpacts.Value;

Direction = projectile.Direction;
Position = projectile.Position;
Velocity = projectile.Velocity;
InheritedVelocity = projectile.InheritedVelocity;
RemainingImpacts = projectile.RemainingImpacts;
TickUpdate(delta);
}

public SerializableProjectile AsSerializable()
/// <summary>
/// Returns the projectile as a network-ready projectile info class. 0 = max detail, 2+ = min detail
/// </summary>
/// <param name="DetailLevel"></param>
/// <returns></returns>
public SerializableProjectile AsSerializable(int DetailLevel = 1)
{
return new SerializableProjectile()
SerializableProjectile projectile = new SerializableProjectile()
{
IsActive = !QueuedDispose,
Id = Id,
DefinitionId = DefinitionId,
Position = Position,
Direction = Direction,
InheritedVelocity = InheritedVelocity,
Velocity = Velocity,
Timestamp = DateTime.Now.Ticks,
};

switch (DetailLevel)
{
case 0:
projectile.DefinitionId = DefinitionId;
projectile.Position = Position;
projectile.Direction = Direction;
projectile.InheritedVelocity = InheritedVelocity;
projectile.Velocity = Velocity;
break;
case 1:
projectile.Position = Position;
if (Definition.Guidance.Length > 0)
projectile.Direction = Direction;
if (Definition.PhysicalProjectile.Acceleration > 0)
projectile.Velocity = Velocity;
break;
}

return projectile;
}

public void QueueDispose()
Expand Down
Loading

0 comments on commit 1738680

Please sign in to comment.