Skip to content

Commit

Permalink
Merge pull request #683 from starfi5h/pr-log
Browse files Browse the repository at this point in the history
Enable Log.Debug and cleanup
  • Loading branch information
starfi5h authored Jun 28, 2024
2 parents f911f28 + 4da5184 commit 37d990e
Show file tree
Hide file tree
Showing 37 changed files with 70 additions and 130 deletions.
11 changes: 4 additions & 7 deletions NebulaModel/Logger/Log.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#region

using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using WebSocketSharp;

Expand All @@ -21,13 +20,11 @@ public static void Init(ILogger logger)
Log.logger = logger;
}

[Conditional("DEBUG")]
public static void Debug(string message)
{
logger.LogDebug(message);
}

[Conditional("DEBUG")]
public static void Debug(object message)
{
Debug(message?.ToString());
Expand Down Expand Up @@ -93,10 +90,10 @@ public static void Error(string message, Exception ex)
public static void SocketOutput(LogData data, string _)
{
var log = data.ToString();
const string ipv4Regex = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
const string ipv6Regex = "([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}";
log = Regex.Replace(log, ipv4Regex, "(IPv4 Address)");
log = Regex.Replace(log, ipv6Regex, "(IPv6 Address)");
const string Ipv4Regex = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
const string Ipv6Regex = "([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}";
log = Regex.Replace(log, Ipv4Regex, "(IPv4 Address)");
log = Regex.Replace(log, Ipv6Regex, "(IPv6 Address)");

if (data.Level >= LogLevel.Warn)
{
Expand Down
2 changes: 2 additions & 0 deletions NebulaModel/Packets/Combat/DFRelay/DFRelayArriveBasePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public DFRelayArriveBasePacket(in DFRelayComponent dFRelay)
if (factory != null)
{
NextGroundEnemyId = factory.enemyRecycleCursor > 0 ? factory.enemyRecycle[factory.enemyRecycleCursor - 1] : factory.enemyCursor;
HasFactory = factory.entityCount > 0;
}
}

public int HiveAstroId { get; set; }
public int RelayId { get; set; }
public int HiveRtseed { get; set; }
public int NextGroundEnemyId { get; set; }
public bool HasFactory { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace NebulaNetwork.PacketProcessors.Chat;

[RegisterPacketProcessor]
internal class PlyaerDataCommmandProcessor : PacketProcessor<PlayerDataCommandPacket>
internal class PlayerDataCommmandProcessor : PacketProcessor<PlayerDataCommandPacket>
{
protected override void ProcessPacket(PlayerDataCommandPacket packet, NebulaConnection conn)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace NebulaNetwork.PacketProcessors.Chat;

[RegisterPacketProcessor]
internal class RemoteServerCommmandProcessor : PacketProcessor<RemoteServerCommandPacket>
internal class RemoteServerCommandProcessor : PacketProcessor<RemoteServerCommandPacket>
{
private const int SERVERSAVE_COOLDOWN = 60;
private const int SERVERLOGIN_COOLDOWN = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ protected override void ProcessPacket(DFRelayArriveBasePacket packet, NebulaConn
EnemyManager.SetPlanetFactoryNextEnemyId(factory, packet.NextGroundEnemyId);
}
dfrelayComponent.ArriveBase();

// Display message when DF relay is successfully landed and the planet has buildings
if (packet.HasFactory && dfrelayComponent.baseId > 0)
{
Multiplayer.Session.Enemies.DisplayPlanetPingMessage("DF relay landed on planet".Translate(), dfrelayComponent.targetAstroId, dfrelayComponent.targetLPos);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void ProcessPacket(BeltReverseRequestPacket packet, INebulaConne
var beltWindow = UIRoot.instance.uiGame.beltWindow;
try
{
beltWindow._Close(); // close the window first to avoid changing unwant variable when setting beltId
beltWindow._Close(); // close the window first to avoid changing unrelated variables when setting beltId
beltWindow.factory = factory;
beltWindow.traffic = factory.cargoTraffic;
beltWindow.player = GameMain.mainPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void ProcessPacket(BeltUpdatePickupItemsPacket packet, Nebula
}
var beltComponent = traffic.beltPool[t.BeltId];
var cargoPath = traffic.GetCargoPath(beltComponent.segPathId);
var ItemId = t.ItemId;
var itemId = t.ItemId;
//Check if belt exists
if (cargoPath == null)
{
Expand All @@ -40,21 +40,21 @@ protected override void ProcessPacket(BeltUpdatePickupItemsPacket packet, Nebula
k <= beltComponent.segIndex + beltComponent.segLength - 1;
k++)
{
if (cargoPath.TryPickItem(k - 4 - 1, 12, ItemId, out _, out _) != 0)
if (cargoPath.TryPickItem(k - 4 - 1, 12, itemId, out _, out _) != 0)
{
return;
}
}
// Search upstream for target item
for (var k = beltComponent.segIndex + beltComponent.segPivotOffset - 1; k >= beltComponent.segIndex; k--)
{
if (cargoPath.TryPickItem(k - 4 - 1, 12, ItemId, out _, out _) != 0)
if (cargoPath.TryPickItem(k - 4 - 1, 12, itemId, out _, out _) != 0)
{
return;
}
}
Log.Warn(
$"BeltUpdatePickupItem: Cannot pick item{ItemId} on belt{t.BeltId}, planet{packet.PlanetId}");
$"BeltUpdatePickupItem: Cannot pick item{itemId} on belt{t.BeltId}, planet{packet.PlanetId}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override void ProcessPacket(MonitorSettingUpdatePacket packet, NebulaC
break;

default:
Log.Warn($"MonitorSettingUpdatePacket: Unkown MonitorSettingEvent {packet.Event}");
Log.Warn($"MonitorSettingUpdatePacket: Unknown MonitorSettingEvent {packet.Event}");
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ protected override void ProcessPacket(PowerTowerChargerUpdate packet, NebulaConn
{
Multiplayer.Session.PowerTowers.RemoteChargerHashIds.Add(hashId, 1);
}
NebulaModel.Logger.Log.Debug($"Add remote charger [{packet.PlanetId}-{packet.NodeId}]: {Multiplayer.Session.PowerTowers.RemoteChargerHashIds[hashId]}");
// Add remote charger [{packet.PlanetId}-{packet.NodeId}]: {Multiplayer.Session.PowerTowers.RemoteChargerHashIds[hashId]}
}
else
{
if (!Multiplayer.Session.PowerTowers.RemoteChargerHashIds.TryGetValue(hashId, out var playerCount))
{
return;
}
NebulaModel.Logger.Log.Debug($"Remove remote charger [{packet.PlanetId}-{packet.NodeId}]: {Multiplayer.Session.PowerTowers.RemoteChargerHashIds[hashId] - 1}");
// Remove remote charger [{packet.PlanetId}-{packet.NodeId}]: {Multiplayer.Session.PowerTowers.RemoteChargerHashIds[hashId] - 1}
Multiplayer.Session.PowerTowers.RemoteChargerHashIds[hashId] = playerCount - 1;
if (playerCount <= 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ namespace NebulaNetwork.PacketProcessors.GameHistory;
[RegisterPacketProcessor]
internal class GameHistoryEnqueueTechProcessor : PacketProcessor<GameHistoryEnqueueTechPacket>
{
public GameHistoryEnqueueTechProcessor()
{
}

protected override void ProcessPacket(GameHistoryEnqueueTechPacket packet, NebulaConnection conn)
{
if (IsHost)
{
var player = Players.Get(conn);
if (player == null)
{
return;
}
using (Multiplayer.Session.History.IsIncomingRequest.On())
{
GameMain.history.EnqueueTech(packet.TechId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,11 @@ namespace NebulaNetwork.PacketProcessors.GameHistory;
[RegisterPacketProcessor]
internal class GameHistoryRemoveTechProcessor : PacketProcessor<GameHistoryRemoveTechPacket>
{
public GameHistoryRemoveTechProcessor()
{
}

protected override void ProcessPacket(GameHistoryRemoveTechPacket packet, NebulaConnection conn)
{
var valid = true;
if (IsHost)
{
var player = Players.Get(conn);
if (player != null)
{
Server.SendPacketExclude(packet, conn);
}
else
{
valid = false;
}
}

if (!valid)
{
return;
Server.SendPacketExclude(packet, conn);
}
using (Multiplayer.Session.History.IsIncomingRequest.On())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#region

using System;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.GameStates;
using NebulaWorld;
using NebulaWorld.GameStates;

#endregion
Expand All @@ -16,5 +19,6 @@ public class GameStateSaveInfoProcessor : PacketProcessor<GameStateSaveInfoPacke
protected override void ProcessPacket(GameStateSaveInfoPacket packet, NebulaConnection conn)
{
GameStatesManager.LastSaveTime = packet.LastSaveTime;
Log.Info("LastSaveTime: " + DateTimeOffset.FromUnixTimeSeconds(packet.LastSaveTime).ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GameStateUpdateProcessor : PacketProcessor<GameStateUpdate>
{
private readonly float BUFFERING_TICK = 60f;
private readonly float BUFFERING_TIME = 30f;
private float avaerageUPS = 60f;
private float averageUPS = 60f;

private int averageRTT;
private bool hasChanged;
Expand All @@ -30,7 +30,7 @@ protected override void ProcessPacket(GameStateUpdate packet, NebulaConnection c
{
var rtt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - packet.SentTime;
averageRTT = (int)(averageRTT * 0.8 + rtt * 0.2);
avaerageUPS = avaerageUPS * 0.8f + packet.UnitsPerSecond * 0.2f;
averageUPS = averageUPS * 0.8f + packet.UnitsPerSecond * 0.2f;
Multiplayer.Session.World.UpdatePingIndicator($"Ping: {averageRTT}ms");

// We offset the tick received to account for the time it took to receive the packet
Expand All @@ -39,7 +39,7 @@ protected override void ProcessPacket(GameStateUpdate packet, NebulaConnection c
var diff = currentGameTick - GameMain.gameTick;

// Discard abnormal packet (usually after host saving the file)
if (rtt > 2 * averageRTT || avaerageUPS - packet.UnitsPerSecond > 15)
if (rtt > 2 * averageRTT || averageUPS - packet.UnitsPerSecond > 15)
{
// Initial connection
if (GameMain.gameTick < 1200L)
Expand All @@ -48,7 +48,7 @@ protected override void ProcessPacket(GameStateUpdate packet, NebulaConnection c
GameMain.gameTick = currentGameTick;
}
Log.Debug(
$"GameStateUpdate unstable. RTT:{rtt}(avg{averageRTT}) UPS:{packet.UnitsPerSecond:F2}(avg{avaerageUPS:F2})");
$"GameStateUpdate unstable. RTT:{rtt}(avg{averageRTT}) UPS:{packet.UnitsPerSecond:F2}(avg{averageUPS:F2})");
return;
}

Expand All @@ -57,7 +57,7 @@ protected override void ProcessPacket(GameStateUpdate packet, NebulaConnection c
// We allow for a small drift of 5 ticks since the tick offset using the ping is only an approximation
if (GameMain.gameTick > 0 && Mathf.Abs(diff) > 5)
{
Log.Info($"Game Tick got updated since it was desynced, was {GameMain.gameTick}, diff={diff}");
Log.Debug($"Game Tick desync. {GameMain.gameTick} skip={diff} UPS:{packet.UnitsPerSecond:F2}(avg{averageUPS:F2})");
GameMain.gameTick = currentGameTick;
}
// Reset FixUPS when user turns off the option
Expand All @@ -71,39 +71,39 @@ protected override void ProcessPacket(GameStateUpdate packet, NebulaConnection c
}

// Adjust client's UPS to match game tick with server, range 30~120 UPS
var UPS = diff / 1f + avaerageUPS;
var ups = diff / 1f + averageUPS;
long skipTick = 0;
switch (UPS)
switch (ups)
{
case > GameStatesManager.MaxUPS:
{
// Try to disturbute game tick difference into BUFFERING_TIME (seconds)
if (diff / BUFFERING_TIME + avaerageUPS > GameStatesManager.MaxUPS)
// Try to distribute game tick difference into BUFFERING_TIME (seconds)
if (diff / BUFFERING_TIME + averageUPS > GameStatesManager.MaxUPS)
{
// The difference is too large, need to skip ticks to catch up
skipTick = (long)(UPS - GameStatesManager.MaxUPS);
skipTick = (long)(ups - GameStatesManager.MaxUPS);
}
UPS = GameStatesManager.MaxUPS;
ups = GameStatesManager.MaxUPS;
break;
}
case < GameStatesManager.MinUPS:
{
if (diff + avaerageUPS - GameStatesManager.MinUPS < -BUFFERING_TICK)
if (diff + averageUPS - GameStatesManager.MinUPS < -BUFFERING_TICK)
{
skipTick = (long)(UPS - GameStatesManager.MinUPS);
skipTick = (long)(ups - GameStatesManager.MinUPS);
}
UPS = GameStatesManager.MinUPS;
ups = GameStatesManager.MinUPS;
break;
}
}
if (skipTick != 0)
{
Log.Info($"Game Tick was desynced. skip={skipTick} diff={diff,2}, RTT={rtt}ms, UPS={packet.UnitsPerSecond:F2}");
Log.Debug($"Game Tick desync. skip={skipTick} diff={diff,2}, RTT={rtt}ms, UPS={packet.UnitsPerSecond:F2}(avg{averageUPS:F2})");
GameMain.gameTick += skipTick;
}
FPSController.SetFixUPS(UPS);
FPSController.SetFixUPS(ups);
hasChanged = true;
// Tick difference in the next second. Expose for other mods
GameStatesManager.NotifyTickDifference(diff / 1f + avaerageUPS - UPS);
GameStatesManager.NotifyTickDifference(diff / 1f + averageUPS - ups);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void ProcessPacket(ILSAddStationComponent packet, NebulaConne
{
// If we have loaded the factory where the new station was created on, should be able to find
// it in our PlanetTransport.stationPool
// Assgin gid here so this station will go to galacticTransport.stationPool[gid]
// Assign gid here so this station will go to galacticTransport.stationPool[gid]
stationPool[packet.StationId].gid = packet.StationGId;
if (galacticTransport.AddStationComponent(packet.PlanetId, stationPool[packet.StationId]) != packet.StationGId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/*
* when a client arrives at a star he needs to sync the ILS storages as update events are sent only to corresponding stars
* and also to sync the belt filters conencted to the ILS
* and also to sync the belt filters connected to the ILS
*/
namespace NebulaNetwork.PacketProcessors.Logistics;

Expand All @@ -28,11 +28,6 @@ protected override void ProcessPacket(ILSArriveStarPlanetRequest packet, NebulaC
return;
}

var player = Players.Get(conn, EConnectionStatus.Connected) ?? Players.Get(conn, EConnectionStatus.Syncing);
if (player == null)
{
return;
}
var stationGId = new List<int>();
var stationPId = new List<int>();
var stationMaxShips = new List<int>();
Expand Down Expand Up @@ -91,7 +86,7 @@ protected override void ProcessPacket(ILSArriveStarPlanetRequest packet, NebulaC
offsetStorage += storageLength[i];
}

player.SendPacket(new ILSArriveStarPlanetResponse(stationGId.ToArray(),
conn.SendPacket(new ILSArriveStarPlanetResponse(stationGId.ToArray(),
stationPId.ToArray(),
stationMaxShips.ToArray(),
storageLength.ToArray(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#region

using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Logistics;
Expand All @@ -16,7 +15,6 @@ public class ILSUpdateRouteProcessor : PacketProcessor<ILSUpdateRoute>
{
protected override void ProcessPacket(ILSUpdateRoute packet, NebulaConnection conn)
{
Log.Debug($"{packet.Type} id0:{packet.Id0} id1:{packet.Id1}");
using (Multiplayer.Session.Ships.PatchLockILS.On())
{
var galacticTransport = GameMain.data.galacticTransport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ protected override void ProcessPacket(ILSRequestgStationPoolSync packet, NebulaC
return;
}

var player = Players.Get(conn, EConnectionStatus.Connected) ?? Players.Get(conn, EConnectionStatus.Syncing);
if (player == null)
{
return;
}
var iter = 0;

var countILS = GameMain.data.galacticTransport.stationPool.Count(stationComponent => stationComponent != null);
Expand Down Expand Up @@ -147,6 +142,6 @@ protected override void ProcessPacket(ILSRequestgStationPoolSync packet, NebulaC
shipAngularVel.ToArray(),
shipPPosTemp.ToArray(),
shipPRotTemp.ToArray());
player.SendPacket(packet2);
conn.SendPacket(packet2);
}
}
Loading

0 comments on commit 37d990e

Please sign in to comment.