Skip to content

Commit 4c72309

Browse files
committed
Bridge logs: add bold blue UNITY-MCP prefix; gate PortManager logs behind Debug Logs toggle; improve Python and UV detection on Windows (flex versions, where.exe/Path scan); tidy installer messages
1 parent f24e124 commit 4c72309

File tree

5 files changed

+190
-73
lines changed

5 files changed

+190
-73
lines changed

UnityMcpBridge/Editor/Helpers/PackageInstaller.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ private static void InstallServerOnFirstLoad()
2525
{
2626
try
2727
{
28-
Debug.Log("Unity MCP: Installing Python server...");
28+
Debug.Log("<b><color=#2EA3FF>UNITY-MCP</color></b>: Installing Python server...");
2929
ServerInstaller.EnsureServerInstalled();
3030

3131
// Mark as installed
3232
EditorPrefs.SetBool(InstallationFlagKey, true);
3333

34-
Debug.Log("Unity MCP: Python server installation completed successfully.");
34+
Debug.Log("<b><color=#2EA3FF>UNITY-MCP</color></b>: Python server installation completed successfully.");
3535
}
3636
catch (System.Exception ex)
3737
{
38-
Debug.LogError($"Unity MCP: Failed to install Python server: {ex.Message}");
39-
Debug.LogWarning("Unity MCP: You may need to manually install the Python server. Check the Unity MCP Editor Window for instructions.");
38+
Debug.LogError($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Failed to install Python server: {ex.Message}");
39+
Debug.LogWarning("<b><color=#2EA3FF>UNITY-MCP</color></b>: You may need to manually install the Python server. Check the Unity MCP Editor Window for instructions.");
4040
}
4141
}
4242
}

UnityMcpBridge/Editor/Helpers/PortManager.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using UnityEditor;
34
using System.Net;
45
using System.Net.Sockets;
56
using System.Security.Cryptography;
@@ -15,6 +16,12 @@ namespace UnityMcpBridge.Editor.Helpers
1516
/// </summary>
1617
public static class PortManager
1718
{
19+
private static bool IsDebugEnabled()
20+
{
21+
try { return EditorPrefs.GetBool("UnityMCP.DebugLogs", false); }
22+
catch { return false; }
23+
}
24+
1825
private const int DefaultPort = 6400;
1926
private const int MaxPortAttempts = 100;
2027
private const string RegistryFileName = "unity-mcp-port.json";
@@ -41,7 +48,7 @@ public static int GetPortWithFallback()
4148
string.Equals(storedConfig.project_path ?? string.Empty, Application.dataPath ?? string.Empty, StringComparison.OrdinalIgnoreCase) &&
4249
IsPortAvailable(storedConfig.unity_port))
4350
{
44-
Debug.Log($"Using stored port {storedConfig.unity_port} for current project");
51+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Using stored port {storedConfig.unity_port} for current project");
4552
return storedConfig.unity_port;
4653
}
4754

@@ -50,7 +57,7 @@ public static int GetPortWithFallback()
5057
{
5158
if (WaitForPortRelease(storedConfig.unity_port, 1500))
5259
{
53-
Debug.Log($"Stored port {storedConfig.unity_port} became available after short wait");
60+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Stored port {storedConfig.unity_port} became available after short wait");
5461
return storedConfig.unity_port;
5562
}
5663
// Prefer sticking to the same port; let the caller handle bind retries/fallbacks
@@ -71,7 +78,7 @@ public static int DiscoverNewPort()
7178
{
7279
int newPort = FindAvailablePort();
7380
SavePort(newPort);
74-
Debug.Log($"Discovered and saved new port: {newPort}");
81+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Discovered and saved new port: {newPort}");
7582
return newPort;
7683
}
7784

@@ -84,18 +91,18 @@ private static int FindAvailablePort()
8491
// Always try default port first
8592
if (IsPortAvailable(DefaultPort))
8693
{
87-
Debug.Log($"Using default port {DefaultPort}");
94+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Using default port {DefaultPort}");
8895
return DefaultPort;
8996
}
9097

91-
Debug.Log($"Default port {DefaultPort} is in use, searching for alternative...");
98+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Default port {DefaultPort} is in use, searching for alternative...");
9299

93100
// Search for alternatives
94101
for (int port = DefaultPort + 1; port < DefaultPort + MaxPortAttempts; port++)
95102
{
96103
if (IsPortAvailable(port))
97104
{
98-
Debug.Log($"Found available port {port}");
105+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Found available port {port}");
99106
return port;
100107
}
101108
}
@@ -204,7 +211,7 @@ private static void SavePort(int port)
204211
string legacy = Path.Combine(GetRegistryDirectory(), RegistryFileName);
205212
File.WriteAllText(legacy, json);
206213

207-
Debug.Log($"Saved port {port} to storage");
214+
if (IsDebugEnabled()) Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: Saved port {port} to storage");
208215
}
209216
catch (Exception ex)
210217
{

UnityMcpBridge/Editor/Helpers/ServerInstaller.cs

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public static bool RepairPythonEnvironment()
281281
return false;
282282
}
283283

284-
Debug.Log("Unity MCP: Python environment repaired successfully.");
284+
Debug.Log("<b><color=#2EA3FF>UNITY-MCP</color></b>: Python environment repaired successfully.");
285285
return true;
286286
}
287287
catch (Exception ex)
@@ -305,47 +305,100 @@ private static string FindUvPath()
305305
catch { }
306306

307307
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) ?? string.Empty;
308-
string[] candidates =
308+
309+
// Platform-specific candidate lists
310+
string[] candidates;
311+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
312+
{
313+
candidates = new[]
314+
{
315+
// Common per-user installs
316+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) ?? string.Empty, @"Programs\Python\Python313\Scripts\uv.exe"),
317+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) ?? string.Empty, @"Programs\Python\Python312\Scripts\uv.exe"),
318+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) ?? string.Empty, @"Programs\Python\Python311\Scripts\uv.exe"),
319+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) ?? string.Empty, @"Programs\Python\Python310\Scripts\uv.exe"),
320+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) ?? string.Empty, @"Python\Python313\Scripts\uv.exe"),
321+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) ?? string.Empty, @"Python\Python312\Scripts\uv.exe"),
322+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) ?? string.Empty, @"Python\Python311\Scripts\uv.exe"),
323+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) ?? string.Empty, @"Python\Python310\Scripts\uv.exe"),
324+
// Program Files style installs (if a native installer was used)
325+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) ?? string.Empty, @"uv\uv.exe"),
326+
// Try simple name resolution later via PATH
327+
"uv.exe",
328+
"uv"
329+
};
330+
}
331+
else
309332
{
310-
"/opt/homebrew/bin/uv",
311-
"/usr/local/bin/uv",
312-
"/usr/bin/uv",
313-
"/opt/local/bin/uv",
314-
Path.Combine(home, ".local", "bin", "uv"),
315-
"/opt/homebrew/opt/uv/bin/uv",
316-
// Framework Python installs
317-
"/Library/Frameworks/Python.framework/Versions/3.13/bin/uv",
318-
"/Library/Frameworks/Python.framework/Versions/3.12/bin/uv",
319-
// Fallback to PATH resolution by name
320-
"uv"
321-
};
333+
candidates = new[]
334+
{
335+
"/opt/homebrew/bin/uv",
336+
"/usr/local/bin/uv",
337+
"/usr/bin/uv",
338+
"/opt/local/bin/uv",
339+
Path.Combine(home, ".local", "bin", "uv"),
340+
"/opt/homebrew/opt/uv/bin/uv",
341+
// Framework Python installs
342+
"/Library/Frameworks/Python.framework/Versions/3.13/bin/uv",
343+
"/Library/Frameworks/Python.framework/Versions/3.12/bin/uv",
344+
// Fallback to PATH resolution by name
345+
"uv"
346+
};
347+
}
348+
322349
foreach (string c in candidates)
323350
{
324351
try
325352
{
326-
if (ValidateUvBinary(c)) return c;
353+
if (File.Exists(c) && ValidateUvBinary(c)) return c;
327354
}
328355
catch { /* ignore */ }
329356
}
330357

331-
// Try which uv (explicit path)
358+
// Use platform-appropriate which/where to resolve from PATH
332359
try
333360
{
334-
var whichPsi = new System.Diagnostics.ProcessStartInfo
361+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
335362
{
336-
FileName = "/usr/bin/which",
337-
Arguments = "uv",
338-
UseShellExecute = false,
339-
RedirectStandardOutput = true,
340-
RedirectStandardError = true,
341-
CreateNoWindow = true
342-
};
343-
using var wp = System.Diagnostics.Process.Start(whichPsi);
344-
string output = wp.StandardOutput.ReadToEnd().Trim();
345-
wp.WaitForExit(3000);
346-
if (wp.ExitCode == 0 && !string.IsNullOrEmpty(output) && File.Exists(output))
363+
var wherePsi = new System.Diagnostics.ProcessStartInfo
364+
{
365+
FileName = "where",
366+
Arguments = "uv.exe",
367+
UseShellExecute = false,
368+
RedirectStandardOutput = true,
369+
RedirectStandardError = true,
370+
CreateNoWindow = true
371+
};
372+
using var wp = System.Diagnostics.Process.Start(wherePsi);
373+
string output = wp.StandardOutput.ReadToEnd().Trim();
374+
wp.WaitForExit(3000);
375+
if (wp.ExitCode == 0 && !string.IsNullOrEmpty(output))
376+
{
377+
foreach (var line in output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
378+
{
379+
string path = line.Trim();
380+
if (File.Exists(path) && ValidateUvBinary(path)) return path;
381+
}
382+
}
383+
}
384+
else
347385
{
348-
if (ValidateUvBinary(output)) return output;
386+
var whichPsi = new System.Diagnostics.ProcessStartInfo
387+
{
388+
FileName = "/usr/bin/which",
389+
Arguments = "uv",
390+
UseShellExecute = false,
391+
RedirectStandardOutput = true,
392+
RedirectStandardError = true,
393+
CreateNoWindow = true
394+
};
395+
using var wp = System.Diagnostics.Process.Start(whichPsi);
396+
string output = wp.StandardOutput.ReadToEnd().Trim();
397+
wp.WaitForExit(3000);
398+
if (wp.ExitCode == 0 && !string.IsNullOrEmpty(output) && File.Exists(output))
399+
{
400+
if (ValidateUvBinary(output)) return output;
401+
}
349402
}
350403
}
351404
catch { }
@@ -359,8 +412,11 @@ private static string FindUvPath()
359412
{
360413
try
361414
{
362-
string candidate = Path.Combine(part, "uv");
363-
if (File.Exists(candidate) && ValidateUvBinary(candidate)) return candidate;
415+
// Check both uv and uv.exe
416+
string candidateUv = Path.Combine(part, "uv");
417+
string candidateUvExe = Path.Combine(part, "uv.exe");
418+
if (File.Exists(candidateUv) && ValidateUvBinary(candidateUv)) return candidateUv;
419+
if (File.Exists(candidateUvExe) && ValidateUvBinary(candidateUvExe)) return candidateUvExe;
364420
}
365421
catch { }
366422
}

UnityMcpBridge/Editor/UnityMcpBridge.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static void Start()
128128
// Don't restart if already running on a working port
129129
if (isRunning && listener != null)
130130
{
131-
Debug.Log($"UnityMcpBridge already running on port {currentUnityPort}");
131+
Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: UnityMcpBridge already running on port {currentUnityPort}");
132132
return;
133133
}
134134

@@ -194,7 +194,7 @@ public static void Start()
194194

195195
isRunning = true;
196196
isAutoConnectMode = false;
197-
Debug.Log($"UnityMcpBridge started on port {currentUnityPort}.");
197+
Debug.Log($"<b><color=#2EA3FF>UNITY-MCP</color></b>: UnityMcpBridge started on port {currentUnityPort}.");
198198
Task.Run(ListenerLoop);
199199
EditorApplication.update += ProcessCommands;
200200
// Write initial heartbeat immediately
@@ -226,7 +226,7 @@ public static void Stop()
226226
listener?.Stop();
227227
listener = null;
228228
EditorApplication.update -= ProcessCommands;
229-
Debug.Log("UnityMcpBridge stopped.");
229+
Debug.Log("<b><color=#2EA3FF>UNITY-MCP</color></b>: UnityMcpBridge stopped.");
230230
}
231231
catch (Exception ex)
232232
{

0 commit comments

Comments
 (0)