forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheServerPreferences.cs
More file actions
298 lines (260 loc) · 14.6 KB
/
Copy pathCacheServerPreferences.cs
File metadata and controls
298 lines (260 loc) · 14.6 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using Microsoft.Win32;
using UnityEngine;
using UnityEditor;
using UnityEditor.Collaboration;
using UnityEditorInternal;
using System.Collections.Generic;
using System;
using System.IO;
namespace UnityEditor
{
internal class CacheServerPreferences
{
internal class Styles
{
public static readonly GUIContent browse = EditorGUIUtility.TrTextContent("Browse...");
public static readonly GUIContent maxCacheSize = EditorGUIUtility.TrTextContent("Maximum Cache Size (GB)", "The size of the local asset cache server folder will be kept below this maximum value.");
public static readonly GUIContent customCacheLocation = EditorGUIUtility.TrTextContent("Custom cache location", "Specify the local asset cache server folder location.");
public static readonly GUIContent cacheFolderLocation = EditorGUIUtility.TrTextContent("Cache Folder Location", "The local asset cache server folder is shared between all projects.");
public static readonly GUIContent cleanCache = EditorGUIUtility.TrTextContent("Clean Cache");
public static readonly GUIContent enumerateCache = EditorGUIUtility.TrTextContent("Check Cache Size", "Check the size of the local asset cache server - can take a while");
public static readonly GUIContent browseCacheLocation = EditorGUIUtility.TrTextContent("Browse for local asset cache server location");
}
internal class Constants
{
public GUIStyle cacheFolderLocation = new GUIStyle(GUI.skin.label);
public Constants()
{
cacheFolderLocation.wordWrap = true;
}
}
const string kIPAddressKey = "CacheServerIPAddress";
const string kIpAddressKeyArgs = "-" + kIPAddressKey;
const string kModeKey = "CacheServerMode";
const string kDeprecatedEnabledKey = "CacheServerEnabled";
private static bool s_PrefsLoaded;
private static bool s_HasPendingChanges = false;
enum ConnectionState { Unknown, Success, Failure };
private static ConnectionState s_ConnectionState;
public enum CacheServerMode { Local, Remote, Disabled }
private static CacheServerMode s_CacheServerMode;
private static string s_CacheServerIPAddress;
private static int s_LocalCacheServerSize;
private static long s_LocalCacheServerUsedSize = -1;
private static bool s_EnableCustomPath;
private static string s_CachePath;
static Constants s_Constants = null;
public static void ReadPreferences()
{
s_CacheServerIPAddress = EditorPrefs.GetString(kIPAddressKey, s_CacheServerIPAddress);
s_CacheServerMode = (CacheServerMode)EditorPrefs.GetInt(kModeKey, (int)(EditorPrefs.GetBool(kDeprecatedEnabledKey) ? CacheServerMode.Remote : CacheServerMode.Disabled));
s_LocalCacheServerSize = EditorPrefs.GetInt(LocalCacheServer.SizeKey, 10);
s_CachePath = EditorPrefs.GetString(LocalCacheServer.PathKey);
s_EnableCustomPath = EditorPrefs.GetBool(LocalCacheServer.CustomPathKey);
}
public static void WritePreferences()
{
// Don't change anything if there's a command line override
if (GetCommandLineRemoteAddressOverride() != null)
return;
CacheServerMode oldMode = (CacheServerMode)EditorPrefs.GetInt(kModeKey);
var oldPath = EditorPrefs.GetString(LocalCacheServer.PathKey);
var oldCustomPath = EditorPrefs.GetBool(LocalCacheServer.CustomPathKey);
bool changedDir = false;
if (oldMode != s_CacheServerMode && oldMode == CacheServerMode.Local)
changedDir = true;
if (s_EnableCustomPath && oldPath != s_CachePath)
changedDir = true;
if (s_EnableCustomPath != oldCustomPath && s_CachePath != LocalCacheServer.GetCacheLocation() && s_CachePath != "")
changedDir = true;
if (changedDir)
{
s_LocalCacheServerUsedSize = -1;
var message = s_CacheServerMode == CacheServerMode.Local ?
"You have changed the location of the local cache storage." :
"You have disabled the local cache.";
message += " Do you want to delete the old locally cached data at " + LocalCacheServer.GetCacheLocation() + "?";
if (EditorUtility.DisplayDialog("Delete old Cache", message, "Delete", "Don't Delete"))
{
LocalCacheServer.Clear();
s_LocalCacheServerUsedSize = -1;
}
}
EditorPrefs.SetString(kIPAddressKey, s_CacheServerIPAddress);
EditorPrefs.SetInt(kModeKey, (int)s_CacheServerMode);
EditorPrefs.SetInt(LocalCacheServer.SizeKey, s_LocalCacheServerSize);
EditorPrefs.SetString(LocalCacheServer.PathKey, s_CachePath);
EditorPrefs.SetBool(LocalCacheServer.CustomPathKey, s_EnableCustomPath);
LocalCacheServer.Setup();
if (changedDir)
{
//Call ExitGUI after bringing up a dialog to avoid an exception
EditorGUIUtility.ExitGUI();
}
}
private static string GetCommandLineRemoteAddressOverride()
{
string address = null;
var argv = Environment.GetCommandLineArgs();
var index = Array.IndexOf(argv, kIpAddressKeyArgs);
if (index >= 0 && argv.Length > index + 1)
address = argv[index + 1];
return address;
}
[PreferenceItem("Cache Server")]
private static void OnGUI()
{
// Get event type before the event is used.
var eventType = Event.current.type;
if (s_Constants == null)
{
s_Constants = new Constants();
}
if (!InternalEditorUtility.HasTeamLicense())
GUILayout.Label(EditorGUIUtility.TempContent("You need to have a Pro or Team license to use the cache server.", EditorGUIUtility.GetHelpIcon(MessageType.Warning)), EditorStyles.helpBox);
using (new EditorGUI.DisabledScope(!InternalEditorUtility.HasTeamLicense()))
{
if (!s_PrefsLoaded)
{
ReadPreferences();
if (s_CacheServerMode != CacheServerMode.Disabled && s_ConnectionState == ConnectionState.Unknown)
{
if (InternalEditorUtility.CanConnectToCacheServer())
s_ConnectionState = ConnectionState.Success;
else
s_ConnectionState = ConnectionState.Failure;
}
s_PrefsLoaded = true;
}
EditorGUI.BeginChangeCheck();
var overrideAddress = GetCommandLineRemoteAddressOverride();
if (overrideAddress != null)
{
EditorGUILayout.HelpBox("Cache Server preferences cannot be modified because a remote address was specified via command line argument. To modify Cache Server preferences, restart Unity without the " + kIpAddressKeyArgs + " command line argument.", MessageType.Info, true);
}
using (new EditorGUI.DisabledScope(overrideAddress != null))
{
var displayMode = overrideAddress != null ? CacheServerMode.Remote : s_CacheServerMode;
s_CacheServerMode = (CacheServerMode)EditorGUILayout.EnumPopup("Cache Server Mode", displayMode);
}
if (s_CacheServerMode == CacheServerMode.Remote)
{
using (new EditorGUI.DisabledScope(overrideAddress != null))
{
var displayAddress = overrideAddress != null ? overrideAddress : s_CacheServerIPAddress;
s_CacheServerIPAddress = EditorGUILayout.DelayedTextField("IP Address", displayAddress);
if (GUI.changed)
{
s_ConnectionState = ConnectionState.Unknown;
}
}
GUILayout.Space(5);
if (GUILayout.Button("Check Connection", GUILayout.Width(150)))
{
if (InternalEditorUtility.CanConnectToCacheServer())
s_ConnectionState = ConnectionState.Success;
else
s_ConnectionState = ConnectionState.Failure;
}
GUILayout.Space(-25);
switch (s_ConnectionState)
{
case ConnectionState.Success:
EditorGUILayout.HelpBox("Connection successful.", MessageType.Info, false);
break;
case ConnectionState.Failure:
EditorGUILayout.HelpBox("Connection failed.", MessageType.Warning, false);
break;
case ConnectionState.Unknown:
GUILayout.Space(44);
break;
}
}
else if (s_CacheServerMode == CacheServerMode.Local)
{
const int kMinSizeInGigabytes = 1;
const int kMaxSizeInGigabytes = 200;
// Write size in GigaBytes.
s_LocalCacheServerSize = EditorGUILayout.IntSlider(Styles.maxCacheSize, s_LocalCacheServerSize, kMinSizeInGigabytes, kMaxSizeInGigabytes);
s_EnableCustomPath = EditorGUILayout.Toggle(Styles.customCacheLocation, s_EnableCustomPath);
// browse for cache folder if not per project
if (s_EnableCustomPath)
{
GUIStyle style = EditorStyles.miniButton;
GUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel(Styles.cacheFolderLocation, style);
Rect r = GUILayoutUtility.GetRect(GUIContent.none, style);
GUIContent guiText = string.IsNullOrEmpty(s_CachePath) ? Styles.browse : new GUIContent(s_CachePath);
if (EditorGUI.DropdownButton(r, guiText, FocusType.Passive, style))
{
string pathToOpen = s_CachePath;
string path = EditorUtility.OpenFolderPanel(Styles.browseCacheLocation.text, pathToOpen, "");
if (!string.IsNullOrEmpty(path))
{
if (LocalCacheServer.CheckValidCacheLocation(path))
{
s_CachePath = path;
WritePreferences();
}
else
EditorUtility.DisplayDialog("Invalid Cache Location", string.Format("The directory {0} contains some files which don't look like Unity Cache server files. Please delete the directory contents or choose another directory.", path), "OK");
EditorGUIUtility.ExitGUI();
}
}
GUILayout.EndHorizontal();
}
else
s_CachePath = "";
bool locationExists = LocalCacheServer.CheckCacheLocationExists();
if (locationExists == true)
{
GUIContent cacheSizeIs = EditorGUIUtility.TrTextContent("Cache size is unknown");
if (s_LocalCacheServerUsedSize != -1)
{
cacheSizeIs = EditorGUIUtility.TextContent("Cache size is " + EditorUtility.FormatBytes(s_LocalCacheServerUsedSize));
}
GUILayout.BeginHorizontal();
GUIStyle style = EditorStyles.miniButton;
EditorGUILayout.PrefixLabel(cacheSizeIs, style);
Rect r = GUILayoutUtility.GetRect(GUIContent.none, style);
if (EditorGUI.Button(r, Styles.enumerateCache, style))
{
s_LocalCacheServerUsedSize = LocalCacheServer.CheckCacheLocationExists() ? FileUtil.GetDirectorySize(LocalCacheServer.GetCacheLocation()) : 0;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUIContent spacerContent = EditorGUIUtility.blankContent;
EditorGUILayout.PrefixLabel(spacerContent, style);
Rect r2 = GUILayoutUtility.GetRect(GUIContent.none, style);
if (EditorGUI.Button(r2, Styles.cleanCache, style))
{
LocalCacheServer.Clear();
s_LocalCacheServerUsedSize = 0;
}
GUILayout.EndHorizontal();
}
else
{
EditorGUILayout.HelpBox("Local cache directory does not exist - please check that you can access the cache folder and are able to write to it", MessageType.Warning, false);
//If the cache server was on an external HDD or on a temporarily unavailable network drive, set the size to unknown so that the user re-queries it when they've reconnected
s_LocalCacheServerUsedSize = -1;
}
GUILayout.Label(Styles.cacheFolderLocation.text + ":");
GUILayout.Label(LocalCacheServer.GetCacheLocation(), s_Constants.cacheFolderLocation);
}
if (EditorGUI.EndChangeCheck())
s_HasPendingChanges = true;
// Only commit changes when we don't have an active hot control, to avoid restarting the cache server all the time while the slider is dragged, slowing down the UI.
if (s_HasPendingChanges && GUIUtility.hotControl == 0)
{
s_HasPendingChanges = false;
WritePreferences();
ReadPreferences();
}
}
}
}
}