-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileManager.cs
120 lines (102 loc) · 3.32 KB
/
ProfileManager.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
#if UNITY_EDITOR
using System.Security.Cryptography;
using System.Text;
#endif
using UnityEngine;
namespace Unity.BossRoom.Utils
{
public class ProfileManager
{
public const string AuthProfileCommandLineArg = "-AuthProfile";
string m_Profile;
public string Profile
{
get
{
if (m_Profile == null)
{
m_Profile = GetProfile();
}
return m_Profile;
}
set
{
m_Profile = value;
onProfileChanged?.Invoke();
}
}
public event Action onProfileChanged;
List<string> m_AvailableProfiles;
public ReadOnlyCollection<string> AvailableProfiles
{
get
{
if (m_AvailableProfiles == null)
{
LoadProfiles();
}
return m_AvailableProfiles.AsReadOnly();
}
}
public void CreateProfile(string profile)
{
m_AvailableProfiles.Add(profile);
SaveProfiles();
}
public void DeleteProfile(string profile)
{
m_AvailableProfiles.Remove(profile);
SaveProfiles();
}
static string GetProfile()
{
var arguments = Environment.GetCommandLineArgs();
for (int i = 0; i < arguments.Length; i++)
{
if (arguments[i] == AuthProfileCommandLineArg)
{
var profileId = arguments[i + 1];
return profileId;
}
}
#if UNITY_EDITOR
// When running in the Editor make a unique ID from the Application.dataPath.
// This will work for cloning projects manually, or with Virtual Projects.
// Since only a single instance of the Editor can be open for a specific
// dataPath, uniqueness is ensured.
var hashedBytes = new MD5CryptoServiceProvider()
.ComputeHash(Encoding.UTF8.GetBytes(Application.dataPath));
Array.Resize(ref hashedBytes, 16);
// Authentication service only allows profile names of maximum 30 characters. We're generating a GUID based
// on the project's path. Truncating the first 30 characters of said GUID string suffices for uniqueness.
return new Guid(hashedBytes).ToString("N")[..30];
#else
return "";
#endif
}
void LoadProfiles()
{
m_AvailableProfiles = new List<string>();
var loadedProfiles = ClientPrefs.GetAvailableProfiles();
foreach (var profile in loadedProfiles.Split(',')) // this works since we're sanitizing our input strings
{
if (profile.Length > 0)
{
m_AvailableProfiles.Add(profile);
}
}
}
void SaveProfiles()
{
var profilesToSave = "";
foreach (var profile in m_AvailableProfiles)
{
profilesToSave += profile + ",";
}
ClientPrefs.SetAvailableProfiles(profilesToSave);
}
}
}