Newer
Older
HoloAnatomy / Assets / HoloToolkit / Sharing / Scripts / Extensions / SessionExtensions.cs
SURFACEBOOK2\jackwynne on 25 May 2018 2 KB v1
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3.  
  4. using System.Collections.Generic;
  5.  
  6. namespace HoloToolkit.Sharing
  7. {
  8. /// <summary>
  9. /// Extensions methods for the Session class.
  10. /// </summary>
  11. public static class SessionExtensions
  12. {
  13. /// <summary>
  14. /// Returns an unused username that can be used for this session.
  15. /// </summary>
  16. /// <param name="session">Session object for which this is being called.</param>
  17. /// <param name="baseName">Base name to use for the username.</param>
  18. /// <param name="excludedUserId">
  19. /// User ID whose username excluded from the unique name check.
  20. /// If not specified, all users in the session will be taken into account to find
  21. /// a unique name.
  22. /// </param>
  23. /// <returns></returns>
  24. public static string GetUnusedName(this Session session, string baseName, int excludedUserId = int.MaxValue)
  25. {
  26. List<string> nameList = new List<string>();
  27. return GetUnusedName(session, baseName, excludedUserId, nameList);
  28. }
  29.  
  30. /// <summary>
  31. /// Returns an unused username that can be used for this session.
  32. /// </summary>
  33. /// <param name="session">Session object for which this is being called.</param>
  34. /// <param name="baseName">Base name to use for the username.</param>
  35. /// <param name="excludedUserId">
  36. /// User ID whose username excluded from the unique name check.
  37. /// If not specified, all users in the session will be taken into account to find
  38. /// a unique name.
  39. /// </param>
  40. /// <param name="cachedList">Cached list that can be provided to avoid extra memory allocations.
  41. /// </param>
  42. /// <returns></returns>
  43. public static string GetUnusedName(this Session session, string baseName, int excludedUserId, List<string> cachedList)
  44. {
  45. cachedList.Clear();
  46.  
  47. for (int i = 0; i < session.GetUserCount(); i++)
  48. {
  49. using (var user = session.GetUser(i))
  50. using (var userName = user.GetName())
  51. {
  52. string userNameString = userName.GetString();
  53. if (user.GetID() != excludedUserId && userNameString.StartsWith(baseName))
  54. {
  55. cachedList.Add(userNameString);
  56. }
  57. }
  58. }
  59.  
  60. cachedList.Sort();
  61.  
  62. int counter = 0;
  63. string currentName = baseName;
  64. while (cachedList.Contains(currentName))
  65. {
  66. currentName = baseName + (++counter);
  67. }
  68.  
  69. return currentName;
  70. }
  71. }
  72. }