forked from MunifiSense/VRChat-Build-Size-Viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildSizeViewer.cs
More file actions
1214 lines (1112 loc) · 45.2 KB
/
Copy pathBuildSizeViewer.cs
File metadata and controls
1214 lines (1112 loc) · 45.2 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// VRC Build Size Viewer
// Licensed under the MIT License.
// Created by MunifiSense
// <https://github.com/MunifiSense/VRChat-Build-Size-Viewer>
//
// Modified by trigger_segfault
// <https://github.com/trigger-segfault/VRChat-Build-Size-Viewer>
//
// 2025-10-12, trigger_segfault
// * Added support for reading build logs on Linux and OSX. See:
// <https://github.com/nfya-lab/VRChat-Build-Size-Viewer/commit/44797c50ef04cb049f7b763e5fbe0a0a6a51ca03>
// * Multiple build logs can be read and switched between.
// * `Editor-prev.log` log is now read as well.
// * Added preference to restrict how many build logs are kept in memory.
// * Language support (no translations besides English added yet).
// * Total Uncompressed Size is now shown just below Total Compressed Size.
// * File icons are shown next to file paths (if the file still exists).
// * Fancy data grid style for files and categories, with proper column right
// alignment for numbers.
// * Unidirectional column sorting by clicking on column headers. "Extension"
// and "Original Order" column headers are included for extra options.
// * "Go" button replaced with just clicking the file path.
// * Horizontal scrollbar is now shown if the file paths are too long.
// * Added preference for hiding the Categories list, since it takes up an
// excessive amount of vertical space
// * Added preference to disable mouse hover highlighting for performance.
// * List item virtualization and moving away from the layout system. This
// drastically reduces the number of allocations and processing per `OnGUI()`
// call.
// * Menu path for showing window is now under `Trigger Segfault/`.
// * Changed window name to "Build Size" and added window icon.
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
namespace TriggerSegfault.editor
{
// TODO: Check if new builds since last Read Build Logs (using Bundle Name
// as unique ID).
[EditorWindowTitle(title = "Build Size",
icon = "UnityEditor.ConsoleWindow")]
public class BuildSizeViewer : EditorWindow
{
const string MenuPath = "Window/Trigger Segfault/VRC Build Size Viewer";
const string PrefsNamespace = "TriggerSegfault/BuildSizeViewer/";
const string LanguagePref = PrefsNamespace + "Language";
const string ShowCategoriesPref = PrefsNamespace + "ShowCategories";
const string EnableHoverPref = PrefsNamespace + "EnableHover";
const string MaxLogCountPref = PrefsNamespace + "MaxLogCount";
// Not readonly to allow for serialization.
private List<BuildLog> m_buildLogs = new List<BuildLog>();
private string[] m_buildLogPopupNames = Array.Empty<string>();
private int m_buildLogIndex = -1;
[NonSerialized]
private int m_hoverIndex = -1;
private Vector2 m_scrollPos = Vector2.zero;
// Temporary fields for the lifetime of DrawBuildLog().
[NonSerialized]
private int m_hoverableIndex;
[NonSerialized]
private int m_newHoverIndex;
[NonSerialized]
private float m_columnPercent;
[NonSerialized]
private float m_columnSize;
[NonSerialized]
private Rect m_fileVisibleRect = Rect.zero;
[NonSerialized]
private float m_cachedFileMaxWidth = 0f;
#region Preferences
public const Languages DefaultLanguage = Languages.English;
public enum Languages
{
English,
// No other languages yet... you can help by expanding it.
}
private static TranslationLanguage s_translation;
private static TranslationLanguage Translation
{
get
{
if (s_translation == null &&
!Translations.TryGetValue(Language, out s_translation))
{
s_translation = Translations[Languages.English];
}
return s_translation;
}
}
private static Languages? s_language;
public static Languages Language
{
// Store enum pref as a string, since that's less-likely to change.
get
{
if (!s_language.HasValue)
{
string value = EditorPrefs.GetString(
LanguagePref, DefaultLanguage.ToString().ToLowerInvariant()
);
if (!Enum.TryParse(value, ignoreCase: true, out Languages lang))
{
lang = DefaultLanguage;
}
s_language = lang;
// Invalidate to reload translation on next access.
s_translation = null;
}
return s_language.Value;
}
set
{
if (Language != value)
{
s_language = value;
EditorPrefs.SetString(LanguagePref, value.ToString().ToLowerInvariant());
}
}
}
private static bool? s_showCategories;
public static bool ShowCategories
{
get
{
s_showCategories ??= EditorPrefs.GetBool(ShowCategoriesPref, true);
return s_showCategories.Value;
}
set
{
if (ShowCategories != value)
{
s_showCategories = value;
EditorPrefs.SetBool(ShowCategoriesPref, value);
}
}
}
private static bool? s_enableHover;
public static bool EnableHover
{
get
{
s_enableHover ??= EditorPrefs.GetBool(EnableHoverPref, true);
return s_enableHover.Value;
}
set
{
if (EnableHover != value)
{
s_enableHover = value;
EditorPrefs.SetBool(EnableHoverPref, value);
}
}
}
private static int? s_maxLogCount;
public static int MaxLogCount
{
get
{
s_maxLogCount ??= EditorPrefs.GetInt(MaxLogCountPref, 20);
return Math.Max(1, s_maxLogCount.Value);
}
set
{
if (MaxLogCount != value)
{
s_maxLogCount = value;
EditorPrefs.SetInt(MaxLogCountPref, value);
}
}
}
#endregion
#region OnGUI and Events
[MenuItem(MenuPath, false)]
static void ShowWindow()
{
EditorWindow.GetWindow<BuildSizeViewer>();
}
void OnProjectChange()
{
// Uncache asset objects, since files may have changed.
foreach (var buildLog in m_buildLogs)
{
foreach (var file in buildLog.Files)
{
file.AssetObject = null;
}
}
}
void OnGUI()
{
InitializeStyles();
EditorGUILayout.LabelField(Translation.Instructions, EditorStyles.label);
if (GUILayout.Button(TempContent(Translation.ReadBuildLogs, Translation.ReadBuildLogsTooltip)))
{
ReadBuildLogs();
m_buildLogIndex = (m_buildLogs.Count > 0 ? 0 : -1);
m_hoverIndex = -1;
m_scrollPos = Vector2.zero;
// Repaint to reflect new build log selection.
Repaint();
GUIUtility.ExitGUI();
}
DrawPreferences();
int newIndex = EditorGUILayout.Popup(
TempContent(Translation.SelectBuildLog, Translation.SelectBuildLogTooltip),
m_buildLogIndex,
m_buildLogPopupNames
);
if (m_buildLogIndex != newIndex)
{
m_buildLogIndex = newIndex;
m_hoverIndex = -1;
// Preserve scroll position, it may be useful when switching back
// and forth between logs.
// Repaint to reflect new build log selection.
Repaint();
GUIUtility.ExitGUI();
}
else if (m_buildLogIndex >= 0 && m_buildLogIndex < m_buildLogs.Count)
{
this.wantsMouseMove = EnableHover;
this.wantsMouseEnterLeaveWindow = EnableHover;
DrawBuildLog(m_buildLogs[m_buildLogIndex]);
}
else
{
this.wantsMouseMove = false;
this.wantsMouseEnterLeaveWindow = false;
// Reset scroll position if nothing is being viewed.
m_scrollPos = Vector2.zero;
}
}
private void DrawPreferences()
{
// Don't waste space showing language dropdown if there's only one.
if (Translations.Count > 1)
{
Languages newLanguage = (Languages)EditorGUILayout.EnumPopup("Language", Language);
if (Language != newLanguage)
{
Language = newLanguage;
// Repaint to reflect new language.
Repaint();
GUIUtility.ExitGUI();
}
}
EditorGUILayout.BeginHorizontal();
try
{
bool newShowCategories = EditorGUILayout.Toggle(
TempContent(Translation.ShowCategories, Translation.ShowCategoriesTooltip),
ShowCategories
);
if (ShowCategories != newShowCategories)
{
ShowCategories = newShowCategories;
// Repaint to reflect categories visibility.
Repaint();
GUIUtility.ExitGUI();
}
EnableHover = EditorGUILayout.Toggle(
TempContent(Translation.EnableHover, Translation.EnableHoverTooltip),
EnableHover
);
}
finally
{
EditorGUILayout.EndHorizontal();
}
MaxLogCount = Math.Clamp(
EditorGUILayout.IntField(
TempContent(Translation.MaxLogCount, Translation.MaxLogCountTooltip),
MaxLogCount
),
1, 1000
);
}
private void DrawBuildLog(BuildLog log)
{
// Reset indices for detecting control receiving mouse hover.
m_newHoverIndex = -1;
m_hoverableIndex = 0;
// Cache column widths that are used for every file item.
m_columnPercent = Styles.RightHeader.CalcSize(TempContent("100.0%")).x + 10f;
m_columnSize = Styles.RightHeader.CalcSize(TempContent("1000.0 mb")).x + 10f;
float columnTotalSize = EditorStyles.label.CalcSize(
TempContent(Translation.TotalUncompressedSize)
).x;
void DrawTotalSize(string text, FileSize size)
{
EditorGUILayout.BeginHorizontal();
try
{
EditorGUILayout.LabelField(
text, EditorStyles.label, GUILayout.Width(columnTotalSize)
);
EditorGUILayout.LabelField(
size.ToString(), Styles.RightLabel, GUILayout.Width(m_columnSize)
);
}
finally
{
EditorGUILayout.EndHorizontal();
}
}
EditorGUILayout.Separator();
DrawTotalSize(Translation.TotalCompressedSize, log.CompressedSize);
DrawTotalSize(Translation.TotalUncompressedSize, log.UncompressedSize);
if (ShowCategories)
{
EditorGUILayout.Separator();
DrawCategoryList(log);
}
EditorGUILayout.Separator();
DrawFileList(log);
// Update index of control receiving mouse hover.
switch (Event.current.type)
{
case EventType.MouseMove:
case EventType.MouseLeaveWindow:
if (EnableHover && m_hoverIndex != m_newHoverIndex)
{
m_hoverIndex = m_newHoverIndex;
// Repaint to reflect new control with mouse hover.
Repaint();
}
break;
}
}
private void DrawCategoryList(BuildLog log)
{
DrawColumnHeaders(log.Categories, isFile: false);
Rect areaRect = GUILayoutUtility.GetRect(
this.position.width, log.Categories.Count * Styles.ItemHeight
);
// Category rows have no interaction, and so only need to be
// processed during repaint.
if (Event.current.type == EventType.Repaint)
{
DrawStyle(Styles.AreaBg, areaRect, GUIContent.none);
Rect itemRect = areaRect;
itemRect.height = Styles.ItemHeight;
foreach (var category in log.Categories)
{
DrawRow(category, itemRect, isFile: false);
itemRect.y += Styles.ItemHeight;
}
}
}
private void DrawFileList(BuildLog log)
{
Event e = Event.current;
DrawColumnHeaders(log.Files, isFile: true);
// Get the current Y position. This tells us how much remaining
// space is available to be consumed by the scroll view.
float yStart = GUILayoutUtility.GetLastRect().yMax;
Vector2 origIconSize = EditorGUIUtility.GetIconSize();
m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, Styles.AreaBg);
try
{
if (e.type == EventType.Layout || e.type == EventType.Repaint)
{
// We're either laying out, and need the icon dimensions
// for sizing, or need the icon dimensions for repainting.
EditorGUIUtility.SetIconSize(Styles.IconSize);
}
// Measure max width so that we can show horizontal scrollbar.
// Layout is always the first event, so no need to check if the
// cached width is assigned.
if (e.type == EventType.Layout)
{
float maxWidth = 0f;
foreach (var file in log.Files)
{
float width = Styles.LeftHover.CalcSize(TempContent(
file.FullName,
// Use dummy texture, since all we need is the area
// consumed by the fixed-size icon.
EditorGUIUtility.whiteTexture
)).x;
maxWidth = Math.Max(maxWidth, width);
}
m_cachedFileMaxWidth = maxWidth + m_columnPercent + m_columnSize
+ Styles.ColumnNameSpacing + Styles.TextPadding;
}
// Reserve the scrollable area dimensions, now that we know how
// much horizontal space is needed.
Rect areaRect = GUILayoutUtility.GetRect(
m_cachedFileMaxWidth, log.Files.Count * Styles.ItemHeight
);
if (e.type == EventType.Layout)
{
// Layouting is done.
return;
}
// Get the rect used for collision checks and positioning.
m_fileVisibleRect = GetVisibleScrollArea(
m_scrollPos, this.position, yStart, areaRect
);
// Only draw visible list items, based on scroll position.
int visibleStart = Math.Max(
0, Mathf.FloorToInt(m_scrollPos.y / Styles.ItemHeight)
);
int visibleEnd = Math.Min(
log.Files.Count,
Mathf.CeilToInt(
(m_scrollPos.y + m_fileVisibleRect.height) / Styles.ItemHeight
)
);
Rect itemRect = areaRect;
itemRect.height = Styles.ItemHeight;
for (int i = visibleStart; i < visibleEnd; i++)
{
itemRect.y = i * Styles.ItemHeight;
DrawRow(log.Files[i], itemRect, isFile: true);
}
}
finally
{
EditorGUILayout.EndScrollView();
EditorGUIUtility.SetIconSize(origIconSize);
}
}
private static Rect GetVisibleScrollArea(
Vector2 scrollPos, Rect position, float yStart, Rect area
)
{
Rect rect = new Rect(
scrollPos.x, scrollPos.y,
position.width, Math.Max(0f, position.height - yStart)
);
// Cut-off scrollbar sizes if present.
if (area.height > rect.height)
{
rect.width -= GUI.skin.verticalScrollbar.fixedWidth;
// Vertical is needed, check if horizontal is now needed.
if (area.width > rect.width)
{
rect.height -= GUI.skin.horizontalScrollbar.fixedHeight;
}
}
else if (area.width > rect.width)
{
rect.height -= GUI.skin.horizontalScrollbar.fixedHeight;
// Horizontal is needed, check if vertical is now needed.
if (area.height > rect.height)
{
rect.width -= GUI.skin.verticalScrollbar.fixedWidth;
}
}
rect.size = Vector2.Max(Vector2.zero, rect.size);
return rect;
}
private void DrawStyle(GUIStyle style, Rect rect, string text, bool hoverable = false)
{
DrawStyle(style, rect, TempContent(text), hoverable);
}
private void DrawStyle(GUIStyle style, Rect rect, GUIContent content, bool hoverable = false)
{
if (Event.current.type == EventType.Repaint)
{
style.Draw(
rect,
content,
isHover: (EnableHover && hoverable &&
m_hoverIndex == m_hoverableIndex),
isActive: false,
on: false,
hasKeyboardFocus: false
);
}
}
private void DrawColumnHeaders(List<BuildItem> list, bool isFile)
{
Rect rect = EditorGUILayout.GetControlRect(false, Styles.HeaderHeight, Styles.HeaderBg);
string nameText = isFile
? Translation.FilePathColumn : Translation.CategoryColumn;
string extraText = isFile
? Translation.ExtensionColumn : Translation.OriginalOrderColumn;
float columnName = Styles.LeftHeader.CalcSize(TempContent(nameText)).x
+ Styles.TextPadding;
float columnExtra = Styles.RightHeader.CalcSize(TempContent(extraText)).x;
Rect percentRect = rect;
Rect sizeRect = rect;
Rect nameRect = rect;
Rect extraRect = rect;
percentRect.width = m_columnPercent;
sizeRect.xMin = percentRect.xMax;
sizeRect.width = m_columnSize;
nameRect.xMin = sizeRect.xMax + Styles.ColumnNameSpacing;
nameRect.width = columnName;
extraRect.xMin = nameRect.xMax;
extraRect.width = Math.Max(columnExtra, extraRect.width - Styles.TextPadding);
DrawStyle(Styles.HeaderBg, rect, GUIContent.none);
DrawStyle(Styles.RightHeader, percentRect, Translation.PercentColumn, true);
HandleColumnHeader(list, percentRect, CompareSize);
DrawStyle(Styles.RightHeader, sizeRect, Translation.SizeColumn, true);
HandleColumnHeader(list, sizeRect, CompareSize);
DrawStyle(Styles.LeftHeader, nameRect, nameText, true);
HandleColumnHeader(list, nameRect, CompareName);
DrawStyle(Styles.RightHeader, extraRect, extraText, true);
HandleColumnHeader(
list, extraRect, isFile ? CompareExtension : CompareIndex
);
}
private void DrawRow(BuildItem item, Rect rect, bool isFile)
{
Rect percentRect = rect;
Rect sizeRect = rect;
Rect nameRect = rect;
percentRect.width = m_columnPercent;
sizeRect.xMin = percentRect.xMax;
sizeRect.width = m_columnSize;
nameRect.xMin = sizeRect.xMax + Styles.ColumnNameSpacing;
DrawStyle(Styles.RightLabel, percentRect, item.PercentString);
DrawStyle(Styles.RightLabel, sizeRect, item.Size.ToString());
if (!isFile)
{
DrawStyle(EditorStyles.label, nameRect, item.FullName);
}
else
{
// Don't waste time looking up the icon when not repainting.
var icon = (Event.current.type == EventType.Repaint)
? item.Icon : Texture2D.whiteTexture;
GUIContent nameContent = TempContent(item.FullName, icon, item.FullName);
DrawStyle(Styles.LeftHover, nameRect, nameContent, true);
HandleFileRow(item, nameRect);
}
}
private void HandleColumnHeader(List<BuildItem> list, Rect rect, Comparison<BuildItem> comparison)
{
Event e = Event.current;
switch (e.type)
{
case EventType.MouseMove:
//case EventType.MouseEnterWindow:
if (EnableHover && rect.Contains(e.mousePosition))
{
m_newHoverIndex = m_hoverableIndex;
}
break;
case EventType.MouseDown:
if (e.button == 0 && rect.Contains(e.mousePosition))
{
list.Sort(comparison);
// Repaint to reflect new list order.
Repaint();
e.Use();
GUIUtility.ExitGUI();
}
break;
}
m_hoverableIndex++;
}
private void HandleFileRow(BuildItem item, Rect rect)
{
Event e = Event.current;
switch (e.type)
{
case EventType.MouseMove:
//case EventType.MouseEnterWindow:
if (EnableHover && rect.Contains(e.mousePosition) &&
m_fileVisibleRect.Contains(e.mousePosition))
{
m_newHoverIndex = m_hoverableIndex;
}
break;
case EventType.MouseDown:
if (e.button == 0 && rect.Contains(e.mousePosition))
{
if (item.FullName != "Resources/unity_builtin_extra")
{
var assetObject = item.AssetObject;
if (assetObject != null)
{
Selection.activeObject = assetObject;
EditorGUIUtility.PingObject(assetObject);
}
}
e.Use();
GUIUtility.ExitGUI();
}
break;
}
m_hoverableIndex++;
}
#endregion
#region GUI Helpers
private static class Styles
{
public static readonly Vector2 IconSize = new Vector2(16f, 16f);
public const float ItemHeight = 18f;
public const float HeaderHeight = 21f;
public const float ColumnNameSpacing = 24f;
public const float TextPadding = 6f;
public static Texture2D TransparentIcon;
public static GUIStyle RightLabel;
public static GUIStyle LeftHeader;
public static GUIStyle RightHeader;
public static GUIStyle LeftHover;
public static GUIStyle HeaderBg;
public static GUIStyle AreaBg;
}
private static bool s_stylesInitialized = false;
private static void InitializeStyles()
{
if (s_stylesInitialized)
{
return;
}
s_stylesInitialized = true;
{
Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
texture.SetPixel(0, 0, new Color(0f, 0f, 0f, 0f));
texture.Apply();
Styles.TransparentIcon = texture;
}
{
GUIStyle style = new GUIStyle(EditorStyles.label);
style.alignment = TextAnchor.MiddleRight;
Styles.RightLabel = style;
}
{
GUIStyle style = new GUIStyle(new GUIStyle("OL ResultLabel"));
style.fontStyle = FontStyle.Bold;
style.fixedHeight = Styles.HeaderHeight;
style.margin = new RectOffset();
style.padding = new RectOffset();
// Highlight column header on hover.
style.hover.textColor = EditorStyles.whiteLabel.normal.textColor;
style.alignment = TextAnchor.MiddleLeft;
Styles.LeftHeader = style;
}
{
GUIStyle style = new GUIStyle(Styles.LeftHeader);
style.alignment = TextAnchor.MiddleRight;
Styles.RightHeader = style;
}
{
GUIStyle style = new GUIStyle(new GUIStyle("OL ResultLabel"));
style.margin = new RectOffset();
style.padding = new RectOffset();
// Highlight column on hover.
style.hover.textColor = EditorStyles.whiteLabel.normal.textColor;
Styles.LeftHover = style;
}
{
GUIStyle style = new GUIStyle(new GUIStyle("ProjectBrowserTopBarBg"));
style.fixedHeight = Styles.HeaderHeight;
style.margin = new RectOffset();
style.padding = new RectOffset();
Styles.HeaderBg = style;
}
{
GUIStyle style = new GUIStyle(new GUIStyle("ProjectBrowserIconAreaBg"));
// The style is being used as-is, but it's good practice to
// copy it anyway, in-case future changes are mistakenly
// made without changing it back to a copy.
Styles.AreaBg = style;
}
}
private readonly static GUIContent s_tempContent = new GUIContent();
private static GUIContent TempContent(string text, string tooltip)
{
return TempContent(text, null, tooltip);
}
private static GUIContent TempContent(string text, Texture2D image = null, string tooltip = null)
{
s_tempContent.text = text;
s_tempContent.tooltip = tooltip;
s_tempContent.image = image;
return s_tempContent;
}
#endregion
#region BuildLog Reading
private static string GetBuildLogPath(bool previous)
{
string name = (previous ? "Editor-prev.log" : "Editor.log");
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Unity/Editor/" + name
);
case RuntimePlatform.OSXEditor:
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
"Library/Logs/Unity/" + name
);
case RuntimePlatform.LinuxEditor:
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
".config/unity3d/" + name
);
default:
Debug.LogWarning($"Unsupported OS: {Application.platform}");
return string.Empty;
}
}
private static string TemporaryBuildLogPath => GetBuildLogPath(false) + "copy";
private void ReadBuildLogs()
{
m_buildLogs.Clear();
m_buildLogPopupNames = Array.Empty<string>();
var prevBuildLogPath = GetBuildLogPath(previous: true);
var currentBuildLogPath = GetBuildLogPath(previous: false);
var tempBuildLogPath = TemporaryBuildLogPath;
try
{
if (File.Exists(currentBuildLogPath))
{
FileUtil.ReplaceFile(currentBuildLogPath, tempBuildLogPath);
using var reader = File.OpenText(tempBuildLogPath);
m_buildLogs.AddRange(BuildLog.ReadAll(tempBuildLogPath));
}
// OPTIMIZE: Avoid reading previous editor log if we already
// reached the max log count. This is helpful if the previous
// log is noticeably large, since we have to read least-to-most
// recent.
if (m_buildLogs.Count < MaxLogCount && File.Exists(prevBuildLogPath))
{
FileUtil.ReplaceFile(prevBuildLogPath, tempBuildLogPath);
using var reader = File.OpenText(tempBuildLogPath);
m_buildLogs.AddRange(BuildLog.ReadAll(tempBuildLogPath));
}
}
catch (Exception ex)
{
Debug.LogWarning($"Error reading build logs:\n{ex}");
}
finally
{
try
{
FileUtil.DeleteFileOrDirectory(tempBuildLogPath);
}
catch {}
if (m_buildLogs.Count > MaxLogCount)
{
m_buildLogs.RemoveRange(MaxLogCount, m_buildLogs.Count - MaxLogCount);
}
m_buildLogPopupNames = m_buildLogs.Select((b, i) => $"[{i}] {b.Name}")
.ToArray();
}
}
#endregion
#region BuildLog Sorting
private static int CompareIndex(BuildItem x, BuildItem y)
{
return x.Index.CompareTo(y.Index);
}
private static int CompareSize(BuildItem x, BuildItem y)
{
int cmp;
// It's possible for Percent to have more precision than Size if low kilobytes.
// Compare this first, since it's cheaper to calculate.
if (0 != (cmp = x.Percent.CompareTo(y.Percent)) ||
0 != (cmp = x.Size.SizeInBytes.CompareTo(y.Size.SizeInBytes)))
{
// Negate for descending size.
return -cmp;
}
// Size is identical (or we don't have enough precision to tell).
return CompareIndex(x, y);
}
private static int CompareName(BuildItem x, BuildItem y)
{
return string.Compare(
x.FullName,
y.FullName,
StringComparison.InvariantCultureIgnoreCase
);
}
private static int CompareExtension(BuildItem x, BuildItem y)
{
int cmp = string.Compare(
Path.GetExtension(x.FullName),
Path.GetExtension(y.FullName),
StringComparison.InvariantCultureIgnoreCase
);
if (cmp != 0)
{
return cmp;
}
return CompareName(x, y);
}
#endregion
#region BuildLog Classes
[Serializable]
private struct FileSize
{
public float Size;
public string Units;
public long SizeInBytes
{
get
{
switch (Units)
{
case "b": // Not sure if bytes are ever used as units.
case "byte":
case "bytes":
default: return (long)Size;
case "kb": return (long)((double)Size * (1024));
case "mb": return (long)((double)Size * (1024 * 1024));
case "gb": return (long)((double)Size * (1024 * 1024 * 1024));
}
}
}
[NonSerialized]
private string m_cachedString;
public override string ToString() => (m_cachedString ??= $"{Size:0.0} {Units}");
public static FileSize Parse(Match m)
{
return new FileSize
{
Size = float.Parse(m.Groups["size"].Value),
Units = m.Groups["units"].Value,
};
}
}
[Serializable]
private class BuildItem
{
public int Index; // Only exists for sorting
public string FullName; // File path or category name
public FileSize Size;
public float Percent;
[NonSerialized]
private UnityEngine.Object m_cachedAssetObject;
[NonSerialized]
private bool m_isAssetObjectCached = false;
public UnityEngine.Object AssetObject
{
get
{
if (!m_isAssetObjectCached)
{
m_isAssetObjectCached = true;
m_cachedAssetObject = AssetDatabase.LoadMainAssetAtPath(FullName);
}
return m_cachedAssetObject;
}
set
{
m_isAssetObjectCached = (value != null);
m_cachedAssetObject = value;
}
}
[NonSerialized]
private string m_cachedPercentString;
public string PercentString => (m_cachedPercentString ??= $"{Percent:0.0}%");
public Texture2D Icon
{
get
{
var icon = AssetDatabase.GetCachedIcon(FullName) as Texture2D;
if (icon != null)
{
return icon;
}
// Icon may not be cached yet.
icon = AssetPreview.GetMiniThumbnail(AssetObject);
if (icon != null)
{
return icon;
}
// File may not exist anymore, or icon can't be obtained.
return Styles.TransparentIcon;
}
}
public static BuildItem Parse(string line, Regex regex)
{
Match m = regex.Match(line);
if (m != null)
{
string percent = m.Groups["percent"].Value;
if (string.IsNullOrEmpty(percent))
{
percent = "0.0";
}
string fullName = m.Groups["name"].Value;
return new BuildItem
{
FullName = fullName,
Percent = float.Parse(percent),
Size = FileSize.Parse(m),
};
}
else
{
return null;
}
}
}
[Serializable]
private class BuildLog
{
public string Name;
public FileSize CompressedSize;
public FileSize UncompressedSize;
// Not readonly to allow for serialization.
public List<BuildItem> Categories = new List<BuildItem>();
public List<BuildItem> Files = new List<BuildItem>();
public static List<BuildLog> ReadAll(string filePath)
{
using var reader = File.OpenText(filePath);
List<BuildLog> logs = new List<BuildLog>();
string line;
while (null != (line = reader.ReadLine()))