-
Notifications
You must be signed in to change notification settings - Fork 35
/
Form1.cs
501 lines (405 loc) · 17.4 KB
/
Form1.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using osum;
using osum.GameModes;
using osum.GameModes.Play;
using osum.GameplayElements;
using osum.GameplayElements.Beatmaps;
using osum.GameplayElements.HitObjects;
using osum.Helpers;
using osum.Support.Desktop;
namespace StreamTester
{
public partial class Form1 : Form
{
string tempDir = Path.GetTempPath() + "osu!stream";
string osusDir = Environment.CurrentDirectory;
const string BEATMAP_PATH = "Beatmaps\\";
public Form1()
{
InitializeComponent();
new TextBoxStreamWriter(console, modconsole);
if (Directory.Exists(tempDir))
Directory.Delete(tempDir, true);
Directory.CreateDirectory(tempDir);
maps = findMaps(BEATMAP_PATH);
maps.Sort();
listAvailableMaps.Items.AddRange(maps.ToArray());
}
private List<ListEntry> findMaps(string p, List<ListEntry> entries = null)
{
if (entries == null)
entries = new List<ListEntry>();
if (Directory.Exists(p))
{
foreach (string d in Directory.GetDirectories(p))
findMaps(d, entries);
string[] files = Directory.GetFiles(p, "*.osu");
if (files.Length > 0)
{
string displayName = files[0];
displayName = displayName.Remove(displayName.IndexOf('['));
displayName = displayName.Substring(displayName.LastIndexOf('\\') + 1);
displayName = displayName.Replace(".osu", "");
entries.Add(new ListEntry(p, displayName));
}
}
return entries;
}
string filename;
private bool checkingForChanges;
private GameBaseDesktop game;
private string Filename
{
get { return filename; }
set
{
filename = value;
buttonTestOnce.Enabled = !File.Exists(filename);
buttonTestOnSave.Enabled = !File.Exists(filename);
modconsole.Text = string.Empty;
Invoke((MethodInvoker)delegate
{
checkBoxQuick.Checked = true;
ThreadPool.QueueUserWorkItem(delegate
{
CombinateAndTest(false);
File.Delete(tempDir + "\\" + Path.GetFileNameWithoutExtension(Filename) + ".osz2");
});
});
}
}
int beatmapLength;
private void visualisePackage(string package)
{
using (Beatmap b = new Beatmap(package))
using (HitObjectManagerLoadAll hom = new HitObjectManagerLoadAll(b))
{
labelBeatmapTitle.Text = b.Title;
labelBeatmapArtist.Text = b.Artist;
hom.LoadFile();
int width = beatmapLayout.Width;
int height = beatmapLayout.Height;
beatmapLayout.Image = new Bitmap(width, height);
int lastMap = 3;
while (hom.StreamHitObjects[lastMap] == null)
lastMap--;
beatmapLength = hom.StreamHitObjects[lastMap].FindLast(s => s == s).EndTime + 1000;
using (Graphics g = Graphics.FromImage(beatmapLayout.Image))
{
g.Clear(Color.White);
drawHitObjects(g, hom.StreamHitObjects[0], Color.YellowGreen, 0);
drawHitObjects(g, hom.StreamHitObjects[1], Color.CornflowerBlue, 1);
drawHitObjects(g, hom.StreamHitObjects[2], Color.PaleVioletRed, 2);
drawHitObjects(g, hom.StreamHitObjects[3], Color.BlueViolet, 3);
//draw bookmarks
if (b.StreamSwitchPoints != null)
{
foreach (int time in b.StreamSwitchPoints)
{
int xPos = (int)((float)time / beatmapLength * width);
g.DrawLine(new Pen(Color.Black), new Point(xPos - 1, 0), new Point(xPos - 1, height));
g.DrawLine(new Pen(Color.Red), new Point(xPos, 0), new Point(xPos, height));
g.DrawLine(new Pen(Color.Black), new Point(xPos + 1, 0), new Point(xPos + 1, height));
}
}
}
}
}
private void drawHitObjects(Graphics g, pList<HitObject> objects, Color color, int vOffset)
{
if (objects == null) return;
int width = beatmapLayout.Width;
int height = (int)(beatmapLayout.Height / 4f);
int h1 = height * vOffset;
int h2 = height * (vOffset + 1) - 1;
Pen brush = new Pen(Color.FromArgb(100, color.R, color.G, color.B));
foreach (HitObject h in objects)
{
int objWidth = (int)Math.Max(1, (float)(h.EndTime - h.StartTime) / beatmapLength);
g.DrawRectangle(brush, new Rectangle((int)((float)h.StartTime / beatmapLength * width), h1, objWidth, height));
}
}
bool isDragging;
private List<ListEntry> maps;
private void beatmapLayout_MouseDown(object sender, MouseEventArgs e)
{
checkBoxEditorTime.Checked = false;
updatePosition();
isDragging = true;
}
private void beatmapLayout_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void updatePosition()
{
int time = (int)((float)beatmapLength * beatmapLayout.PointToClient(Cursor.Position).X / beatmapLayout.Width);
updateStartTime(time);
}
private void updateStartTime(int time)
{
if (beatmapLength == 0 || time < 0 || time > beatmapLength)
return;
textBoxStartTime.Text = time.ToString();
arrow.Location = new Point(PointToClient(Cursor.Position).X - arrow.Width / 2, arrow.Location.Y);
}
private void beatmapLayout_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
updatePosition();
}
private void buttonTestOnce_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(w => { CombinateAndTest(); });
}
private void button1_Click(object s1, EventArgs e1)
{
checkingForChanges = !checkingForChanges;
if (checkingForChanges)
{
checkBoxEditorTime.Checked = true;
buttonTestOnSave.Text = "Cancel";
bool hasChanges = false;
ThreadPool.QueueUserWorkItem(w =>
{
FileSystemWatcher fsw = new FileSystemWatcher(filename);
string changedFilename = null;
fsw.Changed += delegate(object sender, FileSystemEventArgs e)
{
changedFilename = e.FullPath;
hasChanges = true;
};
fsw.EnableRaisingEvents = true;
Console.WriteLine("Waiting for changes...");
while (checkingForChanges)
{
if (hasChanges)
{
fsw.EnableRaisingEvents = false;
hasChanges = false;
Console.WriteLine("Detected changes; recombinating!");
if (checkBoxEditorTime.Checked)
{
foreach (string l in File.ReadAllLines(changedFilename))
{
if (l.StartsWith("CurrentTime:"))
{
updateStartTime(Int32.Parse(l.Split(':')[1].Trim()));
break;
}
}
}
if (checkBoxEditorDifficulty.Checked)
{
if (changedFilename.Contains("Easy"))
radioButtonEasy.Checked = true;
else if (changedFilename.Contains("Normal"))
radioButtonNormal.Checked = true;
else if (changedFilename.Contains("Hard"))
radioButtonHard.Checked = true;
else if (changedFilename.Contains("Expert"))
radioButtonExpert.Checked = true;
}
CombinateAndTest();
fsw.EnableRaisingEvents = true;
Console.WriteLine("Waiting for changes...");
}
Thread.Sleep(100);
}
});
}
else
{
buttonTestOnSave.Text = "Automatically test on beatmap save";
}
}
private string CombinateAndTest(bool runTest = true)
{
string packageName = null;
Invoke((MethodInvoker)delegate
{
console.Text = string.Empty;
if (checkBoxAnalysis.Checked) modconsole.Text = string.Empty;
panelButtons.Enabled = false;
});
if (game != null)
{
GameBase.Scheduler.Add(delegate { Director.ChangeMode(OsuMode.PositioningTest, null); }, true);
while (Director.CurrentOsuMode == OsuMode.PlayTest)
Thread.Sleep(100);
}
try
{
GameBase.Instance = null;
//temporarily remove any instance to ensure we get correct and speedy calculations.
//this will be restored at the end of processing.
Environment.CurrentDirectory = tempDir;
BeatmapCombinator.BeatmapCombinator.Analysis = checkBoxAnalysis.Checked;
packageName = tempDir + "\\" + BeatmapCombinator.BeatmapCombinator.Process(Filename, checkBoxQuick.Checked);
Environment.CurrentDirectory = osusDir;
GameBase.Instance = game;
PlayTest.StartTime = Int32.Parse(textBoxStartTime.Text);
PlayTest.AllowStreamSwitch = streamSwitch.Checked;
Player.Beatmap = new Beatmap(packageName);
Player.Autoplay = checkBoxAutoplay.Checked;
if (radioButtonStreamUp.Checked)
PlayTest.InitialHp = 200;
else if (radioButtonStreamDown.Checked)
PlayTest.InitialHp = 0;
else
PlayTest.InitialHp = 100;
if (radioButtonEasy.Checked)
PlayTest.InitialDifficulty = Difficulty.Easy;
else if (radioButtonNormal.Checked)
PlayTest.InitialDifficulty = Difficulty.Normal;
else if (radioButtonHard.Checked)
PlayTest.InitialDifficulty = Difficulty.Hard;
else if (radioButtonExpert.Checked)
PlayTest.InitialDifficulty = Difficulty.Expert;
switch (PlayTest.InitialDifficulty)
{
case Difficulty.Expert:
Player.Difficulty = Difficulty.Expert;
break;
default:
Player.Difficulty = PlayTest.AllowStreamSwitch ? Difficulty.Normal : PlayTest.InitialDifficulty;
break;
}
Invoke((MethodInvoker)delegate { visualisePackage(packageName); });
if (runTest && !checkBoxm4a.Checked)
{
if (game == null)
{
ThreadPool.QueueUserWorkItem(w =>
{
try
{
game = new GameBaseDesktop(OsuMode.PlayTest);
game.Run();
}
catch (ApplicationException ex)
{
MessageBox.Show(ex.Message);
game.Exit();
}
});
}
else
{
GameBase.Scheduler.Add(delegate { Director.ChangeMode(OsuMode.PlayTest, null); }, true);
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR:\n" + ex);
}
Invoke((MethodInvoker)delegate { panelButtons.Enabled = true; });
File.Delete(tempDir + "\\" + Path.GetFileNameWithoutExtension(Filename) + ".osc");
if (checkBoxm4a.Checked)
Process.Start(tempDir);
return packageName;
}
private void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
Filename = files[0];
panelDrop.BackColor = Color.DimGray;
}
protected override void OnClosing(CancelEventArgs e)
{
Environment.Exit(-1);
base.OnClosing(e);
}
private void streamSwitch_CheckedChanged(object sender, EventArgs e)
{
groupBoxStreamSwitch.Enabled = streamSwitch.Checked;
}
private void difficultyChanged(object sender, EventArgs e)
{
radioButtonStreamNormal.Checked = true;
radioButtonStreamDown.Enabled = sender != radioButtonEasy && sender != radioButtonExpert;
radioButtonStreamUp.Enabled = sender != radioButtonHard && sender != radioButtonExpert;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
buttonTestOnSave.Enabled = !checkBoxm4a.Checked;
groupBoxDifficulty.Enabled = !checkBoxm4a.Checked;
groupBoxStreamSwitch.Enabled = !checkBoxm4a.Checked;
buttonTestOnce.Text = checkBoxm4a.Checked ? "Create Package" : "Test Once";
}
private void listAvailableMaps_SelectedValueChanged(object sender, EventArgs e)
{
if (listAvailableMaps.SelectedItem == null) return;
string path = ((ListEntry)listAvailableMaps.SelectedItem).Path;
if (path.Contains(":"))
Filename = path;
else
Filename = osusDir + "\\" + path;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string searchQuery = textBox1.Text == "search..." ? string.Empty : textBox1.Text.ToLower();
listAvailableMaps.Items.Clear();
List<ListEntry> filtered = maps.FindAll(en => en.Display.ToLower().Contains(searchQuery));
filtered.Sort();
listAvailableMaps.Items.AddRange(filtered.ToArray());
}
private void textBox1_Enter(object sender, EventArgs e)
{
if (textBox1.Text == "search...")
textBox1.Text = string.Empty;
textBox1.ForeColor = Color.Black;
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "search...";
textBox1.ForeColor = Color.Gray;
}
}
}
public class HitObjectManagerLoadAll : HitObjectManager
{
public HitObjectManagerLoadAll(Beatmap beatmap)
: base(beatmap)
{
}
protected override bool shouldLoadDifficulty(Difficulty difficulty)
{
return true;
}
}
public class ListEntry : IComparable<ListEntry>
{
public string Display;
public string Path;
public ListEntry(string path, string display)
{
Display = display;
Path = path;
}
public override string ToString()
{
return Display;
}
#region IComparable<ListEntry> Members
public int CompareTo(ListEntry other)
{
return Display.CompareTo(other.Display);
}
#endregion
}
}