-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDebugSettingsDrawer.cs
More file actions
373 lines (293 loc) · 14.8 KB
/
Copy pathDebugSettingsDrawer.cs
File metadata and controls
373 lines (293 loc) · 14.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using DCL.WebRequests.ChromeDevtool;
using Global.AppArgs;
using Global.Dynamic.DebugSettings;
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;
namespace Global.Editor
{
[CustomPropertyDrawer(typeof(DebugSettings))]
public class DebugSettingsDrawer : PropertyDrawer
{
private static readonly string DEFAULT_CREATOR_HUB_PATH = CreatorHubBrowser.DEFAULT_CREATOR_HUB_BIN_PATH;
private static float SingleLineHeight => EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position.height = SingleLineHeight;
SerializedProperty appParameters = property.FindPropertyRelative("appParameters");
bool cdpEnabled = HasFlag(appParameters, AppArgsFlags.LAUNCH_CDP_MONITOR_ON_START);
string? currentCreatorHubPath = GetFlagValue(appParameters, AppArgsFlags.CREATOR_HUB_BIN_PATH);
string detectedCreatorHubPath = FindCreatorHubPath();
bool creatorHubInstalled = !string.IsNullOrEmpty(detectedCreatorHubPath);
// CDP DevTools Section
EditorGUI.LabelField(position, "Chrome DevTools Protocol", EditorStyles.boldLabel);
position.y += SingleLineHeight;
// Creator Hub status
var statusStyle = new GUIStyle(EditorStyles.label);
if (creatorHubInstalled)
{
statusStyle.normal.textColor = new Color(0.2f, 0.7f, 0.2f);
EditorGUI.LabelField(position, $"Creator Hub found: {detectedCreatorHubPath}", statusStyle);
}
else
{
statusStyle.normal.textColor = new Color(0.9f, 0.5f, 0.1f);
EditorGUI.LabelField(position, "Creator Hub not found at default location", statusStyle);
}
position.y += SingleLineHeight;
// Enable CDP toggle
EditorGUI.BeginChangeCheck();
bool newCdpEnabled = EditorGUI.Toggle(position, new GUIContent("Enable DevTools on Start", "Launches Chrome DevTools Protocol bridge when Play mode starts"), cdpEnabled);
if (EditorGUI.EndChangeCheck() && newCdpEnabled != cdpEnabled)
{
if (newCdpEnabled)
EnableCdpDevTools(appParameters, detectedCreatorHubPath);
else
DisableCdpDevTools(appParameters);
}
position.y += SingleLineHeight;
// Custom Creator Hub path (only if CDP is enabled)
if (cdpEnabled)
{
EditorGUI.BeginChangeCheck();
Rect pathFieldRect = position;
pathFieldRect.width -= 80;
Rect browseButtonRect = position;
browseButtonRect.x = position.xMax - 75;
browseButtonRect.width = 75;
string displayPath = currentCreatorHubPath ?? detectedCreatorHubPath ?? "Not found";
string newPath = EditorGUI.TextField(pathFieldRect, new GUIContent("Creator Hub Path"), displayPath);
if (GUI.Button(browseButtonRect, "Browse"))
{
string initialDir = !string.IsNullOrEmpty(currentCreatorHubPath) ? Path.GetDirectoryName(currentCreatorHubPath) :
!string.IsNullOrEmpty(detectedCreatorHubPath) ? Path.GetDirectoryName(detectedCreatorHubPath) : Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
#if UNITY_EDITOR_WIN
string selectedPath = EditorUtility.OpenFilePanel("Select Creator Hub Executable", initialDir ?? "", "exe");
#else
string selectedPath = EditorUtility.OpenFilePanel("Select Creator Hub Executable", initialDir ?? "", "");
#endif
if (!string.IsNullOrEmpty(selectedPath))
newPath = selectedPath;
}
if (EditorGUI.EndChangeCheck() && newPath != displayPath)
SetCreatorHubPath(appParameters, newPath);
position.y += SingleLineHeight;
// Clear custom path button if a custom path is set
if (!string.IsNullOrEmpty(currentCreatorHubPath))
{
Rect clearButtonRect = position;
clearButtonRect.x = position.xMax - 150;
clearButtonRect.width = 150;
if (GUI.Button(clearButtonRect, "Use Default Path"))
RemoveFlag(appParameters, AppArgsFlags.CREATOR_HUB_BIN_PATH);
position.y += SingleLineHeight;
}
}
// Separator
position.y += 5;
EditorGUI.LabelField(position, "", GUI.skin.horizontalSlider);
position.y += SingleLineHeight;
// Draw all other properties (including appParameters as raw array)
position = DrawDefaultProperties(position, property);
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SerializedProperty appParameters = property.FindPropertyRelative("appParameters");
bool cdpEnabled = HasFlag(appParameters, AppArgsFlags.LAUNCH_CDP_MONITOR_ON_START);
string? currentCreatorHubPath = GetFlagValue(appParameters, AppArgsFlags.CREATOR_HUB_BIN_PATH);
// CDP section: header + status + toggle
int lineCount = 3;
// Custom path field (if CDP enabled)
if (cdpEnabled)
{
lineCount += 1;
// Clear button (if custom path is set)
if (!string.IsNullOrEmpty(currentCreatorHubPath))
lineCount += 1;
}
// Separator
lineCount += 1;
// Default properties (including appParameters as raw array)
float defaultPropertiesHeight = GetDefaultPropertiesHeight(property);
return (lineCount * SingleLineHeight) + 5 + defaultPropertiesHeight;
}
private static Rect DrawDefaultProperties(Rect position, SerializedProperty property)
{
SerializedProperty iterator = property.Copy();
SerializedProperty endProperty = property.GetEndProperty();
bool enterChildren = true;
bool isCustomGatekeeper = IsCustomGatekeeperMode(property);
while (iterator.NextVisible(enterChildren) && !SerializedProperty.EqualContents(iterator, endProperty))
{
enterChildren = false;
if (iterator.name == "customGatekeeperUrl" && !isCustomGatekeeper)
continue;
float propertyHeight = EditorGUI.GetPropertyHeight(iterator, true);
position.height = propertyHeight;
EditorGUI.PropertyField(position, iterator, true);
position.y += propertyHeight + EditorGUIUtility.standardVerticalSpacing;
}
return position;
}
private static float GetDefaultPropertiesHeight(SerializedProperty property)
{
float height = 0;
SerializedProperty iterator = property.Copy();
SerializedProperty endProperty = property.GetEndProperty();
bool enterChildren = true;
bool isCustomGatekeeper = IsCustomGatekeeperMode(property);
while (iterator.NextVisible(enterChildren) && !SerializedProperty.EqualContents(iterator, endProperty))
{
enterChildren = false;
if (iterator.name == "customGatekeeperUrl" && !isCustomGatekeeper)
continue;
height += EditorGUI.GetPropertyHeight(iterator, true) + EditorGUIUtility.standardVerticalSpacing;
}
return height;
}
private static bool IsCustomGatekeeperMode(SerializedProperty property)
{
SerializedProperty gatekeeperMode = property.FindPropertyRelative("gatekeeperMode");
Assert.IsNotNull(gatekeeperMode, "Failed to find 'gatekeeperMode' property.");
return gatekeeperMode.intValue == (int)GatekeeperMode.Custom;
}
private static string? FindCreatorHubPath()
{
// Check default path
if (File.Exists(DEFAULT_CREATOR_HUB_PATH))
return DEFAULT_CREATOR_HUB_PATH;
#if UNITY_EDITOR_WIN
// Search common Windows installation locations
string[] searchPaths =
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "creator-hub", "Decentraland Creator Hub.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "creator-hub", "Decentraland Creator Hub.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "creator-hub", "Decentraland Creator Hub.exe"),
};
foreach (string path in searchPaths)
{
if (File.Exists(path))
return path;
}
#else
// Search common macOS locations
string[] searchPaths =
{
"/Applications/Decentraland Creator Hub.app/Contents/MacOS/Decentraland Creator Hub",
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Applications", "Decentraland Creator Hub.app", "Contents", "MacOS", "Decentraland Creator Hub"),
};
foreach (string path in searchPaths)
{
if (File.Exists(path))
return path;
}
#endif
return null;
}
private static string FormatFlag(string flag) =>
$"--{flag}";
private static bool HasFlag(SerializedProperty appParameters, string flag)
{
string formattedFlag = FormatFlag(flag);
for (int i = 0; i < appParameters.arraySize; i++)
{
if (appParameters.GetArrayElementAtIndex(i).stringValue == formattedFlag)
return true;
}
return false;
}
private static string? GetFlagValue(SerializedProperty appParameters, string flag)
{
string formattedFlag = FormatFlag(flag);
for (int i = 0; i < appParameters.arraySize; i++)
{
if (appParameters.GetArrayElementAtIndex(i).stringValue == formattedFlag && i + 1 < appParameters.arraySize)
return appParameters.GetArrayElementAtIndex(i + 1).stringValue;
}
return null;
}
private static void EnableCdpDevTools(SerializedProperty appParameters, string? creatorHubPath)
{
if (!HasFlag(appParameters, AppArgsFlags.LAUNCH_CDP_MONITOR_ON_START))
{
int insertIndex = appParameters.arraySize;
// Add --flag
appParameters.InsertArrayElementAtIndex(insertIndex);
appParameters.GetArrayElementAtIndex(insertIndex).stringValue = FormatFlag(AppArgsFlags.LAUNCH_CDP_MONITOR_ON_START);
// Add value
appParameters.InsertArrayElementAtIndex(insertIndex + 1);
appParameters.GetArrayElementAtIndex(insertIndex + 1).stringValue = "true";
}
// Add Creator Hub path if found
if (!string.IsNullOrEmpty(creatorHubPath))
SetCreatorHubPath(appParameters, creatorHubPath);
appParameters.serializedObject.ApplyModifiedProperties();
}
private static void DisableCdpDevTools(SerializedProperty appParameters)
{
RemoveFlag(appParameters, AppArgsFlags.LAUNCH_CDP_MONITOR_ON_START);
RemoveFlag(appParameters, AppArgsFlags.CREATOR_HUB_BIN_PATH);
appParameters.serializedObject.ApplyModifiedProperties();
}
private static void SetCreatorHubPath(SerializedProperty appParameters, string path)
{
// First remove existing path if any
RemoveFlagWithValue(appParameters, AppArgsFlags.CREATOR_HUB_BIN_PATH);
// Add --flag and value
int insertIndex = appParameters.arraySize;
appParameters.InsertArrayElementAtIndex(insertIndex);
appParameters.GetArrayElementAtIndex(insertIndex).stringValue = FormatFlag(AppArgsFlags.CREATOR_HUB_BIN_PATH);
appParameters.InsertArrayElementAtIndex(insertIndex + 1);
appParameters.GetArrayElementAtIndex(insertIndex + 1).stringValue = path;
appParameters.serializedObject.ApplyModifiedProperties();
}
private static void RemoveFlag(SerializedProperty appParameters, string flag)
{
string formattedFlag = FormatFlag(flag);
for (int i = appParameters.arraySize - 1; i >= 0; i--)
{
if (appParameters.GetArrayElementAtIndex(i).stringValue == formattedFlag)
{
// Remove the value first (if exists)
if (i + 1 < appParameters.arraySize)
{
string nextValue = appParameters.GetArrayElementAtIndex(i + 1).stringValue;
// Only remove if it's not another flag
if (!nextValue.StartsWith("--"))
appParameters.DeleteArrayElementAtIndex(i + 1);
}
// Then remove the flag
appParameters.DeleteArrayElementAtIndex(i);
appParameters.serializedObject.ApplyModifiedProperties();
return;
}
}
}
private static void RemoveFlagWithValue(SerializedProperty appParameters, string flag)
{
string formattedFlag = FormatFlag(flag);
for (int i = appParameters.arraySize - 1; i >= 0; i--)
{
if (appParameters.GetArrayElementAtIndex(i).stringValue == formattedFlag)
{
// Remove the value first (if exists)
if (i + 1 < appParameters.arraySize)
{
string nextValue = appParameters.GetArrayElementAtIndex(i + 1).stringValue;
// Only remove if it's not another flag
if (!nextValue.StartsWith("--"))
appParameters.DeleteArrayElementAtIndex(i + 1);
}
// Then remove the flag
appParameters.DeleteArrayElementAtIndex(i);
appParameters.serializedObject.ApplyModifiedProperties();
return;
}
}
}
}
}