Skip to content

Commit

Permalink
Rewrite debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
warent committed Dec 29, 2024
1 parent 14132e6 commit 5056b21
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Godot;
using HLNC.Utils;

namespace HLNC.Utilities
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public void OnNetworkChangeAnimationPosition(int tick, float old_val, float new_
{
return;
}
GD.Print("Seeking to " + new_val);
Debugger.Log($"Seeking to {new_val}");
animation_player.Seek(new_val, true, true);
}

Expand All @@ -53,7 +54,7 @@ public void Play(string animation)
int animation_index = System.Array.IndexOf(animations, animation);
if (animation_index == -1)
{
GD.Print("Animation not found: " + animation);
Debugger.Log($"Animation not found: {animation}", Debugger.DebugLevel.ERROR);
return;
}
active_animation = animation_index;
Expand Down
6 changes: 2 additions & 4 deletions addons/HLNC/Addons/NetworkTransform/NetworkTransform.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Diagnostics;
using Godot;
using Godot.Collections;
using HLNC.Utils;

namespace HLNC.Utilities
{
Expand Down Expand Up @@ -50,7 +48,7 @@ public Node3D GetParent3D()
{
return (Node3D)parent;
}
GD.PrintErr("NetworkTransform parent is not a Node3D");
Debugger.Log("NetworkTransform parent is not a Node3D", Debugger.DebugLevel.ERROR);
return null;
}

Expand Down
9 changes: 5 additions & 4 deletions addons/HLNC/Addons/Questing/QuestController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Godot;
using HLNC;
using HLNC.Utils;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -72,19 +73,19 @@ public void OnNetworkChangeCompletedSteps(Tick tick, byte[] oldValue, byte[] new
{
if (newValue[i] == 1)
{
GD.Print("Responsibility accepted!");
// GD.Print("Responsibility accepted!");
}
else
{
GD.Print("Responsibility step completed!");
// GD.Print("Responsibility step completed!");
}
}
}
else
{
if (newValue[i] == 1)
{
GD.Print("Responsibility accepted!");
// GD.Print("Responsibility accepted!");
}
}
}
Expand Down Expand Up @@ -170,7 +171,7 @@ public void ServerCompleteTask(int questId, byte stepId, byte taskId)
}
if (stepId + 1 > 63)
{
GD.PrintErr($"Step ID {stepId} is out of bounds for quest {questId}");
Debugger.Log($"Step ID {stepId} is out of bounds for quest {questId}", Debugger.DebugLevel.ERROR);
return;
}
CompletedSteps[questId] = CompletedSteps[questId] | (1 << stepId);
Expand Down
13 changes: 7 additions & 6 deletions addons/HLNC/Core/NetworkNode3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using HLNC.Addons.Lazy;
using HLNC.Serialization;
using HLNC.Serialization.Serializers;
using HLNC.Utils;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;

Expand Down Expand Up @@ -170,8 +171,8 @@ internal set
continue;
}
var prop = GetNode(nodePath).Get(property.Name);
var val = HLNC.Serialization.BsonSerialize.SerializeVariant(context, prop, property.Subtype);
// GD.Print("Serializing: ", nodePath, ".", property.Name, " with value: ", val);
var val = Serialization.BsonSerialize.SerializeVariant(context, prop, property.Subtype);
Debugger.Log($"Serializing: {nodePath}.{property.Name} with value: {val}", Debugger.DebugLevel.VERBOSE);
if (val == null) continue;
nodeData[property.Name] = val;
hasValues = true;
Expand Down Expand Up @@ -261,7 +262,7 @@ public static async Task<T> FromBSON<T>(Variant context, BsonDocument data, T fi
var targetNode = node.GetNodeOrNull(nodePath);
if (targetNode == null)
{
GD.PrintErr("Node not found for: ", nodePath);
Debugger.Log($"Node not found for: ${nodePath}", Debugger.DebugLevel.ERROR);
continue;
}
foreach (var prop in nodeProps)
Expand Down Expand Up @@ -339,7 +340,7 @@ public static async Task<T> FromBSON<T>(Variant context, BsonDocument data, T fi
}
catch (InvalidCastException)
{
GD.PrintErr("Failed to set property: ", prop.Name, " on ", nodePath, " with value: ", prop.Value, " and type: ", variantType);
Debugger.Log($"Failed to set property: {prop.Name} on {nodePath} with value: {prop.Value} and type: {variantType}", Debugger.DebugLevel.ERROR);
}
}
}
Expand All @@ -355,7 +356,7 @@ public static async Task<T> FromBSON<T>(Variant context, BsonDocument data, T fi
var parent = node.GetNodeOrNull(nodePath);
if (parent == null)
{
GD.PrintErr("Parent node not found for: ", nodePath);
Debugger.Log($"Parent node not found for: {nodePath}", Debugger.DebugLevel.ERROR);
continue;
}
parent.AddChild(childNode);
Expand Down Expand Up @@ -536,7 +537,7 @@ public void PrepareSpawn(NetPeer peer)
}
public async void _prepareSpawn(NetPeer peer)
{
GD.Print("Loading peer values for: ", Name);
Debugger.Log($"Loading peer values for: {Name}", Debugger.DebugLevel.VERBOSE);
// TODO: Will this cause an exception if peer is altered?
var peerLoader = this as ILazyPeerStatesLoader;
var result = await peerLoader.LoadPeerValues(peer);
Expand Down
31 changes: 14 additions & 17 deletions addons/HLNC/Core/NetworkRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HLNC.Serialization;
using System;
using HLNC.Addons.Blastoff;
using HLNC.Utils;

namespace HLNC
{
Expand Down Expand Up @@ -143,49 +144,45 @@ public override void _EnterTree()
}
Instance = this;
}
internal static void DebugPrint(string msg)
{
GD.Print($"{(OS.HasFeature("dedicated_server") ? "Server" : "Client")}: {msg}");
}

public void InstallBlastoffServerDriver(IBlastoffServerDriver blastoff)
{
if (!OS.HasFeature("dedicated_server"))
{
DebugPrint("Incorrectly installing Blastoff server driver on client.");
Debugger.Log("Incorrectly installing Blastoff server driver on client.");
return;
}
BlastoffServer = blastoff;
DebugPrint("Blastoff Installed");
Debugger.Log("Blastoff Installed");
}

public void InstallBlastoffClientDriver(IBlastoffClientDriver blastoff)
{
if (OS.HasFeature("dedicated_server"))
{
DebugPrint("Incorrectly installing Blastoff client driver on server.");
Debugger.Log("Incorrectly installing Blastoff client driver on server.");
return;
}
BlastoffClient = blastoff;
DebugPrint("Blastoff Installed");
Debugger.Log("Blastoff Installed");
}

public void StartServer()
{
IsServer = true;
DebugPrint("Starting Server");
Debugger.Log("Starting Server");
GetTree().MultiplayerPoll = false;

ENet = new ENetConnection();
var err = ENet.CreateHostBound(ServerAddress, Port, MaxPeers);
ENet.Compress(ENetConnection.CompressionMode.RangeCoder);
if (err != Error.Ok)
{
DebugPrint($"Error starting: {err}");
Debugger.Log($"Error starting: {err}");
return;
}
NetStarted = true;
DebugPrint($"Started on {ServerAddress}:{Port}");
Debugger.Log($"Started on {ServerAddress}:{Port}");
}

public void StartClient()
Expand All @@ -196,14 +193,14 @@ public void StartClient()
ENet.Compress(ENetConnection.CompressionMode.RangeCoder);
if (ENetHost == null)
{
DebugPrint($"Error connecting.");
Debugger.Log($"Error connecting.");
return;
}
NetStarted = true;
var worldRunner = new WorldRunner();
WorldRunner.CurrentWorld = worldRunner;
GetTree().CurrentScene.AddChild(worldRunner);
DebugPrint("Started");
Debugger.Log("Started");
}

/// <summary>
Expand Down Expand Up @@ -354,13 +351,13 @@ private void StartBlastoffNegotiation()
var err = ENetHost.Send((int)ENetChannelId.BlastoffAdmin, tokenBytes, (int)ENetPacketPeer.FlagReliable);
if (err != Error.Ok)
{
DebugPrint($"Error sending Blastoff data: {err}");
Debugger.Log($"Error sending Blastoff data: {err}");
}
}

private void _OnConnectedToServer()
{
DebugPrint("Connected to server");
Debugger.Log("Connected to server");
if (BlastoffClient != null)
{
StartBlastoffNegotiation();
Expand All @@ -369,7 +366,7 @@ private void _OnConnectedToServer()

private void _OnPeerConnected(NetPeer peer)
{
DebugPrint($"Peer {peer} joined");
Debugger.Log($"Peer {peer} joined");
if (BlastoffServer != null)
{
BlastoffPendingValidation.Add(peer);
Expand Down Expand Up @@ -422,7 +419,7 @@ public WorldRunner SetupWorldInstance(Guid worldId, NetworkNodeWrapper node, Net

public void _OnPeerDisconnected(ENetPacketPeer peer)
{
DebugPrint($"Peer disconnected peerId: {peer}");
Debugger.Log($"Peer disconnected peerId: {peer}");
}

public IEnumerable<NetworkNode3D> GetAllNetworkNodes(Node node, bool onlyScenes = false)
Expand Down
5 changes: 3 additions & 2 deletions addons/HLNC/Core/Serialization/BsonSerialize.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Godot;
using HLNC.Utils;
using MongoDB.Bson;

namespace HLNC.Serialization
Expand Down Expand Up @@ -68,7 +69,7 @@ public static BsonValue SerializeVariant(Variant context, Variant variant, Varia
// Ensure obj implements IBsonSerializable.
if (!(obj is IBsonSerializable))
{
GD.PrintErr("Object does not implement IBsonSerializable: ", obj);
Debugger.Log($"Object does not implement IBsonSerializable: {obj}", Debugger.DebugLevel.ERROR);
return null;
}
return (obj as IBsonSerializable).BsonSerialize(context);
Expand Down Expand Up @@ -97,7 +98,7 @@ public static BsonValue SerializeVariant(Variant context, Variant variant, Varia
}
else
{
GD.PrintErr("Serializing to JSON unsupported property type: ", variant.VariantType);
Debugger.Log($"Serializing to JSON unsupported property type: {variant.VariantType}", Debugger.DebugLevel.ERROR);
return null;
}
}
Expand Down
9 changes: 5 additions & 4 deletions addons/HLNC/Core/Serialization/HLBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using Godot;
using HLNC.Utils;

namespace HLNC.Serialization
{
Expand Down Expand Up @@ -60,7 +61,7 @@ public static void PackVariant(HLBuffer buffer, Variant varVal, bool packLength
}
else
{
GD.Print("HLBytes.Pack: Unhandled type: " + varVal.VariantType);
Debugger.Log($"HLBytes.Pack: Unhandled type: {varVal.VariantType}", Debugger.DebugLevel.ERROR);
}
}

Expand Down Expand Up @@ -349,7 +350,7 @@ public static void Pack(HLBuffer buffer, string varVal, bool packType = false)
}
else
{
GD.Print("HLBytes.UnpackVariant: Unhandled type: " + type);
Debugger.Log($"HLBytes.UnpackVariant: Unhandled type: {type}", Debugger.DebugLevel.ERROR);
return null;
}
}
Expand Down Expand Up @@ -469,7 +470,7 @@ public static Godot.Collections.Array UnpackArray(HLBuffer buffer)
}
else
{
GD.Print("Failed to unpack array element " + i);
Debugger.Log($"Failed to unpack array element {i}", Debugger.DebugLevel.ERROR);
}
}
return result;
Expand Down Expand Up @@ -503,7 +504,7 @@ public static Godot.Collections.Dictionary UnpackDictionary(HLBuffer buffer) {
}
else
{
GD.Print("Failed to unpack dictionary element " + i);
Debugger.Log($"Failed to unpack dictionary element {i}", Debugger.DebugLevel.ERROR);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Godot;
using HLNC.Utils;

namespace HLNC.Serialization.Serializers
{
Expand Down Expand Up @@ -65,7 +66,7 @@ public override void _EnterTree()
}
else
{
GD.PrintErr("Property not found: ", nodePath, ":", propertyName);
Debugger.Log($"Property not found: {nodePath}:{propertyName}", Debugger.DebugLevel.ERROR);
}
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Godot;
using HLNC.Utils;

namespace HLNC.Serialization.Serializers
{
Expand Down Expand Up @@ -68,7 +69,7 @@ public void Import(WorldRunner currentWorld, HLBuffer buffer, out NetworkNodeWra
if (data.parentId != 0 && networkParent == null)
{
// The parent node is not registered, so we can't spawn this node
GD.PrintErr("Parent node not found for: ", NetworkScenesRegister.UnpackScene(data.classId).ResourcePath, " - Parent ID: ", data.parentId);
Debugger.Log($"Parent node not found for: {NetworkScenesRegister.UnpackScene(data.classId).ResourcePath} - Parent ID: {data.parentId}", Debugger.DebugLevel.ERROR);
return;
}

Expand Down
Loading

0 comments on commit 5056b21

Please sign in to comment.