Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/PublicApi/Modules/GregFacilityModule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
using UnityEngine;

namespace gregCore.PublicApi.Modules;

public sealed class GregFacilityModule
{
private readonly GregApiContext _ctx;
internal GregFacilityModule(GregApiContext ctx) => _ctx = ctx;

public int GetRackCount() => UnityEngine.Object.FindObjectsOfType<global::Il2Cpp.Rack>().Length;
}
using UnityEngine;

namespace gregCore.PublicApi.Modules;

public sealed class GregFacilityModule
{
private readonly GregApiContext _ctx;
internal GregFacilityModule(GregApiContext ctx) => _ctx = ctx;

// Index 2 represents racks in GetNumberOfDevices array (0: servers, 1: switches)
private const int DEVICE_INDEX_RACKS = 2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Suggestion: The use of a hardcoded index 2 for DEVICE_INDEX_RACKS is brittle. If the game's internal NetworkMap structure changes, this logic will return incorrect device counts without a runtime crash. Search the global::Il2Cpp namespace for an Enum or constants related to NetworkMap.GetNumberOfDevices indices and refactor to use a named constant if available.

See Complexity in Codacy


public int GetRackCount()
{
// Optimization: Use O(1) device counts from NetworkMap instead of O(N) FindObjectsOfType
var networkMap = global::Il2Cpp.NetworkMap.instance;
if (networkMap != null)
{
var counts = networkMap.GetNumberOfDevices();
if (counts != null && counts.Length > DEVICE_INDEX_RACKS)
{
return counts[DEVICE_INDEX_RACKS];
}
}

// Fallback during uninitialized game states
return UnityEngine.Object.FindObjectsOfType<global::Il2Cpp.Rack>().Length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Suggestion: The fallback FindObjectsOfType is an O(N) operation that scans all GameObjects. When NetworkMap.instance is null (e.g., main menu), this will execute repeatedly and cause stutters. Since NetworkMap is the authority, a null instance should likely default to returning 0 to avoid the expensive scan.

}
}
Loading