-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilescount.cs
More file actions
136 lines (114 loc) · 2.99 KB
/
filescount.cs
File metadata and controls
136 lines (114 loc) · 2.99 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
// Written by Andrew Poženel - 2025
using Godot;
[Tool]
public class FileCountPlugin : EditorPlugin
{
private const string DockNameRes = "File Count - RES://";
private const string DockNameUser = "File Count - USER://";
private const string LabelTextRes = "File Count in RES://: 0";
private const string LabelTextUser = "File Count in USER://: 0";
private static readonly Vector2 DockSize = new Vector2(200, 100);
private static readonly Vector2 LabelSize = new Vector2(200, 50);
private static readonly Color PanelColor = new Color(0.2f, 0.2f, 0.2f, 1);
[Export]
public float UpdateTimerTime = 10.0f;
private Control fileCountDock;
private Label fileCountLabel;
private Control fileCountDock2;
private Label fileCountLabel2;
private Timer updateTimer;
public override void _EnterTree()
{
fileCountDock = CreateDock(DockNameRes, LabelTextRes);
fileCountDock2 = CreateDock(DockNameUser, LabelTextUser);
updateTimer = new Timer();
updateTimer.WaitTime = UpdateTimerTime;
updateTimer.Connect("timeout", new Callable(this, nameof(OnUpdateTimerTimeout)));
AddChild(updateTimer);
updateTimer.Start();
UpdateFileCount();
}
public override void _ExitTree()
{
if (updateTimer != null)
{
updateTimer.Stop();
updateTimer.QueueFree();
updateTimer = null;
}
RemoveControlFromBottomPanel(fileCountDock);
RemoveControlFromBottomPanel(fileCountDock2);
fileCountDock.QueueFree();
fileCountDock2.QueueFree();
fileCountDock = null;
fileCountDock2 = null;
}
private Control CreateDock(string name, string labelText)
{
var dock = new Control();
dock.Name = name;
dock.SetSize(DockSize);
var panelStyle = new StyleBoxFlat();
panelStyle.BgColor = PanelColor;
dock.AddThemeStyleboxOverride("panel", panelStyle);
var label = new Label();
label.Text = labelText;
label.SetSize(LabelSize);
dock.AddChild(label);
AddControlToBottomPanel(dock, name);
if (name == DockNameRes)
{
fileCountLabel = label;
}
else
{
fileCountLabel2 = label;
}
return dock;
}
private void UpdateFileCount()
{
var counts = CountFilesInProject();
fileCountLabel.Text = $"File Count in RES://: {counts[0]}";
fileCountLabel2.Text = $"File Count in USER://: {counts[1]}";
}
private void OnUpdateTimerTimeout()
{
UpdateFileCount();
}
private int CountFilesInDirectory(string path)
{
var dir = DirAccess.Open(path);
if (dir == null)
{
GD.PushError($"Error: Could not open directory '{path}'.");
return 0;
}
var count = 0;
dir.ListDirBegin();
var fileName = dir.GetNext();
while (fileName != "")
{
if (dir.CurrentIsDir())
{
if (fileName != "." && fileName != "..")
{
count += CountFilesInDirectory(path + fileName + "/");
}
}
else
{
count += 1;
}
fileName = dir.GetNext();
}
dir.ListDirEnd();
return count;
}
private int[] CountFilesInProject()
{
var resCount = CountFilesInDirectory("res://");
var userCount = CountFilesInDirectory("user://");
return new int[] { resCount, userCount };
}
}