-
Notifications
You must be signed in to change notification settings - Fork 2
/
UtilsTemplatesAndMisc.cs
374 lines (310 loc) · 11.4 KB
/
UtilsTemplatesAndMisc.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace MW5_Mod_Manager
{
internal static class Utils
{
public static bool StringNullEmptyOrWhiteSpace(string txt)
{
return string.IsNullOrEmpty(txt) || string.IsNullOrWhiteSpace(txt);
}
public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
Directory.CreateDirectory(destDirName);
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDirName, file.Name);
file.CopyTo(tempPath, true);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string tempPath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, tempPath, copySubDirs);
}
}
}
public static long DirSize(DirectoryInfo d)
{
long size = 0;
// Add file sizes.
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
{
size += fi.Length;
}
// Add subdirectory sizes.
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
size += DirSize(di);
}
return size;
}
}
#region extra designer items
//The rotating label for priority indication.
public class RotatingLabel : System.Windows.Forms.Label
{
private int m_RotateAngle = 0;
private string m_NewText = string.Empty;
public int RotateAngle { get { return m_RotateAngle; } set { m_RotateAngle = value; Invalidate(); } }
public string NewText { get { return m_NewText; } set { m_NewText = value; Invalidate(); } }
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Func<double, double> DegToRad = (angle) => Math.PI * angle / 180.0;
Brush b = new SolidBrush(this.ForeColor);
SizeF size = e.Graphics.MeasureString(this.NewText, this.Font, this.Parent.Width);
int normalAngle = ((RotateAngle % 360) + 360) % 360;
double normaleRads = DegToRad(normalAngle);
int hSinTheta = (int)Math.Ceiling((size.Height * Math.Sin(normaleRads)));
int wCosTheta = (int)Math.Ceiling((size.Width * Math.Cos(normaleRads)));
int wSinTheta = (int)Math.Ceiling((size.Width * Math.Sin(normaleRads)));
int hCosTheta = (int)Math.Ceiling((size.Height * Math.Cos(normaleRads)));
int rotatedWidth = Math.Abs(hSinTheta) + Math.Abs(wCosTheta);
int rotatedHeight = Math.Abs(wSinTheta) + Math.Abs(hCosTheta);
this.Width = rotatedWidth;
this.Height = rotatedHeight;
int numQuadrants =
(normalAngle >= 0 && normalAngle < 90) ? 1 :
(normalAngle >= 90 && normalAngle < 180) ? 2 :
(normalAngle >= 180 && normalAngle < 270) ? 3 :
(normalAngle >= 270 && normalAngle < 360) ? 4 :
0;
int horizShift = 0;
int vertShift = 0;
if (numQuadrants == 1)
{
horizShift = Math.Abs(hSinTheta);
}
else if (numQuadrants == 2)
{
horizShift = rotatedWidth;
vertShift = Math.Abs(hCosTheta);
}
else if (numQuadrants == 3)
{
horizShift = Math.Abs(wCosTheta);
vertShift = rotatedHeight;
}
else if (numQuadrants == 4)
{
vertShift = Math.Abs(wSinTheta);
}
e.Graphics.TranslateTransform(horizShift, vertShift);
e.Graphics.RotateTransform(this.RotateAngle);
e.Graphics.DrawString(this.NewText, this.Font, b, 0f, 0f);
base.OnPaint(e);
}
}
public enum ProgressBarDisplayMode
{
NoText,
Percentage,
CurrProgress,
CustomText,
TextAndPercentage,
TextAndCurrProgress
}
public class TextProgressBar : ProgressBar
{
[Description("Font of the text on ProgressBar"), Category("Additional Options")]
public Font TextFont { get; set; } = new Font(FontFamily.GenericSerif, 11, FontStyle.Bold | FontStyle.Italic);
private SolidBrush _textColourBrush = (SolidBrush)Brushes.Black;
[Category("Additional Options")]
public Color TextColor
{
get
{
return _textColourBrush.Color;
}
set
{
_textColourBrush.Dispose();
_textColourBrush = new SolidBrush(value);
}
}
private SolidBrush _progressColourBrush = (SolidBrush)Brushes.LightGreen;
[Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public Color ProgressColor
{
get
{
return _progressColourBrush.Color;
}
set
{
_progressColourBrush.Dispose();
_progressColourBrush = new SolidBrush(value);
}
}
private ProgressBarDisplayMode _visualMode = ProgressBarDisplayMode.CurrProgress;
[Category("Additional Options"), Browsable(true)]
public ProgressBarDisplayMode VisualMode
{
get
{
return _visualMode;
}
set
{
_visualMode = value;
Invalidate();//redraw component after change value from VS Properties section
}
}
private string _text = string.Empty;
[Description("If it's empty, % will be shown"), Category("Additional Options"), Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public string CustomText
{
get
{
return _text;
}
set
{
_text = value;
Invalidate();//redraw component after change value from VS Properties section
}
}
private string _textToDraw
{
get
{
string text = CustomText;
switch (VisualMode)
{
case (ProgressBarDisplayMode.Percentage):
text = _percentageStr;
break;
case (ProgressBarDisplayMode.CurrProgress):
text = _currProgressStr;
break;
case (ProgressBarDisplayMode.TextAndCurrProgress):
text = $"{CustomText}: {_currProgressStr}";
break;
case (ProgressBarDisplayMode.TextAndPercentage):
text = $"{CustomText}: {_percentageStr}";
break;
}
return text;
}
set { }
}
private string _percentageStr { get { return $"{(int)((float)Value - Minimum) / ((float)Maximum - Minimum) * 100 } %"; } }
private string _currProgressStr
{
get
{
return $"{Value}/{Maximum}";
}
}
public TextProgressBar()
{
Value = Minimum;
FixComponentBlinking();
}
private void FixComponentBlinking()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
DrawProgressBar(g);
DrawStringIfNeeded(g);
}
private void DrawProgressBar(Graphics g)
{
Rectangle rect = ClientRectangle;
ProgressBarRenderer.DrawHorizontalBar(g, rect);
rect.Inflate(-3, -3);
if (Value > 0)
{
Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
g.FillRectangle(_progressColourBrush, clip);
}
}
private void DrawStringIfNeeded(Graphics g)
{
if (VisualMode != ProgressBarDisplayMode.NoText)
{
string text = _textToDraw;
SizeF len = g.MeasureString(text, TextFont);
Point location = new Point(((Width / 2) - (int)len.Width / 2), ((Height / 2) - (int)len.Height / 2));
g.DrawString(text, TextFont, (Brush)_textColourBrush, location);
}
}
public new void Dispose()
{
_textColourBrush.Dispose();
_progressColourBrush.Dispose();
base.Dispose();
}
}
#endregion extra designer items
public class ModObject
{
public string displayName { set; get; }
public string version { set; get; }
public int buildNumber { set; get; }
public string description { set; get; }
public string author { set; get; }
public string authorURL { set; get; }
public float defaultLoadOrder { set; get; }
public string gameVersion { set; get; }
public List<string> manifest { get; set; }
public long steamPublishedFileId { set; get; }
public long steamLastSubmittedBuildNumber { set; get; }
public string steamModVisibility { set; get; }
public List<string> Requires { set; get; }
}
public class ProgramData
{
public string vendor { set; get; }
public float version { set; get; }
public string[] installdir { set; get; }
}
public class OverridingData
{
public string mod { set; get; }
public bool isOverriden { set; get; }
public bool isOverriding { set; get; }
public Dictionary<string, List<string>> overrides { set; get; }
public Dictionary<string, List<string>> overriddenBy { set; get; }
}
public class ModItem : ListViewItem
{
public ModItem() : base("", 0)
{
//other stuff here
}
public string DisplayName
{
get { return this.SubItems[1].Text; }
set { this.SubItems[1].Text = value; }
}
public string FolderName
{
get { return this.SubItems[2].Text; }
set { this.SubItems[2].Text = value; }
}
}
}