using System.Collections.Generic;
using UnityEngine;
namespace io.github.hatayama.uLoopMCP
{
///
/// Class responsible for auto-updating MCP configuration files.
/// Automatically updates configuration files when paths change after package updates.
/// Single Responsibility Principle: Only responsible for auto-updating configuration files.
///
/// Related classes:
/// - McpConfigServiceFactory: Provides ConfigService for each editor type
/// - McpConfigService: Handles configuration file read/write and update detection
/// - McpServerOperations: Calls this class during server startup
///
public static class McpConfigAutoUpdater
{
///
/// Checks all configured editors and updates their configuration files if needed.
/// Automatically updates configurations when paths have changed (e.g., after package updates).
///
/// The port number to use
public static void UpdateAllConfiguredEditors(int port)
{
McpConfigServiceFactory factory = new McpConfigServiceFactory();
IReadOnlyDictionary allServices = factory.GetAllConfigServices();
foreach (KeyValuePair entry in allServices)
{
UpdateEditorConfigIfNeeded(entry.Key, entry.Value, port);
}
}
///
/// Updates the specified editor's configuration if needed.
///
/// The editor type
/// The configuration service
/// The port number
private static void UpdateEditorConfigIfNeeded(McpEditorType editorType, IMcpConfigService configService, int port)
{
bool isConfigured = configService.IsConfigured();
if (!isConfigured)
{
return;
}
bool isUpdateNeeded = configService.IsUpdateNeeded(port);
if (!isUpdateNeeded)
{
return;
}
configService.AutoConfigure(port);
string editorName = GetEditorDisplayName(editorType);
Debug.Log($"[uLoopMCP] Auto-updated {editorName} configuration (path changed)");
}
///
/// Gets the display name for the editor type.
///
/// The editor type
/// The display name
private static string GetEditorDisplayName(McpEditorType editorType)
{
return editorType switch
{
McpEditorType.Cursor => "Cursor",
McpEditorType.ClaudeCode => "Claude Code",
McpEditorType.VSCode => "VSCode",
McpEditorType.GeminiCLI => "Gemini CLI",
McpEditorType.Windsurf => "Windsurf",
McpEditorType.Codex => "Codex",
McpEditorType.McpInspector => "MCP Inspector",
_ => editorType.ToString()
};
}
}
}