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