|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO; |
3 | 4 | using System.Security.Cryptography; |
4 | 5 | using System.Text; |
| 6 | +using System.Threading; |
5 | 7 | using MCPForUnity.Editor.Constants; |
6 | 8 | using UnityEditor; |
7 | 9 | using UnityEngine; |
@@ -257,4 +259,191 @@ private static string TryComputeFallbackProjectHash() |
257 | 259 | } |
258 | 260 | } |
259 | 261 | } |
| 262 | + |
| 263 | + /// <summary> |
| 264 | + /// Stores connection and local-server lifecycle preferences per Unity project while |
| 265 | + /// retaining a single-owner read fallback for legacy machine-global preferences. |
| 266 | + /// </summary> |
| 267 | + internal static class ProjectScopedEditorPrefs |
| 268 | + { |
| 269 | + private const string MigrationOwnerKey = "MCPForUnity.ProjectScopedPrefs.MigrationOwner"; |
| 270 | + private const string MigrationMutexName = "MCPForUnity.ProjectPreferenceMigration"; |
| 271 | + private static readonly HashSet<string> KnownProjectScopedKeys = new HashSet<string> |
| 272 | + { |
| 273 | + EditorPrefKeys.UseHttpTransport, |
| 274 | + EditorPrefKeys.HttpBaseUrl, |
| 275 | + EditorPrefKeys.HttpTransportScope, |
| 276 | + EditorPrefKeys.LastLocalHttpServerPid, |
| 277 | + EditorPrefKeys.LastLocalHttpServerPort, |
| 278 | + EditorPrefKeys.LastLocalHttpServerStartedUtc, |
| 279 | + EditorPrefKeys.LastLocalHttpServerPidArgsHash, |
| 280 | + EditorPrefKeys.LastLocalHttpServerPidFilePath, |
| 281 | + EditorPrefKeys.LastLocalHttpServerInstanceToken, |
| 282 | + }; |
| 283 | + |
| 284 | + internal static string GetKey(string baseKey, string projectHash = null) |
| 285 | + { |
| 286 | + string hash = string.IsNullOrWhiteSpace(projectHash) |
| 287 | + ? ProjectIdentityUtility.GetProjectHash() |
| 288 | + : projectHash; |
| 289 | + return $"{baseKey}.{hash}"; |
| 290 | + } |
| 291 | + |
| 292 | + internal static string GetMigrationOwnerKey() |
| 293 | + { |
| 294 | + return MigrationOwnerKey; |
| 295 | + } |
| 296 | + |
| 297 | + internal static string ResolveStorageKey(string baseKey, string projectHash = null) |
| 298 | + { |
| 299 | + return KnownProjectScopedKeys.Contains(baseKey) ? GetKey(baseKey, projectHash) : baseKey; |
| 300 | + } |
| 301 | + |
| 302 | + internal static bool HasKey(string baseKey) |
| 303 | + { |
| 304 | + return EditorPrefs.HasKey(ResolveStorageKey(baseKey)); |
| 305 | + } |
| 306 | + |
| 307 | + internal static bool GetBool( |
| 308 | + string baseKey, |
| 309 | + bool defaultValue, |
| 310 | + string projectHash = null, |
| 311 | + bool allowLegacyFallback = true) |
| 312 | + { |
| 313 | + string scopedKey = ResolveStorageKey(baseKey, projectHash); |
| 314 | + if (EditorPrefs.HasKey(scopedKey)) |
| 315 | + { |
| 316 | + return EditorPrefs.GetBool(scopedKey, defaultValue); |
| 317 | + } |
| 318 | + |
| 319 | + if (allowLegacyFallback |
| 320 | + && EditorPrefs.HasKey(baseKey) |
| 321 | + && TryClaimLegacyPreference(projectHash)) |
| 322 | + { |
| 323 | + bool migrated = EditorPrefs.GetBool(baseKey, defaultValue); |
| 324 | + EditorPrefs.SetBool(scopedKey, migrated); |
| 325 | + return migrated; |
| 326 | + } |
| 327 | + |
| 328 | + return defaultValue; |
| 329 | + } |
| 330 | + |
| 331 | + internal static int GetInt( |
| 332 | + string baseKey, |
| 333 | + int defaultValue, |
| 334 | + string projectHash = null, |
| 335 | + bool allowLegacyFallback = true) |
| 336 | + { |
| 337 | + string scopedKey = ResolveStorageKey(baseKey, projectHash); |
| 338 | + if (EditorPrefs.HasKey(scopedKey)) |
| 339 | + { |
| 340 | + return EditorPrefs.GetInt(scopedKey, defaultValue); |
| 341 | + } |
| 342 | + |
| 343 | + if (allowLegacyFallback |
| 344 | + && EditorPrefs.HasKey(baseKey) |
| 345 | + && TryClaimLegacyPreference(projectHash)) |
| 346 | + { |
| 347 | + int migrated = EditorPrefs.GetInt(baseKey, defaultValue); |
| 348 | + EditorPrefs.SetInt(scopedKey, migrated); |
| 349 | + return migrated; |
| 350 | + } |
| 351 | + |
| 352 | + return defaultValue; |
| 353 | + } |
| 354 | + |
| 355 | + internal static string GetString( |
| 356 | + string baseKey, |
| 357 | + string defaultValue, |
| 358 | + string projectHash = null, |
| 359 | + bool allowLegacyFallback = true) |
| 360 | + { |
| 361 | + string scopedKey = ResolveStorageKey(baseKey, projectHash); |
| 362 | + if (EditorPrefs.HasKey(scopedKey)) |
| 363 | + { |
| 364 | + return EditorPrefs.GetString(scopedKey, defaultValue); |
| 365 | + } |
| 366 | + |
| 367 | + if (allowLegacyFallback |
| 368 | + && EditorPrefs.HasKey(baseKey) |
| 369 | + && TryClaimLegacyPreference(projectHash)) |
| 370 | + { |
| 371 | + string migrated = EditorPrefs.GetString(baseKey, defaultValue); |
| 372 | + EditorPrefs.SetString(scopedKey, migrated); |
| 373 | + return migrated; |
| 374 | + } |
| 375 | + |
| 376 | + return defaultValue; |
| 377 | + } |
| 378 | + |
| 379 | + internal static void SetBool(string baseKey, bool value) |
| 380 | + { |
| 381 | + EditorPrefs.SetBool(ResolveStorageKey(baseKey), value); |
| 382 | + } |
| 383 | + |
| 384 | + internal static void SetInt(string baseKey, int value) |
| 385 | + { |
| 386 | + EditorPrefs.SetInt(ResolveStorageKey(baseKey), value); |
| 387 | + } |
| 388 | + |
| 389 | + internal static void SetString(string baseKey, string value) |
| 390 | + { |
| 391 | + EditorPrefs.SetString(ResolveStorageKey(baseKey), value); |
| 392 | + } |
| 393 | + |
| 394 | + internal static void DeleteKey(string baseKey) |
| 395 | + { |
| 396 | + EditorPrefs.DeleteKey(ResolveStorageKey(baseKey)); |
| 397 | + } |
| 398 | + |
| 399 | + private static bool TryClaimLegacyPreference(string projectHash) |
| 400 | + { |
| 401 | + string hash = string.IsNullOrWhiteSpace(projectHash) |
| 402 | + ? ProjectIdentityUtility.GetProjectHash() |
| 403 | + : projectHash; |
| 404 | + string ownerKey = GetMigrationOwnerKey(); |
| 405 | + |
| 406 | + Mutex migrationMutex = null; |
| 407 | + bool lockTaken = false; |
| 408 | + try |
| 409 | + { |
| 410 | + migrationMutex = new Mutex(false, MigrationMutexName); |
| 411 | + try |
| 412 | + { |
| 413 | + lockTaken = migrationMutex.WaitOne(TimeSpan.FromSeconds(2)); |
| 414 | + } |
| 415 | + catch (AbandonedMutexException) |
| 416 | + { |
| 417 | + lockTaken = true; |
| 418 | + } |
| 419 | + |
| 420 | + if (!lockTaken) |
| 421 | + { |
| 422 | + return false; |
| 423 | + } |
| 424 | + |
| 425 | + string owner = EditorPrefs.GetString(ownerKey, string.Empty); |
| 426 | + if (string.IsNullOrEmpty(owner)) |
| 427 | + { |
| 428 | + EditorPrefs.SetString(ownerKey, hash); |
| 429 | + owner = EditorPrefs.GetString(ownerKey, string.Empty); |
| 430 | + } |
| 431 | + |
| 432 | + return string.Equals(owner, hash, StringComparison.Ordinal); |
| 433 | + } |
| 434 | + catch |
| 435 | + { |
| 436 | + return false; |
| 437 | + } |
| 438 | + finally |
| 439 | + { |
| 440 | + if (lockTaken) |
| 441 | + { |
| 442 | + try { migrationMutex.ReleaseMutex(); } catch { } |
| 443 | + } |
| 444 | + |
| 445 | + migrationMutex?.Dispose(); |
| 446 | + } |
| 447 | + } |
| 448 | + } |
260 | 449 | } |
0 commit comments