Skip to content

Commit ee23346

Browse files
committed
feat: installer cleanup, auto-migration, logging normalization
1 parent 97fb4a7 commit ee23346

File tree

8 files changed

+461
-20
lines changed

8 files changed

+461
-20
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace MCPForUnity.Editor.Helpers
5+
{
6+
internal static class McpLog
7+
{
8+
private const string Prefix = "<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>:";
9+
10+
private static bool IsDebugEnabled()
11+
{
12+
try { return EditorPrefs.GetBool("MCPForUnity.DebugLogs", false); } catch { return false; }
13+
}
14+
15+
public static void Info(string message, bool always = true)
16+
{
17+
if (!always && !IsDebugEnabled()) return;
18+
Debug.Log($"{Prefix} {message}");
19+
}
20+
21+
public static void Warn(string message)
22+
{
23+
Debug.LogWarning($"<color=#cc7a00>{Prefix} {message}</color>");
24+
}
25+
26+
public static void Error(string message)
27+
{
28+
Debug.LogError($"<color=#cc3333>{Prefix} {message}</color>");
29+
}
30+
}
31+
}
32+
33+

UnityMcpBridge/Editor/Helpers/McpLog.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace MCPForUnity.Editor.Helpers
5+
{
6+
/// <summary>
7+
/// Auto-runs legacy/older install detection on package load/update (log-only).
8+
/// Runs once per embedded server version using an EditorPrefs version-scoped key.
9+
/// </summary>
10+
[InitializeOnLoad]
11+
public static class PackageDetector
12+
{
13+
private const string DetectOnceFlagKeyPrefix = "MCPForUnity.LegacyDetectLogged:";
14+
15+
static PackageDetector()
16+
{
17+
try
18+
{
19+
string pkgVer = ReadPackageVersionOrFallback();
20+
string key = DetectOnceFlagKeyPrefix + pkgVer;
21+
22+
// Always force-run if legacy roots exist or canonical install is missing
23+
bool legacyPresent = LegacyRootsExist();
24+
bool canonicalMissing = !System.IO.File.Exists(System.IO.Path.Combine(ServerInstaller.GetServerPath(), "server.py"));
25+
26+
if (!EditorPrefs.GetBool(key, false) || legacyPresent || canonicalMissing)
27+
{
28+
EditorApplication.delayCall += () =>
29+
{
30+
try
31+
{
32+
ServerInstaller.EnsureServerInstalled();
33+
}
34+
catch (System.Exception ex)
35+
{
36+
Debug.LogWarning("MCP for Unity: Auto-detect on load failed: " + ex.Message);
37+
}
38+
finally
39+
{
40+
EditorPrefs.SetBool(key, true);
41+
}
42+
};
43+
}
44+
}
45+
catch { /* ignore */ }
46+
}
47+
48+
private static string ReadEmbeddedVersionOrFallback()
49+
{
50+
try
51+
{
52+
if (ServerPathResolver.TryFindEmbeddedServerSource(out var embeddedSrc))
53+
{
54+
var p = System.IO.Path.Combine(embeddedSrc, "server_version.txt");
55+
if (System.IO.File.Exists(p))
56+
return (System.IO.File.ReadAllText(p)?.Trim() ?? "unknown");
57+
}
58+
}
59+
catch { }
60+
return "unknown";
61+
}
62+
63+
private static string ReadPackageVersionOrFallback()
64+
{
65+
try
66+
{
67+
var info = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(PackageDetector).Assembly);
68+
if (info != null && !string.IsNullOrEmpty(info.version)) return info.version;
69+
}
70+
catch { }
71+
// Fallback to embedded server version if package info unavailable
72+
return ReadEmbeddedVersionOrFallback();
73+
}
74+
75+
private static bool LegacyRootsExist()
76+
{
77+
try
78+
{
79+
string home = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile) ?? string.Empty;
80+
string[] roots =
81+
{
82+
System.IO.Path.Combine(home, ".config", "UnityMCP", "UnityMcpServer", "src"),
83+
System.IO.Path.Combine(home, ".local", "share", "UnityMCP", "UnityMcpServer", "src")
84+
};
85+
foreach (var r in roots)
86+
{
87+
try { if (System.IO.File.Exists(System.IO.Path.Combine(r, "server.py"))) return true; } catch { }
88+
}
89+
}
90+
catch { }
91+
return false;
92+
}
93+
}
94+
}
95+
96+

UnityMcpBridge/Editor/Helpers/PackageDetector.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)