Skip to content

Commit 19ae137

Browse files
Add more comments
1 parent c3c7ce7 commit 19ae137

254 files changed

Lines changed: 6927 additions & 120 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.githooks/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ set -euo pipefail
2020

2121
# dotnet format mode: whitespace or full.
2222
# whitespace is faster for commit-time checks while still using dotnet format.
23-
: "${PRECOMMIT_FORMAT_MODE:=whitespace}"
23+
: "${PRECOMMIT_FORMAT_MODE:=full}"
2424

2525
# true: run targeted builds for changed project areas.
2626
: "${PRECOMMIT_ENABLE_BUILD_GODOTUTILS:=true}"

Template.GodotUtils/Debugging/JsonExceptionHandler.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,41 @@ public static class JsonExceptionHandler
1515
private const int ContextLinesAfter = 8;
1616

1717
/// <summary>
18-
/// Prints a formatted JSON parsing error with context.
18+
/// Prints a formatted JSON parsing error with nearby source context when line data is available.
1919
/// </summary>
20+
/// <param name="ex">JSON exception describing the parse failure.</param>
21+
/// <param name="jsonText">Original JSON source text.</param>
22+
/// <param name="path">Path of the JSON resource being parsed.</param>
2023
public static void Handle(JsonException ex, string jsonText, string path)
2124
{
22-
// Extract relevant information from the exception
2325
long? lineNumber = ex.LineNumber;
2426

27+
// Prefer context-rich output when the parser reports a source line number.
2528
if (lineNumber.HasValue)
2629
{
27-
// Split the JSON into lines
2830
string[] lines = jsonText.Split('\n');
2931

30-
// Get the problematic line
3132
int lineIndex = (int)lineNumber.Value;
3233
lineIndex = Math.Clamp(lineIndex, 0, Math.Max(0, lines.Length - 1));
3334
string problematicLine = lines[lineIndex];
3435

35-
// Determine the range of lines to display
3636
int startLine = Math.Max(0, lineIndex - ContextLinesBefore);
3737
int endLine = Math.Min(lines.Length, lineIndex + ContextLinesAfter);
3838

39-
// Create the error message
4039
StringBuilder errorMessage = new();
4140

4241
errorMessage.AppendLine($"ERROR: Failed to parse {Path.GetFileName(path)}");
4342
errorMessage.AppendLine();
4443
errorMessage.AppendLine($"{ex.Message}");
4544
errorMessage.AppendLine();
4645

47-
// Add the lines before the problematic line
4846
for (int i = startLine; i < lineIndex; i++)
4947
{
5048
errorMessage.AppendLine(lines[i]);
5149
}
5250

53-
// Add the problematic line with the caret indicating the error position
5451
errorMessage.AppendLine($"{problematicLine} <--- Syntax error could be on this line or the next line");
5552

56-
// Add the lines after the problematic line
5753
for (int i = lineIndex + 1; i < endLine; i++)
5854
{
5955
errorMessage.AppendLine(lines[i]);

Template.GodotUtils/Debugging/ParamValidator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ namespace GodotUtils.Debugging;
1111
public static class ParamValidator
1212
{
1313
/// <summary>
14-
/// Throws when <paramref name="obj"/> is null and disables the node.
14+
/// Validates an object argument and disables the node before throwing when the value is null.
1515
/// </summary>
16+
/// <param name="node">Node to disable when validation fails.</param>
17+
/// <param name="obj">Argument value to validate.</param>
18+
/// <param name="paramName">Name of the validated argument.</param>
19+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="obj"/> is null.</exception>
1620
public static void ThrowIfNull(Node node, object obj, [CallerArgumentExpression(nameof(obj))] string paramName = "")
1721
{
1822
ArgumentNullException.ThrowIfNull(node, nameof(node));
1923

24+
// Disable processing before throwing so invalid nodes stop running immediately.
2025
if (obj == null)
2126
{
2227
Script script = node.GetScript().As<Script>();

Template.GodotUtils/Deprecated/EventManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public void AddListener<T>(TEvent eventType, Action<T> action, string id = "")
5858
/// </summary>
5959
public void RemoveListeners(TEvent eventType, string id = "")
6060
{
61+
// Removing from an unknown event type is treated as a usage error.
6162
if (!_eventListeners.TryGetValue(eventType, out List<Listener>? listeners))
6263
throw new InvalidOperationException($"Tried to remove listener of event type '{eventType}' from an event type that has not even been defined yet");
6364

@@ -81,11 +82,13 @@ public void RemoveAllListeners()
8182
/// </summary>
8283
public void Notify(TEvent eventType, params object[] args)
8384
{
85+
// Unknown event types simply have no listeners to notify.
8486
if (!_eventListeners.TryGetValue(eventType, out List<Listener>? value))
8587
{
8688
return;
8789
}
8890

91+
// Iterate snapshot to avoid collection-modified issues during callbacks.
8992
foreach (Listener listener in value.ToList()) // if ToList() is not here then issue #137 will occur
9093
{
9194
((Action<object[]>)listener.Action)(args);
@@ -94,6 +97,7 @@ public void Notify(TEvent eventType, params object[] args)
9497

9598
private void AddListenerInternal(TEvent eventType, Action<object[]> action, string id)
9699
{
100+
// Lazily create listener bucket for first subscription of an event type.
97101
if (!_eventListeners.TryGetValue(eventType, out List<Listener>? listeners))
98102
{
99103
listeners = [];
@@ -107,6 +111,7 @@ private static Action<object[]> WrapAction<T>(Action<T> action)
107111
{
108112
return args =>
109113
{
114+
// Typed listeners consume only the first argument when available.
110115
if (args == null || args.Length == 0)
111116
return;
112117

Template.GodotUtils/Extensions/AnimatedSprite2DExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ public static void InstantPlay(this AnimatedSprite2D sprite, string anim, int fr
2525

2626
int frameCount = sprite.SpriteFrames.GetFrameCount(anim);
2727

28+
// Apply requested frame only when index is within animation bounds.
2829
if (frameCount - 1 >= frame)
2930
{
3031
sprite.Frame = frame;
3132
}
33+
// Log out-of-range frame selection for debugging.
3234
else
3335
{
3436
GD.Print($"The frame '{frame}' specified for {sprite.Name} is " +
@@ -125,6 +127,7 @@ public static int GetPixelBottomY(this AnimatedSprite2D sprite, string anim = ""
125127

126128
for (int y = size.Y - 1; y >= 0; y--)
127129
{
130+
// Stop once an opaque pixel is reached in center column.
128131
if (img.GetPixel(size.X / 2, y).A != 0)
129132
{
130133
break;

Template.GodotUtils/Extensions/AnimationTreeExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public static class AnimationTreeExtensions
1010
/// <summary>
1111
/// Sets a condition briefly and auto-resets after 0.1 seconds.
1212
/// </summary>
13+
/// <param name="tree">Animation tree to update.</param>
14+
/// <param name="path">Condition parameter path segment.</param>
15+
/// <param name="value">Condition value to set briefly.</param>
1316
public static void SetCondition(this AnimationTree tree, StringName path, bool value)
1417
{
1518
tree.SetParam($"conditions/{path}", value);
@@ -22,6 +25,9 @@ public static void SetCondition(this AnimationTree tree, StringName path, bool v
2225
/// <summary>
2326
/// Sets the blend position of a BlendSpace1D by name.
2427
/// </summary>
28+
/// <param name="tree">Animation tree to update.</param>
29+
/// <param name="name">Blend space parameter name.</param>
30+
/// <param name="value">Blend position value.</param>
2531
public static void SetBlendSpace1DPosition(this AnimationTree tree, StringName name, float value)
2632
{
2733
tree.SetParam($"{name}/blend_position", value);
@@ -30,6 +36,9 @@ public static void SetBlendSpace1DPosition(this AnimationTree tree, StringName n
3036
/// <summary>
3137
/// Sets a parameter value on the animation tree.
3238
/// </summary>
39+
/// <param name="tree">Animation tree to update.</param>
40+
/// <param name="path">Parameter path segment under <c>parameters/</c>.</param>
41+
/// <param name="value">Value to assign.</param>
3342
public static void SetParam(this AnimationTree tree, StringName path, Variant value)
3443
{
3544
tree.Set($"parameters/{path}", value);
@@ -38,6 +47,9 @@ public static void SetParam(this AnimationTree tree, StringName path, Variant va
3847
/// <summary>
3948
/// Gets a parameter value from the animation tree.
4049
/// </summary>
50+
/// <param name="tree">Animation tree to read.</param>
51+
/// <param name="path">Parameter path segment under <c>parameters/</c>.</param>
52+
/// <returns>Resolved parameter value.</returns>
4153
public static Variant GetParam(this AnimationTree tree, StringName path)
4254
{
4355
return tree.Get($"parameters/{path}");
@@ -46,6 +58,9 @@ public static Variant GetParam(this AnimationTree tree, StringName path)
4658
/// <summary>
4759
/// Gets a condition value from the animation tree.
4860
/// </summary>
61+
/// <param name="tree">Animation tree to read.</param>
62+
/// <param name="path">Condition path segment under <c>conditions/</c>.</param>
63+
/// <returns>Resolved condition value.</returns>
4964
public static bool GetCondition(this AnimationTree tree, StringName path)
5065
{
5166
return (bool)tree.GetParam($"conditions/{path}");
@@ -54,6 +69,8 @@ public static bool GetCondition(this AnimationTree tree, StringName path)
5469
/// <summary>
5570
/// Gets the state machine playback controller.
5671
/// </summary>
72+
/// <param name="tree">Animation tree to read.</param>
73+
/// <returns>State machine playback controller.</returns>
5774
public static AnimationNodeStateMachinePlayback GetStateMachine(this AnimationTree tree)
5875
{
5976
return tree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>();

Template.GodotUtils/Extensions/Area2DExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static class Area2DExtensions
1010
/// <summary>
1111
/// Sets Monitoring using deferred property updates.
1212
/// </summary>
13+
/// <param name="area">Area to update.</param>
14+
/// <param name="enabled">Target monitoring state.</param>
1315
public static void SetMonitoringDeferred(this Area2D area, bool enabled)
1416
{
1517
area.SetDeferred(Area2D.PropertyName.Monitoring, enabled);
@@ -18,6 +20,8 @@ public static void SetMonitoringDeferred(this Area2D area, bool enabled)
1820
/// <summary>
1921
/// Sets Monitorable using deferred property updates.
2022
/// </summary>
23+
/// <param name="area">Area to update.</param>
24+
/// <param name="enabled">Target monitorable state.</param>
2125
public static void SetMonitorableDeferred(this Area2D area, bool enabled)
2226
{
2327
area.SetDeferred(Area2D.PropertyName.Monitorable, enabled);

Template.GodotUtils/Extensions/Area3DExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static class Area3DExtensions
1010
/// <summary>
1111
/// Sets Monitoring using deferred property updates.
1212
/// </summary>
13+
/// <param name="area">Area to update.</param>
14+
/// <param name="enabled">Target monitoring state.</param>
1315
public static void SetMonitoringDeferred(this Area3D area, bool enabled)
1416
{
1517
area.SetDeferred(Area3D.PropertyName.Monitoring, enabled);
@@ -18,6 +20,8 @@ public static void SetMonitoringDeferred(this Area3D area, bool enabled)
1820
/// <summary>
1921
/// Sets Monitorable using deferred property updates.
2022
/// </summary>
23+
/// <param name="area">Area to update.</param>
24+
/// <param name="enabled">Target monitorable state.</param>
2125
public static void SetMonitorableDeferred(this Area3D area, bool enabled)
2226
{
2327
area.SetDeferred(Area3D.PropertyName.Monitorable, enabled);

Template.GodotUtils/Extensions/Camera2DExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static class Camera2DExtensions
1010
/// <summary>
1111
/// Sets the camera position without smoothing, then restores smoothing if needed.
1212
/// </summary>
13+
/// <param name="camera">Camera to reposition.</param>
14+
/// <param name="position">Target position.</param>
1315
public static void SetPositionIgnoreSmoothing(this Camera2D camera, Vector2 position)
1416
{
1517
bool smoothEnabled = camera.PositionSmoothingEnabled;
@@ -24,6 +26,7 @@ public static void SetPositionIgnoreSmoothing(this Camera2D camera, Vector2 posi
2426

2527
if (smoothEnabled)
2628
{
29+
// Restore smoothing after the snapped position has been applied.
2730
Tweens.Delay(camera, 0.01, () => camera.PositionSmoothingEnabled = true);
2831
}
2932
}

Template.GodotUtils/Extensions/CanvasItemExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static class CanvasItemExtensions
1010
/// <summary>
1111
/// Applies an unshaded material to the canvas item.
1212
/// </summary>
13+
/// <param name="canvasItem">Canvas item to modify.</param>
1314
public static void SetUnshaded(this CanvasItem canvasItem)
1415
{
1516
canvasItem.Material = new CanvasItemMaterial
@@ -21,6 +22,8 @@ public static void SetUnshaded(this CanvasItem canvasItem)
2122
/// <summary>
2223
/// Converts the canvas item position to a screen position.
2324
/// </summary>
25+
/// <param name="canvasItem">Canvas item to project into screen coordinates.</param>
26+
/// <returns>Screen position of the canvas item origin.</returns>
2427
public static Vector2 GetScreenPosition(this CanvasItem canvasItem)
2528
{
2629
// Code retrieved from https://www.reddit.com/r/godot/comments/1aq1f0b/comment/kqa6z0u/
@@ -34,6 +37,8 @@ public static Vector2 GetScreenPosition(this CanvasItem canvasItem)
3437
/// <summary>
3538
/// Sets the canvas item fully transparent and returns it.
3639
/// </summary>
40+
/// <param name="canvasItem">Canvas item to modify.</param>
41+
/// <returns>The same canvas item instance.</returns>
3742
public static CanvasItem SetTransparent(this CanvasItem canvasItem)
3843
{
3944
canvasItem.SelfModulate = new Color(1, 1, 1, 0);

0 commit comments

Comments
 (0)